import pygame
pygame.init()
pygame.font.init()
my_font = pygame.font.SysFont("monospace", 30)
text_surface = my_font.render("YOU LOSE", True, (255, 0, 0))
width = 800
height = 600
ecran = pygame.display.set_mode((width, height))
pygame.display.set_caption("PONG")
fond = pygame.image.load("backgroundpong.jpg").convert()
fond = pygame.transform.scale(fond, (width, height))
horloge = pygame.time.Clock()
balais_image = pygame.image.load("balais").convert_alpha()
balais_image = pygame.transform.scale(balais_image, (180, 50))
balais_rect = balais_image.get_rect(midbottom=(400, 550))
ball_image = pygame.image.load("ball").convert_alpha()
ball_image = pygame.transform.scale(ball_image, (40, 40))
ball_rect = ball_image.get_rect(topleft=(60, 60))
ballspeed = [5, 5]
speed = 10
rungame = True
game_over = False
while rungame:
for event in pygame.event.get():
if event.type == pygame.QUIT:
rungame = False
ecran.blit(fond, (0, 0))
if not game_over:
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
balais_rect.x -= speed
if keys[pygame.K_RIGHT]:
balais_rect.x += speed
balais_rect.x = max(
0, min(balais_rect.x, width - balais_rect.width)
)
ball_rect = ball_rect.move(ballspeed)
if ball_rect.top <= 0:
ball_rect.top = 0
ballspeed[1] = abs(ballspeed[1])
if ball_rect.left <= 0:
ball_rect.left = 0
ballspeed[0] = abs(ballspeed[0])
if ball_rect.right >= width:
ball_rect.right = width
ballspeed[0] = -abs(ballspeed[0])
if ball_rect.colliderect(balais_rect) and ballspeed[1] > 0:
ball_rect.bottom = balais_rect.top
ballspeed[1] = -abs(ballspeed[1])
if ball_rect.top >= height:
game_over = True
ecran.blit(ball_image, ball_rect)
ecran.blit(balais_image, balais_rect)
if game_over:
ecran.blit(
text_surface,
text_surface.get_rect(center=(width // 2, height // 2))
)
pygame.display.flip()
horloge.tick(60)
pygame.quit()
To embed this project on your website, copy the following code and paste it into your website's HTML: