Hardware Reference
In-Depth Information
In order to implement the high-score table, I have used the shelve module, which is a use-
ful module that handles most of the work in making a persistent database. Basically, it main-
tains a dictionary and stores its entries in an unsorted manner. It is a simple enough matter
to transfer this to a list, and sort the list before displaying it. When you run this code for the
irst time a ile containing the high-score table will be generated; subsequently, the ile will be
used. If you want to start afresh, simply delete this ile.
Generating the next colour is done in the newColour function. For the two simplest levels,
it simply picks a colour from a list deined at the start of the code as easyCols and intCols .
However, if the game is running at the most complex level, a random bit is toggled or
changed. his function generates a number with only a single bit set in it by shifting a one a
random number of places to the left; it then uses the exclusive or function, or XOR , to change
that bit in the colour number.
he checkInput function does two jobs: First it acts as a delay, and while it is delaying it
constantly checks to see if a button has been pressed. If a button has been pressed, it triggers
the snap sound and calls the checkResult function to see if the call is correct. If the snap is
correct, an applause sound is played, along with a printout of the successful player's name;
otherwise, a fail sound is played, and the incorrect player's score is decremented. hen the
last two colours are alternated; you will see them lash if they are diferent.
Over to You
he irst thing you can customise are the sounds, especially the snap sounds. It is great to
have those said by a person you actually recognise. You could change colours in the colour
list. You could even add an extra resistor to two of the LEDs to get an even wider range of
subtle colour variations.
You could make the scoring more sophisticated so that instead of simply storing the score of
a player, you also stored the number of rounds he or she has played and sorted the list on the
ratio of score-to-rounds played.
Search WWH ::




Custom Search