PyGame Tidbits
A few other items that you'll probably want to investigate in PyGame are covered below, be sure to read the pygame documentation for the (many) others that are available.
When we first set up our displays, we initialized pygame and used set_mode to set up the size of the window, e.g.
import pygame pygame.init() size = 640, 480 display = pygame.display.set_mode(size)We can also put a caption on the window, e.g. adding
Pygame allows us to take an image and create variants (transformations) of it, specifically by rotating it, flipping it, or changing its size:
# load the original version of the image originalImage = pygame.image.load("somefile.gif") # create the version we'll be rotating and displaying realVersion = pygame.transform.rotate(originalImage, 0) realBox = realVersion.get_rect() # rotate it 90 degrees and display it realVersion = pygame.transform.rotate(originalImage, 90) display.blit(realVersion, realBox) # now flip it horizontally realVersion = pygame.transform.flip(originalImage, True, False) display.blit(realVersion, realBox) # now flip it vertically realVersion = pygame.transform.flip(originalImage, False, True) display.blit(realVersion, realBox) # now flip it horizontally AND vertically realVersion = pygame.transform.flip(originalImage, True, True) display.blit(realVersion, realBox) # now resize it, giving a new width and height (pixels) realVersion = pygame.transform.scale(originalImage, (100, 120)) display.blit(realVersion, realBox)
To draw text, you first create a font variable, then use that to "render" the text and place the text on the screen:
The PyGame draw module provides many routines for drawing lines, rectangles, circles, polygons, arcs, etc.
In general, these involve specifying the screen to draw on, the colour to use, the key coordinates or sizes, and the thickness of the lines used in drawing the shape. E.g.:
# draw a black line of width 3, from position 10,100 to position 50,200 pygame.draw.line(display, black, (10,100), (50,200), 3) # draw a polygon with line width 3 and "corners" at the list of (x,y) points # (if you use a width 0 it fills in the polygon) myPoints = [(10, 12), (20,30), (25,12), (17,8), (6,10)] pygame.draw.polygon(display, black, myPoints, 3) # draw a red circle with line width 3 and radius 10 centered around point (50,70) # (if you use a width 0 it fills in the circle) pygame.draw.circle(display, red, (50,70), 10, 3)
The PyGame mixer module provides many routines for playing sound effects and music.
To play sounds, one must first load the sound file using the
pygame.mixer.Sound routine, e.g.
explosion = pygame.mixer.Sound("explosion.wav")
Anytime after that, you can play the sound effect using
the play routine, e.g.
explosion.play()
You can set the volume anywhere between 0.0 (off) and 1.0 (max),
e.g.
explosion.set_volume(0.7)
You can also stop the sound, e.g. explosion.stop()
To play background music, one uses a different routine to
load the file, pygame.mixer.music.load after which
you can issue the music.play command,
telling it how many times to repeat, e.g.:
pygame.mixer.music.load("muzac.wav")
pygame.mixer.music.play(-1)
(Telling it to repeat -1 times will cause it to repeat endlessly.)
You can control the volume, again between 0 and 1, e.g.
pygame.mixer.music.set_volume(0.5)
You can also rewind, pause, unpause, or stop the currently playing music, e.g.
pygame.mixer.music.rewind()
pygame.mixer.music.pause()
pygame.mixer.music.unpause()
pygame.mixer.music.stop()
As with sound and music, PyGame supports playing animations, which must be loaded first and then can be played, stopped, rewound, etc. E.g.
myMovie = pygame.movie.Movie('someMovie.mpeg') .... myMovie.play(3) # the number tells it how often to repeat myMovie.rewind() myMovie.set_volume(0.6) myMovie.stop()
To get random events or characteristics in the game we typically use built-in routines to generate random numbers, then have different things happen depending on which number came up.
At the start of the program, we need to import and
initialize the random number
generator by including the commands
import random
random.seed()
Anytime after that, and as often as you like, you can generate new random integers by specifying what range they should come from, e.g.
# initialize the random number generator import random random.seed() # generate a random number between 10 and 100 myNum = random.randint(10, 100) # generate a random number between 5 and 200 myNum = random.randint(5, 200)
We often need timers in games, to establish delays between events (e.g. generate a new enemy every 30 seconds, or 10 seconds after the player enters a room, etc).
There are a handful of routines for obtaining the current time in game, pausing the program for set amounts of time, and setting timed events using pygame:
To avoid conflicts with the build in event types, we pick an 'id' value that is between pygame.USEREVENT and pygame.NUMEVENTS, e.g.
# pick id numbers for two kinds of timed events wanderingMonsterID = pygame.USEREVENT patrollingGuardID = pygame.USEREVENT + 1 # set up timers to go off every 120 seconds for the monsters # and every 200 seconds for the guards pygame.time.set_timer(wanderingMonsterID, 120000) pygame.time.set_timer(patrollingGuardID, 200000) # inside our event processing loop, add code that should # run whenever the timers go off for event in pygame.event.get(): # see if it was one of our timer events if event.type == wanderingMonsterID: print "ah, here we should do something about wandering monsters" if event.type == patrollingGuardID: print "and here we should do something about patrolling guards"
Sometimes we may want to get or change the position or visibility of the mouse on the screen.
Pygame provides a few routines for handling this, such as:
x,y = pygame.mouse.get_pos() # looks up the current position of the cursor pygame.mouse.set_pos(x,y) # moves the cursor to the specified position pygame.mouse.set_visible(False) # hides the cursor pygame.mouse.set_visible(True) # shows the cursor