Ping Pong game in Pygame python

michael_linson_0078

Michael Linson

Posted on November 24, 2024

Ping Pong game in Pygame python

Importing

import pygame
import sys
Enter fullscreen mode Exit fullscreen mode

Pygame is the module we are using to make games. It provided us with tools for graphics, sound, and more.

sys is a module in Python that helps us interact with the Python interpreter.

Intializes

pygame.init()
Enter fullscreen mode Exit fullscreen mode

Initializes all the Pygame modules and makes them ready to use.

Constants

#dimensions
WIDTH, HEIGHT=800,600
#frame rate
FPS=60
#the paddles at the side of ping pong
PADDLE_WIDTH, PADDLE_HEIGHT=15,90
#the balls radius
BALL_RADIUS=15
#the color of the ball and paddle
WHITE=(255, 255, 255)
Enter fullscreen mode Exit fullscreen mode
  • WIDTH and HEIGHT: Dimensions of the game window. 800px is for the width and 600px is for the height
  • FPS: Frames per second, which controls the game’s speed and smoothness.
  • PADDLE_WIDTH, PADDLE_HEIGHT: Dimensions of the paddles.
  • BALL_RADIUS: Radius of the ball.
  • WHITE: The RGB value for white, is used for paddles, ball, and text.

Make a Screen

screen=pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Ping Pong")
Enter fullscreen mode Exit fullscreen mode

you will have a window named Ping Pong with the assigned WIDTH and HEIGHT

window named Ping Pong

Paddles and Ball setup

left_paddle=pygame.Rect(50, HEIGHT//2 - PADDLE_HEIGHT //2, PADDLE_WIDTH, PADDLE_HEIGHT)

right_paddle=pygame.Rect(WIDTH - 50 - PADDLE_WIDTH, HEIGHT //2- PADDLE_HEIGHT //2, PADDLE_WIDTH, PADDLE_HEIGHT)

ball=pygame.Rect(WIDTH //2 - BALL_RADIUS, HEIGHT //2 - BALL_RADIUS, BALL_RADUIS *2, BALL_RADIUS *2)
Enter fullscreen mode Exit fullscreen mode

Representing (0,0)

In Pygame the left top corner of the screen represents (0,0) in coordinates.

  • pygame.Rect: Is used to create rectangles in pygame(used here for the paddles and the ball).
pygame.Rect(x, y, width, height)
Enter fullscreen mode Exit fullscreen mode
  • left_paddle: Positioned near the left side of the screen, vertically centered.
pygame.Rect(50, HEIGHT//2 - PADDLE_HEIGHT //2, PADDLE_WIDTH, PADDLE_HEIGHT)
Enter fullscreen mode Exit fullscreen mode
  1. First, we position the left paddle 50px towards the right from the left side.

  2. Then we do HEIGHT//2 - PADDLE_HEIGHT //2 because if you just did HEIGHT//2 it will look like the way it is in the picture. It goes down the screen. To center it we do - PADDLE_HEIGHT //2

centering the paddle

This is what we did for the right paddle to center it.

  • right_paddle: Positioned near the right side of the screen, vertically centered.
right_paddle=pygame.Rect(WIDTH - 50 - PADDLE_WIDTH, HEIGHT //2- PADDLE_HEIGHT //2, PADDLE_WIDTH, PADDLE_HEIGHT)
Enter fullscreen mode Exit fullscreen mode
  • ball: Initially positioned in the center of the screen.
ball=pygame.Rect(WIDTH //2 - BALL_RADIUS, HEIGHT //2 - BALL_RADIUS, BALL_RADUIS *2, BALL_RADIUS *2)
Enter fullscreen mode Exit fullscreen mode

For the ball to center it, we subtracted by the radius.

Speed

ball_speed_x=7
ball_speed_y=7
paddle_speed=10
Enter fullscreen mode Exit fullscreen mode

ball_speed_x and ball_speed_y controls the horizontal and vertical speed of the ball.

paddle_speed: Controls the movement speed of the paddles.

Score Variables

left_score=0
right_score=0
font=pygame.font.SysFont(None,55)
Enter fullscreen mode Exit fullscreen mode
  • left_score and right_score: Track the scores of the players.
  • font: Used to render text on the screen for scores. None uses the default font, and 55 is the font size.

Function to draw everything

def draw():
   screen.fill((0,0,0)) #Fill the screen with black
   pygame.draw.rect(screen, WHITE, left_paddle)
   pygame.draw.rect(screen, WHITE, right_paddle)
   pygame.draw.ellipse(screen, WHITE, ball)
Enter fullscreen mode Exit fullscreen mode
  • fill((0, 0, 0)): Fills the screen with black (RGB: 0, 0, 0).
  • pygame.draw.rect: Draws the rectangular paddles.
  • pygame.draw.ellipse: Draws the ball as a circle (bounded by the rectangle ball).

Draw the center line

   pygame.draw.aaline(screen, WHITE, (WIDTH //2, 0), (WIDTH //2, HEIGHT))
Enter fullscreen mode Exit fullscreen mode
  • Draws a vertical center line to divide the playing field.

Draw Scores

   left_text=font.render(str(left_score),True, WHITE)
   screen.blit(left_text, (WIDTH // 4 - left_text.get_width() // 2, 20))
   right_text=font.render(str(right_score), True, WHITE)
   screen.blit(right_text, (WIDTH * 3 // 4 - right_text.get_width() //2, 20))
Enter fullscreen mode Exit fullscreen mode

Renders the scores for both players and positions them on the screen.

Update the screen

   pygame.display.flip()

Enter fullscreen mode Exit fullscreen mode

Updates the display with the latest changes.

#Main game loop
while True:
Enter fullscreen mode Exit fullscreen mode

Keeps the game running indefinitely.

for event in pygame.event.get():
   if event.type == pygame.QUITT:
      pygame.quit()
      sys.exit()
Enter fullscreen mode Exit fullscreen mode

This will go through all the events that can happen in pygame and if one of them is closing the window then quit pygame and close the window.

Paddle Controls

#Paddle controls
keys pygame.key.get_pressed()
if keys [pygame.K_w] and left_paddle.top > 0: 
   left_paddle.y-=paddle_speed
if keys [pygame.K_s] and left_paddle.bottom < HEIGHT: 
   left_paddle.y += paddle_speed 
if keys [pygame.K_UP] and right_paddle.top > 0: 
   right_paddle.y -= paddle_speed
if keys [pygame.K_DOWN] and right_paddle.bottom < HEIGHT: 
   right_paddle.y += paddle_speed 66

Enter fullscreen mode Exit fullscreen mode

Detects key presses:

  • W and S: Move the left paddle up and down.
    • pygame.K_w is the w key
    • pygame.K_s is the s key
  • UP and DOWN: Move the right paddle up and down.
    • pygame.K_UP is the up key
    • pygame.K_DOWN is the down key
  • Includes checks to prevent paddles from moving off the screen.
    • left_paddle.top > 0checks to see if top of paddles coordinates is greater than 0. To check to see if it is hitting the top of the screen when you click W.
    • left_paddle.bottom < HEIGHTchecks to see if bottom of paddles coordinates is greater than height of the screen. To check to see if it is hitting the bottom of the screen when you click K.
    • right_paddle.top > 0checks to see if top of paddles coordinates is greater than 0. To check to see if it is hitting the top of the screen when you click Up key.
    • right_paddle.bottom < HEIGHTchecks to see if bottom of paddles coordinates is greater than height of the screen. To check to see if it is hitting the bottom of the screen when you click Down key.

Ball movement

   ball.x += ball_speed_x
   ball.y + ball_speed_y
Enter fullscreen mode Exit fullscreen mode

Moves the ball by adding its speed to its current position

Ball collision with top and bottom walls

if ball.top <= 0 or ball.bottom >= HEIGHT:
   ball_speed_y=-ball_speed_y
Enter fullscreen mode Exit fullscreen mode

Reverses the ball's vertical direction if it hits the top or bottom of the screen

Ball collision with paddles

if ball.colliderect(left_paddle) or ball.colliderect(right_paddle): 
   ball_speed_x = -ball_speed_x
Enter fullscreen mode Exit fullscreen mode

Reverses the ball's horizontal direction if it collides with a paddle.

Scoring

If ball.left <- 8:
   right score + 1
   pygame.Rect(WIDTH //2 -BALL RADIUS, HEIGHT //2 - BALL RADIUS, BALL 
   RADIUS *2, BALL RADIUS *2) 
   ball_speed_x=-ball_speed_x
If ball.right >= WIDTH: 
   left score +=1
   ball=pygame.Rect(WIDTH //2 - BALL RADIUS, HEIGHT //2 - BALL RADIUS, BALL RADIUS * 2, BALL RADIUS * 2) 
   ball_speed_x=-ball_speed_x

draw()
Enter fullscreen mode Exit fullscreen mode
  • Updates the score if the ball goes out of bounds.
  • Resets the ball to the center and reverses its direction.

Timing

pygame.time.Clock().tick (FPS)
Enter fullscreen mode Exit fullscreen mode

Limits the game to run at a maximum of 60 frames per second, ensuring smooth gameplay.

Full code

import pygame
import sys

pygame.init()

# Constants
WIDTH, HEIGHT = 800, 600
FPS = 60
PADDLE_WIDTH, PADDLE_HEIGHT = 15, 90
BALL_RADIUS = 15
WHITE = (255, 255, 255)

# Setup screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pong")

# Paddles and ball setup
left_paddle = pygame.Rect(50, HEIGHT // 2 - PADDLE_HEIGHT // 2, PADDLE_WIDTH, PADDLE_HEIGHT)
right_paddle = pygame.Rect(WIDTH - 50 - PADDLE_WIDTH, HEIGHT // 2 - PADDLE_HEIGHT // 2, PADDLE_WIDTH, PADDLE_HEIGHT)
ball = pygame.Rect(WIDTH // 2 - BALL_RADIUS, HEIGHT // 2 - BALL_RADIUS, BALL_RADIUS * 2, BALL_RADIUS * 2)

# Speeds
ball_speed_x = 7
ball_speed_y = 7
paddle_speed = 10

# Score variables
left_score = 0
right_score = 0
font = pygame.font.SysFont(None, 55)

# Function to draw everything
def draw():
        screen.fill((0, 0, 0))  # Fill screen with black
        pygame.draw.rect(screen, WHITE, left_paddle)
        pygame.draw.rect(screen, WHITE, right_paddle)
        pygame.draw.ellipse(screen, WHITE, ball)

        # Draw the center line
        pygame.draw.aaline(screen, WHITE, (WIDTH // 2, 0), (WIDTH // 2, HEIGHT))

        # Draw scores
        left_text = font.render(str(left_score), True, WHITE)
        screen.blit(left_text, (WIDTH // 4 - left_text.get_width() // 2, 20))
        right_text = font.render(str(right_score), True, WHITE)
        screen.blit(right_text, (WIDTH * 3 // 4 - right_text.get_width() // 2, 20))

        pygame.display.flip()

# Main game loop
while True:
        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                        pygame.quit()
                        sys.exit()

        # Paddle controls
        keys = pygame.key.get_pressed()
        if keys[pygame.K_w] and left_paddle.top > 0:
                left_paddle.y -= paddle_speed
        if keys[pygame.K_s] and left_paddle.bottom < HEIGHT:
                left_paddle.y += paddle_speed
        if keys[pygame.K_UP] and right_paddle.top > 0:
                right_paddle.y -= paddle_speed
        if keys[pygame.K_DOWN] and right_paddle.bottom < HEIGHT:
                right_paddle.y += paddle_speed

        # Ball movement
        ball.x += ball_speed_x
        ball.y += ball_speed_y

        # Ball collision with top and bottom walls
        if ball.top <= 0 or ball.bottom >= HEIGHT:
                ball_speed_y = -ball_speed_y

        # Ball collision with paddles
        if ball.colliderect(left_paddle) or ball.colliderect(right_paddle):
                ball_speed_x = -ball_speed_x

        # Scoring
        if ball.left <= 0:
                right_score += 1
                ball = pygame.Rect(WIDTH // 2 - BALL_RADIUS, HEIGHT // 2 - BALL_RADIUS, BALL_RADIUS * 2, BALL_RADIUS * 2)
                ball_speed_x = -ball_speed_x
        if ball.right >= WIDTH:
                left_score += 1
                ball = pygame.Rect(WIDTH // 2 - BALL_RADIUS, HEIGHT // 2 - BALL_RADIUS, BALL_RADIUS * 2, BALL_RADIUS * 2)
                ball_speed_x = -ball_speed_x

        draw()
        pygame.time.Clock().tick(FPS)
Enter fullscreen mode Exit fullscreen mode

The final output

💖 💪 🙅 🚩
michael_linson_0078
Michael Linson

Posted on November 24, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related