-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaderboard.py
80 lines (70 loc) · 3.29 KB
/
leaderboard.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
import shelve
from resources import Resources
import pygame
from interface import Interface
import sys
class LeaderBoard:
def __init__(self):
self.resources = Resources()
self.width = self.resources.WIDTH
self.height = self.resources.HEIGHT
self.filename = self.resources.leaderboard_filename
self.running = False
self.clock = pygame.time.Clock()
self.mx, self.my = pygame.mouse.get_pos()
self.buttons = [Interface.Button(self.resources.screen, self.width / 2 - 100,
self.height-200,
200, 50, "Main menu", self.resources.font_name, self.exit)]
def save_score(self, name, score):
with shelve.open(self.filename) as lb:
if not name in lb.keys():
lb[name] = (score,)
else:
lb[name] += (score,)
def get_scores(self, name):
with shelve.open(self.filename) as lb:
return lb[name]
def _process_events(self):
self.click = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
self.click = True
def exit(self):
self.running = False
def run(self):
self.running = True
lb = self.get_leaderboard()
self.resources.screen.fill((0, 0, 0))
while self.running:
self.mx, self.my = pygame.mouse.get_pos()
self._process_events()
for button in self.buttons:
button.handle(self.mx, self.my, self.click)
Interface.draw_text_centered(self.resources.screen, "Name", 32, self.width * 0.2, self.height * 0.1,
self.resources.font_name, (255, 255, 255))
Interface.draw_text_centered(self.resources.screen, "Score", 32, self.width * 0.6, self.height * 0.1,
self.resources.font_name, (255, 255, 255))
for i in range(5):
if lb == [] or i > len(lb)-1:
self.draw_draw_line(i, '', '')
continue
self.draw_draw_line(i, lb[i][0], lb[i][1])
pygame.display.update()
self.clock.tick(60)
def draw_draw_line(self, number, name, score):
line = pygame.Rect(self.width * 0.15, self.height*0.15 + self.height*0.1*number,
self.width * 0.7, 50)
pygame.draw.rect(self.resources.screen, (255, 255, 255), line)
Interface.draw_text(self.resources.screen, name, 32, self.width * 0.16, self.height * 0.15 + self.height * 0.1 * number, self.resources.font_name, (0, 0, 0))
Interface.draw_text(self.resources.screen, str(score), 32, self.width * 0.6, self.height * 0.15 + self.height * 0.1 * number, self.resources.font_name, (0, 0, 0))
def get_leaderboard(self):
leaderboard = []
with shelve.open(self.filename) as lb:
for name in lb.keys():
for score in lb[name]:
leaderboard.append((name, score))
leaderboard.sort(key=lambda x: (-x[1], x[0]))
return leaderboard