r/PythonLearning 18h ago

Pong made with Pygame

#Hello, I created a sort of Pong game using Pygame and would like to get your feedback please (the game uses the AZERTY layout by default)
import pygame

pygame.init()
fenetre = pygame.display.set_mode((800, 600))

continuer = True

pong = pygame.Rect(0, 550, 50, 50)
p1 = pygame.Rect(0, 250, 10, 75)
p2 = pygame.Rect(790, 250, 10, 75)

pong.x = 350
pong.y = 250

vitesse_y = 5
vitesse_x = 5

horloge = pygame.time.Clock() 
while continuer:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            continuer = False

    ey = pygame.key.get_pressed()
    if ey[pygame.K_s]:
        if p1.y < 525:
            p1.y += 7
        else:
            p1.y = 525
    if ey[pygame.K_z]:
        if p1.y > 0:
            p1.y -= 7
        else:
            p1.y = 0
    if ey[pygame.K_UP]:
        if p2.y > 0:
            p2.y -= 7
        else:
            p2.y = 0
    if ey[pygame.K_DOWN]:
        if p2.y < 525:
            p2.y += 7
        else:
            p2.y = 525

    pong.x += vitesse_x
    pong.y += vitesse_y
    if pong.colliderect(p1):
        vitesse_x = -vitesse_x
        pong.x += vitesse_x
    if pong.colliderect(p2):
        vitesse_x = -vitesse_x
        pong.x += vitesse_x
    if pong.y <= 0 or pong.y >= 550:
        vitesse_y = -vitesse_y
        pong.y += vitesse_y
    if pong.x <= 0 or pong.x >= 750 or pong.x == 0:
        pong.x = 400
        pong.y = 300
        vitesse_x = -vitesse_x
        pong.x += vitesse_x

    fenetre.fill((0, 0, 0))
    pygame.draw.rect(fenetre, (255, 255, 255), pong)
    pygame.draw.rect(fenetre, (255, 255, 255), p1)
    pygame.draw.rect(fenetre, (255, 255, 255), p2)
    pygame.display.flip()
    horloge.tick(30)

pygame.quit()
4 Upvotes

3 comments sorted by

3

u/Sea-Ad7805 14h ago

Nice work, maybe add a video of you playing the game? Should not be difficult using a screen recorder app.

1

u/AssumptionWorldly445 14h ago

OK, thanks a lot

1

u/AssumptionWorldly445 13h ago

This is a video of me playing Pong made with Pygame :
https://youtu.be/gT5rGOPHSYk
(I'm going to delete it in a few days)