0% found this document useful (0 votes)
18 views

Code 2

b

Uploaded by

pronnier.evan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Code 2

b

Uploaded by

pronnier.evan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import pygame

import random

# Initialisation de Pygame
pygame.init()

# Dimensions de la fenêtre
WIDTH, HEIGHT = 800, 600
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Jeu de Balle")

# Couleurs
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)

# Paramètres de la balle
ball_radius = 15
ball_x = random.randint(ball_radius, WIDTH - ball_radius)
ball_y = ball_radius
ball_speed_x = 5
ball_speed_y = 5

# Paramètres de la plateforme
paddle_width = 100
paddle_height = 15
paddle_x = WIDTH // 2 - paddle_width // 2
paddle_y = HEIGHT - 50
paddle_speed = 10

# Score
score = 0
font = pygame.font.Font(None, 36)

# Boucle principale
running = True
clock = pygame.time.Clock()

while running:
win.fill(BLACK)

# Quitter le jeu
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

# Contrôle de la plateforme
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and paddle_x > 0:
paddle_x -= paddle_speed
if keys[pygame.K_RIGHT] and paddle_x < WIDTH - paddle_width:
paddle_x += paddle_speed

# Mouvement de la balle
ball_x += ball_speed_x
ball_y += ball_speed_y

# Collision avec les murs


if ball_x <= ball_radius or ball_x >= WIDTH - ball_radius:
ball_speed_x *= -1
if ball_y <= ball_radius:
ball_speed_y *= -1

# Collision avec la plateforme


if paddle_y <= ball_y + ball_radius <= paddle_y + paddle_height and paddle_x <= ball_x <=
paddle_x + paddle_width:
ball_speed_y *= -1
score += 1

# Perte si la balle touche le bas


if ball_y >= HEIGHT - ball_radius:
running = False # Fin du jeu

# Affichage de la balle et de la plateforme


pygame.draw.circle(win, RED, (ball_x, ball_y), ball_radius)
pygame.draw.rect(win, BLUE, (paddle_x, paddle_y, paddle_width, paddle_height))

# Affichage du score
score_text = font.render("Score: " + str(score), True, WHITE)
win.blit(score_text, (10, 10))

# Rafraîchissement de la fenêtre
pygame.display.flip()
clock.tick(60)

pygame.quit()

You might also like