Earlier, developing a game was hard, because we needed to configure hardware according to the needs of the game. With the development of SDL (Simple DirectMedia Layer), developers don’t need to mess with low-level programming any more. Pygame is the Python extension of SDL, with some additional benefits.
Like SDL, it automatically covers the hardware interface, so you just need to code at a high level. It has replaced the older pySDL module. Pygame was released by Pete Shinners, under the LGPL. One of its greatest strengths is the online community. This link is worth a visit, as this community motivates programmers to build small games using this great API, through competitions and help.
Pygame should come pre-installed with Python. I am using the Ubuntu 10.04 32-bit desktop edition with Python 2.6.5. My Pygame version is 1.9.1. To check whether it is installed on your system, run the following in the interactive mode of your Python interpreter:
import pygame pygame.ver
If installed, it will return the version of the Pygame module. If it shows an error, it has not installed. Download it and run the following steps as the root user to install it:
tar zxf pygame-1.9.1release.tar.gz cd pygame-1.9.1release python setup.py install
Beginning with a “Hello World” program is a tradition in the programming world. Let’s follow the tradition, but with some extended/advanced code. Given below is a short script,
hello.py, which will display Hello World along with a background image.
import pygame #1 from pygame.locals import * from sys import exit pygame.init() #2 screen=pygame.display.set_mode((640, 480),0, 24) #3 pygame.display.set_caption("Hello World") #4 create=pygame.font.SysFont("comicsansms",30) #5 f=create.render("Hello World",True,(0, 0, 0),(255, 255, 255)) #6 img=pygame.image.load("simple.jpg").convert() #7 while True: for i in pygame.event.get(): #8 if i.type==QUIT: exit() screen.blit(img,(0, 0)) #9 screen.blit(f,(200, 200)) pygame.display.update() #10
The output of this program can be seen in Figure 1.
Let’s try to understand the output by following the code, step by step.
- We import pygame so that we can use the module’s functionality.
- Pygame is initialised. Manual initialisation of resources leads to faster processing.
- Everything in Pygame runs on surfaces; so we create a display surface and set its properties using the
set_modefunction, where the first argument is a tuple consisting of height and width as elements, the second a flag which is by default zero, and the third the colour information (8, 16, 24, 32-bit).
- We set the caption of the created display to “Hello World”.
- We create an object that stores information about the font type and size we want to use.
- We use this object to render text on the surface, using the render method, whose signature is render(text, anti-aliasing, text color, background color).
- We create another object — an image we loaded.
pygame.event.get()call is used to get all the events that are occurring. When it encounters
QUIT(when the user closes the window), it will exit the loop, and close the display.
- Blitting is like copying one surface to another surface. We copy the image surface object to the display surface object at the (x, y) co-ordinates. In the same way, we blit the font object to the display surface.
- Blitting always takes place in a buffer; we will not be able to see its results. To display the result, we use this line of code, which updates the display from the buffer.
Knowledge of event handling is very important if you are creating a game. Developers must know how their games ought to respond when a user clicks the mouse button, for instance. The number of events that can occur within the game are infinite. We will cover the major events, such as
MouseButtonUp, etc., with another sample script,
import pygame from pygame.locals import * from sys import exit pygame.init() a=pygame.display.set_mode((650, 400),RESIZABLE, 32) pygame.display.set_caption("Event list ") font=pygame.font.SysFont("arial",16) back=pygame.Surface(a.get_size()) back=back.convert() back.fill((255, 255, 255)) while True: for i in pygame.event.get(): if i.type==QUIT: exit() else: text=font.render("%s"%i, 1,(10, 10, 10),(255, 255, 255)) a.blit(back,(0, 0)) a.blit(text,(0, 0)) pygame.display.update()
The output is as shown in Figure 2.
Events are stored in the form of a queue.
pygame.event.get() gives us the list of occurring events within the queue. Running the above script will display all the events that occurred within the display, with all the information about the event.
Let’s go into the details for some of the important events:
MouseMotion: It is a dictionary with the key elements buttons, pos, and rel. The key element button corresponds to a value like (0, 0, 0), in which the 0th element represents the mouse button on the left extreme, the 1st element the middle button, and the 3rd the right button. If clicked, the button value is 1; if not clicked, 0. The pos element represents the current coordinate of the mouse, and rel shows the difference in mouse position, with respect to the last event co-ordinates. You can move the mouse in the space with the mouse button held down, too.
MouseButtonDown: The key elements are button and pos, with the same explanations as above.
MouseButtonUp: This is similar to
MouseButtonDown; the only difference is that button value is 0 instead of 1.
KeyDown: The key elements of the dictionary are scancode, key, unicode and mod. When a key is pressed, this event is emitted. The scancode value is the platform-specific value of the key (key is the value of the key) and you can find out the values of all the keys in pygame.locals. mod indicates the combination mode (something like Ctrl+X will give a different mod value). unicode represents the Unicode value of the key that was pressed.
KeyUp: Similar to
KeyDown, but it doesn’t contain the unicode element.
These events are used in almost every game. There are other events too, such as
videoresize, etc. You can learn more about them on the Web. Pygame is of great use if you start your work from scratch — it’s worth trying out this API. I am still reading about the module in depth, so I will try to provide readers with more knowledge about it in the future. Suggestions and queries are always welcome.
Don’t forget to read the next part for more…
The author loves to explore open source technologies, and enjoys programming in C, C++, Java and Python. A metal head at heart, he loves to try his hands on the guitar sometimes.