User manual

56
while mainloop:
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYUP and
event.key == K_ESCAPE):
mainloop = False
if event.type == KEYDOWN:
FELD.fill(BLAU)
ZAHL = random.randrange (1, 7); print ZAHL
if ZAHL == 1:
pygame.draw.circle(FELD, WEISS, P1, 40)
if ZAHL == 2:
pygame.draw.circle(FELD, WEISS, P2, 40)
pygame.draw.circle(FELD, WEISS, P7, 40)
if ZAHL == 3:
pygame.draw.circle(FELD, WEISS, P1, 40)
pygame.draw.circle(FELD, WEISS, P4, 40)
pygame.draw.circle(FELD, WEISS, P5, 40)
if ZAHL == 4:
pygame.draw.circle(FELD, WEISS, P2, 40)
pygame.draw.circle(FELD, WEISS, P4, 40)
pygame.draw.circle(FELD, WEISS, P5, 40)
pygame.draw.circle(FELD, WEISS, P7, 40)
if ZAHL == 5:
pygame.draw.circle(FELD, WEISS, P1, 40)
pygame.draw.circle(FELD, WEISS, P2, 40)
pygame.draw.circle(FELD, WEISS, P4, 40)
pygame.draw.circle(FELD, WEISS, P5, 40)
pygame.draw.circle(FELD, WEISS, P7, 40)
if ZAHL == 6:
pygame.draw.circle(FELD, WEISS, P2, 40)
pygame.draw.circle(FELD, WEISS, P3, 40)
pygame.draw.circle(FELD, WEISS, P4, 40)
pygame.draw.circle(FELD, WEISS, P5, 40)
pygame.draw.circle(FELD, WEISS, P6, 40)
pygame.draw.circle(FELD, WEISS, P7, 40)
pygame.display.update()
pygame.quit()
8.1.1 Tako deluje
Ta program prikazuje številne nove funkcije, predvsem za prikaz grafike s knjižnico PyGame,
ki je primerna ne samo za prikaz grafike v igrah temveč tudi za vsako drugo grafiko na
zaslonu.
import pygame, sys, random
from pygame.locals import *
pygame.init()
Te tri programske vrstice se nahajajo na začetku skoraj vsakega programa, ki uporablja
PyGame. Poleg že omenjenega modula random za ustvarjanje naključnih števil se naložita
sam modul pygame kot tudi modul sys, ki vsebuje pomembne sistemske funkcije, ki jih
potrebuje PyGame, kot je npr. odpiranje in zapiranje oken. Uvozijo se vse funkcije iz
knjižnice PyGame, nato pa se inicializira dejanski modul PyGame.
FELD = pygame.display.set_mode((320, 320))