Building game show buzzers with a Raspberry Pi

Pygame Setup – Lines 45 to 46

Pygame is the graphics library that will provide images on the HDMI output. Pygame provides modules for image files; the drawing of 2D primitives; image transformations; the playing of sound and music files; accessing webcams, joysticks, and other user input devices; and much more. My project primarily uses the image and surface functions. The code


initializes the Pygame graphics subsystem. The code

screen = pygame.display.set_mode ( ( 1680 , 1050 ) )

sets the size of the graphics surface, the window, or both and returns a reference to it. If the Raspberry Pi is not running the X Window system, then the full display will be initialized to the resolution provided. Note that the resolution must match the "console resolution" in /boot/config.txt! If you are running inside of X, then set_mode will default to a window of the requested size. You can add pygame.FULLSCREEN to get a full-screen window inside X, like this:

screen = pygame.display.set_mode ( ( 1680 , 1050 ) , pygame.FULLSCREEN )

Surfaces are like Pygame's variables. Any bitmapped element is stored as a Pygame surface. Surfaces also provide a set of functions to manipulate their contents. A Pygame surface has an origin (0,0) at the upper left corner and takes whatever dimensions you specify. A Pygame surface can be used as working space in memory and is not required to be on the actual screen to function.

set_mode returns a special surface – the screen itself. All of Pygame's functions can access the screen, just like any other surface. When pygame.display.flip() is called, the contents of the screen surface are copied to the framebuffer and appear on the display.

Curses Setup – Lines 48 to 50

Curses is a library to control character cell displays or, colloquially, terminals, which these days are most likely to be an xterm inside of the X Window system or an SSH connection. Curses dates back to when a Wyse VT100 was the terminal of choice. In fact, that is where the curses library shows some of its best strengths. Behind the scenes, curses maintains libraries for every major terminal manufacturer and protocol that's ever been created. Regardless of what type of physical (or virtual) terminal is displaying the software, curses provides a common interface that always functions the same way.

Curses allows you to position text anywhere on the screen by providing a set of coordinates, but there's one caveat: Curses coordinates are in (y, x) order rather than the algebraic (x, y). Keep this in mind when coding, especially in conjunction with Pygame, where coordinates are in the traditional (x, y) format. The line

terminal = curses.initscr()

initializes the text display and returns a reference to the display. This is used for all future interactions with curses. The code


disables line buffering and provides each character to Python as soon as it is received from the keyboard. The interrupt, quit, suspend, and flow control characters retain their functionality in this mode. The terminal.nodelay function

terminal.nodelay ( 1 )

makes character input commands non-blocking. If no key has been pressed when terminal.getch() is called, then it will immediately return -1. This allows a main loop to maintain other functions or do other tasks without freezing while waiting for user input.

Note that this function is a function of the window rather than curses itself. Curses lets you define regions within a text display as separate windows. Each window operates independently and can be configured differently as well. This functionality allows you to have a text input window that waits for key presses and echoes them as they are printed, along with a separate menu window that masks the input and acts immediately on key presses, for example.

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

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.