-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstriking-clock.py
90 lines (73 loc) · 2.23 KB
/
striking-clock.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#import simplegui
import SimpleGUICS2Pygame.simpleguics2pygame as simplegui
# define global variables
intervals = 100
cnt = 0
total_stops = 0
correct_stops = 0
stop = True
# define helper function format that converts time
# in tenths of seconds into formatted display_number A:BC.D
def format(t):
tenth_of_a_sec = (t) % 10
sec = int(t / 10) % 10
mins = int(t / 600) % 600
ten_of_a_min = int(t / 100) % 6
display_number = str(mins) + ":" + str(ten_of_a_min) + str(sec) + "." + str(tenth_of_a_sec)
return display_number
pass
# define event handlers for buttons; "Start", "Stop", "Reset"
def Start():
global cnt, stop
stop = False
timer.start()
def Stop():
global total_stops, correct_stops, stop
if stop == False :
if cnt % 20 <= 3 and cnt != 0 :
correct_stops += 1
total_stops += 1
elif cnt != 0 :
total_stops += 1
stop = True
timer.stop()
def Strike():
global total_stops, correct_stops, stop
if stop == False :
if cnt % 20 <= 3 and cnt != 0 :
correct_stops += 1
total_stops += 1
elif cnt != 0 :
total_stops += 1
def Reset():
global cnt, correct_stops, total_stops
cnt = 0
stop = True
total_stops = 0
correct_stops = 0
timer.stop()
# define event handler for timer with 0.1 sec intervals
def tick():
global cnt
cnt += 1
# define draw handler
def draw(canvas):
text = format(cnt)
canvas.draw_circle([130, 115], 70, 5, 'Yellow', 'Orange')
canvas.draw_text( text, (80, 125), 42, "white")
canvas.draw_text( "interval is 2s", (160,45),16, "black")
canvas.draw_text(str(correct_stops) + '/' + str(total_stops), (190,30), 24, "purple")
canvas.draw_text( "Striking watch", (40,220),30, "black")
# Create a frame
frame = simplegui.create_frame("Stopwatch game", 250, 250)
frame.set_canvas_background('cyan')
# Register event handlers
frame.add_button("Start", Start, 100)
frame.add_button("Strike now", Strike, 100)
frame.add_button("Stop", Stop, 100)
frame.add_button("Reset", Reset, 100)
frame.set_draw_handler(draw)
timer = simplegui.create_timer(intervals, tick)
# Start the frame animation
frame.start()
Reset()