-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
282 lines (242 loc) · 9.88 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import pygame
import sys
from button import Button
from query import answerQuery as aq
import speech_recognition as sr
import os
import platform
pygame.init()
# VARIABLES
WIDTH, HEIGHT = 1280, 720
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
WHITE = (255, 255, 255)
BLURPLE = (138, 43, 226)
BLUE = (0, 0, 128)
BLACK = (0, 0, 0)
PLANETS = ["earth", "jupiter", "mars", "mercury",
"neptune", "saturn", "uranus", "venus", "sun"]
def main():
# LOAD IMGS
background = pygame.image.load("assets/spaceBG.jpeg")
btnSearch = pygame.image.load(
"assets/btnSearch.png").convert_alpha()
btnSearchVoice = pygame.image.load(
"assets/btnSearchVoice.png").convert_alpha()
btnSolarSystem = pygame.image.load(
"assets/btnSolarSystem.png").convert_alpha()
btnTrackISS = pygame.image.load("assets/btnTrackISS.png")
btnQuit = pygame.image.load("assets/btnQuit.png").convert_alpha()
# TITLE
font = pygame.font.SysFont("corbel", 60)
title = font.render("Welcome to Space Query!", True, WHITE, BLUE)
titleRect = title.get_rect()
titleRect.center = (WIDTH//2, HEIGHT//8)
# BUTTONS
btnSearch = Button(WIDTH//4*3, HEIGHT//4, btnSearch, 0.8)
btnQuit = Button(WIDTH//4*3, HEIGHT//8*7, btnQuit, 0.8)
btnSearchVoice = Button(WIDTH//4*3, HEIGHT//2, btnSearchVoice, 0.8)
btnSolarSystem = Button(WIDTH//10, HEIGHT//4*3, btnSolarSystem, 0.8)
btnTrackISS = Button(WIDTH//5*3, HEIGHT//4*3, btnTrackISS, 0.8)
# TEXT INPUT
baseFont = pygame.font.Font(None, 40)
userText = ""
previousLetters = 0
inputRect = pygame.Rect(WIDTH//10, HEIGHT//8*2, 800, 50)
activeColour = pygame.Color("cadetblue1")
passiveColour = pygame.Color("blueviolet")
colour = passiveColour
active = False
# VOICE INPUT
searchVoice = False
voiceText = ""
voiceInputrect = pygame.Rect(WIDTH//10, HEIGHT//2, 800, 50)
voiceActiveColour = pygame.Color("cadetblue1")
voicePassiveColour = pygame.Color("blueviolet")
voiceColour = passiveColour
voiceActive = False
# DISPLAY
pygame.display.set_caption("Main Menu")
SCREEN.fill(BLACK)
clock = pygame.time.Clock()
while True:
# Sets FPS to 60. Adds background and title to screen.
clock.tick(60)
SCREEN.blit(background, (0, 0))
SCREEN.blit(title, titleRect)
# DISPLAY BUTTONS AND CHECK IF IT HAS BEEN PRESSED
if btnSearch.draw(SCREEN):
spaceQuery(userText)
if btnSearchVoice.draw(SCREEN):
voiceActive = True
searchVoice = True
voiceText = "Listening..."
if btnSolarSystem.draw(SCREEN):
return
if btnTrackISS.draw(SCREEN):
if platform.system() == "Windows":
os.system("python iss.py")
else:
os.system("python3 iss.py")
if btnQuit.draw(SCREEN):
pygame.quit()
sys.exit()
# DISPLAY TEXT INPUT
if active:
colour = activeColour
else:
colour = passiveColour
pygame.draw.rect(SCREEN, colour, inputRect)
textSurface = baseFont.render(
userText[previousLetters:], True, (255, 255, 255))
SCREEN.blit(textSurface, (inputRect.x+5, inputRect.y+5))
# DISPLAY VOICE INPUT
if voiceActive:
voiceColour = voiceActiveColour
else:
voiceColour = voicePassiveColour
pygame.draw.rect(SCREEN, voiceColour, voiceInputrect)
voiceTextSurface = baseFont.render(
voiceText, True, (255, 255, 255))
SCREEN.blit(voiceTextSurface, (voiceInputrect.x+5, voiceInputrect.y+5))
pygame.display.flip()
# TEXT
# Quits program if application is closed
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Input text of query
if event.type == pygame.MOUSEBUTTONDOWN:
if inputRect.collidepoint(event.pos):
active = True
else:
active = False
if event.type == pygame.KEYDOWN:
if active == True:
if len(userText) >= 45:
if event.key == pygame.K_BACKSPACE:
userText = userText[:-1]
if previousLetters > 0:
previousLetters -= 1
else:
previousLetters += 1
userText += event.unicode
else:
if event.key == pygame.K_BACKSPACE:
userText = userText[:-1]
else:
userText += event.unicode
if event.key == pygame.K_RETURN:
spaceQuery(userText)
# VOICE
if event.type == pygame.MOUSEBUTTONDOWN:
if btnSearchVoice.rect.collidepoint(event.pos):
voiceActive = True
else:
voiceActive = False
if searchVoice:
# variable for checking if the program didn't catch the voice
correct = True
r = sr.Recognizer()
while correct:
text = ''
with sr.Microphone() as source:
# listen for 1 second to calibrate the energy threshold for ambient noise levels
r.adjust_for_ambient_noise(source)
# read the audio data from the default microphone, will record for 10 seconds
# audio_data = r.record(source, duration=10)
audio_data = r.listen(source)
# convert speech to text
try:
text = r.recognize_google(audio_data)
voiceText = text
pygame.draw.rect(
SCREEN, voiceColour, voiceInputrect)
voiceTextSurface = baseFont.render(
voiceText, True, (255, 255, 255))
SCREEN.blit(
voiceTextSurface, (voiceInputrect.x+5, voiceInputrect.y+5))
pygame.display.flip()
spaceQuery(voiceText)
except:
voiceText = "Sorry, I didn't get that"
main()
# except sr.RequestError as e:
#print("Could not request results from Google Speech Recognition service; {0}".format(e))
pygame.display.update()
def spaceQuery(userText):
# LOAD IMGS
background = pygame.image.load("assets/spaceBG.jpeg")
btnQuit = pygame.image.load("assets/btnQuit.png").convert_alpha()
btnBack = pygame.image.load("assets/btnBack.png").convert_alpha()
earthImg = pygame.image.load("assets/earth.jpeg")
jupiterImg = pygame.image.load("assets/jupiter.jpeg")
marsImg = pygame.image.load("assets/mars.jpeg")
mercuryImg = pygame.image.load("assets/mercury.jpeg")
neptuneImg = pygame.image.load("assets/neptune.jpeg")
saturnImg = pygame.image.load("assets/saturn.jpeg")
uranusImg = pygame.image.load("assets/uranus.png")
venusImg = pygame.image.load("assets/venus.jpeg")
sunImg = pygame.image.load("assets/sun.jpeg")
planetImgs = [earthImg, jupiterImg, marsImg, mercuryImg,
neptuneImg, saturnImg, uranusImg, venusImg, sunImg]
for i in range(len(planetImgs)):
planetImgs[i] = pygame.transform.scale(planetImgs[i], (250, 250))
# TITLE
font = pygame.font.SysFont("corbel", 60)
title = font.render("To Answer Your Question...",
True, WHITE, BLUE)
titleRect = title.get_rect()
titleRect.center = (WIDTH//2, HEIGHT//8)
# ANSWER
answerText = aq(userText)[0]
font = pygame.font.SysFont("corbel", 40)
if len(answerText) > 60:
answerText1 = answerText[:81]
if answerText[81] != " ":
answerText2 = "-" + answerText[81:]
else:
answerText2 = answerText[81:]
answerText1 = font.render(answerText1, True, WHITE, BLURPLE)
answerText2 = font.render(answerText2, True, WHITE, BLURPLE)
answerRect1 = answerText1.get_rect()
answerRect2 = answerText2.get_rect()
answerRect1.center = (WIDTH//2, HEIGHT//8*3)
answerRect2.center = (WIDTH//2, HEIGHT//2)
else:
answerText = font.render(answerText, True, WHITE, BLURPLE)
answerRect = answerText.get_rect()
answerRect.center = (WIDTH//2, HEIGHT//8*3)
# BUTTONS
btnQuit = Button((WIDTH//4*3), HEIGHT/8*7, btnQuit, 0.8)
btnBack = Button(WIDTH//10, HEIGHT/8*7, btnBack, 0.8)
# DISPLAY
pygame.display.set_caption("Query")
SCREEN.fill(BLACK)
clock = pygame.time.Clock()
while True:
# Sets FPS to 60. Adds background and title to screen.
clock.tick(60)
SCREEN.blit(background, (0, 0))
SCREEN.blit(title, titleRect)
try:
SCREEN.blit(answerText, answerRect)
except:
SCREEN.blit(answerText1, answerRect1)
SCREEN.blit(answerText2, answerRect2)
for i in range(len(PLANETS)):
if PLANETS[i] in userText.lower():
SCREEN.blit(planetImgs[i], (WIDTH//5*2, HEIGHT//3*2))
# Adds buttons to screen and checks if it is pressed every frame
if btnBack.draw(SCREEN):
main()
if btnQuit.draw(SCREEN):
pygame.quit()
sys.exit()
# Quits program if application is closed
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
main()