This repository has been archived by the owner on Oct 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
stopwatch.py
124 lines (101 loc) · 3.29 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'''
The MIT License (MIT)
Copyright (c) 2016 Antal Janos Monori.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''
###########
# Imports #
###########
try:
import simplegui
except ImportError:
import SimpleGUICS2Pygame.simpleguics2pygame as simplegui
import simpleplot
#############
# Variables #
#############
# define global variables
tick = 0
tries = 0
win = 0
stopped = True
####################
# Helper functions #
####################
# define helper function format that converts integer
# counting tenths of seconds into formatted string A:BC.D
def format(t):
min_tick = t / 600
sec_tick = (min_tick - int(min_tick)) * 60
mil_tick = (sec_tick - int(sec_tick)) * 10
A = str(int(min_tick))
BC = (str(int(sec_tick)) if sec_tick >= 10 else str(0)+str(int(sec_tick)))
D = str(int(mil_tick))
outcome = A+":"+BC+"."+D
return outcome
# the game part of the project
def reflex():
global tries, win
if (format(tick)[5:] == "0"):
win += 1
tries += 1
else: tries += 1
############
# Handlers #
############
# define event handlers for buttons; "Start", "Stop", "Reset"
def start():
global stopped
timer.start()
stopped = False
def stop():
global stopped
timer.stop()
if (stopped == False): reflex()
stopped = True
def reset():
global tick, tries, win, stopped
timer.stop()
stopped = True
tick = 0
tries = 0
win = 0
# define event handler for timer
def update():
global tick
tick += 1
print tick
# draw handler
def draw(canvas):
global tick, tries, win
timer = format(tick)
score = str(win)+" / "+str(tries)
canvas.draw_text(timer,[70, 70], 18, "White")
canvas.draw_text(score, [145, 15], 14, "Green")
######
# UI #
######
# create frame
frame = simplegui.create_frame("Stopwatch: The Game", 200, 120)
# register event handlers
timer = simplegui.create_timer(100, update)
start = frame.add_button("Start", start, 50)
stop = frame.add_button("Stop", stop, 50)
reset = frame.add_button("Reset", reset, 50)
frame.set_draw_handler(draw)
########
# Main #
########
# start timer and frame
frame.start()
#########
# Tests #
#########
#print "Some tests:"
#print "Outcome | Expected"
#print " "+format(0)+" = 0:00.0"
#print " "+format(11)+" = 0:01.1"
#print " "+format(321)+" = 0:32.1"
#print " "+format(613)+" = 1:01.3"