mirror of
https://github.com/Fare-spec/cours.git
synced 2025-12-08 03:00:37 +00:00
formatte file
This commit is contained in:
@@ -13,8 +13,8 @@ SCREEN_HEIGHT = 720
|
||||
# Définition des couleurs (R, G, B)
|
||||
BLACK = (0, 0, 0)
|
||||
WHITE = (255, 255, 255)
|
||||
BLUE = (0, 0, 255)
|
||||
RED = (255, 0, 0)
|
||||
BLUE = (0, 0, 255)
|
||||
RED = (255, 0, 0)
|
||||
|
||||
# Création de la fenêtre de jeu
|
||||
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
||||
@@ -23,6 +23,7 @@ pygame.display.set_caption("Casse-Brique Auto-Play")
|
||||
clock = pygame.time.Clock()
|
||||
FPS = 100
|
||||
|
||||
|
||||
# Fonction d'affichage du menu de démarrage
|
||||
def show_menu():
|
||||
menu_running = True
|
||||
@@ -32,22 +33,24 @@ def show_menu():
|
||||
button_height = 50
|
||||
button_rect = pygame.Rect(0, 0, button_width, button_height)
|
||||
button_rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
|
||||
|
||||
|
||||
while menu_running:
|
||||
clock.tick(FPS)
|
||||
screen.fill(BLACK)
|
||||
|
||||
|
||||
# Affichage du titre
|
||||
title_surface = title_font.render("Casse-Brique", True, WHITE)
|
||||
title_rect = title_surface.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 3))
|
||||
title_rect = title_surface.get_rect(
|
||||
center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 3)
|
||||
)
|
||||
screen.blit(title_surface, title_rect)
|
||||
|
||||
|
||||
# Dessiner le bouton "Jouer"
|
||||
pygame.draw.rect(screen, BLUE, button_rect)
|
||||
button_text = button_font.render("Jouer", True, WHITE)
|
||||
button_text_rect = button_text.get_rect(center=button_rect.center)
|
||||
screen.blit(button_text, button_text_rect)
|
||||
|
||||
|
||||
# Gestion des événements du menu
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
@@ -57,32 +60,35 @@ def show_menu():
|
||||
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
||||
if button_rect.collidepoint(event.pos):
|
||||
menu_running = False
|
||||
|
||||
|
||||
pygame.display.flip()
|
||||
|
||||
|
||||
# Fonction principale du jeu
|
||||
def game_loop():
|
||||
# Paramètres de la raquette
|
||||
PADDLE_WIDTH = 100
|
||||
PADDLE_HEIGHT = 15
|
||||
paddle_x = (SCREEN_WIDTH - PADDLE_WIDTH) / 2 # centré horizontalement
|
||||
paddle_y = SCREEN_HEIGHT - PADDLE_HEIGHT - 10 # position verticale en bas
|
||||
paddle_y = SCREEN_HEIGHT - PADDLE_HEIGHT - 10 # position verticale en bas
|
||||
paddle_speed = 7
|
||||
|
||||
# Paramètres de la balle
|
||||
ball_radius = 10
|
||||
# Liste de balles (initialement une seule)
|
||||
balls = [{
|
||||
'x': SCREEN_WIDTH / 2,
|
||||
'y': SCREEN_HEIGHT / 2,
|
||||
'radius': ball_radius,
|
||||
'speed_x': 9 * random.choice([-1, 1]),
|
||||
'speed_y': -9
|
||||
}]
|
||||
balls = [
|
||||
{
|
||||
"x": SCREEN_WIDTH / 2,
|
||||
"y": SCREEN_HEIGHT / 2,
|
||||
"radius": ball_radius,
|
||||
"speed_x": 9 * random.choice([-1, 1]),
|
||||
"speed_y": -9,
|
||||
}
|
||||
]
|
||||
|
||||
# Paramètres des briques
|
||||
BRICK_ROWS = 5 # nombre de lignes de briques
|
||||
BRICK_COLUMNS = 10 # nombre de colonnes de briques
|
||||
BRICK_ROWS = 5 # nombre de lignes de briques
|
||||
BRICK_COLUMNS = 10 # nombre de colonnes de briques
|
||||
BRICK_WIDTH = SCREEN_WIDTH // BRICK_COLUMNS
|
||||
BRICK_HEIGHT = 20
|
||||
brick_padding = 5
|
||||
@@ -94,7 +100,9 @@ def game_loop():
|
||||
for col in range(BRICK_COLUMNS):
|
||||
brick_x = col * BRICK_WIDTH + brick_padding
|
||||
brick_y = row * (BRICK_HEIGHT + brick_padding) + brick_padding
|
||||
brick_rect = pygame.Rect(brick_x, brick_y, BRICK_WIDTH - brick_padding * 2, BRICK_HEIGHT)
|
||||
brick_rect = pygame.Rect(
|
||||
brick_x, brick_y, BRICK_WIDTH - brick_padding * 2, BRICK_HEIGHT
|
||||
)
|
||||
brick_row.append(brick_rect)
|
||||
bricks.append(brick_row)
|
||||
|
||||
@@ -112,7 +120,7 @@ def game_loop():
|
||||
# On cherche une balle qui descend (speed_y > 0), sinon on prend la première balle
|
||||
target_ball = None
|
||||
for ball in balls:
|
||||
if ball['speed_y'] > 0:
|
||||
if ball["speed_y"] > 0:
|
||||
target_ball = ball
|
||||
break
|
||||
if target_ball is None:
|
||||
@@ -120,9 +128,9 @@ def game_loop():
|
||||
# Calcul du centre de la raquette
|
||||
paddle_center = paddle_x + PADDLE_WIDTH / 2
|
||||
# Si le centre est trop à gauche ou à droite de la balle cible, on déplace la raquette
|
||||
if paddle_center < target_ball['x'] - 5:
|
||||
if paddle_center < target_ball["x"] - 5:
|
||||
paddle_x += paddle_speed
|
||||
elif paddle_center > target_ball['x'] + 5:
|
||||
elif paddle_center > target_ball["x"] + 5:
|
||||
paddle_x -= paddle_speed
|
||||
|
||||
# Limiter la raquette à l'intérieur de l'écran
|
||||
@@ -135,31 +143,40 @@ def game_loop():
|
||||
|
||||
# Mise à jour de la position des balles
|
||||
for ball in balls:
|
||||
ball['x'] += ball['speed_x']
|
||||
ball['y'] += ball['speed_y']
|
||||
ball["x"] += ball["speed_x"]
|
||||
ball["y"] += ball["speed_y"]
|
||||
|
||||
# Collision avec les murs latéraux
|
||||
if ball['x'] - ball['radius'] <= 0 or ball['x'] + ball['radius'] >= SCREEN_WIDTH:
|
||||
ball['speed_x'] *= -1
|
||||
if (
|
||||
ball["x"] - ball["radius"] <= 0
|
||||
or ball["x"] + ball["radius"] >= SCREEN_WIDTH
|
||||
):
|
||||
ball["speed_x"] *= -1
|
||||
|
||||
# Collision avec le haut de l'écran
|
||||
if ball['y'] - ball['radius'] <= 0:
|
||||
ball['speed_y'] *= -1
|
||||
if ball["y"] - ball["radius"] <= 0:
|
||||
ball["speed_y"] *= -1
|
||||
|
||||
# Création du rectangle de la balle pour la détection des collisions
|
||||
ball_rect = pygame.Rect(ball['x'] - ball['radius'], ball['y'] - ball['radius'],
|
||||
ball['radius'] * 2, ball['radius'] * 2)
|
||||
ball_rect = pygame.Rect(
|
||||
ball["x"] - ball["radius"],
|
||||
ball["y"] - ball["radius"],
|
||||
ball["radius"] * 2,
|
||||
ball["radius"] * 2,
|
||||
)
|
||||
|
||||
# Collision entre la balle et la raquette (uniquement si la balle descend)
|
||||
if ball_rect.colliderect(paddle_rect) and ball['speed_y'] > 0:
|
||||
ball['speed_y'] *= -1 # Inversion de la vitesse verticale
|
||||
offset = (ball['x'] - (paddle_x + PADDLE_WIDTH / 2)) / (PADDLE_WIDTH / 2)
|
||||
ball['speed_x'] = 4 * offset
|
||||
if ball_rect.colliderect(paddle_rect) and ball["speed_y"] > 0:
|
||||
ball["speed_y"] *= -1 # Inversion de la vitesse verticale
|
||||
offset = (ball["x"] - (paddle_x + PADDLE_WIDTH / 2)) / (
|
||||
PADDLE_WIDTH / 2
|
||||
)
|
||||
ball["speed_x"] = 4 * offset
|
||||
|
||||
# Création d'une nouvelle balle lors de la collision
|
||||
new_ball = ball.copy()
|
||||
new_ball['speed_x'] *= -1
|
||||
new_ball['speed_x'] += random.choice([-1, 1])
|
||||
new_ball["speed_x"] *= -1
|
||||
new_ball["speed_x"] += random.choice([-1, 1])
|
||||
nouvelles_balles.append(new_ball)
|
||||
|
||||
# Collision entre la balle et les briques
|
||||
@@ -168,7 +185,7 @@ def game_loop():
|
||||
for brick in row:
|
||||
if brick and ball_rect.colliderect(brick):
|
||||
hit_brick = brick
|
||||
ball['speed_y'] *= -1 # Inversion de la vitesse verticale
|
||||
ball["speed_y"] *= -1 # Inversion de la vitesse verticale
|
||||
break
|
||||
if hit_brick:
|
||||
row[row.index(hit_brick)] = None
|
||||
@@ -180,27 +197,33 @@ def game_loop():
|
||||
# Gestion des collisions entre balles
|
||||
for i in range(len(balls)):
|
||||
for j in range(i + 1, len(balls)):
|
||||
dx = balls[i]['x'] - balls[j]['x']
|
||||
dy = balls[i]['y'] - balls[j]['y']
|
||||
dx = balls[i]["x"] - balls[j]["x"]
|
||||
dy = balls[i]["y"] - balls[j]["y"]
|
||||
distance = math.sqrt(dx * dx + dy * dy)
|
||||
if distance < balls[i]['radius'] + balls[j]['radius']:
|
||||
if distance < balls[i]["radius"] + balls[j]["radius"]:
|
||||
# Échange des vitesses pour simuler une collision élastique
|
||||
balls[i]['speed_x'], balls[j]['speed_x'] = balls[j]['speed_x'], balls[i]['speed_x']
|
||||
balls[i]['speed_y'], balls[j]['speed_y'] = balls[j]['speed_y'], balls[i]['speed_y']
|
||||
balls[i]["speed_x"], balls[j]["speed_x"] = (
|
||||
balls[j]["speed_x"],
|
||||
balls[i]["speed_x"],
|
||||
)
|
||||
balls[i]["speed_y"], balls[j]["speed_y"] = (
|
||||
balls[j]["speed_y"],
|
||||
balls[i]["speed_y"],
|
||||
)
|
||||
# Ajustement des positions pour éviter la superposition
|
||||
overlap = balls[i]['radius'] + balls[j]['radius'] - distance
|
||||
overlap = balls[i]["radius"] + balls[j]["radius"] - distance
|
||||
if distance != 0:
|
||||
nx = dx / distance
|
||||
ny = dy / distance
|
||||
else:
|
||||
nx, ny = 1, 0
|
||||
balls[i]['x'] += nx * overlap / 2
|
||||
balls[i]['y'] += ny * overlap / 2
|
||||
balls[j]['x'] -= nx * overlap / 2
|
||||
balls[j]['y'] -= ny * overlap / 2
|
||||
balls[i]["x"] += nx * overlap / 2
|
||||
balls[i]["y"] += ny * overlap / 2
|
||||
balls[j]["x"] -= nx * overlap / 2
|
||||
balls[j]["y"] -= ny * overlap / 2
|
||||
|
||||
# Suppression des balles qui sortent par le bas de l'écran
|
||||
balls = [ball for ball in balls if ball['y'] - ball['radius'] <= SCREEN_HEIGHT]
|
||||
balls = [ball for ball in balls if ball["y"] - ball["radius"] <= SCREEN_HEIGHT]
|
||||
if not balls:
|
||||
print("Game Over!")
|
||||
running = False
|
||||
@@ -216,15 +239,17 @@ def game_loop():
|
||||
|
||||
# Dessiner les balles
|
||||
for ball in balls:
|
||||
pygame.draw.circle(screen, WHITE, (int(ball['x']), int(ball['y'])), ball['radius'])
|
||||
pygame.draw.circle(
|
||||
screen, WHITE, (int(ball["x"]), int(ball["y"])), ball["radius"]
|
||||
)
|
||||
|
||||
pygame.display.flip()
|
||||
|
||||
pygame.quit()
|
||||
sys.exit()
|
||||
|
||||
|
||||
# Programme principal
|
||||
if __name__ == "__main__":
|
||||
show_menu() # Afficher le menu de démarrage
|
||||
game_loop() # Lancer le jeu
|
||||
|
||||
|
||||
Reference in New Issue
Block a user