User manual

67
8.1.1 How does it work?
This programmr displays many new features, especially for graphics output by the PyGame library which of
course is not only used for games only for games but also for any other graphics on the screen.
import pygame, sys, random
from pygame.locals import *
pygame.init()
These three lines of code stand at the beginning of almost every programme that uses PyGame. Besides the
aforementioned module
random to generate random numbers, the module pygame itself and the module sys
are loaded, as it contains important, by PyGame required system functions such as the opening and closing
of windows. All functions of the PyGame library are imported, and then the actual PyGame module is
initialized.
FIELD = pygame.display.set_mode((320, 320))
This is an important function in any programme that uses a graphical output, it defines a drawing area, a so-
called surface, which in our example has the size 320 x 320 pixels and is named
FIELD. Note the notation in
double brackets; these are basically used for graphical screen coordinates. Such a surface is displayed in a
new window on-screen.
pygame.display.set_caption(“Dice")
This line will enter the window name.
BlUE = (0, 0, 255); WHITE = (255, 255, 255)
These commando lines define the two colors used, blue and white. You could also directly give the color
values each time in the programme, but this is not very helpful in terms of clarity.
Representation of colours on-screen.
Colours in Python and in most other programming languages are defined by three numbers between 0 and 255
that define the three colour components, red, green and blue Monitors use additive colour mixing, where all
three colour combined together in full saturation result in White.
P1 = ((160, 160)); P2 = ((60, 60)); P3 = ((160, 60)); P4 = ((260, 60)); P5 = ((60,
260)); P6 = ((160, 260)); P7 = ((260, 260))
These lines define the midpoints of the dice. On the 320 x 320 pixels large character field, the three axes of
the dots have the coordinates
60, 160 and 260 respectively.
The coordinate system for computer graphics
Each dot in a window or on a surface object is denoted by an x and y coordinate. The zero point of the
coordinate system is on the top left and not as we have learnt in school, bottom left. Just as reading a text
from top left to bottom right, also the x-axis extends from left to right, the y-axis from top to bottom.