-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
81 lines (63 loc) · 2.26 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Example file showing a circle moving on screen
from datetime import datetime
import math
import pygame
from contextlib import contextmanager
from dataclasses import dataclass
@contextmanager
def pygame_init():
pygame.init()
try:
yield
finally:
pygame.quit()
@dataclass
class Clock:
hand_color: str
center: pygame.Vector2
size: float
def draw_second_hand(self, screen, second: int):
hand = pygame.Vector2(0, -self.size).rotate(second / 60 * 360)
self._draw_hand(screen, hand)
def draw_minute_hand(self, screen, minute: int):
hand = pygame.Vector2(0, -self.size * 3 / 4).rotate(minute / 60 * 360)
self._draw_hand(screen, hand, width=2)
def draw_hour_hand(self, screen, hour: int):
hand = pygame.Vector2(0, -self.size / 2).rotate(hour / 24 * 360)
self._draw_hand(screen, hand, width=4)
def _draw_hand(self, screen, hand: pygame.Vector2, width: int = 1):
pygame.draw.line(
screen, self.hand_color, self.center, self.center + hand, width=width
)
def main():
width, height = (1280, 720)
screen = pygame.display.set_mode((width, height), pygame.NOFRAME)
game_clock = pygame.time.Clock()
running = True
dt = 0
clock = Clock(
center=pygame.Vector2(screen.get_width() / 2, screen.get_height() / 2),
size=min(screen.get_width() / 3, screen.get_height() / 3),
hand_color="white",
)
while running:
# poll for events
# pygame.QUIT event means the user clicked X to close your window
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# fill the screen with a color to wipe away anything from last frame
screen.fill("black")
now = datetime.now()
clock.draw_second_hand(screen, now.second)
clock.draw_minute_hand(screen, now.minute)
clock.draw_hour_hand(screen, now.hour)
# flip() the display to put your work on screen
pygame.display.flip()
# limits FPS to 60
# dt is delta time in seconds since last frame, used for framerate-
# independent physics.
dt = game_clock.tick(60) / 1000
if __name__ == "__main__":
with pygame_init():
main()