Recently my son won a bet and as a prize he asked me to teach him how to write a platformer game in PyGame. I love seeing where his imagination takes him, and so, with some Python coding experience already under my belt, I decided to have a go.
What is included
This is Part 1 of a series of platformer tutorials. It aims to cover the following:
- Basic game setup, with PyGame initialisation and the game loop
I’ll continue with the series until we’ve got a playable game. The direction will be shaped by my son’s input, since ultimately he is the target audience.
Here is a screenshot of the finished game. Don’t expect too much from the graphics at this stage, we might get round to later on.

My PyGame programming setup
I’m not a Python developer by trade – although I’ve worked on some Python and Django projects in the past. So I’ve decided to use the tools that I’m familiar with. In this case it’s Visual Studio Code as the code editor. The tools inside the code editor also let me run and debug the PyGame code which is a great help.
My son uses a cloud-based code editor called Replit at school. My goal is that he will be able to copy-paste the code into Replit and run it online without any problems. The simpler the better really. In my experience getting results quickly is the best motivation.
If you’re still looking for information on how to get started with PyGame try heading over to their Getting Started Page. For me it helped but personally I don’t think it’s written with beginners in mind. The language used assumes existing technical knowledge and some knowledge on Python in general. The way that PyGame is taught in schools these days that’s definitely not a given!
Another site I’ve found useful is this PyGame Primer. In the section Background and Setup it shows you how to install PyGame and run it via the command line. Sort of useful if you’re already familiar with command lines and the pip command. I was, so it helped – but if you’re very new to programming this too could be too complex.
Let me know in the comments which starting guide you’ve found the most helpful!
Ok, enough setup – let’s get started with the tutorial.
Initialising PyGame
There are certain things that you have to do for every PyGame game – whether it’s a platformer or any other kind of game. These are:
- Import the PyGame code library
- Initialise PyGame in your code
- Set up a basic game loop
This can all be done in a single file with only a few lines of code. Here is an example:
import pygame
pygame.init()
pygame.display.set_caption("Platformer")
HEIGHT = 450
WIDTH = 400
FPS = 60
FramePerSec = pygame.time.Clock()
displaysurface = pygame.display.set_mode((WIDTH, HEIGHT))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
displaysurface.fill((0, 0, 0))
pygame.display.update()
FramePerSec.tick(FPS)
This is the most basic PyGame starter code that will open up a PyGame window. It’s not super exciting – it’s just a blank screen that looks like this:

Try to get this running first though – if it works then it means that your overall setup is correct and you can safely continue coding.
If it doesn’t work then go back to one of the Getting Started PyGame pages to check that you have installed all the required software.
And finally, once it works, start experimenting. For example:
- Change the WIDTH and HEIGHT variables to change the size of the window
- Change the colour values in the line
displaysurface.fill((0, 0, 0))to see what happens
The Game Loop
The Game Loop is a central part of any PyGame platformer game. It’s such an important concept that it’s worth understanding it a bit more.
Let’s break the code from before down bit by bit. Here it is again with lots of comments added to explain what is happening:
# this line imports the PyGame code library
import pygame
# this line initialises the PyGame code
pygame.init()
# this line sets the title text of the window that popups up
pygame.display.set_caption("Platformer")
# these variables control width, height and frame rate
# you can change these to what you need
HEIGHT = 450
WIDTH = 400
FPS = 60
# with a frame rate of 60 FPS the display needs to update 60
# times per second. The computer needs to use a clock to keep # track of all this. Luckily PyGame has a clock built-in, we
# just need to write the code to use it
FramePerSec = pygame.time.Clock()
# all of the shapes in the game - the player, the platforms,
# the enemies - need to be drawn to the screen.
# PyGame calls it the display. It can be different sizes.
# this line sets the width and height of the display
displaysurface = pygame.display.set_mode((WIDTH, HEIGHT))
# this is the famous GAME LOOP!
# it will just continue forever until you quit the game
while True:
# this next 'for' block checks whether you've quit
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
# every frame the entire game is redrawn
# this next line fills it with a black colour
displaysurface.fill((0, 0, 0))
# this next line tells PyGame to update what the user can
# see on screen
pygame.display.update()
# FramePerSec is our game clock - this next line tells
# PyGame to wait for the next 'tick' of the clock
# It ticks fast enough to render at 60 FPS
# When the clock ticks again, the entire game loop
# runs one more time
FramePerSec.tick(FPS)
Even if you can’t be bothered to read all the explanations I’d like you to understand this:
- The Game Loop is a piece of code that runs again and again, 60 times per second
- Each time it runs, the screen is wiped blank, and everything that is in the game is redrawn from scratch
If you imagine the game as drawing on a piece of paper, 60 times per second the computer erases everything that was drawn before and draws the picture again.
Next Steps
The code you have so far is the basic setup code of a PyGame platformer. Whenever you start coding a new game you can copy-paste this code – it’s really general and can be the starting point for all sorts of different games.
Next, continue with Part 2 where we’ll draw the player graphic.