-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.py
209 lines (162 loc) · 6.48 KB
/
timer.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# Pygame Timer made by Huatao
###########
# Imports #
###########
import time
import pygame
import sys
from pygame.locals import *
from pygame import mixer
##############
# Game Setup #
##############
pygame.init()
mixer.init()
pygame.display.set_caption("Pygame Timer")
# Define Surface
gameDisplay = pygame.display.set_mode((400, 400))
# Set Clock
clock = pygame.time.Clock()
green = (0, 255, 0)
grey = (128, 128, 128)
rectpos = (0, 0)
# List to store user's set time
seconds = [""]
alarm_state = "Off"
font = pygame.font.SysFont(None, 40)
font2 = pygame.font.SysFont(None, 30)
# Set up alarm sound
sound = mixer.Sound("bell.wav")
##################
# Function Setup #
##################
def remove_ls():
try:
seconds.remove("Times up!")
except:
return ""
def start_timer():
try:
times = "".join(seconds)
times = times.lstrip("0")
final_time = int(times)
time.sleep(final_time)
if alarm_state == "On":
sound.play()
clear_timer()
seconds.append("Times up!")
except:
print("Error.")
pygame.quit()
sys.exit()
def clear_timer():
seconds.clear()
def quit_timer():
pygame.quit()
sys.exit()
##########################
# Event and Screen Setup #
##########################
while True:
try:
for event in pygame.event.get():
if event.type == pygame.MOUSEMOTION:
# Move the timer's "mouse" accordingly to the real mouse
rectpos = event.pos
if event.type == pygame.MOUSEBUTTONDOWN:
# Detect if the mouse presses down on the buttons
if mouse.colliderect(button1):
remove_ls()
seconds.append("1")
if mouse.colliderect(button2):
remove_ls()
seconds.append("2")
if mouse.colliderect(button3):
remove_ls()
seconds.append("3")
if mouse.colliderect(button4):
remove_ls()
seconds.append("4")
if mouse.colliderect(button5):
remove_ls()
seconds.append("5")
if mouse.colliderect(button6):
remove_ls()
seconds.append("6")
if mouse.colliderect(button7):
remove_ls()
seconds.append("7")
if mouse.colliderect(button8):
remove_ls()
seconds.append("8")
if mouse.colliderect(button9):
remove_ls()
seconds.append("9")
if mouse.colliderect(button0):
remove_ls()
seconds.append("0")
if mouse.colliderect(start):
start_timer()
if mouse.colliderect(clear):
clear_timer()
sound.stop()
if mouse.colliderect(exit_timer):
quit_timer()
if mouse.colliderect(off):
alarm_state = "Off"
if mouse.colliderect(on):
alarm_state = "On"
if event.type == QUIT:
pygame.quit()
sys.exit()
gameDisplay.fill((255, 255, 255))
# Draw the buttons
button1 = pygame.draw.rect(gameDisplay, green, Rect((20, 100), (40, 40)))
gameDisplay.blit(font.render("1", True, (0, 0, 200)), (31, 107.5))
button2 = pygame.draw.rect(gameDisplay, green, Rect((100, 100), (40, 40)))
gameDisplay.blit(font.render("2", True, (0, 0, 200)), (111, 107.5))
button3 = pygame.draw.rect(gameDisplay, green, Rect((180, 100), (40, 40)))
gameDisplay.blit(font.render("3", True, (0, 0, 200)), (191, 107.5))
button4 = pygame.draw.rect(gameDisplay, green, Rect((260, 100), (40, 40)))
gameDisplay.blit(font.render("4", True, (0, 0, 200)), (271, 107.5))
button5 = pygame.draw.rect(gameDisplay, green, Rect((340, 100), (40, 40)))
gameDisplay.blit(font.render("5", True, (0, 0, 200)), (351, 107.5))
button6 = pygame.draw.rect(gameDisplay, green, Rect((20, 260), (40, 40)))
gameDisplay.blit(font.render("6", True, (0, 0, 200)), (31, 267.5))
button7 = pygame.draw.rect(gameDisplay, green, Rect((100, 260), (40, 40)))
gameDisplay.blit(font.render("7", True, (0, 0, 200)), (111, 267.5))
button8 = pygame.draw.rect(gameDisplay, green, Rect((180, 260), (40, 40)))
gameDisplay.blit(font.render("8", True, (0, 0, 200)), (191, 267.5))
button9 = pygame.draw.rect(gameDisplay, green, Rect((260, 260), (40, 40)))
gameDisplay.blit(font.render("9", True, (0, 0, 200)), (271, 267.5))
button0 = pygame.draw.rect(gameDisplay, green, Rect((340, 260), (40, 40)))
gameDisplay.blit(font.render("0", True, (0, 0, 200)), (351, 267.5))
on = pygame.draw.rect(gameDisplay, (225, 0, 0), Rect((310, 170), (70, 60)))
gameDisplay.blit(font2.render("Alarm", True, green), (315, 180))
gameDisplay.blit(font2.render("On", True, green), (330, 205))
off = pygame.draw.rect(gameDisplay, (225, 0, 0), Rect((20, 170), (70, 60)))
gameDisplay.blit(font2.render("Alarm", True, green), (25, 180))
gameDisplay.blit(font2.render("Off", True, green), (38, 205))
start = pygame.draw.rect(
gameDisplay, (230, 200, 0), Rect((110, 170), (180, 60))
)
gameDisplay.blit(font.render("Start Timer", True, (100, 50, 50)), (125, 187))
clear = pygame.draw.rect(gameDisplay, green, Rect((60, 325), (120, 40)))
gameDisplay.blit(font2.render("Clear Time", True, (0, 0, 200)), (65, 335))
exit_timer = pygame.draw.rect(gameDisplay, green, Rect((220, 325), (120, 40)))
gameDisplay.blit(font2.render("Quit Timer", True, (0, 0, 200)), (225, 335))
display = pygame.draw.rect(gameDisplay, grey, Rect((40, 30), (280, 40)))
alarm = pygame.draw.rect(gameDisplay, (255, 0, 0), Rect((340, 30), (40, 40)))
gameDisplay.blit(
font2.render(alarm_state, True, green),
(345, 43),
)
mouse = pygame.draw.rect(gameDisplay, (200, 100, 0), Rect((rectpos), (10, 10)))
gameDisplay.blit(
font2.render("".join(seconds), True, (255, 255, 255)),
(60, 40),
)
clock.tick(60)
pygame.display.update()
except SyntaxError:
quit()