-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStop_Watch
More file actions
76 lines (66 loc) · 1.74 KB
/
Stop_Watch
File metadata and controls
76 lines (66 loc) · 1.74 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
interval = 100
width = 500
height =400
position=[width/4,height *3 /5]
count = 0 # count the number of tenth second
x = 0
y = 0
# define helper function format that converts time
# in tenths of seconds into formatted string A:BC.D
def format(t):
global tenthsecond
minute = (t// 10 )//60
second = t //10 - minute * 60
tenthsecond = t % 10
if second < 10:
return str(minute) + ':' + '0' + str(second) +'.'+ str(tenthsecond)
else:
return str(minute) + ':' + str(second) +'.'+ str(tenthsecond)
# define event handlers for buttons; "Start", "Stop", "Reset"
def start():
global flag
timer.start()
flag = timer.is_running()
def stop():
global flag
global x
global y
flag = timer.is_running()
if flag:
timer.stop()
y = y + 1
if tenthsecond == 0:
x = x +1
def reset():
global count
global x
global y
timer.stop()
x = 0
y = 0
count = 0
# define event handler for timer with 0.1 sec interval
def tick():
global count
count = count + 1
# define draw handler
def draw(canvas):
global count
message = format(count) # time A:BC:D
canvas.draw_text(message, position, 120, 'White')
canvas.draw_text(str(x)+'/'+str(y),[width*5/6,height*1/6],50,'Green')
# create frame
frame = simplegui.create_frame('Stopwatch', width, height,)
# register event handlers
frame.add_button("Start", start, 50)
frame.add_button("Stop", stop, 50)
frame.add_button("Reset", reset, 50)
frame.set_draw_handler(draw)
timer = simplegui.create_timer(interval, tick)
# start frame
frame.start()
timer.stop()
# Please remember to review the grading rubric