-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiniProject_Week3.py
More file actions
76 lines (57 loc) · 1.75 KB
/
MiniProject_Week3.py
File metadata and controls
76 lines (57 loc) · 1.75 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
# template for "Stopwatch: The Game"
import simplegui
# define global variables
time_num=0
success_count=0
total_count=0
timer=''
# define helper function format that converts time
# in tenths of seconds into formatted string A:BC.D
def format(t):
B_str=''
A=t/(60*10)
B=(t-(A*60*10))/10
if len(str(B))==1:
B_str='0'+str(B)
else:
B_str=str(B)
D=t%10
return str(A)+':'+B_str+'.'+str(D)
# define event handlers for buttons; "Start", "Stop", "Reset"
def start_button_handler():
global timer
if not timer.is_running():
timer.start()
def stop_button_handler():
global timer, success_count, total_count, time_num
if timer.is_running():
timer.stop()
total_count += 1
if time_num % 10 == 0:
success_count += 1
def reset_button_handler():
global timer, time_num, success_count, total_count
if timer.is_running():
timer.stop()
time_num = 0
success_count=0
total_count=0
# define event handler for timer with 0.1 sec interval
def timer_handler():
global time_num
time_num += 1
# define draw handler
def draw_handler(canvas):
canvas.draw_text(format(time_num), (100, 150), 30, 'White')
canvas.draw_text(str(success_count)+'/'+str(total_count), (260, 25), 20, 'White')
# create frame
frame = simplegui.create_frame("Stopwatch: The Game", 300,300)
# register event handlers
frame.set_draw_handler(draw_handler)
start_but = frame.add_button('Start', start_button_handler, 70)
stop_but = frame.add_button('Stop', stop_button_handler, 70)
reset_but = frame.add_button('Restart', reset_button_handler, 70)
# start frame
frame.start()
timer = simplegui.create_timer(100, timer_handler)
# Please remember to review the grading rubric