python logo


Python hosting: Host, run, and code Python in the cloud!

Pygame Python Pygame Python

Welcome to the first tutorial of the series: Building games with Pygame. Games you create with Pygame can be run on any machine that supports Python, including Windows, Linux and Mac OS.

In this tutorial we will explain the fundamental of building a game with Pygame. We’ll start of with the basics and will teach you how to create the basic framework. In the next tutorials you will learn how to make certain types of games.

You may like
Create Space Invaders with Python

PyGame introduction
You’ll end up with a program similar to the one on the right:

A game always starts in an order similar to this (pseudo code):

while running():

The game starts with initialization. All graphics are loaded, sounds are loaded, levels are loaded and any data that needs to be loaded. The game continues running until it receives a quit event. In this game loop we update the game, get input and update the screen. Depending on the game the implementation widely varies, but this fundamental structure is common in all games.

In Pygame we define this as:

import pygame
from pygame.locals import *

class App:

windowWidth = 640
windowHeight = 480
x = 10
y = 10

def __init__(self):
self._running = True
self._display_surf = None
self._image_surf = None

def on_init(self):
self._display_surf = pygame.display.set_mode((self.windowWidth,self.windowHeight), pygame.HWSURFACE)
self._running = True
self._image_surf = pygame.image.load("pygame.png").convert()

def on_event(self, event):
if event.type == QUIT:
self._running = False

def on_loop(self):

def on_render(self):

def on_cleanup(self):

def on_execute(self):
if self.on_init() == False:
self._running = False

while( self._running ):
for event in pygame.event.get():

if __name__ == "__main__" :
theApp = App()

The Pygame program starts with the constructor init(). Once that is finished on_execute() is called. This method runs the game: it updates the events, updates the screen. Finally, the game is deinitialized using on_cleanup().

In the initialiasation phase we set the screen resolution and start the Pygame library:

def on_init(self):
self._display_surf = pygame.display.set_mode((self.windowWidth,self.windowHeight), pygame.HWSURFACE)

We also load the image.

self._image_surf = pygame.image.load("pygame.png").convert()

This does not draw the image to the screen, that occurs in on_render().

def on_render(self):

The blit method draws the image (image_surf) to the coordinate (x,y). In Pygame the coordinates start from (0,0) top left to (wind0wWidth, windowHeight). The method call pygame.display.flip() updates the screen.

You may like
Create Space Invaders with Python

Continue the next tutorial and learn how to add game logic and build games :-)


Leave a Reply:

Thor Thu, 14 May 2015

Thanks! Making games is something I always wanted t
a whack at, Python/PyGame may just bring that within reach

Frank Thu, 14 May 2015

Glad to help! Do you wish to know about any specific type of game?

Jordan Fri, 15 May 2015

I was on the python beginner tutorials and somehow ended up here after the polymorphism stuff. There's too much new stuff here that I don't think I'm ready for. Where do I go from polymorphism?

Frank Fri, 15 May 2015

On which concepts would you like more tutorials? I'll write them if you let me know. You could try the network tutorials or the Tk (gui) tutorials that I'll upload in a second.

Jordan Mon, 18 May 2015

I want to learn any concepts that will be useful in getting me an entry level job without a bachelor's degree.

Frank Mon, 18 May 2015

Hi Jordan, I'll add more tutorials which will be helpful in achieving your goal :-)

Thor Sun, 24 May 2015

A jump-n-run in the line of Bruce Lee - C64 style :)

Frank Sun, 24 May 2015

Added a short tutorial on jump-n-run logic.

Thor Fri, 29 May 2015

Thankzzzzz :)

Phil Sat, 13 Jun 2015

First of all, nice job ! Started off with Python game programming myself, with the help of this tutorial. There is one thing I do not understand though.

def on_execute(self):
if self.on_init() == False:
self._running = False

on_init has no return value defined, how are you able to check on_init() for False?

Would appreciate an answer :)

Frank Sat, 13 Jun 2015

Hi Phil, thanks! At present that statement is not reachable. You could make on_init() return a value in case the game data loading fails. When calling pygame.init(), any errors will cause an exception. You can call pygame.display.get_init() which returns True if the display has been initialised.

Yasir Thu, 29 Oct 2015

Hi Frank!
, i need to make a dynamic GUI on python. It is for simulating a horizon indicator in which the horizon plane translates and rotates showing roll and pitch. i have made a static GUI in PYQT using different Widgets and now i am stuck. can you please guide how can i show pich and roll movements.

Frank Sat, 31 Oct 2015

Sure, could you post your code?