Hardware Reference
In-Depth Information
he setUpScreen function does just that - it puts everything onto the screen. First of it
clears the screen and then puts up the background picture. hen there is a small delay for the
players to prepare themselves, and the components of the question are separated out into
distinct variables in a list as delimited by the commas. Next you need to put the answers in
random positions. Although in the question ile the correct answer is always the irst one, it
would be a bit of a dead giveaway if the correct answer always appeared in the same place on
the screen, so the game has to scramble the positions. his is done by creating a list called
aPos , short for answer position, that holds numbers 1 to 4 to show where each one is to be
displayed. his is done in a function called, surprisingly enough, scramble . he way this is
done is by generating a random number between 1 and 4 and then checking through the list
to see if that number appears already. he irst time around it doesn't appear, so it is stored
in the irst position. Next a candidate for the second position is generated, and the list
searched to see if it has used it before. If it has, it generates more random numbers, and con-
tinues doing so until it inds one that hasn't been used before. his is done for all four posi-
tions. Although it might sound a bit silly to do this for the last position, you can simply use
the same code as you did before. his is sort of like the instruction to throw a six before
continuing, which is found in some simple board games. However, as the computer is very
fast it will eventually come up with the missing number before a noticeable time has passed.
So having got the list of where to put each answer, the setUpScreen function continues by
drawing the question and answer on the screen. It does this by calling up the drawWords
function, which takes in three variables - the text to draw, the position number to draw it in
and a logic input that determines if the text is to be rendered in the alternate text colour or
not. You will see at the end of this section how that feature is used when the program lashes
the correct answer in response to an incorrect one. he position number is used to access the
global lists placeX and placeY deined at the start of the code. Normally this position
deines the centre of the text rectangle but in the case of positions 2 and 3, the X position
deines an end of the text rectangle. his is so that those answers can be placed close to the
central graphic and the variable length text box can extend in either direction from that posi-
tion. hen the text background rectangle is drawn, the text over the top of it and inally an
outlined rectangle drawn on the top of that.
Back in the setUpScreen function, you make a note of what the correct answer is and what
position it is in. You will use that later when it comes to lashing the correct answer.
After setting up the question on the screen you need to check for the answer coming in, see
if it is correct and take the appropriate action. All this is done in the checkInput function.
his starts with a loop that checks the input and looks for any pygame events such as quit .
As soon as a player button press is detected the code works out which player has pressed irst
by looking at the input bit pattern as a number. Because you have arranged all the buttons
for one player to occupy four consecutive bits, this test is easy. If the number is greater or
equal to the hex value 0x10, player two has pressed irst; otherwise it is player one. he next
Search WWH ::




Custom Search