Hardware Reference
In-Depth Information
Listing 8-2 continued
“””
import time # for delays
import piface.pfio as pfio # piface library
pfio.init() # initialise piface
def main():
print”Testing the colour snap hardware Ctrl C to quit”
print”showing all the colours”
while True :
for c in range(1,63) :
pfio.write_output(c) # turn on the colour
time.sleep(.5)
pfio.write_output(0) # turn off the light
time.sleep(0.8)
print “repeating sequence”
if __name__ == '__main__':
main()
If you ind that colours such as yellow look a little mottled, try a bit more predifusion of the
LED - either sand it some more or ind a bit of more textured translucent plastic to put over
it. he colours seen on the table tennis ball should be smooth and even.
The Software for the Game
Now on to the full game! here are many ways this game could be programmed to play, so
what I give here is just one implementation. It is important, when writing any piece of soft-
ware, to have the numbers that deine it stored in a variable name at the start of the code so
that it can be easily changed with an edit in only one line. he way this is set up is that there
are two players and the program asks for their names irst. It looks up their score in the high-
score table and makes an entry for them if they are not in it already. here are three rounds,
and the player that wins a round by correctly pressing the Snap button is awarded points
based on the diiculty level of the game. At the end of the game, a winner is announced, and
the points acquired in the game are transferred to the high-score table. After the names have
been inputted, you are asked to enter a diiculty level, which is a number from one to three
and refers to how much a colour can change from one presentation to the next. In the easiest
level, the colours displayed are only the primary and secondary colours, so it is simple to
distinguish one from the other. In the middle level, any colour can be chosen from the 63
Search WWH ::




Custom Search