A Python project – The Pong game in Processing Python Mode
Lesson 8 of 15 · Python projects
A more detailed version:
Pong is covered in detail in four Python and Processing lessons (levels 1–3 and OOP). See: Mini project – Pong.
Pong is covered in detail in four Python and Processing lessons (levels 1–3 and OOP). See: Mini project – Pong.
In this project we will make a simple version of the well-known game Pong.
The game consists of a ball that bounces off the walls and a paddle controlled by the player.
In this project you will learn:
- how to make an animation in Processing Python Mode
- how to move objects on the screen
- how to detect a collision between objects
- how to make a simple game
How the game works
- the ball moves around the screen
- the player moves the paddle left and right
- the ball bounces off the paddle
- if the ball gets past the paddle – the game is over
The game code
x = 300
y = 200
dx = 3
dy = 3
paddle_x = 250
def setup():
size(600,400)
def draw():
global x,y,dx,dy,paddle_x
background(0)
ellipse(x,y,20,20)
rect(paddle_x,370,100,10)
x += dx
y += dy
if x < 0 or x > width:
dx *= -1
if y < 0:
dy *= -1
if y > 360 and paddle_x < x < paddle_x + 100:
dy *= -1
# the ball got past the paddle – game over
if y > height:
textAlign(CENTER)
textSize(32)
text("GAME OVER", width/2, height/2)
noLoop()
def mouseMoved():
global paddle_x
paddle_x = mouseX
How this code works
- ellipse() draws the ball
- rect() draws the paddle
- the variables dx and dy decide the speed of the ball
- the ball bounces off the walls
- the paddle follows the position of the mouse
You can extend the game by adding
a score or by increasing the speed of the ball.
Possible improvements to the project
- add a score
- increase the speed of the ball
- add more levels to the game
- add an opponent
Next project
How to make the project your own:
1) first run the given code without changes, 2) change one thing and predict what will happen, 3) add at least one feature of your own, 4) write down in a notebook what you changed and why. A work log like that is the basis of a creative programming paper.
1) first run the given code without changes, 2) change one thing and predict what will happen, 3) add at least one feature of your own, 4) write down in a notebook what you changed and why. A work log like that is the basis of a creative programming paper.
