-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStopwatch.py
83 lines (64 loc) · 2.02 KB
/
Stopwatch.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
import simplegui
# define global variables
games = 0 # the number of games
win_games = 0 # the number of games won
clock = 0 # timer
milli_sec = 0 # millisecond
stop_clock = False # Stop or Start timer?
# define helper function format that converts time
# in tenths of seconds into formatted string A:BC.D
def format(t):
# Split into minute, second and millisecond
global milli_sec
minute = t // 1000
sec = (t - minute * 1000) // 10
milli_sec = (t - minute * 1000) % 10
if sec < 10:
return str(minute) + ":0" + str(sec) + "." + str(milli_sec)
else:
return str(minute) + ":" + str(sec) + "." + str(milli_sec)
# define event handlers for buttons; "Start", "Stop", "Reset"
def start():
global stop_clock
if stop_clock == False:
timer.start()
stop_clock = True
def stop():
global games, win_games, stop_clock
if stop_clock == True:
timer.stop()
stop_clock = False
games += 1
if milli_sec == 0:
win_games += 1
def reset():
global games, win_games, clock, milli_sec, stop_clock
if stop_clock == True:
timer.stop()
stop_clock = False
games = 0
win_games = 0
clock = 0
milli_sec = 0
# define event handler for timer with 0.1 sec interval
def tick():
global clock
clock += 1
# 60 sec == 1 min
if clock - clock // 1000 * 1000 == 600:
clock += 400
# define draw handler
def draw(canvas):
canvas.draw_text(str(win_games)+"/"+str(games), [150, 20], 20, "Green")
canvas.draw_text(format(clock),[70, 110], 28, "White")
# create frame and timer
frame = simplegui.create_frame("Stopwatch: The Game", 200, 200)
timer = simplegui.create_timer(100, tick)
# register event handlers
frame.add_button("Start", start, 100)
frame.add_button("Stop", stop, 100)
frame.add_button("Reset", reset, 100)
# register draw handler
frame.set_draw_handler(draw)
# start frame
frame.start()