Pygame for Gaming

Python is actually known for its simplicity. Its simple in its syntax and usage. Python provides a package named “pygame” for writing games. Pygame is a set of Python modules designed for writing graphical games. It is written on top of the excellent SDL library. This allows you to create fully featured games and multimedia programs in the python language. The package is highly portable.

Pygame is not naturally available with python but should be installed. PyGame supplies the following:

* Surfaces(Hardware/Software Accelerated)
* Fonts
* Events(Keyboard, Mouse)
* Audio
* Video
* Sprites, Images
* Basic Drawing Routines

Now import two modules before building a game.

import pygame, sys
from pygame.locals import *

Pygame.locals contains many constant variables that we will use with Pygame such as QUIT or K_ESCAPE.

All pygameprograms have to satisfy some initial conditions. This should be done before calling any pygame function.

pygame.init()

Now a GUI window object is created by calling the set_mode((width(in pixel),height(in pixel)),0,32). The second and 3rd parametrs are passed as zero and 32 by default. This returns a pygame.Surface objects.

windowSurface = pygame.display.set_mode((500, 400), 0,32)
pygame.display.set_caption(‘Hello world!’)

Any colour can be considered as the combination of 3 primary colours…RED,GREEN and BLUE. So colours can be represented as the tuples of these colours.

RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

To fill the window with some colurs a function called fill(colour) is used. This function is for surface objects.

windowSurface.fill(WHITE)

Fonts for text can be set using the method sysFont(name of font,size of font). To take the default font we pass None for the 1st parameter.

basicFont = pygame.font.SysFont(None, 48)

Another useful method is render() which will create a surface objects with the text. This act on the font objects. The render has the following parameters – render(text,boolean value to select anti-aliasing,colour1,colour2).

text = basicFont.render(‘Hello world!’, True, WHITE,BLUE)

Different geometrical figures can be made like

pygame.draw.circle(windowSurface, BLUE, (300, 50), 20, 0)
pygame.draw.line(windowSurface,BLUE, (60, 60), (120,60), 4)

Of these figures rect() has many attributes to control its left, right, top and bottom motion.

Now to draw the text we created to surface a blit() function is available

windowSurface.blit(text, textRect)

The first parameter is the content to be drawn and second parameter is the place in the window surface where it should be drawn.

Now this window will be displayed to the screen only on calling the update() function.

pygame.display.update()

Pygame has another module called time. This can be used when we are limiting the no:of iterations per second. This can be done by exploiting the functions clock() and tick() in time module.

Images can be loaded to the game.The image formats Pygame supports include BMP, PNG, JPG (and JPEG), and GIF.

playerImage = pygame.image.load(‘player.png’)

Pygame works with a concept called game loop. The game loop is a loop that constantly checks for new events, updates the state of thewindow, and draws the window on the screen. This runs until the user explicitly terminates the loop.

for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()

If the event user selected is to quit, the window has to be closed. Here we have to select an exit for both the pygame and python.