-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontEnd.py
467 lines (355 loc) · 13.6 KB
/
frontEnd.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import pygame
from backEnd import *
from random import choice
#############################
#VARIABLES
#############################
#RGB colors for pygame to read
BLACK = (0,0,0)
WHITE = (255,255, 255)
#BLUE = (66, 134, 244) #It's like a nice relaxing blue.
RED = (230, 20, 20)
pi = 3.141592653578
start = pi/2
ticks = pygame.time.get_ticks #because I dont want to type all that
#self-explanatory
# PlayerOneScore = 0
# PlayerTwoScore = 0
screen_x = 1000
screen_y = 700
###########################
#CUSTOM CLASSES
###########################
class WhiteButton():
def __init__(self, text, left, top, width, height):
self.pr = pygame.Rect(left, top, width, height)
self.left = left
self.top = top
self.width = width
self.height = height
pic = pygame.image.load("WhiteButton.png")
pic = pygame.transform.scale(pic, (width, height))
screen.blit(pic, (left, top))
#lets say its X pixels wide. how many characters of a font of given size can that hold?
#If theres more, just truncate it with an ellipsis.
self.text = text
font = pygame.font.SysFont("trebuchetms", 36)
self.pgtext = font.render(text, 1, BLACK)
right = left + width
center = (left + right)/2
stringLen = len(text)
pixelShift = stringLen*8
screen.blit(self.pgtext, (center - pixelShift, top))
pygame.display.update(self.pr)
class GreenButton():
def __init__(self, text, left, top, width, height):
self.pr = pygame.Rect(left, top, width, height)
pic = pygame.image.load("GreenButton.png")
pic = pygame.transform.scale(pic, (width, height))
screen.blit(pic, (left, top))
#lets say its X pixels wide. how many characters of a font of given size can that hold?
#If theres more, just truncate it with an ellipsis.
self.text = text
font = pygame.font.SysFont("trebuchetms", 36)
self.pgtext = font.render(text, 1, BLACK)
right = left + width
center = (left + right)/2
stringLen = len(text)
pixelShift = stringLen*8
screen.blit(self.pgtext, (center - pixelShift, top))
pygame.display.update(self.pr)
class RedButton():
def __init__(self, text, left, top, width, height):
self.pr = pygame.Rect(left, top, width, height)
pic = pygame.image.load("RedButton.png")
pic = pygame.transform.scale(pic, (width, height))
screen.blit(pic, (left, top))
#lets say its X pixels wide. how many characters of a font of given size can that hold?
#If theres more, just truncate it with an ellipsis.
self.text = text
font = pygame.font.SysFont("trebuchetms", 36)
self.pgtext = font.render(text, 1, BLACK)
right = left + width
center = (left + right)/2
stringLen = len(text)
pixelShift = stringLen*8
screen.blit(self.pgtext, (center - pixelShift, top))
pygame.display.update(self.pr)
class GrayButton():
def __init__(self, text, left, top, width, height):
self.pr = pygame.Rect(left, top, width, height)
pic = pygame.image.load("GrayButton.png")
pic = pygame.transform.scale(pic, (width, height))
screen.blit(pic, (left, top))
#lets say its X pixels wide. how many characters of a font of given size can that hold?
#If theres more, just truncate it with an ellipsis.
self.text = text
font = pygame.font.SysFont("trebuchetms", 36)
self.pgtext = font.render(text, 1, BLACK)
right = left + width
center = (left + right)/2
stringLen = len(text)
pixelShift = stringLen*8
screen.blit(self.pgtext, (center - pixelShift, top))
pygame.display.update(self.pr)
############################
#FUNCTIONS
############################
#wait for the given number of seconds. Can still quit.
def customSleep(sec):
start_time = ticks()
while(1):
if(ticks() - start_time > sec*1000):
return
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_F4):
deleteFiles()
exit()
#If the user wants to quit, let em.
def quitCheck():
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_F4):
return True
return False
def deleteFiles():
import os
for x in os.listdir("Songs"):
try:
os.remove("Songs/"+x) #This causes an error if pygame played the song in question last. Solution unknown.
except PermissionError:
pass #Just leave the file. its ok.
for x in os.listdir("Pics"):
try:
os.remove("Pics/"+x)
except PermissionError:
pass #Don't think this will ever happen but better safe than sorry
def reveal(theyWereRight, currentPlayer, timeLeft, ButtonClicked, CorrectButton):
#global PlayerOneScore, PlayerTwoScore
answer = pygame.Rect(200, 600, 1000, 50)
if theyWereRight:
GreenButton(ButtonClicked.text, ButtonClicked.left, ButtonClicked.top, ButtonClicked.width, ButtonClicked.height)
else:
RedButton(ButtonClicked.text, ButtonClicked.left, ButtonClicked.top, ButtonClicked.width, ButtonClicked.height)
GreenButton(CorrectButton.text, CorrectButton.left, CorrectButton.top, CorrectButton.width, CorrectButton.height)
pygame.display.update()
points = timeLeft*10
if theyWereRight == False:
points = -(10 - timeLeft) * 10
# if currentPlayer == 1:
# PlayerOneScore += points
# else:
# PlayerTwoScore += points
pygame.mixer.stop()
pygame.mixer.quit()
customSleep(1)
return points
#defines what happens for a song guess. (5 of these happen for each player each round, grand total of 30.)
def songGuess(playerNum, songNum, roundNum, points, otherPlayerScore, playerOneWins, playerTwoWins):
while(1): #This loop simply gets a song. Not a game mechanic loop.
try:
index = random_index()
jsonData = get_track_data(index)
download_mp3(jsonData)
break
except TypeError: #if the song lacks an mp3 url
pass
except OSError: # if the song name has quotes in it...
pass
except ValueError: #if the song lacks a spotify link (I think)
pass
song_name = jsonData["name"]
song_ID = jsonData["id"]
artist = jsonData["artists"][0]["name"]#[0]["name"] #come on spotify...
artist_ID = jsonData["album"]["artists"][0]["id"]
fakes = getOtherSongs(artist_ID, song_name)[:3]
if len(fakes) < 3 or artist == "Kygo" or artist == "Auli'i Cravalho":
return songGuess(playerNum, songNum, roundNum, points, otherPlayerScore, playerOneWins, playerTwoWins)
#Get Image of artist.
verify = True
try:
download_pic(artist_ID)
except IndexError:
verify = False
pic = None
if verify == True:
pic = pygame.image.load("Pics/"+artist+"Pic.png")
pic = pygame.transform.scale(pic, (200, 200))
while(1):
screen.fill(WHITE)
currentPlayer = font.render("Player "+str(playerNum)+"\'s Turn", 1, BLACK)
screen.blit(currentPlayer, (1,1))
currentScore = font.render("Your Score: "+str(points), 1, BLACK)
screen.blit(currentScore, (1, 50))
if playerNum == 2:
enemyScore = font.render("Player 1's Score: "+str(otherPlayerScore), 1, RED)
screen.blit(enemyScore, (1, 100))
roundWins = font.render("Round Wins", 1, BLACK)
p1w = font.render("Player 1: "+str(playerOneWins), 1, BLACK)
p2w = font.render("Player 2: "+str(playerTwoWins), 1, BLACK)
screen.blit(roundWins, (750, 1))
screen.blit(p1w, (750, 50))
screen.blit(p2w, (750, 100))
artist_text = font.render(artist, 1, BLACK)
if roundNum == 0:
screen.blit(artist_text, (500-len(artist)*10, 300))
if pic != None and roundNum < 2:
screen.blit(pic, (402, 100))
button_texts = ["~~~", "~~~", "~~~", "~~~"] #THESE ARE SUBJECT TO CHANGE
BUTTON_TEXT = ""
if len(song_name)>40:
button_texts[randint(0,3)] = song_name[0:38] + "..."
BUTTON_TEXT = song_name[0:38] + "..."
else:
button_texts[randint(0,3)] = song_name
BUTTON_TEXT = song_name
for fake in fakes:
index = randint(0,3)
while button_texts[index] != "~~~":
index = randint(0,3)
if len(fake)>40:
fake = fake[0:38] + "..."
button_texts[index] = fake
left = 50
top1 = 400
top2 = 460
top3 = 520
top4 = 580
width = 850
height = 40
Button_A = WhiteButton(button_texts[0], left, top1, width, height)
Button_B = WhiteButton(button_texts[1], left, top2, width, height)
Button_C = WhiteButton(button_texts[2], left, top3, width, height)
Button_D = WhiteButton(button_texts[3], left, top4, width, height)
CorrectButton = None
for i in [Button_A, Button_B, Button_C, Button_D]:
if i.text == BUTTON_TEXT:
CorrectButton = i
Button_Skip = WhiteButton("Skip", 375, 640, 100, 40)
pygame.display.update() #THIS IS THE LINE THAT UPDATES ALL THE "SEMI PERMANENT" BLITTED ELEMENTS FOR EACH SONG. AKA EVERYTHING EXCEPT TIME LEFT
pygame.mixer.init()
pygame.mixer.music.load("Songs/"+song_name+".mp3")
pygame.mixer.music.play()
start_time = ticks()
while(1):
curr_time = ticks()
timeUpdateRect = pygame.Rect(1, 150, 400, 50)#left, top, width, height
timeLeft = 10-int((curr_time - start_time)/1000)
timeLeftString = font.render("Time Left: "+str(timeLeft), 1, BLACK)
screen.fill(WHITE, timeUpdateRect)
screen.blit(timeLeftString, (1, 150))
pygame.display.update(timeUpdateRect)
arcRect = pygame.Rect(1, 200, 200, 200)
pygame.draw.rect(screen, WHITE, arcRect, 0) #this could be the problem
pic = pygame.image.load("stopwatch.png")
pic = pygame.transform.scale(pic, (200, 200))
screen.blit(pic, (1, 200))
# pygame.draw.circle(screen, BLACK, (101, 300), 100, 100)
pygame.draw.arc(screen, WHITE, arcRect, start, start + (start*(curr_time-start_time)/2500), 100)
pygame.display.update(arcRect)
if curr_time - start_time >= 10040:
pygame.mixer.stop()
pygame.mixer.quit()
#THEY RAN OUT OF TIME!
notif = font.render("You ran out of time!", 1, BLACK)
screen.fill(WHITE, timeUpdateRect)
screen.blit(notif, (1, 150))
GreenButton(CorrectButton.text, CorrectButton.left, CorrectButton.top, CorrectButton.width, CorrectButton.height)
pygame.display.update(timeUpdateRect)
customSleep(1)
return -30
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_F4):
deleteFiles()
exit()
if pygame.mouse.get_pressed()[0]:
location = pygame.mouse.get_pos()
#location is a tuple with X,Y integer coords
if(50<=location[0]<=900 and 400<=location[1]<=440):
return reveal(Button_A.text == BUTTON_TEXT, playerNum, timeLeft, Button_A, CorrectButton)
elif(50<=location[0]<=900 and 460<=location[1]<=500):
return reveal(Button_B.text == BUTTON_TEXT, playerNum, timeLeft, Button_B, CorrectButton)
elif(50<=location[0]<=900 and 520<=location[1]<=560):
return reveal(Button_C.text == BUTTON_TEXT, playerNum, timeLeft, Button_C, CorrectButton)
elif(50<=location[0]<=900 and 580<=location[1]<=620):
return reveal(Button_D.text == BUTTON_TEXT, playerNum, timeLeft, Button_D, CorrectButton)
elif(375<=location[0]<=475 and 640<=location[1]<=680):
#Skip button.
GrayButton(Button_Skip.text, Button_Skip.left, Button_Skip.top, Button_Skip.width, Button_Skip.height)
GreenButton(CorrectButton.text, CorrectButton.left, CorrectButton.top, CorrectButton.width, CorrectButton.height)
pygame.display.update()
pygame.mixer.stop()
pygame.mixer.quit()
customSleep(1)
return -10
return 0
#This function occurs for a player's turn, AKA their 5 songs in a given round.
def playerTurn(playerNum, roundNum, otherPlayerScore, playerOneWins, playerTwoWins):
screen.fill(WHITE)
hereWeGo = font.render("", 1, BLACK)
if playerNum == 2:
hereWeGo = font.render("Player 1\'s score: "+str(otherPlayerScore), 1, BLACK)
screen.blit(hereWeGo, (300, 350))
getReady = font.render("Player "+str(playerNum)+", Get Ready.", 1, BLACK)
screen.blit(getReady, (300, 300))
pygame.display.update()
customSleep(3)
screen.fill(WHITE)
pygame.display.update()
points = 0
for songNum in range(5):
points += songGuess(playerNum, songNum, roundNum, points, otherPlayerScore, playerOneWins, playerTwoWins)
return points
######################################################
#STUFF THAT ACTUALLY HAPPENS
######################################################
pygame.init()
font = pygame.font.SysFont("trebuchetms", 40)
screen = pygame.display.set_mode((screen_x, screen_y))
# pygame.display.set_caption("What's That Song?", "None") #ignore that second thing
screen.fill(WHITE)
playerOneWins = 0
playerTwoWins = 0
for rounds in range(3):
PlayerOnePoints = playerTurn(1, rounds, -34, playerOneWins, playerTwoWins)
#PLAYER ONE'S POINTS MATTER AT NO POINT DURING THE GAME.
#IF THE NUMBER -34 SHOWS UP ANYWHERE... THATS BAD MMKAY
PlayerTwoPoints = playerTurn(2, rounds, PlayerOnePoints, playerOneWins, playerTwoWins)
if PlayerOnePoints > PlayerTwoPoints:
playerOneWins+=1
screen.fill(WHITE)
screen.blit(font.render("Player One wins the round!", 1, BLACK), (300,300))
pygame.display.update()
customSleep(3)
screen.fill(WHITE)
pygame.display.update()
elif PlayerTwoPoints > PlayerOnePoints:
playerTwoWins+=1
screen.fill(WHITE)
screen.blit(font.render("Player Two wins the round!", 1, BLACK), (300,300))
pygame.display.update()
customSleep(3)
screen.fill(WHITE)
pygame.display.update()
else: #a tie
pass #idk what to do here
if (playerOneWins == 2 and playerTwoWins == 0) or (playerTwoWins == 2 and playerOneWins == 0):
break
#The game has ended. display the results.
screen.fill(WHITE)
WhiteButton("Quit", 360, 360, 100, 40)
if(playerTwoWins == playerOneWins):
final = font.render("It's a tie!", 1, BLACK)
screen.blit(final, (300,300))
else:
WhoWon = "One" if playerOneWins>playerTwoWins else "Two"
final = font.render("Player " + WhoWon +" Wins!", 1, BLACK)
screen.blit(final, (300,300))
pygame.display.update()
while(1):
if pygame.mouse.get_pressed()[0]:
location = pygame.mouse.get_pos()
if(360<=location[0]<=460 and 360<=location[1]<=400):
exit()
if quitCheck():
deleteFiles()
exit()