Hardware Reference
In-Depth Information
Listing 14-4 continued
sys.exit()
# see if we need to quit or look at the mouse
def checkForEvent():
event = pygame.event.poll()
if event.type == pygame.QUIT :
terminate()
elif event.type == pygame.MOUSEBUTTONDOWN :
pass
#print pygame.mouse.get_pos()
elif event.type == pygame.KEYDOWN and event.key == ;
pygame.K_ESCAPE :
terminate()
if __name__ == '__main__':
main()
he program starts with a lot of initialisation for screens, colours and variables. hen the ques-
tion ile is opened, and all the lines are read into a list, called, appropriately enough, questions .
he lines are counted, and then the order of the questions is shuled so it is diferent every
time. After that are some variables that deine where things are going to appear on the screen:
the question, the four answers, the player who is correct message and the player who gets to
race message. It is convenient having them all in one place for when you want to tinker with
the screen layout. What's left is a useful development tool in this code, one that prints out the
position of the pointer when you click the mouse. his is useful for knowing where to place
things. If you want to use it, just remove the # in front of the print .mouse.get_pos
instruction. Finally, the list called single has the conversion lookup table required to trans-
late between the button press, and the answer number it signiies.
he main function is, as always, the heart of the program and controls the top-level low. You
can see that it is mainly one endless loop asking the questions one at a time in the list. he
irst thing it does is hold the program in a loop until all buttons are released. hen the screen
is set up with the question on it, and the checkInput function holds until an answer is
entered and the winning player rewarded. When all the questions have been asked, the ques-
tion number is set back to the beginning, and the list of questions is shuled again. his is
because the end of the game is deined by a player's racing car completing the required num-
ber of laps; the Raspberry Pi knows nothing of this. hat is why it is important to have a
decent number of questions in the question ile so that the game does not get too repetitive.
Search WWH ::




Custom Search