Python Forum
Atari Ping Pong Game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Atari Ping Pong Game
#1
import pygame
from random import randint
from math import sin, cos
pygame.init()
 
window = pygame.display.set_mode((800, 600))
keys = pygame.key.get_pressed()
font = pygame.font.Font(None, 100)
 
class Paddle:
    def __init__(self, playernum):
        self.instance = pygame.Surface((10, 70))
        self.rect = self.instance.get_rect(center=((10 if playernum == 1 else 790), 300))
        self.playernum = playernum
        self.instance.fill('white')
    def frame(self, ):
        if self.playernum == 1:
            if keys[pygame.K_w]:
              self.rect.y -= 3
            if keys[pygame.K_x]:
              self.rect.y += 3
        else:
            if keys[pygame.K_i]:
                self.rect.y -= 3
            if keys[pygame.K_m]:
                self.rect.y += 3
 
class Ball:
    def __init__(self):
        self.instance = pygame.Surface((25, 25))
        self.rect = self.instance.get_rect(center=(400, 300))
        self.direction = 270 if randint(1, 2) == 1 else 90
         
        self.instance.fill('white')
    def frame(self):
        global p1score
        global p2score
        if self.rect.colliderect(p1.rect) or self.rect.colliderect(p2.rect) or self.rect.y > 600 or self.rect.y < 0:
            self.direction += 90
        self.rect.x += 3 * sin(self.direction)
        self.rect.y += 3 * cos(self.direction)
             
        if self.rect.x > 790 or self.rect.x < 10 or keys[pygame.K_r]:
            if self.rect.x > 790:
                p1score += 1
            elif self.rect.x < 10:
                p2score += 1
          
            self.rect.x = 400
            self.rect.y = 300
            self.direction = randint(0, 360)

       
p1, p2 = Paddle(1), Paddle(2)
ball = Ball()
       
p1score = 0
p2score = 0

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
             
    window.fill('black')
       
    keys = pygame.key.get_pressed()
    score = font.render(f'{p1score} | {p2score}', False, 'gray')
       
    p1.frame()
    p2.frame()
    ball.frame()
       
    window.blit(p1.instance, p1.rect)
    window.blit(p2.instance, p2.rect)
    window.blit(ball.instance, ball.rect)
    window.blit(score, score.get_rect(center=(400, 75)))
    
    pygame.display.flip()
    pygame.time.Clock().tick(120)
Reply
#2
The ball bounce is wrong for a pong game. Normally the ball will bounce off the wall so the angle of incidence == angle of reflection. You can do the same for the paddles, but it is more interesting if the ball bounce is different depending on where the ball contacts the paddle.

Ball and Paddle should be sprites. Sprites are similar to what you are doing but provides convenience functions like checking if the ball has struck any paddle, or updating both paddle positions with one function call, or drawing the ball, paddles and even the score with one command.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pygame Pong menator01 0 2,953 Dec-26-2022, 06:48 AM
Last Post: menator01
  Pong clone with classes OhNoSegFaultAgain 1 4,643 May-11-2019, 07:44 PM
Last Post: keames
  Pygame simple pong example metulburr 5 9,328 Nov-07-2016, 06:40 PM
Last Post: Kai.

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020