-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
220 lines (177 loc) · 6.28 KB
/
main.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
210
211
212
213
214
215
216
217
218
219
220
"""
-------------
DRY-EYE TIMER
-------------
Program for nerds to run who have dry-eyes because we spend too much
time staring at gam... I mean code
"""
# Imports
import os
import sys
import pygame
import time
from inputimeout import inputimeout, TimeoutOccurred
pygame.init()
pygame.mixer.init()
# Constants
TXT = "preferences/pref.txt"
TIME = "preferences/time.txt"
# Initializing sound to 0
sound = 0
# Default timer time
timer_time = 1200
# Function to read the saved sound preference file
def reader():
global sound # *allowing local var to access global variable
file1 = open(TXT, "r") # reads pref.txt
sound_read = file1.read() # Store value from pref.txt as str because pref.txt is a text file
sound = int(sound_read)
# Function to save a file with the sound preference
def saver():
global sound # *
check = input("Would you like to save this preference to run it in the future(yes/no): ").lower()
if check == 'yes':
file1 = open(TXT, "w") # Here, creates new file to write the preference
str_sound = repr(sound)
file1.write(str_sound)
file1.close() # Always have this line when opening files
if check == 'no':
print("Not saving your preference...")
def testprint(): # Laziness.
print("Preferred Sound Test")
def sleep():
if sound == 4:
time.sleep(5)
# May make vars for sleep timer for custom sounds
def time_reader():
global timer_time # *allowing local var to access global variable
file1 = open(TIME, "r") # reads time.txt
time_read = file1.read() # Store value from time.txt as str because time.txt is a text file
timer_time = int(time_read)
def time_saver():
global timer_time # *
check = input("Would you like to save this TIME preference to run in the future(yes/no): ").lower()
if check == 'yes':
file1 = open(TIME, "w") # Here, creates new file to write the preference
str_time = repr(timer_time)
file1.write(str_time)
file1.close() # Always have this line when opening files
if check == 'no':
print("Not saving your preference...")
def sound_preference_selector():
global sound
sound = int(input("""
Enter Sound Effect
0 = quit
1 = notification
2 = gangsta's paradise
3 = ar
4 = EEEEEEEEEEE
Enter number: """))
return sound
def timer_duration_selector():
global timer_time
timer_time = int(input("""
---------------------
Timer duration setter
1200 seconds = 20 min
20 minutes is default to avoid issues like audio clipping/the same audio file layering on top of itself
Enter timer duration(seconds):
"""))
return timer_time
# ----------------TRY-EXCEPT BLOCKS---------------- #
# Convoluted Try-Except block for sound preference
try:
open(TXT, "r") # Throws FileNotFoundError if pref.txt does not exist to bypass
# try-except TimeoutOccurred block
try:
print("Running from a previously existing preference...")
time.sleep(1)
print("You have 5 seconds to answer")
# Using inputimeout to set a timer on the key_press input
key_press = inputimeout("Press 'S' to break to delete SOUND preference: ", timeout=5).lower()
if key_press == "s":
os.remove(TXT) # Removes pref.txt from the working directory
except TimeoutOccurred:
reader() # If TimeoutOccurred, then calls reader() function
except FileNotFoundError:
sound_preference_selector()
saver() # Calls saver() func incase user wants to save the sound preference
# Try-except block to avoid the program running cus if this weren't here, the program would not
# play any sound even when the timer is running
try:
open(TXT, "r")
except FileNotFoundError:
check = input("""
Would you like to run the timer with the default sound preference? Timer will quit if
the answer is no (don't know how to bring the preference selector back, may fix later):
""").lower()
if check == 'yes':
print("Default sound preference is 1")
sound = 1
else:
print("Bye")
sys.exit(1)
try: # Try-except block for timer duration
open(TIME, "r") # Opens time.txt
try:
print("Running from previously existing TIME preference...")
time.sleep(1)
print("You have 5 seconds to answer...")
# Using inputimeout to set a timer on the key_press input
key_press = inputimeout("Press 'T' to break to delete TIME preference: ", timeout=5).lower()
if key_press == "t":
os.remove(TIME) # Removes time.txt
else:
print("Should've left it empty...")
sys.exit(1) # May not work on Windows. Pls check Windows' users (pls tell if that
# is the wrong punctuation mark)
except TimeoutOccurred:
time_reader()
print("\n")
print("Timer duration is", timer_time, "seconds =", timer_time / 60, "minutes")
except FileNotFoundError:
timer_duration_selector()
time_saver() # Calls time_saver() func incase user wants to save the sound preference
print("\n")
print("Timer duration is", timer_time, "seconds =", timer_time / 60, "minutes")
# -----------SOUND TESTER----------- #
if sound == 4:
testprint()
sounda = pygame.mixer.Sound("funtimes.mp3")
sounda.play()
elif sound == 3: # fixed elif thing
testprint()
sounda = pygame.mixer.Sound("ara-ara.mp3")
sounda.play()
elif sound == 2:
testprint()
sounda = pygame.mixer.Sound("gangsta's paradise.mp3")
sounda.play()
elif sound == 1:
testprint()
sounda = pygame.mixer.Sound("notification.mp3")
sounda.play()
# ------------TIMER WHILE-LOOP------------ #
while sound != 0:
t = timer_time
def countdown(t):
while t:
mins, secs = divmod(t, 60)
timer = '{:02d}:{:02d}'.format(mins, secs)
print(timer, end="\r")
time.sleep(1)
t -= 1
if sound == 4:
sound4 = pygame.mixer.Sound("funtimes.mp3")
sound4.play()
if sound == 3:
sound3 = pygame.mixer.Sound("ara-ara.mp3")
sound3.play()
elif sound == 2:
sound2 = pygame.mixer.Sound("gangsta's paradise.mp3")
sound2.play()
elif sound == 1:
sound1 = pygame.mixer.Sound("notification.mp3")
sound1.play()
countdown(int(t))