User manual

69
This line tells the user, what to do. Each time you press any key on the keyboard rolls the dice.
print
always writes into the Python Shell window, not in the new graphical window.
while mainloop: Now the main loop of the game starts. In many games an infinite loop is used which is
repeated over and over again and constantly queries any user activity. Somewhere in the loop an abort
statement must be defined, which ensures that the game can exit.
We use the variable
mainloop here which only adopts the two Boolean values True und False (true and
false, on and off). It starts with
True and is queried with each loop run. If it adopts the value False during
the loop, the loop is aborted before the next run.
for event in pygame.event.get(): This line reads the most recent user activity and saves it as event. The
game has only two types of game-relevant user activities: The user presses a key and thus rolls the dice, or
the user wants to end the game.
if event.type == QUIT or (event.type == KEYUP
and event.key == K_ESCAPE):
mainloop = False
There are two ways to exit the game: Either you can click the X icon in the upper right corner on the window
or you can press the
[Esc] key. By clicking on the x icon the operation system delivers the event.type ==
QUIT
. If you press and release a key that is the event.type == KEYUP. In this case, the pressed key is also
stored in
event.key.
The described
if statement verifies whether the user wants to close the window, or (or) has pressed and
released a key and (
and) whether this is the key with the internally used name K_ESCAPE. If this is the case,
the variable
mainloop is set to False that will exit the main loop of the game prior the next run.
if event.type == KEYDOWN: The second type of user activity that reoccurs more than once again during the
game, is the user presses a key. It is not important which key, yet with the exception of the key
[Esc]. As
soon as a key is pressed
KEYDOWN), an important part of the program is actuated, which generates and
displays the dice result.
FIELD.fill(BLUE) First the surface-object, the actually window of the application named FIELD is filled
with the color defined as
BLUE to paint over the dice result scored.
NUMBER = random.randrange (1, 7)
The random function random now generates a random number between 1 and 6 and saves it in the variable
NUMBER.
print ZAHL This line writes the score of the dice roll into the Python Shell window. You may also omit this
line if you rather want to do without the text-based output.
if NUMBER == 1:
pygame.draw.circle(FIELD, WHITE, P1, 40)