-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_3.py
63 lines (52 loc) · 1.39 KB
/
1_3.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
import simplegui
# define global variables
time = 0
count = 0
win = 0
running = False
# define helper function format that converts time
# in tenths of seconds into formatted string A:BC.D
def format(t):
global message
min = t // 600
tensec = (t - min*600) // 100
sec = (t - min*600 - tensec*100) // 10
milisec = t%10
return str(min)+":"+str(tensec)+str(sec)+":"+str(milisec)
# define event handlers for buttons; "Start", "Stop", "Reset"
def start():
global running
running = True
timer.start()
def stop():
global running, win, count
timer.stop()
if running == True:
count += 1
if time%10 == 0:
win += 1
def reset():
global running, win, count, time
timer.stop()
running = False
time = 0
win = 0
count = 0
# define event handler for timer with 0.1 sec interval
def timer():
global time
time += 1
# define draw handler
def drawing(canvas):
canvas.draw_text(format(time), (100,100), 30, "Red")
result = str(win)+" / "+str(count)
canvas.draw_text(result, (150, 20), 20, "Green")
# create frame
frame = simplegui.create_frame("GAME", 200, 200)
timer = simplegui.create_timer(100, timer)
# register event handlers
frame.set_draw_handler(drawing)
frame.add_button("Start", start, 100)
frame.add_button("Stop", stop, 100)
frame.add_button("reset", reset, 100)
frame.start()