Chapter 3. Building Games with PyGame

In the previous chapter, we learned how to get started with Minecraft on Raspberry Pi and how to play it and use Python to manipulate things. Continuing with the same theme, we will now take a look at a gaming library in Python called PyGame and also learn how to create simple games with it. In this chapter, we will go through the following topics:

  • Introducing PyGame
  • Drawing a fractal tree
  • Building a simple snake game

Introducing PyGame

PyGame is a set of Python modules designed for the writing of video games. It is built on top of the existing Simple DirectMedia (SDL) library, and it works with multiple backends, such as OpenGL, DirectX, X11, and so on. It was built with the intention of making game programming easier and faster without getting into the low-level C code that was traditionally used to achieve good real-time performance. It is also very flexible and comes with many operating systems. It is very fast as it can use multiple core CPUs very easily and also use optimized C and assembly code for core functions.

PyGame was built to replace PySDL after its development was discontinued. Originally written by Pete Shinners, it is a community project from 2004 and is released under the open source free software GNU's lesser general public license. Since it is very simple to use and is open source, it has a lot of members in the international community and so it enjoys access to a lot of resources that other libraries may lack. There are many tutorials that can build different games with PyGame. It contains the following modules

Module

The description

cdrom

Manages CD-ROM devices and audio playback

cursors

Loads cursor images and includes standard cursors

display

Controls the display window or screen

draw

Draws simple shapes onto a surface

event

Manages events and the event queue

font

Creates and renders Truetype fonts

image

Saves and loads images

joystick

Manages joystick devices

key

Manages the keyboard

mouse

Manages the mouse

movie

Used for the playback of MPEG movies

sndarray

Manipulates sounds with Numeric

surfarray

Manipulates images with Numeric

time

Controls timing

transform

Scales, rotates, and flips images

Note

For more information on the PyGame library, you can visit www.pygame.org.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset