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 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.

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

print(game.w)

and you will get

400

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


class Game:

	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)
		self.loop()

	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
		return game

	def loop(self):
		"The loop that make the game go on"
		game = 1
		while game:
			for event in pg.event.get():
				game = self.close_window(event, game)
		pg.quit()


game = Game(400, 400)

Second part of this serie here.


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.