Game development with Pygame

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

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.

Continue the next tutorial and learn how to add game logic and build games 🙂


13 thoughts on “Game development with Pygame

  1. Phil - June 13, 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 🙂

    1. Frank - June 13, 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.

  2. Jordan - May 15, 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?

    1. Frank - May 15, 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.

      1. Jordan - May 18, 2015

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

        1. Frank - May 18, 2015

          Hi Jordan, I’ll add more tutorials which will be helpful in achieving your goal 🙂

      2. Yasir - October 29, 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.

        1. Frank - October 31, 2015

          Sure, could you post your code?

  3. Thor - May 14, 2015

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

    1. Frank - May 14, 2015

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

      1. Thor - May 24, 2015

        A jump-n-run in the line of Bruce Lee – C64 style 🙂

        1. Frank - May 24, 2015

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

          1. Thor - May 29, 2015

            Thankzzzzz 🙂

Leave a Reply