-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhighScores.py
More file actions
189 lines (141 loc) · 5.83 KB
/
highScores.py
File metadata and controls
189 lines (141 loc) · 5.83 KB
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
import os
import pygame
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
pygame.init()
COLOR_INACTIVE = (0,0,0)
COLOR_ACTIVE = pygame.Color('lightskyblue3')
HIGHSCORE_FONT = pygame.font.SysFont('microsoftjhengheimicrosoftjhengheiuibold', 60)
INITIALS_FONT = pygame.font.SysFont('microsoftjhengheimicrosoftjhengheiuibold', 30)
#Keep the globals empty/False
displayTopScore=''
done = False
def fireBase():
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "spanglish-matching-high-scores-firebase-adminsdk-yzong-17cc906b50.json"
# Use the application default credentials
cred = credentials.ApplicationDefault()
firebase_admin.initialize_app(cred, {
'projectId': 'spanglish-matching-high-scores'
})
db = firestore.client()
def getListOfScores():
#Get the connection to the firebase cloud
db = firestore.client()
#Get all the elements inside the highscores collection
users_ref = db.collection(u'highscores')
docs = users_ref.stream()
#Store results in a list
return sorted([float(doc.id) for doc in docs])
def getListOfNames():
#Get the connection to the firebase cloud
db = firestore.client()
#Get all the elements inside the highscores collection
users_ref = db.collection(u'highscores')
docs = users_ref.stream()
#Store results in a list
return [doc.to_dict()['name'] for doc in docs]
def addScore(score, screen):
#Refresh the global back to empty, in case it is still active from a previous top score
global displayTopScore
displayTopScore=''
font = pygame.font.SysFont('microsoftjhengheimicrosoftjhengheiuibold', 30)
#Get the connection to the firebase cloud
db = firestore.client()
#Get all the elements inside the highscores collection
users_ref = db.collection(u'highscores')
docs = users_ref.stream()
#Store results in a list
listOfScores = getListOfScores()
#Check if score cracks top 10
if score < max(listOfScores):
listOfScores.append(score)
doc_ref = db.collection(u'highscores').document(u'{}'.format(score))
#Place a variable inside the doc, and assign it to the name provided in the text box
doc_ref.set({
u'name' : u'{}'.format(getInputBox(screen, score))
})
#Sort the list to know which index the new score falls under
listOfScores.sort()
print(listOfScores)
#Update the variable (the index it falls under +1)
displayTopScore = 'Congratulations! Top {}!'.format(listOfScores.index(float(score))+1)
#Delete the max score if there are more than 10 scores
if len(listOfScores) > 10:
db.collection(u'highscores').document(u'{}'.format(str(max(listOfScores)))).delete()
class InputBox:
def __init__(self, x, y, w, h, text=''):
self.rect = pygame.Rect(x, y, w, h)
self.color = COLOR_ACTIVE
self.text = text
self.txt_surface = INITIALS_FONT.render(text, True, (0,0,0))
self.active = False
def handle_event(self, event):
global done
if event.type == pygame.MOUSEBUTTONDOWN:
# If the user clicked on the input_box rect.
if self.rect.collidepoint(event.pos):
# Toggle the active variable.
self.active = not self.active
else:
self.active = False
# Change the current color of the input box.
self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
if event.type == pygame.KEYDOWN:
if self.active:
if event.key == pygame.K_RETURN:
#Stop the loop in getInputBox
done = True
#Return the value for assignment in FireBase
return self.text
elif event.key == pygame.K_BACKSPACE:
#Truncate the string if backspace is pressed
self.text = self.text[:-1]
else:
self.text += event.unicode
# Re-render the text.
self.txt_surface = INITIALS_FONT.render(self.text, True, (0,0,0))
def update(self):
# Resize the box if the text is too long.
width = max(200, self.txt_surface.get_width()+10)
self.rect.w = width
def draw(self, screen):
# Blit the text.
screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+5))
# Blit the rect.
pygame.draw.rect(screen, self.color, self.rect, 2)
#This function is actually it's own screen
def getInputBox(screen, final_time):
clock = pygame.time.Clock()
#Create input box
box = InputBox(300, 300, 140, 50)
#Text to display on screen
highScoreText = HIGHSCORE_FONT.render('High Score!', False, (0, 0, 0))
enterInitialsText = INITIALS_FONT.render('Enter Initials:', False, (0, 0, 0))
#Background image
bg_img = pygame.image.load('otherbackground.jpg')
bg = pygame.transform.scale(bg_img, (800, 600))
#Referance to the global variables
global done
#Always false when initializing the function
done = False
while not done:
#Check if top right 'X' is clicked
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit(0)
#Check for input updates in box
name = box.handle_event(event)
#Draw the text
screen.blit(bg, (0,0))
screen.blit(highScoreText,(210, 10))
screen.blit(enterInitialsText,(300, 230))
screen.blit(INITIALS_FONT.render("Your Time: {:0.3f}".format(final_time), False, (0,0,0)), (270, 500))
#Keep the text inside the box updated
box.update()
#Display the box
box.draw(screen)
pygame.display.flip()
clock.tick(30)
#Return the name for assignment in FireBase
return name