User manual

73
FIELD = pygame.display.set_mode((400, 400))
FIELD.fill(WHITE)
MX = 200; MY = 200; MP = ((MX, MY))
def point(A, W):
w1 = radians(W * 6 – 90); x1 = int(MX + A * cos(w1))
y1 = int(MY + A * sin(w1)); return((x1, y1))
for i in range(60):
pygame.draw.circle(FIELD, BLACK, point(190, i), 2)
for i in range(12):
pygame.draw.circle(FIELD,BLACK, point(190, i * 5), 4)
mainloop = True; s1 = 0
while mainloop:
time = time.localtime()
s = time.tm_sec; m = time.tm_min; h = time.tm_hour
if h > 12:
h = h – 12
hm = (h + m / 60.0) * 5
if s1 <> s:
pygame.draw.circle(FIELD, WHITE, MP, 182)
pygame.draw.line(FIELD, BLACK, MP, point(120, hm), 6)
pygame.draw.line(FIELD, BLACK, MP, point(170, m), 4)
pygame.draw.line(FIELD, RED, MP, point(180, s), 2)
s1 = s
pygame.display.set_caption(“Current time: " +
time.asctime())
pygame.display.update()
for event in pygame.event.get():
if event.type == QUIT or (event.type ==
KEYUP and event.key == K_ESCAPE):
mainloop = False
pygame.quit()
9.1.1 How does it work?
This program shows other features of the PyGame library and the time library and simple trigonometric
functions that are used for the presentation of analogue displays.
import pygame, time
from pygame.locals import *
from math import sin, cos, radians
pygame.init()
As in the last programme, the PyGame library is first initialized. In addition, the time library is imported to
establish the time and also imported are three functions from the very extensive
math library.
RED = (255, 0, 0); WHITE = (255, 255, 255); BLACK = (0, 0, 0)
The three colours used in the graphic are saved to three variables.
FIELD = pygame.display.set_mode((400, 400)); FELD.fill(WHITE)
A window of the size 400 x 400-pixel is opened and filled with white.
MX = 200; MY = 200; MP = ((MX, MY))