Pong level 2 — points, game over and restart

Environment:
the examples in this lesson are written for Processing Python Mode. If you work in the recommended Thonny + py5 environment, most of the code stays the same, and names with a capital letter in the middle are written in lower case with an underscore: mouseX → mouse_x, noStroke() → no_stroke(). See the table of differences.

Lesson 10 of 20 · Python and Processing

In the previous lesson we made the basic version of the game: Mini project: Pong . Now we add:

  • Counting points
  • A game over state
  • Restarting the game

1. Adding variables



# Ball
ball_x = 300
ball_y = 200
dx = 4
dy = 3
ball_size = 20

# Paddle
paddle_width = 100
paddle_height = 15

# Score and game state
score = 0
game_over = False
Tip: You can experiment with the size of the ball, the paddle or the speed of the ball.

2. The resetGame() function



def resetGame():
    global ball_x, ball_y, dx, dy, score, game_over

    ball_x = width / 2
    ball_y = height / 2
    dx = 4
    dy = 3
    score = 0
    game_over = False

Explaining the reset function

It resets all the variables to their starting values when the game ends or the player wants to restart.


3. The main game logic (ORIGINAL)



def setup():
    size(600, 400)
    textSize(20)

def draw():
    global ball_x, ball_y, dx, dy, score, game_over

    background(30)

    if game_over:
        fill(255)
        textAlign(CENTER)
        text("GAME OVER", width/2, height/2)
        text("Press R to restart", width/2, height/2 + 40)
        return

    # Ball
    fill(255)
    ellipse(ball_x, ball_y, ball_size, ball_size)

    # Paddle
    rect(mouseX - paddle_width/2, height - 40,
         paddle_width, paddle_height)

    # Moving the ball
    ball_x += dx
    ball_y += dy

    # Bouncing off the walls
    if ball_x < ball_size/2 or ball_x > width - ball_size/2:
        dx = -dx

    if ball_y < ball_size/2:
        dy = -dy

    # Collision with the paddle (PROBLEM ZONE)
    if (ball_y > height - 50 and
        mouseX - paddle_width/2 < ball_x < mouseX + paddle_width/2):
        dy = -dy
        score += 1

    # The ball falls (game over)
    if ball_y > height:
        game_over = True

    # Score
    fill(255)
    textAlign(LEFT)
    text("Score: " + str(score), 20, 30)

IMPORTANT: there is a logical problem here that often causes a "bug" in Pong games.

4. THE BUG / PROBLEM — why does the ball "pass through the paddle" or behave strangely?


The problem is in the condition:


if (ball_y > height - 50 and
    mouseX - paddle_width/2 < ball_x < mouseX + paddle_width/2):

What is NOT ideal here?

  • The condition for the Y axis is "rough" (height - 50 is not the exact position of the paddle)
  • The ball can "skip over" the paddle between frames
  • Python chaining (a < b < c) can make the logic in games confusing

Explanation:

This notation:
mouseX - paddle_width/2 < ball_x < mouseX + paddle_width/2
IS CORRECT in Python, but in games an explicit AND notation is often better.

5. THE FIX — a stable collision (RECOMMENDED SOLUTION)



# a more precise collision with the paddle
paddle_y = height - 40

if (
    ball_y + ball_size/2 >= paddle_y and
    ball_y - ball_size/2 <= paddle_y + paddle_height and
    ball_x >= mouseX - paddle_width/2 and
    ball_x <= mouseX + paddle_width/2
):
    dy = -abs(dy)
    score += 1

✔ Why is this better?

  • it takes the size of the ball into account
  • it uses the real position of the paddle
  • it uses explicit AND conditions (easier debugging)

6. Extras — misses and GAME OVER after 3 mistakes



misses = 0
max_misses = 3


# if the ball falls
if ball_y > height:
    misses += 1
    resetBall()

    if misses >= max_misses:
        game_over = True
        noLoop()

7. A random new ball



def resetBall():
    global ball_x, ball_y, dx, dy

    ball_x = random(width)
    ball_y = height/2

    dx = random(-4, 4)
    dy = 3

8. Restarting the game



def keyPressed():
    global misses, game_over

    if key == 'r' or key == 'R':
        misses = 0
        game_over = False
        resetGame()
        loop()

9. What did we improve?

  • a stable collision (the ball no longer "passes through")
  • counting misses
  • game over after 3 mistakes
  • a random spawn position for the ball
  • noLoop / loop control of the game

Mini idea:
Try to speed up the ball every time it hits the paddle.

Related lessons


Check your knowledge

Open the quiz

Mini quiz: points and restart

1. Why is a stable collision better than just dy = -dy?