Pygame book chapter 1 – how to make game windows

This is a new serie for pygame that want to be esaustive about this topic. As we are now at the 2.0.1 version of pygame, we want to take a close look at making videogame with python and pygame.

So, let’s get started making a window and trying to make everything organized with the new app I am currently making with tkinter that you can find here too (link). If you want to go the repository for these files, go to

We will also use a little app to group all the tools (aka scripts) that we can use to make a game. This tool is called pymemo or pybook (I will choose the definitive name as soon as possible), so that you can use it as a manual or just as a bunch of examples that will help you throught your gaming production. So, first things first… let’s make an empty window.

Video tutorial about pygame to make game windows

As we want to be organized, we make a class called Game to display the window that can be closed easily with the quit button (the red button with the x on the screen) or with the Escape button (also know as Esc, on the top left of the fisical keyboard).

This class has an ‘dunder’ init method that runs when you create an istance (in our example is game = Game(400, 400) ). The first thing this istance does is to pass 400 for both width and height of the screen. The the screen surface (remember this name ‘surface’, it will come along frequently) is then created with the awesome statement self.screen = pg.display.set_mode(self.size).

What the self…?

In case you wonder what is self, consider it a placeholder for the istance that you create. In the class you find self, but when you create an istance (like game = Game(400, 400)) – beware that game is the istance and Game is the class) every self magically becomes game. So: self.w for the istance of game will be referred to game.w, self.h will be referred to game.h for the height etc.

So if you want to know the width of the game istance try


and you will get


That’s what the self is for. Is used to call the attributes (variables) or the methods (functions) inside the class, using the istance name instead of the self. You could create another istance of a class, for example game2 and you will have different attributes like game2.w etc. So, classes are very useful.

Second part of this serie here.

Post written by Avatar A python enthusiast

Higlighted videos

Speech recognition game
Pygame cheatsheets
Let's start using pygame to make games! Pretty fun assured.