Pong-Style Paddle Game in Python

Python game 6 of 20

Mediumโฑ 40 min๐Ÿ“„ 84 lines of Pythonโ˜… 3 challenges๐ŸŽฎ Browser demo + turtle code

Paddle Ball is a Pong-style tennis game: two paddles, one ball, and a lot of bouncing. Writing it in Python teaches you how games really move things: the ball has a speed in x and a speed in y, and every frame you add the speed to the position. Bouncing is just flipping the sign.

Play against the computer in the browser version, then read the Python turtle code: two players share one keyboard (W/S and the arrow keys).

Play Paddle Ball

Score 0

This is a browser copy of the game so you can play it here. The Python version below opens its own window on your computer (how to run it).

How to play

  • Move your paddle up and down (W/S or arrow keys, or drag on a phone).
  • Hit the ball back past the other paddle to score.
  • First to 5 points wins.

The complete Paddle Ball code

Here is the whole program (84 lines). Save it as pong.py and run it with Python 3: the turtle window opens and the game starts.

import turtle

screen = turtle.Screen()
screen.title("Paddle Ball")
screen.bgcolor("black")
screen.setup(800, 600)
screen.tracer(0)


def make(shape, x, w=1, h=1):
    t = turtle.Turtle(shape)
    t.color("white")
    t.shapesize(stretch_wid=h, stretch_len=w)
    t.penup()
    t.goto(x, 0)
    return t


left = make("square", -350, h=5)
right = make("square", 350, h=5)
ball = make("circle", 0)
ball.dx, ball.dy = 4, 4                  # speed in x and y

score = [0, 0]
pen = turtle.Turtle()
pen.hideturtle()
pen.color("white")
pen.penup()
pen.goto(0, 250)


def show_score():
    pen.clear()
    pen.write(f"{score[0]}   {score[1]}", align="center", font=("Courier", 32, "bold"))


def paddle(p, dy):
    y = p.ycor() + dy
    if -250 < y < 250:
        p.sety(y)


screen.listen()
screen.onkeypress(lambda: paddle(left, 30), "w")
screen.onkeypress(lambda: paddle(left, -30), "s")
screen.onkeypress(lambda: paddle(right, 30), "Up")
screen.onkeypress(lambda: paddle(right, -30), "Down")
show_score()


def frame():
    ball.setx(ball.xcor() + ball.dx)
    ball.sety(ball.ycor() + ball.dy)

    if abs(ball.ycor()) > 290:          # top and bottom walls
        ball.dy = -ball.dy

    if ball.xcor() > 390:               # right player missed
        score[0] += 1
        ball.goto(0, 0)
        ball.dx = -4
        show_score()
    elif ball.xcor() < -390:            # left player missed
        score[1] += 1
        ball.goto(0, 0)
        ball.dx = 4
        show_score()

    # paddle hits: the ball is level with a paddle and close to its centre
    if 330 < ball.xcor() < 350 and abs(ball.ycor() - right.ycor()) < 55 and ball.dx > 0:
        ball.dx = -ball.dx * 1.05
    if -350 < ball.xcor() < -330 and abs(ball.ycor() - left.ycor()) < 55 and ball.dx < 0:
        ball.dx = -ball.dx * 1.05

    screen.update()
    if max(score) < 5:
        screen.ontimer(frame, 16)
    else:
        pen.goto(0, 0)
        pen.write("Left wins!" if score[0] == 5 else "Right wins!", align="center", font=("Courier", 28, "bold"))


frame()
screen.mainloop()

How the Paddle Ball game works, step by step

01Give the ball a velocity

The ball stores two speeds: dx (sideways) and dy (up and down). Every frame, position + speed = new position. That's all movement in a 2D game is.

ball.dx, ball.dy = 4, 4
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)

02Bounce off the walls

When the ball goes past the top or bottom, flip the sign of dy. Moving up becomes moving down at the same speed.

if abs(ball.ycor()) > 290:
    ball.dy = -ball.dy

03Hit the paddles

A hit happens when the ball is level with a paddle's x position and its y is within the paddle's half-height. Flip dx and multiply by 1.05 so rallies speed up.

if 330 < ball.xcor() < 350 and abs(ball.ycor() - right.ycor()) < 55:
    ball.dx = -ball.dx * 1.05

04Score and serve

If the ball passes a paddle, the other player scores, the ball goes back to the middle, and it is served towards the player who just lost the point.

if ball.xcor() > 390:
    score[0] += 1
    ball.goto(0, 0)

05Keyboard and timer

onkeypress moves a paddle 30 pixels per key press, and ontimer(frame, 16) runs the next frame about 60 times a second.

screen.onkeypress(lambda: paddle(left, 30), "w")
screen.ontimer(frame, 16)

What you'll learn

  • Velocity: moving with dx and dy
  • Bouncing by flipping a sign
  • Collision checks with abs() and ranges
  • Several turtles as game objects
  • Keyboard control with onkeypress()

Python concepts in this game:

Paddle Ball coding challenges

Write the key parts of the game yourself. Press Run to try your code and Check to test it. Each challenge you pass earns a star (saved in this browser).

1/3

Bounce off the walls

Write wall_bounce(y, dy): if y is above 290 or below -290, return -dy; otherwise return dy unchanged.

challenge 1 ยท edit, Run, then Check
Hint

abs(y) > 290 covers both walls in one test.

Show a solution
def wall_bounce(y, dy):
    if abs(y) > 290:
        return -dy
    return dy

print(wall_bounce(295, 4))
2/3

Did the paddle hit it?

Write hits_paddle(ball_y, paddle_y): the paddle is 100 pixels tall, centred on paddle_y. Return True if the ball's y is less than 55 away from the centre.

challenge 2 ยท edit, Run, then Check
Hint

The distance between two numbers is abs(a - b).

Show a solution
def hits_paddle(ball_y, paddle_y):
    return abs(ball_y - paddle_y) < 55

print(hits_paddle(30, 0))
3/3

Move the ball for 3 frames

Start with x, y = 0, 280 and speeds dx, dy = 5, 6. Run a loop 3 times: add the speeds to the position, then bounce dy if abs(y) > 290. Print the final x y dy separated by spaces.

challenge 3 ยท edit, Run, then Check
Hint

Inside the loop: x += dx, y += dy, then if abs(y) > 290: dy = -dy. In frame 2 the ball reaches y = 292 and turns round.

Show a solution
x, y = 0, 280
dx, dy = 5, 6
for frame in range(3):
    x += dx
    y += dy
    if abs(y) > 290:
        dy = -dy

print(x, y, dy)

Make it your own

Once your challenges pass, try one of these upgrades:

  • Add a computer player that follows the ball (with a speed limit so it can miss).
  • Change the bounce angle depending on where the ball hits the paddle.
  • Play a sound on every hit with the winsound module (Windows).
  • Add a pause key.

Frequently asked questions

How do you make a Pong-style game in Python?

Create two paddle turtles and a ball turtle, give the ball dx and dy speeds, move it every frame with ontimer, flip dy at the walls and dx at the paddles, and add a point when the ball passes a paddle.

Do I need pygame to make a paddle game?

No. The turtle module that comes with Python is enough. pygame gives you more speed and control once you're ready for it.

Why does the ball sometimes go through the paddle?

If the ball moves more pixels per frame than the paddle check allows, it can jump over it. Keep the speed limited or check the whole path the ball moved along.

More games like this