-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplegui stopwatch game.py
59 lines (44 loc) · 1.28 KB
/
simplegui stopwatch 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
# template for "Stopwatch: The Game"
import simplegui
# define global variables
time = 0
tries = 0
successes = 0
# define helper function format that converts time
# in tenths of seconds into formatted string A:BC.D
def format(t):
pass
# define event handlers for buttons; "Start", "Stop", "Reset"
def start():
timer.start()
def stop():
global time, tries, successes
if timer.is_running():
tries += 1
timer.stop()
if time % 10 == 0:
successes += 1
def reset():
global time, tries, successes
time = tries = successes = 0
# define event handler for timer with 0.1 sec interval
def tick():
global time
time += 1
# define draw handler
def draw(canvas):
global time, tries, successes
canvas.draw_text((str)(time), [200, 200], 36, "Cyan")
canvas.draw_text((str)(successes)+" /", [400, 100], 36, "Purple")
canvas.draw_text((str)(tries), [445, 100], 36, "Purple")
# create frame
frame = simplegui.create_frame("Stopwatch", 500, 500)
# register event handlers
frame.set_draw_handler(draw)
timer = simplegui.create_timer(100, tick)
frame.add_button("Start", start)
frame.add_button("Stop", stop)
frame.add_button("Reset", reset)
# start frame
frame.start()
# Please remember to review the grading rubric