-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScoreboard.py
executable file
·382 lines (323 loc) · 14.9 KB
/
Scoreboard.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
import os.path
import pygame
import re
from pygame.locals import *
# module to create a highscorelist for a pygame game
# created by MaX-Lo
class Scoreboard:
def __init__(self, filename="highscore.dat"):
self.screen = pygame.display.set_mode((500, 550))
path = os.path.dirname(os.path.realpath(__file__)) + "/data/" + filename
self.filename = path
# creating higscorefile if given file doesn't exist
if not test_file(self.filename):
create_file(self.filename)
self.entry_list = self.load_highscore_list()
# Entries getting shown
self.points = False
self.steps = True
self.level = False
self.time = True
# Sort property
self.sort_by = "TIME_ASC"
# Window properties
self.border = 15
self.border_color = (230, 230, 230)
self.bg_color = (0, 100, 0)
self.table_color = (132, 151, 0)
# Font
self.font_size = 25
self.line_space = 10
self.font_color = (30, 30, 30)
def save_entry(self, entry):
""" creates new Entry and saves it in a file """
if entry.name == "":
entry.name = "Mr. XXX"
self.entry_list.append(entry)
self.sort_list()
if len(self.entry_list) > 10:
self.entry_list.pop(-1)
self.save_in_file()
def sort_list(self):
# sort the highscorelist
# TODO complete options
if self.sort_by == "POINTS_DESC":
self.sort_by_points("DESC")
elif self.sort_by == "POINTS_ASC":
self.sort_by_points("ASC")
elif self.sort_by == "STEPS_DESC":
self.sort_by_steps("DESC")
elif self.sort_by == "STEPS_ASC":
self.sort_by_steps("ASC")
elif self.sort_by == "TIME_DESC":
self.sort_by_time("DESC")
elif self.sort_by == "TIME_ASC":
self.sort_by_time("ASC")
elif self.sort_by == "LEVEL_DESC":
self.sort_by_level("DESC")
elif self.sort_by == "LEVEL_ASC":
self.sort_by_level("ASC")
def save_in_file(self):
raw_content = []
for entry in self.entry_list:
line = entry.name + "-" + str(entry.points) + "-" + str(entry.steps) + "-" + str(entry.level) + "-" + str(entry.time)
raw_content.append(str(line))
save_file(self.filename, raw_content)
def add(self, name="", points=0, steps=0, level=0, time=0.0):
entry = Entry(name, points, steps, level, time)
new_entry_pos = self.get_position(entry)
if new_entry_pos <= 10 <= len(self.entry_list):
self.entry_list.pop(-1)
clock = pygame.time.Clock()
scoreboard_running = True
while scoreboard_running:
clock.tick(30)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
elif event.type == KEYUP:
if event.key == K_RETURN or event.key == K_ESCAPE:
self.save_entry(entry)
scoreboard_running = False
elif event.key == K_DELETE or event.key == 8:
entry.name = entry.name[:-1] # Deletes last char
elif event.key == K_MINUS: # "-" gets replaced by "_"
entry.name += "_"
elif event.key <= 127:
if len(entry.name) < 9: # name may not be longer than 8 character
if pygame.key.get_mods() & pygame.KMOD_SHIFT:
entry.name += chr(event.key).upper()
else:
entry.name += chr(event.key)
# Table
pygame.draw.rect(self.screen, self.bg_color, (0, 0, self.screen.get_width(), self.screen.get_height()))
x = self.screen.get_width()/2-200
y = 20
width = 400
height = (self.font_size+self.line_space)*11
pygame.draw.rect(self.screen, self.border_color, (x-5, y-5, width+10, height+10))
pygame.draw.rect(self.screen, self.table_color, (x, y, width, height))
fontobject = pygame.font.Font(None, self.font_size)
y += 5
x += 5
y_entry = y
# print table head
xn = x
self.screen.blit(fontobject.render("Name", 1, self.font_color), (xn, y_entry))
xn += 130
if self.points:
self.screen.blit(fontobject.render("Points", 1, self.font_color), (xn, y_entry))
xn += 75
if self.steps:
self.screen.blit(fontobject.render("Steps", 1, self.font_color), (xn, y_entry))
xn += 75
if self.level:
self.screen.blit(fontobject.render("Level", 1, self.font_color), (xn, y_entry))
xn += 75
if self.time:
self.screen.blit(fontobject.render("Time", 1, self.font_color), (xn, y_entry))
xn += 75
y_entry += self.font_size + self.line_space
if len(self.entry_list) == 0:
# printing new entry at correct position
xn = x
self.screen.blit(fontobject.render(entry.name + (9-len(entry.name))*"_", 1, self.font_color), (xn, y_entry))
xn += 130
if self.points:
self.screen.blit(fontobject.render("{0:8d}".format(entry.points), 1, self.font_color), (xn, y_entry))
xn += 75
if self.steps:
self.screen.blit(fontobject.render("{0:8d}".format(entry.steps), 1, self.font_color), (xn, y_entry))
xn += 75
if self.level:
self.screen.blit(fontobject.render("{0:3d}".format(entry.level),
1, self.font_color), (xn, y_entry))
xn += 75
if self.time:
self.screen.blit(fontobject.render("{0:5.2f} sec".format(entry.time),
1, self.font_color), (xn, y_entry))
num = 1 # increased everytime by 1 to know when it's time to draw the new entry
# print all existing entries
for element in self.entry_list:
# printing new entry at correct position
if num == new_entry_pos:
xn = x
self.screen.blit(fontobject.render(entry.name + (9-len(entry.name))*"_", 1, self.font_color), (xn, y_entry))
xn += 130
if self.points:
self.screen.blit(fontobject.render("{0:8d}".format(entry.points), 1, self.font_color), (xn, y_entry))
xn += 75
if self.steps:
self.screen.blit(fontobject.render("{0:8d}".format(entry.steps), 1, self.font_color), (xn, y_entry))
xn += 75
if self.level:
self.screen.blit(fontobject.render("{0:3d}".format(entry.level),
1, self.font_color), (xn, y_entry))
xn += 75
if self.time:
self.screen.blit(fontobject.render("{0:5.2f} sec".format(entry.time),
1, self.font_color), (xn, y_entry))
xn += 75
y_entry += self.font_size + self.line_space
num += 1
# printing already existing entries
xn = x
self.screen.blit(fontobject.render(element.name, 1, self.font_color), (xn, y_entry))
xn += 130
if self.points:
self.screen.blit(fontobject.render("{0:8d}".format(element.points), 1, self.font_color), (xn, y_entry))
xn += 75
if self.steps:
self.screen.blit(fontobject.render("{0:8d}".format(element.steps), 1, self.font_color), (xn, y_entry))
xn += 75
if self.level:
self.screen.blit(fontobject.render("{0:3d}".format(element.level),
1, self.font_color), (xn, y_entry))
xn += 75
if self.time:
self.screen.blit(fontobject.render("{0:5.2f} sec".format(element.time),
1, self.font_color), (xn, y_entry))
xn += 75
y_entry += self.font_size + self.line_space
pygame.display.flip()
def get_position(self, entry):
"""
calculates the position of the new entry in the highscorelist
"""
self.sort_list()
pos = 1
if self.sort_by == "TIME_ASC":
for element in self.entry_list:
if entry.time >= element.time:
pos += 1
else:
return pos
elif self.sort_by == "TIME_DESC":
for element in self.entry_list:
if entry.time <= element.time:
pos += 1
else:
return pos
elif self.sort_by == "POINTS_ASC":
for element in self.entry_list:
if entry.points >= element.points:
pos += 1
else:
return pos
elif self.sort_by == "POINTS_DESC":
for element in self.entry_list:
if entry.points <= element.points:
pos += 1
else:
return pos
elif self.sort_by == "STEPS_ASC":
for element in self.entry_list:
if entry.steps >= element.steps:
pos += 1
else:
return pos
elif self.sort_by == "STEPS_DESC":
for element in self.entry_list:
if entry.steps <= element.steps:
pos += 1
else:
return pos
elif self.sort_by == "LEVEL_ASC":
for element in self.entry_list:
if entry.level >= element.level:
pos += 1
else:
return pos
elif self.sort_by == "LEVEL_DESC":
for element in self.entry_list:
if entry.level <= element.level:
pos += 1
else:
return pos
return pos
def sort_by_points(self, direction):
""" sort the list by points descending """
for num in range(len(self.entry_list)-1, 0, -1): # goes from last to first element
for i in range(num):
if direction == "ASC":
if self.entry_list[i].points > self.entry_list[i+1].points:
self.entry_list[i], self.entry_list[i + 1] = self.entry_list[i + 1], self.entry_list[i] # exchanges the elements
elif direction == "DESC":
if self.entry_list[i].points < self.entry_list[i+1].points:
self.entry_list[i], self.entry_list[i + 1] = self.entry_list[i + 1], self.entry_list[i] # exchanges the elements
def sort_by_steps(self, direction):
""" sort the list by steps ascending """
for num in range(len(self.entry_list)-1, 0, -1): # goes from last to first element
for i in range(num):
if direction == "ASC":
if self.entry_list[i].steps > self.entry_list[i+1].steps:
self.entry_list[i], self.entry_list[i + 1] = self.entry_list[i + 1], self.entry_list[i] # exchanges the elements
elif direction == "DESC":
if self.entry_list[i].steps < self.entry_list[i+1].steps:
self.entry_list[i], self.entry_list[i + 1] = self.entry_list[i + 1], self.entry_list[i] # exchanges the elements
def sort_by_time(self, direction):
for num in range(len(self.entry_list)-1, 0, -1): # goes from last to first element
for i in range(num):
if direction == "ASC":
if self.entry_list[i].time > self.entry_list[i+1].time:
self.entry_list[i], self.entry_list[i + 1] = self.entry_list[i + 1], self.entry_list[i]
elif direction == "DESC":
if self.entry_list[i].time < self.entry_list[i+1].time:
self.entry_list[i], self.entry_list[i + 1] = self.entry_list[i + 1], self.entry_list[i]
def sort_by_level(self, direction):
for num in range(len(self.entry_list)-1, 0, -1): # goes from last to first element
for i in range(num):
if direction == "ASC":
if self.entry_list[i].level > self.entry_list[i+1].level:
self.entry_list[i], self.entry_list[i + 1] = self.entry_list[i + 1], self.entry_list[i]
elif direction == "DESC":
if self.entry_list[i].level < self.entry_list[i+1].level:
self.entry_list[i], self.entry_list[i + 1] = self.entry_list[i + 1], self.entry_list[i]
def load_highscore_list(self):
""" formats the higscore file content
into a List with single entries """
raw_list = load_file(self.filename)
formated_list = []
for line in raw_list:
entry = Entry()
pos = [c.start() for c in re.finditer('-', line)] # getting the divider positions
entry.name = line[:pos[0]]
entry.points = int(line[pos[0]+1:pos[1]])
entry.steps = int(line[pos[1]+1:pos[2]])
entry.level = int(line[pos[2]+1:pos[3]])
entry.time = float(line[pos[3]+1:])
formated_list.append(entry)
return formated_list
def set_border_color(self, c):
self.border_color = c
def set_bg_color(self, c):
self.bg_color = c
def set_table_color(self, c):
self.table_color = c
class Entry:
def __init__(self, name="The Fox", points=0, steps=0, level=0, time=0.0):
self.name = name
self.points = points
self.steps = steps
self.level = level
self.time = time
def save_file(name, content):
fobj = open(name, "w")
for i in range(len(content)):
fobj.write(content[i]+"\n")
fobj.close()
def load_file(name):
content = []
print(name)
fobj = open(name, "r")
for line in fobj:
content.append(line.rstrip()) # rstrip entfernt Leerzeichen und Newlines am rechten Rand
fobj.close()
return content
def test_file(name):
""" checks whether file exists """
name = os.path.abspath(name)
return os.path.isfile(name)
def create_file(filename):
fobj = open(filename, 'w+')
fobj.close()