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

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Creating a multiplayer quick-reaction game

    The quick-reaction game provides an introduction to building simple circuits with the Raspberry Pi and controlling those circuits with ScratchGPIO, an advanced version of Scratch.

  • A Python interface to a large-format pen plotter

    Getting a large-format plotter operational presented a personal challenge. A Raspberry Pi with a USB-to-serial dongle was the easiest way to start plotting on my home network.

  • Pygame modules for interactive programs

    Pygame modules are particularly suited to programming highly interactive software. We look at the modules dedicated to events, sound, and input by keyboard, mouse, and game controller.

  • Graphical displays with Python and Pygame

    As its name implies, Pygame is a set of Python modules designed to write games. However, many Pygame modules are useful for any number of projects. We introduce you to a few Pygame modules that you can use to create custom graphical displays for your project.

  • Fast clocks, model railroads, LED displays, and more

    Some model train enthusiasts like to run their trains like a real railroad with a dispatcher controlling movements, issuing train orders, and making sure the train stays on schedule. But, time runs differently for these trains, thanks to fast clocks.