In this tutorial we will focus on Python's PyGame library that allow to create all staff needed to create all sort of games. But we will not learn Python itself. You must know Python. Classes and modules are obligatory also modules to load files but no other extra knowledge.
PyGame
PyGame can handle time, video (both images and vids), music, fonts, different image formats, cursors, mouse, keyboard, Joysticks and much much more. And all of that is **very** simple.
SDL
PyGame is extension of SDL an C library that was created to allow easy migration form one operating system to another (e.g. Windows to Linux). Its means that PyGame is easy multi-platform multimedia library.
Game loop
Before writing first lines of code I will mention about how game loop is designed in PyGame. Game loop is the place where events, game logic and rendering onto screen is performed. It should look similar to:
while 1:
events()
loop()
render()
Where event() proceeds events like pressed keys, mouse motion etc. loop() compute changes in the game world like NPC's moves, player moves, AI, game score. And render() just print out on the screen graphic. That separation of tasks makes your game design easier and allow to easy changes in code when new ideas rise in you head.
First code
Its time for more code:
import pygame
from pygame.locals import *
class App:
def on_execute(self):
pass
if __name__ == "__main__" :
theApp = App()
theApp.on_execute()
As you can see I prefer classes than functions. That is because OOP is getting more and more fans in game programming due to it correlation with game design. Simply in game we have objects, theirs tasks etc. even when programing without classes.
CApp will be our main class. To run game we will only need call OnExecute() function. Yes this function will contain our Game Loop.
Creating simple window
Lets add some more code:
import pygame
from pygame.locals import *
class App:
def __init__(self):
self._running = True
self._surf_display = None
self.size = self.weight, self.height = 640, 400
def on_init(self):
pygame.init()
self._display_surf = pygame.display.set_mode(self.size, pygame.HWSURFACE | pygame.DOUBLEBUF)
self._running = True
def on_event(self, event):
if event.type == pygame.QUIT:
self._running = False
def on_loop(self):
pass
def on_render(self):
pass
def on_cleanup(self):
pygame.quit()
def on_execute(self):
if self.on_init() == False:
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()
Test
You should see black window. If window don't wont to quit use 'xkill' under linux or task manager under windows.
on_init on_event on_loop on_render on_cleanup
on_init calls pygame.init() that initialize all PyGame modules. Then it create main display - 640x400 window and try to use hardware acceleration. At the end this routine sets _running to True.
on_event check if Quit event happened if so sets _running to False wich will break game loop.
on_loop and OnRender do nothing.
on_cleanup call pygame.quit() that quits all PyGame modules. Anything else will be cleaned up by Python.
on_execute
on_execute initialize pygame than enter main loop in witch check events and then compute and render everything till _running is "True" and only Quit event will set it to "False".
Before quitting it will cleanup.
If you will run this file as program it will create instance of App and call its on_execute(). (3 last lines)
Note: PyGame can create your game's window in multiple modes:
- pygame.FULLSCREEN a fullscreen display
- pygame.DOUBLEBUF recommended for HWSURFACE or OPENGL
- pygame.HWSURFACE hardware accelerated, only in FULLSCREEN
- pygame.OPENGL an opengl renderable display
- pygame.RESIZABLE display window should be sizeable
- pygame.NOFRAME display window will have no border or controls