-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStopwatch-The-Game.py
More file actions
96 lines (73 loc) · 2.48 KB
/
Stopwatch-The-Game.py
File metadata and controls
96 lines (73 loc) · 2.48 KB
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
# "Stopwatch: The Game"
# Import modules
import simplegui
# Define global variables (program state)
elapsed_time = 0
rounds = 0
score = 0
# Define "helper" functions
def format(t):
""" Helper function format that converts time
in tenths of seconds into formatted string A:BC.D """
tenths_of_seconds = t % 10
t /= 10
seconds = t % 60
minutes = t / 60
# Format String: http://www.codeskulptor.org/docs.html#string-formatting
formatted_time = '%(minutes)0d:%(seconds)02d.%(ts)0d' % \
{
"minutes": minutes,
"seconds": seconds,
"ts": tenths_of_seconds
}
return formatted_time
def score_board():
""" Helper function that formats the score board. """
return str(score) + "/" + str(rounds)
# Define event handler functions
def start_handler():
""" Event handler for the Start button. """
timer.start()
def stop_handler():
""" Event handler for the Stop button. """
global rounds
global score
if timer.is_running():
timer.stop()
# Increases the number of rounds
rounds += 1
# If the timmer stoped in a whole second
if elapsed_time % 10 == 0:
# Increases the score
score += 1
def reset_handler():
""" Event handler for the Reset button. """
global elapsed_time
global rounds
global score
# In the Video Lecture the Reset Button stops the time
# https://class.coursera.org/interactivepython-005/lecture/29
timer.stop()
elapsed_time = 0
rounds = 0
score = 0
def timer_handler():
""" Event handler for timer with 0.1 sec interval. """
global elapsed_time
elapsed_time += 1
def draw_handler(canvas):
""" Draws the score board and timer. """
# Draw Score Board
canvas.draw_text(score_board(), (155, 25), 25, 'Green')
# Draw timer
canvas.draw_text(format(elapsed_time), (50, 90), 40, 'White')
# Create a frame
frame = simplegui.create_frame('Stopwatch: The Game', 200, 150)
# Register event handlers
timer = simplegui.create_timer(100, timer_handler)
frame.set_draw_handler(draw_handler)
start_button = frame.add_button('Start', start_handler, 100)
stop_button = frame.add_button('Stop', stop_handler, 100)
reset_handler = frame.add_button('Reset', reset_handler, 100)
# Start frame
frame.start()