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 https://github.com/formazione/pygame_book.
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.
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.
import pygame as pg
def __init__(self, w, h):
"Initialize main surface (screen) and starting the loop"
self.w = w
self.h = h
self.size = w, h
self.screen = pg.display.set_mode(self.size)
def close_window(self, event, game):
"Close the window with the x button or Esc"
quit = event.type == pg.QUIT
escape = event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE
if quit or escape:
game = 0
"The loop that make the game go on"
game = 1
for event in pg.event.get():
game = self.close_window(event, game)
game = Game(400, 400)
Higlighted videosSpeech recognition game
Let's start using pygame to make games! Pretty fun assured.
- Python Snake game
- Snake version 1
- Snake version 2
- Snake Game version 3 with (experimental) music
- Making Snake game Part 1: the menu
- Python vs. Snake v. 1.8.3
- PySnake 1.8.7
- A bigger Snake Game
- PySnake gameover scene (devlog)
- Pysnake devlog 1.9.6
- Looking for fluid movements in snake
- PySnake 2.0.3 music and fly