Simple Paddle Game

Importing Modules

The first step is importing the necessary modules. Here we use random for generating random values and pgzrun to run the Pygame Zero game:


import random
import pgzrun
            

Setting Up the Screen

The screen's size is defined here, with a width of 200 pixels and a height of 300 pixels. These smaller dimensions are great for devices with limited display space:


WIDTH = 200
HEIGHT = 300
            

Defining the Paddle

The paddle is a rectangular object, represented by a Rect object. It starts at the bottom of the screen:


paddle = Rect((WIDTH // 2 - 50, HEIGHT - 20), (100, 10))
            

Setting Up the Ball

We create a small rectangular ball and set its initial speed in both x and y directions:


ball = Rect((WIDTH // 2, HEIGHT // 2), (15, 15))
ball_speed = [3, 3]  # Ball's initial speed (x and y directions)
            

Tracking the Score

A variable score is used to track the player's score:


score = 0
            

Drawing the Game Elements

The draw() function clears the screen and draws the paddle, ball, and the current score:


def draw():
    screen.clear()
    screen.draw.filled_rect(paddle, "blue")
    screen.draw.filled_rect(ball, "red")
    screen.draw.text(f"Score: {score}", (10, 10), color="white", fontsize=20)
            

Updating the Game State

The update() function is the core of the game logic. It handles paddle movement, ball movement, collision detection, and scoring. Here's a step-by-step explanation:

Moving the Paddle

The paddle moves left or right when the corresponding arrow keys are pressed. It stops at the screen edges:


if keyboard.left and paddle.left > 0:
    paddle.x -= 5
if keyboard.right and paddle.right < WIDTH:
    paddle.x += 5
  • keyboard.left: Detects if the left arrow key is pressed.
  • paddle.left > 0: Ensures the paddle doesn’t move beyond the left edge.
  • paddle.right < WIDTH: Ensures the paddle doesn’t move beyond the right edge.

Moving the Ball

The ball’s position is updated each frame based on its current speed:


ball.x += ball_speed[0]
ball.y += ball_speed[1]
  • ball_speed[0]: The ball’s speed in the x-direction (horizontal).
  • ball_speed[1]: The ball’s speed in the y-direction (vertical).

Handling Wall Collisions

The ball bounces off walls when it hits the left, right, or top edges:


if ball.left <= 0 or ball.right >= WIDTH:
    ball_speed[0] = -ball_speed[0]
if ball.top <= 0:
    ball_speed[1] = -ball_speed[1]
  • ball.left <= 0: Detects collision with the left wall.
  • ball.right >= WIDTH: Detects collision with the right wall.
  • ball.top <= 0: Detects collision with the top of the screen.
  • Reverses the ball’s direction using -ball_speed.

Detecting Paddle Collisions

The ball bounces off the paddle, and the score increases when this happens:


if ball.colliderect(paddle) and ball_speed[1] > 0:
    ball_speed[1] = -ball_speed[1]
    score += 1
  • ball.colliderect(paddle): Detects if the ball and paddle are touching.
  • ball_speed[1] > 0: Ensures the ball is moving downward before a collision counts.
  • score += 1: Increases the score by 1 for a successful bounce.

5. Detecting Game Over

If the ball falls below the paddle, the game ends:


if ball.top > HEIGHT:
    game_over()
  • ball.top > HEIGHT: Checks if the ball goes below the bottom of the screen.
  • game_over(): Displays the "GAME OVER" message and schedules the game to quit.

Game Over Logic

If the ball falls below the paddle, the game ends. The game_over() function displays a "GAME OVER" message and exits the game after 3 seconds:


def game_over():
    screen.draw.text("GAME OVER", center=(WIDTH // 2, HEIGHT // 2), color="white", fontsize=30)
    clock.schedule_unique(quit_game, 3)  # Quit after 3 seconds

def quit_game():
    exit()
            

Starting the Game

The game starts by calling pgzrun.go(), which enters a loop that repeatedly calls the update() and draw() functions:


pgzrun.go()
            

Copyright © KX Technology Group, LLC. All Rights Reserved.