-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.py
81 lines (68 loc) · 2.26 KB
/
clock.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
"""
timer.py
Contains the class that tracks the amount of time that the pathfinding algorithms take.
CSC111 Final Project by Tony He, Austin Blackman, Ifaz Alam
"""
import pygame
class Timer:
"""
A class that tracks the amount of time that has passed
Private Instance Attributes:
- _clock: A pygame clock object
- _milliseconds: An integer that represents how many milliseconds have passed
- _seconds: An integer that represents how many seconds have passed
- _minutes: An integer that represents how many minutes have passed
Sample Usage:
>>> my_clock = Timer()
"""
_clock: pygame.time.Clock
_milliseconds: int
_seconds: int
_minutes: int
def __init__(self) -> None:
"""
Initialize a pygame clock
"""
self._clock = pygame.time.Clock()
self._milliseconds = 0
self._seconds = 0
self._minutes = 0
self.update_time()
def update_time(self) -> None:
"""
Update the various time attributes
"""
self._milliseconds += self._clock.tick_busy_loop()
self._seconds = self._milliseconds // 1000
self._minutes = self._seconds // 60
def get_time(self) -> tuple[int, int, int]:
"""
Return a tuple of integers that correspond to (milliseconds, seconds, minutes)
"""
return (self._milliseconds, self._seconds, self._minutes)
def get_text_surface(self) -> pygame.Surface:
"""
Get the python surface with the current time
"""
mi = self._milliseconds % 1000 // 10
s = self._seconds % 60
m = self._minutes
if mi < 10:
mi = f'0{mi}'
if s < 10:
s = f'0{s}'
if m < 10:
m = f'0{m}'
string = f'Timer: {m}:{s}:{mi}'
text_surface = pygame.font.SysFont('Arial', 20).render(string, True, (0, 0, 0))
return text_surface
if __name__ == '__main__':
import python_ta
import python_ta.contracts
python_ta.contracts.check_all_contracts()
python_ta.check_all(config={
'extra-imports': ['pygame'],
'allowed-io': [], # the names (strs) of functions that call print/open/input
'max-line-length': 100,
'disable': ['E1136']
})