Build a classic Pong game in Python using the Pygame library. This tutorial offers a comprehensive guide with complete code, covering initialization, setting up screen dimensions, paddle and ball settings, drawing functions, handling user input, and game logic.
First, you need to install pygame if you haven’t already:
pip install pygame
Here is the complete code for a simple Pong game:
import pygame
import random
# Initialize Pygame
pygame.init()
# Screen dimensions
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Pong Game")
# Colors
white = (255, 255, 255)
black = (0, 0, 0)
# Paddle settings
paddle_width, paddle_height = 10, 100
paddle_speed = 10
# Ball settings
ball_size = 10
ball_speed_x = 5 * random.choice((1, -1))
ball_speed_y = 5 * random.choice((1, -1))
# Initialize paddle positions
left_paddle = pygame.Rect(10, height // 2 - paddle_height // 2, paddle_width, paddle_height)
right_paddle = pygame.Rect(width - 20, height // 2 - paddle_height // 2, paddle_width, paddle_height)
# Initialize ball position
ball = pygame.Rect(width // 2 - ball_size // 2, height // 2 - ball_size // 2, ball_size, ball_size)
# Scores
left_score, right_score = 0, 0
# Font for score display
font = pygame.font.SysFont("comicsansms", 30)
def draw():
screen.fill(black)
pygame.draw.rect(screen, white, left_paddle)
pygame.draw.rect(screen, white, right_paddle)
pygame.draw.ellipse(screen, white, ball)
pygame.draw.aaline(screen, white, (width // 2, 0), (width // 2, height))
left_text = font.render(f"{left_score}", True, white)
right_text = font.render(f"{right_score}", True, white)
screen.blit(left_text, (width // 4, 20))
screen.blit(right_text, (width * 3 // 4, 20))
pygame.display.flip()
def move_paddles():
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
def move_ball():
global ball_speed_x, ball_speed_y, left_score, right_score
ball.x += ball_speed_x
ball.y += ball_speed_y
if ball.top <= 0 or ball.bottom >= height:
ball_speed_y *= -1
if ball.colliderect(left_paddle) or ball.colliderect(right_paddle):
ball_speed_x *= -1
if ball.left <= 0:
right_score += 1
reset_ball()
if ball.right >= width:
left_score += 1
reset_ball()
def reset_ball():
global ball_speed_x, ball_speed_y
ball.center = (width // 2, height // 2)
ball_speed_x *= random.choice((1, -1))
ball_speed_y *= random.choice((1, -1))
def main():
global left_score, right_score
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
move_paddles()
move_ball()
draw()
clock.tick(60)
pygame.quit()
if __name__ == "__main__":
main()
pygame.init() initializes all the modules required for Pygame.main()):
clock.tick(60).