Hardware Reference
In-Depth Information
possible colours; however, because these are picked at random, most of the time they are
very diferent colours. In the advanced level, only one bit in the colour is changed from one
presentation to the next, so this means that there is a good chance of getting a slightly difer-
ent colour, fooling you into thinking it is the same as the previous one. If you press the Snap
button and it is not snap, points are deducted.
All the sounds the game produces are stored in a folder called sounds . hey are all .ogg
format sounds and are mainly self-explanatory. here are two “snap” sounds (one for each
player), a success and fail sound called applause and doh and inally a “tick” sound. his sound
should be very short and is played just before a new colour is displayed.
he inner workings of the game might surprise you as you might think that this way of doing
things is counterintuitive. However, it is actually simpler to program the game this way. First
of all, a random number function makes a number from 3 to 10 to control how many colour
presentations there will be before a match. here is a new colour function whose job is to
generate a colour that is diferent from the last one according to an algorithm chosen by the
diiculty level. he program goes on presenting these colours until the number of presenta-
tions has been reached; then the last colour is simply repeated. If no player sees the matching
colour, the process repeats. You could have the program penalise both players if this hap-
pens, but that is not how I have written this code. he full listing is shown in Listing 8-3.
Listing 8-3 The Colour Snap Game
#!/usr/bin/env python
“””
Colour Snap
“””
import time # for delays
import piface.pfio as pfio # piface library
import sys
import os
import random
import pygame
from pygame.locals import *
import shelve
pygame.init()
pygame.mixer.quit()
pygame.mixer.init()
pfio.init() # initialise piface
snapSound1 = pygame.mixer.Sound(“sounds/snap1.ogg”)
snapSound2 = pygame.mixer.Sound(“sounds/snap2.ogg”)
continued
Search WWH ::




Custom Search