pygame
Python hosting: Host, run, and code Python in the cloud!
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
Create Space Invaders with Python
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):
initialize() |
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 |
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): |
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.
You may like
Create Space Invaders with Python
Continue the next tutorial and learn how to add game logic and build games :-)
Leave a Reply:
Thanks! Making games is something I always wanted t
a whack at, Python/PyGame may just bring that within reach
Glad to help! Do you wish to know about any specific type of game?
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?
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.
I want to learn any concepts that will be useful in getting me an entry level job without a bachelor's degree.
Hi Jordan, I'll add more tutorials which will be helpful in achieving your goal :-)
A jump-n-run in the line of Bruce Lee - C64 style :)
Added a short tutorial on jump-n-run logic.
Thankzzzzz :)
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 :)
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.
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.
Sure, could you post your code?