Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Feat/leaderboard #15

Merged
merged 8 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ def __init__(self, screen: pygame.Surface, img_location: str, img_name: str, sou
# get the actual basket ball field.
field = pygame.image.load(img_location + "terrain_basket_public.png")
field = pygame.transform.scale(field, (1024, 640))
field2 = pygame.image.load(img_location +"terrain_basket_public_2.png")
field2 = pygame.image.load(
img_location + "terrain_basket_public_2.png")
field2 = pygame.transform.scale(field2, (1024, 640))
self.field = field
self.field2 = field2
#get the cursor
# get the cursor
cursor_init = pygame.image.load("img/cursor.png").convert_alpha()
self.cursor = cursor_init
# get the actual ball.
Expand All @@ -49,7 +50,8 @@ def __init__(self, screen: pygame.Surface, img_location: str, img_name: str, sou
Element(placeholder_ball, 170, 450))
# listen to events.
self.listen(pygame.MOUSEMOTION, DragEventListener(self))
self.listen(pygame.MOUSEBUTTONUP, BallReleaseEventListener())
self.listen(pygame.MOUSEBUTTONUP, BallReleaseEventListener())

def get_window(self) -> Window:
"""
:return: the game's window.
Expand All @@ -73,17 +75,20 @@ def register_thread(self, thread) -> None:
Register the given thread into a list.
"""
self.__threads.append(thread)

def display_fps(self):
"""Show the program's FPS in the window handle."""
caption = "{} - FPS: {:.2f}".format("ʙᴀsᴋᴇᴛ ɢᴏ !", self.clock.get_fps())
caption = "{} - FPS: {:.2f}".format("ʙᴀsᴋᴇᴛ ɢᴏ !",
self.clock.get_fps())
pygame.display.set_caption(caption)

def setup(self):
"""
Setup the ressources (background image, audio, etc.)
and listen to all the events built in the game.
"""
while True:
x,y = pygame.mouse.get_pos()
x, y = pygame.mouse.get_pos()
x -= self.cursor.get_width()/2-5
y -= self.cursor.get_height()/2-8
self.clock.tick(self.fps)
Expand All @@ -107,8 +112,8 @@ def setup(self):
if event_pair[0] == event.type:
# run the event.
event_pair[1].run(event, self)
#cursor init
self.__screen.blit(self.cursor,(x,y))
# cursor init
self.__screen.blit(self.cursor, (x, y))
pygame.display.update()

def listen(self, event_type: int, event_listener: EventListener) -> None:
Expand All @@ -128,4 +133,3 @@ def get_screen(self):
Return game's screen.
"""
return self.__screen

112 changes: 112 additions & 0 deletions interface/leaderboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# The Leaderboard class
class Leaderboard:
def __init__(self, path: str = None):
"""
The leaderboard class
@param path: The path of the file
"""
self.leaderboard = {}
self.path = path

def add_player(self, player: str, score: int) -> None:
"""
Add a player to the leaderboard
@param player: The player name
@param score: The player score
@return: None
"""
if player in self.leaderboard:
raise ValueError("Player already exists")
elif len(player) > 10:
raise ValueError("Player name too long")
elif len(player) == 0:
raise ValueError("Player name cannot be empty")
else:
self.leaderboard[player] = score
if self.path:
with open(self.path, "a") as file:
file.write(f"{player}:{score}\n")

def add_player_from_txt(self, player: str, score: int) -> None:
"""
Add a player to the leaderboard without writing in the file
@param player: The player name
@param score: The player score
@return: None
"""
if player in self.leaderboard:
raise ValueError("Player already exists")
elif len(player) > 10:
raise ValueError("Player name too long")
elif len(player) == 0:
raise ValueError("Player name cannot be empty")
else:
self.leaderboard[player] = score

def import_player_from_txt(self) -> None:
"""
Import all players from the file
@return: None
"""
if self.path:
with open(self.path, "r") as file:
for line in file:
player, score = line.split(":")
if len(player) > 10:
raise ValueError("Player name too long")
elif len(player) == 0:
raise ValueError("Player name cannot be empty")
else:
self.add_player_from_txt(player, int(score))
else:
raise ValueError("No path specified")

def remove_player(self, player: str) -> None:
"""
Remove a player from the leaderboard
@param player: The player name
@return: None
"""
self.leaderboard.pop(player)
if self.path:
# delete the line in the file
with open(self.path, "r") as file:
lines = file.readlines()
with open(self.path, "w") as file:
for line in lines:
if line.split(":")[0] != player:
file.write(line)

def get_leaderboard(self) -> dict:
"""
Get the leaderboard
@return: The leaderboard
"""
return self.leaderboard

def get_player_score(self, player: str) -> int:
"""
Get the score of a player
@param player: The player name
@return: The player score
"""
return self.leaderboard[player]

def get_player_rank(self, player: str) -> int:
"""
Get the rank of a player
@param player: The player name
@return: The player rank
"""
return sorted(self.leaderboard.values(), reverse=True).index(self.leaderboard[player]) + 1

def get_leaderboard_rank(self) -> list:
"""
Get the leaderboard sorted by rank
@return: The leaderboard sorted by rank
"""

listSorted = sorted(self.leaderboard.items(),
key=lambda x: x[1], reverse=True)
# return only the 5 first entry
return listSorted[:5]
Loading