This code creates a simple game where the player can move a paddle left and right to catch falling balls. If a ball reaches the bottom without being caught, the game ends. The speed and difficulty can be adjusted by modifying the constants and the frame rate.
import pygame
import sys
import random
# Initialize Pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 800, 600
PADDLE_WIDTH, PADDLE_HEIGHT = 100, 20
BALL_RADIUS = 15
FPS = 60
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
# Create the game window
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Catch the Ball Game")
# Paddle
paddle = pygame.Rect(WIDTH // 2 - PADDLE_WIDTH // 2, HEIGHT - PADDLE_HEIGHT - 10, PADDLE_WIDTH, PADDLE_HEIGHT)
# Ball
balls = []
# Clock to control the frame rate
clock = pygame.time.Clock()
# Game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Move the paddle with arrow keys
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and paddle.left > 0:
paddle.x -= 5
if keys[pygame.K_RIGHT] and paddle.right < WIDTH:
paddle.x += 5
# Create new balls at random positions
if random.randint(0, 100) < 5:
ball = pygame.Rect(random.randint(0, WIDTH - BALL_RADIUS * 2), 0, BALL_RADIUS * 2, BALL_RADIUS * 2)
balls.append(ball)
# Move balls down the screen
for ball in balls:
ball.y += 5
if ball.colliderect(paddle):
balls.remove(ball)
# Check for balls reaching the bottom
for ball in balls:
if ball.y > HEIGHT:
print("Game Over!")
pygame.quit()
sys.exit()
# Draw everything on the screen
screen.fill(WHITE)
pygame.draw.rect(screen, BLUE, paddle)
for ball in balls:
pygame.draw.circle(screen, BLUE, (ball.x + BALL_RADIUS, ball.y + BALL_RADIUS), BALL_RADIUS)
pygame.display.flip()
# Cap the frame rate
clock.tick(45)
Explanation Part Wise:
Import necessary libraries:
import pygame
import sys
import random
The above lines imports the pygame library for game development, the sys module for system-related functions, and the random module for generating random numbers.
Initialize Pygame:
pygame.init()
The above line initializes the Pygame library. It needs to be called before using any Pygame functions.
Set Constants:
WIDTH, HEIGHT = 800, 600
PADDLE_WIDTH, PADDLE_HEIGHT = 100, 20
BALL_RADIUS = 15
FPS = 60
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
These lines define constants for the game, such as the screen width and height, paddle dimensions, ball radius, frames per second, and color values.
Create the game window:
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Catch the Ball Game")
The above lines create the game window with the specified dimensions and set the window caption.
Initialize Paddle:
paddle = pygame.Rect(WIDTH // 2 - PADDLE_WIDTH // 2, HEIGHT - PADDLE_HEIGHT - 10, PADDLE_WIDTH, PADDLE_HEIGHT)
This line initializes the paddle as a rectangular object using the pygame.Rect class. The paddle is positioned at the bottom center of the screen.
Initialize Ball List:
balls = []
This line initializes an empty list to store the ball objects.
Initialize Clock:
clock = pygame.time.Clock()
This line initializes a clock object to control the frame rate of the game.
Game Loop:
while True:
The game loop starts, which continuously runs until the game is closed.
Event Handling:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
This loop handles events, such as the user closing the game window. If the user clicks the close button, the game exits.
Move Paddle with Arrow Keys:
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and paddle.left > 0:
paddle.x -= 5
if keys[pygame.K_RIGHT] and paddle.right < WIDTH:
paddle.x += 5
This block of code checks for arrow key presses and moves the paddle left or right accordingly. The pygame.key.get_pressed() function is used to check the state of all keys.
Create New Balls at Random Positions:
if random.randint(0, 100) < 5:
ball = pygame.Rect(random.randint(0, WIDTH - BALL_RADIUS * 2), 0, BALL_RADIUS * 2, BALL_RADIUS * 2)
balls.append(ball)
This line randomly creates new ball objects and appends them to the balls list. The probability of creating a new ball is controlled by the condition.
Move Balls Down the Screen:
for ball in balls:
ball.y += 5
if ball.colliderect(paddle):
balls.remove(ball)
This block of code iterates through the balls list, moving each ball down the screen. If a ball collides with the paddle, it is removed from the list.
Check for Balls Reaching the Bottom:
for ball in balls:
if ball.y > HEIGHT:
print("Game Over!")
pygame.quit()
sys.exit()
This block of code checks if any ball has reached the bottom of the screen. If so, it prints “Game Over!” to the console, quits Pygame, and exits the script.
Draw Everything on the Screen:
screen.fill(WHITE)
pygame.draw.rect(screen, BLUE, paddle)
for ball in balls:
pygame.draw.circle(screen, BLUE, (ball.x + BALL_RADIUS, ball.y + BALL_RADIUS), BALL_RADIUS)
These lines draw the paddle and balls on the game window
Update the Display:
pygame.display.flip()
This line updates the display, making the drawn elements visible.
Cap the Frame Rate:
clock.tick(45)
This line limits the frame rate of the game to 45 frames per second. Adjusting this value can control the speed of the game.





Leave a Reply