Python PyGame – Pong🎾

In this post we will be creating the Pong game in Python using PyGame. The PyGame library provides us with a suite of modules for coding video games. It enables multimedia support allowing the inclusion of computer graphics and audio files.

In this example we have recreated the classic pong which is one of the first computer games to be ever created. The game is similar to tennis whereby there are two paddles and a single ball. The aim of the game is to hit the moving ball with your paddle, which ever player misses the ball loses. It does sound easy although with each hit the speed of the ball is increased.

Our Pong game uses PyGame, the source code in Python can be found below. This is a single player game where the user moves the paddle using the cursor and the machine paddle follows the location of the ball. This means you will never win!

import pygame

# Initialize pygame
pygame.init()

# Set the window size
screen_size = (600, 400)

# Create the window
screen = pygame.display.set_mode(screen_size)

# Set the title of the window
pygame.display.set_caption("Pong")

# Set up the colors
BLACK = (0, 0, 0)
GREEN = (0,255,0)

# Set up the paddles
paddle_width = 15
paddle_height = 60
paddle_left = pygame.Rect(0, screen_size[1] / 2 - paddle_height / 2, paddle_width, paddle_height)
paddle_right = pygame.Rect(screen_size[0] - paddle_width, screen_size[1] / 2 - paddle_height / 2, paddle_width, paddle_height)

# Set up the ball
ball_size = 20
ball_x = screen_size[0] / 2
ball_y = screen_size[1] / 2
ball_speed = [2, 2]

# Set up the game clock
clock = pygame.time.Clock()

# Set the frame rate
frame_rate = 60

# Run the game loop
running = True
while running:
    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Update the paddles
    paddle_left.y = pygame.mouse.get_pos()[1] - paddle_height / 2
    paddle_right.y = ball_y - paddle_height / 2

    # Update the ball
    ball_x += ball_speed[0]
    ball_y += ball_speed[1]

    # Check for collisions
    if ball_y < 0 or ball_y > screen_size[1] - ball_size:
        ball_speed[1] *= -1
    if ball_x < paddle_left.x + paddle_width:
        if ball_y > paddle_left.y and ball_y < paddle_left.y + paddle_height:
            ball_speed[0] *= -1
            ball_speed[0] += 0.5
            ball_speed[1] += 0.5
    if ball_x > screen_size[0] - paddle_width - ball_size:
        if ball_y > paddle_right.y and ball_y < paddle_right.y + paddle_height:
            ball_speed[0] *= -1
            ball_speed[0] -= 0.5
            ball_speed[1] -= 0.5
    if ball_x < 0:
        running = False
        print("Right player wins!")
      
    if ball_x > screen_size[0]:
        running = False
        print("Left player wins!")
       
    # Draw the screen
    screen.fill(BLACK)
    pygame.draw.rect(screen, GREEN, paddle_left)
    pygame.draw.rect(screen, GREEN, paddle_right)
    pygame.draw.rect(screen, GREEN, pygame.Rect(ball_x, ball_y, ball_size, ball_size))
    pygame.display.flip()

    # Limit the frame rate
    clock.tick(frame_rate)

# Quit pygame
pygame.quit()


An image of gameplay can be seen below.

Pong game made in Python using PyGame

Take a look at some of our other content around the Python programming language by clicking here.

Leave a Reply