Building game show buzzers with a Raspberry Pi
The showWinner Function – Lines 12 to 18
The showWinner
function is called once a winner is determined. It draws the winner on the public (graphics) screen:
def showWinner ( winner ):
def
is Python's keyword to define a function. Like most other languages, the arguments comprise a comma-separated list inside parentheses.
When a function is initialized in Python, it gets its own namespace. The global keyword tells Python to look outside of the function for each global variable:
global screen global numbers
Normally, this functionality would be handled with a class, but because of the rapid deployment of this project, using globals was quicker and easier.
winner
is provided by the code calling this function. It tells me the player number that pressed the button first. The players are numbered 1 through 5; however, Python lists start numbering at 0, so subtracting 1 "aligns" the winner variable with the list:
winner -= 1
In the code in line 17,
screen.blit ( numbers [ winner ] [ 0 ] , ( numbers [ winner ] [ 1 ] , 0 ) )
blit
is a function of a Pygame surface that copies another surface onto its parent, screen
is a surface, and numbers
is the list of surfaces and their offsets from the left side of the screen defined earlier. blit
is provided with two arguments. The first argument is the surface to draw. The [ 0 ]
in numbers [ **winner ] [ 0 ]
gets the first element of the tuple, where I stored the graphic of the team number.
The second argument is the position to use when drawing the surface, which is passed as a tuple (see the "Python Tuples" box for an explanation of tuples). numbers [ winner ] [ 1 ]
says "get the winning entry from the list," and [ 1 ]
gets the second element in the tuple, which is the left offset that was calculated when the graphic loaded. All of that boils down to the x coordinate. I want to draw at the top of the window, so I pass 0 as the y coordinate.
Line 24 tells Pygame to copy the surface screen
to the framebuffer, which makes the graphics appear on screen:
pygame.display.flip()
In this case, it's only a single blit; however, in a larger project, you would perform all of your blits, drawing, and other screen processing and then call pygame.display.flip()
once.
Python Tuples
Python lets you define tuples by surrounding sets of data in parentheses. This approach is typically used for data that has multiple parts. Here are a few examples:
coordinate = ( x , y ) color = ( red , green , blue ) card = ( "Ace" , "Spades" )
Pygame uses tuples extensively to pass coordinates, colors, rectangles, screen regions, and user input values, just to name a few. Tuples can have as many elements as you like. Square brackets after a tuple name will retrieve the requested element of a tuple. In the above example, coordinate [ 0 ]
will retrieve the x
value, color [ 1 ]
will retrieve the green
value, and card [ 1 ]
will retrieve Spades
.
The reset function – Lines 20 to 24
The reset function clears the contestant's LCD when the game is done with the buzzer result. def, global
, and pygame.display.flip
work the same as above, so the only new command is fill
:
screen.fill ( ( 0 , 0 , 0 ) )
fill
is a function of a surface that makes the entire surface the provided color. The one argument is an RGB color provided as a tuple. Once the surface is filled, I flip the display and the screen is empty!
Buy this article as PDF
Pages: 8
(incl. VAT)