-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStopwatch.py
More file actions
68 lines (58 loc) · 1.67 KB
/
Stopwatch.py
File metadata and controls
68 lines (58 loc) · 1.67 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
# template for "Stopwatch: The Game"
import simplegui
# define global variables
count = 0
total_stops = 0
success_stops = 0
stop = True
# define helper function format that converts time
# in tenths of seconds into formatted string A:BC.D
def format(t):
tenth_sec = (t) % 10
sec = int(t / 10) % 10
minutes = int(t / 600) % 600
ten_min = int(t / 100) % 6
string = str(minutes) + ":" + str(ten_min) + str(sec) + "." + str(tenth_sec)
return string
# define event handlers for buttons; "Start", "Stop", "Reset"
def start():
global stop
stop = False
timer.start()
def stop():
global total_stops, success_stops, stop
if stop == False:
if count % 10 == 0:
success_stops += 1
total_stops += 1
else:
total_stops += 1
stop = False
timer.stop()
def reset():
global count, stop, total_stops, success_stops
timer.stop()
count = 0
total_stops = 0
success_stops = 0
# define event handler for timer with 0.1 sec interval
def tick():
global count
count += 1
# define draw handler
def draw_handler(canvas):
text = format(count)
canvas.draw_text(text, (50, 130), 42, "white")
canvas.draw_text(str(success_stops) + '/' + str(total_stops), (10,30), 20, "yellow")
# create frame
frame = simplegui.create_frame('Stopwatch : The Game', 200, 200)
frame.set_canvas_background('black')
# register event handlers
frame.add_button("Start",start)
frame.add_button("Stop",stop)
frame.add_button("Reset", reset)
frame.set_draw_handler(draw_handler)
timer = simplegui.create_timer(100, tick)
# start frame
frame.start()
# Please remember to review the grading rubric