User manual
73
Le programme
uhr01.py affiche une horloge analogique à l'écran :
import pygame, time
from pygame.locals import *
from math import sin, cos, radians
pygame.init()
ROUGE = (255, 0, 0); BLANC = (255, 255, 255); NOIR = (0, 0, 0)
CHAMP = pygame.display.set_mode((400, 400))
CHAMP.fill(BLANC)
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(CHAMP, NOIR, point(190, i), 2)
for i in range(12):
pygame.draw.circle(CHAMP, NOIR, point(190, i * 5), 4)
mainloop = True; s1 = 0
while mainloop:
temps = time.localtime()
s = temps.tm_sec; m = temps.tm_min; h = temps.tm_hour
if h > 12:
h = h – 12
hm = (h + m / 60.0) * 5
if s1 <> s:
pygame.draw.circle(CHAMP, BLANC, MP, 182)
pygame.draw.line(CHAMP, NOIR, MP, point(120, hm), 6)
pygame.draw.line(CHAMP, NOIR, MP, point(170, m), 4)
pygame.draw.line(CHAMP, ROUGE, MP, point(180, s), 2)
s1 = s
pygame.display.set_caption("Heure actuelle : " +
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 Voilà comment cela fonctionne
Ce programme montre d’autres fonctions de la bibliothèque PyGame et la bibliothèque time ainsi que des
fonctions trigonométriques simples, qui sont utilisées pour les affichages analogiques.
import pygame, time
from pygame.locals import *
from math import sin, cos, radians
pygame.init()
Au début, la bibliothèque PyGame est initialisée comme dans le dernier programme. En outre, la bibliothèque
time et trois fonctions de la très vaste bibliothèque math sont importées pour la détermination du temps.
ROUGE = (255, 0, 0); BLANC = (255, 255, 255); NOIR = (0, 0, 0)
Les trois couleurs utilisées dans le graphisme sont enregistrées dans trois variables.