Tagged: Gaming

PyGame : Tutorial PT1 (Setup & Initialisation)

I would consider this my first proper post hehe … I started this blog to make life easier when doing videos about code sometimes the screen capture and editing that goes in to a short clip can be painful and consume a lot of my spare time. Here ill be able to waffle on about code and linux to my hearts content (with the ability to access the articles and update things after they have been published).

So this hopefully will turn in to some kind of archive generated by hakology plus various odds and ends I find lying about the interwebz.

I digress … keeping on point this is a python pygame introduction to simple 2D game engines (with a view to move on to opengl and 3D graphics at some later point) for this tutorial you will need the following:

Python 2.7

Python- Pygame Librarys and Modules

IDE (It doesnt really matter what you use – at the moment I’m really enjoying ninja-ide)

This tutorial is for linux users, the development rig I’m using at the moment is Debian Jessie (Test/Dev Release)

Ok so down to business if your rocking debian jessie or similar you shouldnt have too many problems getting up and running.

First install python

sudo apt-get install python2.7

Now install pygame

sudo apt-get install python-pygame

Now install ninja-ide

sudo apt-get install ninja-ide

Hopefully that all went well 🙂 (if it didn’t please consult google.com/pygame/python/debian documentation)

Now in your home folder we need to create a folder to store everything so pop a terminal and make a directory

mkdir pygame
cd pygame
touch tutorial1.py
ninja-ide tutorial1.py

Now you should have created a folder and a file in the folder and opened that file with ninja-ide

This is where the fun begins.

Add the following to the first lines of your python file ….

import pygame
from pygame.locals import *

The lines above tell the python interpreter to import and load all the librarys and variables from pygame.

Next we need to set some variable to store how big we’d like to make our game window.

wWIDTH = 640
wHEIGHT = 480

Right now were ready to add the first function that is called when the file is executed

if __name__ == “__main__”: # main function call

r = 1 # set variable for return value
pygame.init() # initialise python pygame
GCLOCK = pygame.time.Clock() # set game clock
GSURF = pygame.display.set_mode((wWIDTH, wHEIGHT)) # main game surface
while r != 0: # quit if return not equal to 0 (0 = game exit, 1 = restart main loop)

r = main() # get return value from main loop (1 == OK … 0 == exit)

Ok so lets explain the code … the first line of the above block is defacto python code you will find this in (almost) every python script you encounter, the second line defines a variable called ‘r’ and sets it equal to 1 this value is used later to determine if the game should continue or quit. GCLOCK is a variable used to store pygames clock which is used later for frame rate calculations and game speed. GSURF is the game surface ie. the area or the screen we are going to be drawing to notice we pass in the pre defined screen sizes we set at the top of the script. The while loop and main game call will not quit until the return value is equal to 0. The line within the while loop is the main game loop call when we call this function we expect a return value of either 1 or 0.

Right now to add the main game loop …

When you add this code it needs to go below the variables we created earlier but above the first function call.

def main():

r = 1
while(1): # do for a while (game loop)

for event in pygame.event.get(): # handle events

if event.type == QUIT: # ctrl+c

r = 0 # return 0 = (game is over)

elif event.type == KEYDOWN: # down arrow

if event.key == K_ESCAPE:

r = 0 # return 0 = (game is over)

if event.key == K_q: # q key pressed

r = 0

GSURF.fill((0, 0, 0))

if r != 1: #check value of r

break #exit main game loop

return r

Ok so the above code basically runs and keeps the main game window open it loops forever until the escape key is detected if the escape key is detected or quit program event then the window will close.

You can now test the script by pressing play in the ninja-ide tool bar.

or entering the following in the command line …

cd ~
cd pygame
python tutorial1.py

Enjoy your nice blank window opening and closing.

Whoot well done you and stay tuned for part two coming very very soon 🙂

Pastebin link to raw code – http://pastebin.com/vzACDi3v