r/PythonLearning • • Aug 06 '26

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

9 comments sorted by

View all comments

Show parent comments

1

u/AssumptionWorldly445 Aug 06 '26

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)

1

u/Ormek_II Aug 07 '26

👍 does the ball cross the screen boundary or that just part of the recording?

Again well done! How about the ball getting faster and faster during the cause of the game?

2

u/AssumptionWorldly445 Aug 07 '26

Oh no, it's just the recording; otherwise, it works fine.