-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstopwatch.py
More file actions
84 lines (62 loc) · 1.9 KB
/
stopwatch.py
File metadata and controls
84 lines (62 loc) · 1.9 KB
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
#Try : http://www.codeskulptor.org/#user43_DKCEwE4lo5S4gWE.py
# template for "Stopwatch: The Game"
import simplegui
# define global variables
interval = 100
time = '0:00.0'
t = 0
game_count = 0
win_count = 0
running = 0
# define helper function format that converts time
# in tenths of seconds into formatted string A:BC.D
def format(t):
global time
millis = t % 10
sec = (t - millis) / 10
mins = sec // 60
seconds = sec % 60
if seconds >= 10:
time = str(mins) + ':' + str(seconds) + '.' + str(millis)
else:
time = str(mins) + ':0' + str(seconds) + '.' + str(millis)
# define event handlers for buttons; "Start", "Stop", "Reset"
def start():
global running
running = 1
timer.start()
def stop():
global game_count, win_count, running
timer.stop()
game_count = game_count + running
if time[-1] == '0' and running:
win_count = win_count + 1
running = 0
def reset():
global t, time, game_count, win_count, running
game_count = 0
win_count = 0
running = 0
timer.stop()
t = 0
format(t)
#define event handler for timer with 0.1 sec interval
def timer_handler():
global t
t = t + 1
format(t)
# define draw handler
def draw(canvas):
canvas.draw_text(time, [100,150], 48, "blue")
canvas.draw_text(str(win_count) + '/' + str(game_count),
(250, 20), 20, "Green")
# create frame
frame = simplegui.create_frame("Stopwatch", 300, 300)
# register event handlers
frame.add_button("Start", start,100)
frame.add_button("Stop", stop,100)
frame.add_button("Reset", reset,100)
frame.set_draw_handler(draw)
timer = simplegui.create_timer(interval, timer_handler)
# start frame
frame.start()