python logo

pygame


Python hosting: Host, run, and code Python in the cloud!

Pygame Python
Dive deep into building games using Pygame. Harness the power of Python and create cross-platform games that run seamlessly on Windows, Linux, and Mac OS.

Learn the essentials of game development with Pygame, starting from the foundational framework. As we progress through this tutorial and the subsequent ones, we’ll delve into the intricacies of creating various game types.

Recommended Reading: Create Space Invaders with Python

Grasping the Basics with Pygame
After finishing this segment, you’ll produce a game akin to the one showcased here.

All games, regardless of their complexity, follow a foundational structure:

  • Initialization of resources (graphics, sounds, levels, and data).
  • Continuous running until a termination event occurs.
  • Within this loop, the game’s logic is updated, user input is gathered, and the screen visuals are refreshed.

This universally accepted structure in game development translates to the following pseudo code:

1
2
3
4
5
6
initialize()
while running():
game_logic()
get_input()
update_screen()
deinitialize()

Implementing this in Pygame, we get:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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):
pygame.init()
self._display_surf = pygame.display.set_mode((self.windowWidth, self.windowHeight), pygame.HWSURFACE)
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):
pass

def on_render(self):
self._display_surf.blit(self._image_surf, (self.x, self.y))
pygame.display.flip()

def on_cleanup(self):
pygame.quit()

def on_execute(self):
if not self.on_init():
self._running = False

while self._running:
for event in pygame.event.get():
self.on_event(event)
self.on_loop()
self.on_render()
self.on_cleanup()

if __name__ == "__main__":
theApp = App()
theApp.on_execute()

Here’s a breakdown:

  1. The Pygame program initiates with the __init__() method.
  2. The on_execute() method, responsible for the game’s core operations, is invoked.
  3. Game termination and cleanup occur in the on_cleanup() method.
  4. During initialization (on_init()), we define the game’s resolution and initialize the Pygame library.
  5. Images are loaded in but not displayed immediately. This action is reserved for the on_render() method.

The blit method sketches the image (image_surf) onto a specific coordinate (x,y). In Pygame, coordinates begin at (0,0) (top-left) and span to (windowWidth, windowHeight). The call to pygame.display.flip() refreshes the game screen.

Further Reading: Create Space Invaders with Python

Ready to further your knowledge and grasp game logic? Dive into the next tutorial to continue this exciting journey of game creation with Python!

Previous Tutorial | Next Tutorial






Leave a Reply:




Thor Thu, 14 May 2015

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

Frank Thu, 14 May 2015

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

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

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

Jordan Mon, 18 May 2015

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

Frank Mon, 18 May 2015

Hi Jordan, I'll add more tutorials which will be helpful in achieving your goal :-)

Thor Sun, 24 May 2015

A jump-n-run in the line of Bruce Lee - C64 style :)

Frank Sun, 24 May 2015

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

Thor Fri, 29 May 2015

Thankzzzzz :)

Phil Sat, 13 Jun 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 :)

Frank Sat, 13 Jun 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.

Yasir Thu, 29 Oct 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.

Frank Sat, 31 Oct 2015

Sure, could you post your code?