-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygame_stopwatch.py
100 lines (65 loc) · 2.41 KB
/
pygame_stopwatch.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from threading import Thread
import time, sys, glob, pygame
screen = None
myfont = None
height = 0
width = 0
fontsize = 0
pygame.init()
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
pygame.display.toggle_fullscreen()
width = screen.get_width()
height = screen.get_height()
fontsize = int((width / 8) * 2.5)
myfont = pygame.font.SysFont(None, int(fontsize))
class StopWatchThread(Thread):
def __init__(self, stopwatch_instance):
Thread.__init__(self)
self.stopwatch_instance = stopwatch_instance
def run(self):
start_time = self.stopwatch_instance.start_time
while self.stopwatch_instance.stopwatch_running:
currtime = time.time() - start_time
milisec = int((currtime % 1) * 100)
mins = int(currtime/60)
sec = int(currtime) % 60
text = ("0" + str(mins))[-2:]+":" + ("0" + str(sec))[-2:] + ":" + ("0" + str(milisec))[-2:]
screen.fill((0, 0, 0))
label = myfont.render(text, False, (255, 255, 0))
screen.blit(label, (int(fontsize / 4), int((height / 5))))
pygame.display.flip()
pygame.event.clear()
class StopWatch():
def __init__(self):
self.stopwatch_running = False
self.start_time = None
def startStopWatch(self, usb_port=""):
#Countdown
"""
countdown_start_time = time.time()
countdown_from = 5
while ((time.time() - countdown_start_time) < countdown_from):
color_step = int(255/countdown_from)
text = " " + str(countdown_from - int(time.time() - countdown_start_time))
screen.fill((0, 0, 0))
r = int(text)*color_step
g = 255-int(text)*color_step
label = myfont.render(text, False, (r, g, 0))
screen.blit(label, (int(width / 3), int((height / 5))))
pygame.display.flip()
pygame.event.clear()
time.sleep(0.05)
"""
self.stopwatch_thread = StopWatchThread(self)
self.start_time = time.time()
self.stopwatch_running = True
self.stopwatch_thread.start()
def stopStopWatch(self):
self.stopwatch_running = False
elapsed_time = time.time() - self.start_time
return elapsed_time
if __name__ == "__main__":
stopwatch = StopWatch()
stopwatch.startStopWatch()
time.sleep(5)
stopwatch.stopStopWatch()