User manual
65
activate or deactivate GPIO outputs. The function
int() calculates a character’s numeric value. The
character is read out from a certain position of the pattern string via the loop counter
i.
time.sleep (1.0) The program waits 1 second until the next iteration. To save performance, you can also
prolong the latency until the calculation of free space is repeated.
At this point, the
while...True loop restarts. If the user in the meantime however, has pressed the shortkey
[Ctrl]+[C] the KeyboardInterrupt is triggered and the loop exits. After that, the GPIO ports are closed
and shutting the LEDs off.
8 Graphical dice
An addicting game needs graphics and not just text output as was the case in the times of the very first DOS
computer. The library PyGamehas predefined functions and objects to display graphics and programming for
games. Thus no need to invent everything from scratch.
Many games require a dice, but often there is none at hand. The next programming sample shows how easy
it is to use the Raspberry Pi with the help of Python and PyGame as a dice:
Fig. 8.1: The Raspberry Pi as dice.
The dice should be as simple as possible and be operated by using only one key while the randomly diced
result should be graphically represented as a "real" dice. The following program
wuerfel.py simulate such
a dice on the screen.
# -*- coding: utf-8 -*-
import pygame, sys, random
from pygame.locals import *
pygame.init()
FELD = pygame.display.set_mode((320, 320))