Python and classic arcade games: Pong

Let’s start

In this post you will find the whole code to make a game with Python, finally. You will find also a live coding video here (go check below).

Let’s examine the different parts of the code.

But, first, let’s take a look to the screen of the game:

Part 1: Import Pygame

You need pygame. I suggest you to use python 3.7 at the moment, because pygame does not works for python 3.8 (we’re at november 2019… it will work soon, so if you read this later, try to make it with 3.8, 3.9 or… 4.0!).

import pygame

Part 2: The global variables

Nothing fancy here, just variables for colors and for the positions of:

  • player 1 (x1, y1)
  • player 2 (x2, y2)
  • the ball (xb, yb)

This are useful for:

  • moving the “sprites”
  • intercept the collision among ball and sprites
  • intercept when the ball goes out of the screen
  • intercept when the ‘bar’ toucht the limit of the screen

So… they are simple variables, but make the game work.

P.S.: there are the 2 variables for the score too and the direction of the ball (left or right, up or down… this are useful for bouncing the ball on the walls or on the sprite bars).

BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# Coordinates p1, p2 and ball
x1 = 490
y1 = 250
x2 = 0
y2 = 250
xb = 300
yb = 300

dbo = 'left'
dbv = 'down'

scorep1 = 0
scorep2 = 0

Part 3: Initialization of pygame and screen

This is for the frame rate and the screen size and the title of the screen (that will have score in it).

clock = pygame.time.Clock()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption("My game" + "Score player 1: " + str(scorep1) + " - Score player 2: " + str(scorep2))

pygame.init()

Part 4: Draw the ball… it’s a circle

def ball():
	"Draw the ball"
	global xb, yb
	pygame.draw.ellipse(screen, GREEN, (xb, yb, 10, 10))

The position of the ball uses global variables that we defined above (part 2).

Part 5: draw sprite 1 and 2

def sprite1(x,y):
	"Draw Player 1"
	pygame.draw.rect(screen, RED, (x, y, 10, 50))

def sprite2(x,y):
	"Draw Player 2"
	pygame.draw.rect(screen, GREEN, (x, y, 10, 50))

This are the bars (a couples of rectangles)

Move the ball please

The ball start moving from left and right (going up or down) and if finds a wall it bounces.

def move_ball(x,y):
	"The ball moves"
	global xb, yb, dbo, dbv
	if dbo == "left":
		xb -= 10
	if dbv == 'down':
		yb += 10
		if yb > 490:
			dbv = 'up'
	if dbv == 'up':
		yb -= 10
		if yb < 10:
			dbv = 'down'
	if dbo == "right":
		xb += 10

What if… collision?

If the ball meets a bar bounce back, else… it restart and give score to the player.

def collision():
	global x1, y1 # the player 1 x and y (on the right)
	global x2, y2 # the player 2 x and y (on the left)
	global xb, yb # the ball x and y
	global dbo
	global scorep1, scorep2
	if dbo == "left":
		if xb < 10:
			if yb >= y2 and  yb < y2 + 50:
				dbo = "right"
			else:
				pygame.draw.ellipse(screen, BLACK, (xb, yb, 10, 10))
				pygame.display.update()
				xb, yb = 300, 300
				scorep2 += 10
				pygame.display.set_caption("My game" + "Score player 1: " + str(scorep1) + " - Score player 2: " + str(scorep2))
	else:
		if xb > 480:
			if yb >= y1 and  yb < y1 + 50:
				dbo = "left"		
			else:
				pygame.draw.ellipse(screen, BLACK, (xb, yb, 10, 10))
				pygame.display.update()
				xb, yb = 300, 300
				scorep1 += 10
				pygame.display.set_caption("My game" + "Score player 1: " + str(scorep1) + " - Score player 2: " + str(scorep2))

How to move the bars

The bar moves with a and z and k and m for player 1 and 2 respectively.

def move1():
	global y2
	if y2 <= 450:
		if keys[pygame.K_z]:
			y2 += 20
	if y2 > 0:
		if keys[pygame.K_a]:
			y2 -= 20

def move2():
	global y1
	if y1 <= 450:
		if keys[pygame.K_m]:
			y1 += 20
	if y1 > 0:
		if keys[pygame.K_k]:
			y1 -= 20

Finally: the loop that makes the game and ball go round

This is the infinite loop that moves the objects, update the screen, looks for collision and ends the game when you press the quit button of the window.

loop = 1
while loop:
	keys = pygame.key.get_pressed()
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			loop = 0
	move1()
	move2()
	move_ball(xb, yb)
	ball()
	sprite1(x1,y1)
	sprite2(x2,y2)
	collision()
	pygame.display.update()
	screen.fill((0, 0, 0))
	clock.tick(30)



pygame.quit()

Now, that we examined the parts of the game, go and watch the video with the live coding. At the end you will find the whole code.

The full live coding video about Pong with Python

So, this is the full live coding of the video, made… live. You will find some pauses in the video. The first 35 minutes are without audio. The last part is with audio.

The Whole code of Pong v. 1.0

# pong!
import pygame


BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# Coordinates p1, p2 and ball
x1 = 490
y1 = 250
x2 = 0
y2 = 250
xb = 300
yb = 300

dbo = 'left'
dbv = 'down'

scorep1 = 0
scorep2 = 0

clock = pygame.time.Clock()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption("My game" + "Score player 1: " + str(scorep1) + " - Score player 2: " + str(scorep2))

pygame.init()

def ball():
	"Draw the ball"
	global xb, yb
	pygame.draw.ellipse(screen, GREEN, (xb, yb, 10, 10))

def sprite1(x,y):
	"Draw Player 1"
	pygame.draw.rect(screen, RED, (x, y, 10, 50))

def sprite2(x,y):
	"Draw Player 2"
	pygame.draw.rect(screen, GREEN, (x, y, 10, 50))

def move_ball(x,y):
	"The ball moves"
	global xb, yb, dbo, dbv
	if dbo == "left":
		xb -= 10
	if dbv == 'down':
		yb += 10
		if yb > 490:
			dbv = 'up'
	if dbv == 'up':
		yb -= 10
		if yb < 10:
			dbv = 'down'
	if dbo == "right":
		xb += 10
	
def collision():
	global x1, y1 # the player 1 x and y (on the right)
	global x2, y2 # the player 2 x and y (on the left)
	global xb, yb # the ball x and y
	global dbo
	global scorep1, scorep2
	if dbo == "left":
		if xb < 10:
			if yb >= y2 and  yb < y2 + 50:
				dbo = "right"
			else:
				pygame.draw.ellipse(screen, BLACK, (xb, yb, 10, 10))
				pygame.display.update()
				xb, yb = 300, 300
				scorep2 += 10
				pygame.display.set_caption("My game" + "Score player 1: " + str(scorep1) + " - Score player 2: " + str(scorep2))
	else:
		if xb > 480:
			if yb >= y1 and  yb < y1 + 50:
				dbo = "left"		
			else:
				pygame.draw.ellipse(screen, BLACK, (xb, yb, 10, 10))
				pygame.display.update()
				xb, yb = 300, 300
				scorep1 += 10
				pygame.display.set_caption("My game" + "Score player 1: " + str(scorep1) + " - Score player 2: " + str(scorep2))



def move1():
	global y2
	if y2 <= 450:
		if keys[pygame.K_z]:
			y2 += 20
	if y2 > 0:
		if keys[pygame.K_a]:
			y2 -= 20

def move2():
	global y1
	if y1 <= 450:
		if keys[pygame.K_m]:
			y1 += 20
	if y1 > 0:
		if keys[pygame.K_k]:
			y1 -= 20

loop = 1
while loop:
	keys = pygame.key.get_pressed()
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			loop = 0
	move1()
	move2()
	move_ball(xb, yb)
	ball()
	sprite1(x1,y1)
	sprite2(x2,y2)
	collision()
	pygame.display.update()
	screen.fill((0, 0, 0))
	clock.tick(30)



pygame.quit()

Next version: using the mouse to move the bars

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.