-
Notifications
You must be signed in to change notification settings - Fork 0
/
StopWatch.py
75 lines (59 loc) · 1.74 KB
/
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
# template for "Stopwatch: The Game"
import simplegui
# define global variables
count = 0
status, last_status = False, False
x, y = 0, 0
# define helper function format that converts time
# in tenths of seconds into formatted string A:BC.D
def format(t):
tsec = t % 10
min = t // 10 // 60
sec = t // 10 % 60
time = ""
time += str(min) + ":"
if sec < 10:
time += "0" + str(sec)
else:
time += str(sec)
time += "." + str(tsec)
return time
# define event handlers for buttons; "Start", "Stop", "Reset"
def start():
global status, last_status
status, last_status = True, True
def stop():
global status, x, y, last_status
status = False
if last_status:
if count % 10 == 0:
x += 1
y += 1
else:
y += 1
last_status = False
def reset():
global count, status, last_status, x, y
status, last_status = False, False
count, x, y = 0, 0, 0
# define event handler for timer with 0.1 sec interval
def timer_handler():
global count
if status:
count += 1
# define draw handler
def draw_handler(canvas):
canvas.draw_text(format(count), (45, 110), 40, "White")
canvas.draw_text(str(x) + "/" + str(y), (150, 40), 30, "Green")
# create frame
frame = simplegui.create_frame("Stopwatch: The Game", 200, 200)
start = frame.add_button("Start", start, 60)
stop = frame.add_button("Stop", stop, 60)
reset = frame.add_button("Reset", reset, 60)
timer = simplegui.create_timer(100, timer_handler)
# register event handlers
frame.set_draw_handler(draw_handler)
# start frame
frame.start()
timer.start()
# Please remember to review the grading rubric