voiture.py

Created by planetepanda70

Created on September 27, 2023

1.84 KB

rrggr


import time
import random

# Initialisation de la position de la voiture
voiture_x = 4
voiture_y = 9

# Initialisation de la largeur de la route
largeur_route = 10

# Initialisation de la position de l'obstacle
obstacle_x = random.randint(1, largeur_route - 2)
obstacle_y = 0

# Variables de jeu
score = 0
vies = 3

# Fonction pour afficher l'écran de jeu
def afficher_ecran():
    # Effacer l'écran
    sys.stdout.write("\033c")
    
    # Afficher la route
    for i in range(10):
        for j in range(largeur_route):
            if i == voiture_y and j == voiture_x:
                sys.stdout.write("V")
            elif i == obstacle_y and j == obstacle_x:
                sys.stdout.write("X")
            else:
                sys.stdout.write(" ")
        sys.stdout.write("\n")
    
    # Afficher les informations du jeu
    sys.stdout.write("\nScore: " + str(score) + "  Vies: " + str(vies) + "\n")

# Boucle principale du jeu
while vies > 0:
    # Déplacer l'obstacle
    obstacle_y += 1
    
    # Gérer les entrées utilisateur
    touche = input("Appuyez sur 'A' pour aller à gauche, 'D' pour aller à droite : ").strip().upper()
    
    if touche == 'A':
        voiture_x -= 1
    elif touche == 'D':
        voiture_x += 1
    
    # Limiter la position de la voiture à la route
    if voiture_x < 1:
        voiture_x = 1
    elif voiture_x > largeur_route - 2:
        voiture_x = largeur_route - 2
    
    # Vérifier la collision avec l'obstacle
    if obstacle_x == voiture_x and obstacle_y == voiture_y:
        vies -= 1
        obstacle_x = random.randint(1, largeur_route - 2)
        obstacle_y = 0
    elif obstacle_y > 9:
        score += 1
        obstacle_x = random.randint(1, largeur_route - 2)
        obstacle_y = 0
    
    afficher_ecran()
    time.sleep(0.2)

print("Fin de la partie. Votre score final est de", score)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.