Tutorials Basic

Here you can find tutorial guiding you through process of making simple game with Python and PyGame. Starting from scratch we will make simple yet useful game engine (backbone of every game), and few games showing by example how complete games look like.
Hope you will enjoy it!


Probably best programming language out there! Chosen because of its simplicity and small learning curve.


PyGame can handle time, video (both still images and vids), music, fonts, different image formats, cursors, mouse, keyboard, Joysticks and much much more. And all of that is very simple.


PyGame is based on SDL. A C library that is cross platform and also very simple. PyGame however is not just simple wrapper for it, but also add its own features, so writing games is even simpler.

Versions used during this tutorial:

  • Python 2.7
  • PyGame 1.9.1

If you can not run tutorial examples in Python 3, pleas report it as bugs. However there are many out there who still use Python 2, so it will be used as primary version.

Source code

Last thing before we start our codding.
Source code for this tutorial is hosted at bitbucket.com
If you want to you can clone whole repo. Each tutorial have its own tag or multiple tags if there are more then one version of code discussed during tutorial.
If you do not know what I'm talking about, then do not worry! I will provide links to downloads of source code for and at the bottom of each tutorial.

Game loop

Before writing first lines of code I will mention about how game loop is designed in PyGame. Game loop is the place where events, game logic and rendering onto screen is performed. It should look similar to:

while True:

Where event() proceeds events like pressed keys, mouse motion etc. loop() compute changes in the game world like NPC's moves, player moves, AI, game score. And render() just print out on the screen graphic. That separation of tasks makes your game design easier and allow to easy changes in code when new ideas rise in you head.

First code

Its time for more code:

import pygame
from pygame.locals import *
class App:
    def on_execute(self):
if __name__ == "__main__" :
    theApp = App()

As you can see I prefer classes than functions. That is because OOP is getting more and more fans in game programming due to it correlation with game design. Simply in game we have objects, theirs tasks etc. even when programing without classes.

App will be our main class. To run game we will only need call on_execute() function. Yes this function will contain our Game Loop.

Creating simple window

Lets add some more code:

import pygame
from pygame.locals import *
class App:
    def __init__(self):
        self._running = True
        self._display_surf = None
        self.size = self.weight, self.height = 640, 400
    def on_init(self):
        self._display_surf = pygame.display.set_mode(self.size, pygame.HWSURFACE | pygame.DOUBLEBUF)
        self._running = True
    def on_event(self, event):
        if event.type == pygame.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()


You should see black window. If window don't wont to quit use 'xkill' under linux or task manager under windows.

on_init on_event on_loop on_render on_cleanup

on_init calls pygame.init() that initialize all PyGame modules. Then it create main display - 640x400 window and try to use hardware acceleration. At the end this routine sets _running to True.
on_event check if Quit event happened if so sets _running to False wich will break game loop.
on_loop and OnRender do nothing.
on_cleanup call pygame.quit() that quits all PyGame modules. Anything else will be cleaned up by Python.


on_execute initialize pygame than enter main loop in which check events and then compute and render everything till _running is "True" and only Quit event will set it to "False".
Before quitting it will cleanup.

If you will run this file as program it will create instance of App and call its on_execute(). (3 last lines)

Big thx to people who helped me to improve this tutorial by woicing their ideas in comments (which are now deleted):

  • Sin
  • fidget
  • Alan
  • aanund
  • Kwright

Zipped source code:

Small example.
Big example

Add a New Comment