-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStopwatch_The_Game.py
96 lines (79 loc) · 2.19 KB
/
Stopwatch_The_Game.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
91
92
93
94
95
96
try:
import simplegui
except ImportError:
import SimpleGUICS2Pygame.simpleguics2pygame as simplegui
# define global variables
n = 0
min = 0
sec = 0
tenth_sec = 0
is_running = False
count_stop = 0
count_success_stop = 0
# define helper function format that converts time
# in tenths of seconds into formatted string A:BC.D
def format(t):
global sec
global tenth_sec
global min
#global is_running
sec = int (t / 10)
tenth_sec = t % 10
min = int (t / 600)
#return sec
if sec < 10:
return str(min) + ":0" + str(sec) + ":" + str(tenth_sec)
elif sec >= 10 and sec <= 59:
return str(min) + "0:" + str(sec) + ":" + str(tenth_sec)
elif sec >= 60:
sec = sec % 60
if sec >=10:
return str(min) + ":" + str(sec) + ":" + str(tenth_sec)
else:
return str(min) + ":" + "0" + str(sec) + ":" + str(tenth_sec)
#is_running = True
# define event handlers for buttons; "Start", "Stop", "Reset"
def button_start():
global count_start
global is_running
global check
timer.start()
is_running = True
check = True
def button_stop():
global count_stop
global count_success_stop
if timer.is_running():
count_stop += 1
if tenth_sec == 0:
count_success_stop += 1
timer.stop()
def button_reset():
global n
global count_stop
global count_success_stop
n = 0
count_stop = 0
count_success_stop = 0
timer.stop()
# define event handler for timer with 0.1 sec interval
def timer_handler():
global n
n = n + 1
#print n
# define draw handler
def draw_handler(canvas):
canvas.draw_text(format(n),[120,150],30,"Black")
canvas.draw_text(str(count_stop),[230,40],20,"Green")
canvas.draw_text("/",[250,40],20,"Green")
canvas.draw_text(str(count_success_stop),[265,40],20,"Green")
# create frame
# register event handlers
frame = simplegui.create_frame("Stopwatch", 300, 300)
frame.add_button("Start",button_start)
frame.add_button("Stop",button_stop)
frame.add_button("Reset",button_reset)
timer = simplegui.create_timer(100, timer_handler)
# start frame
frame.start()
frame.set_draw_handler(draw_handler)