From cef3174727286b66fdc9a270fd56ad48aee032d8 Mon Sep 17 00:00:00 2001 From: Louis Gallet Date: Sun, 18 Dec 2022 20:45:35 +0100 Subject: [PATCH 1/8] feat: starting display a fake leaderboard Create a fake leaderboard in a dict variable and show in the main window of PyGame. --- main.py | 182 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 102 insertions(+), 80 deletions(-) diff --git a/main.py b/main.py index b546ad4..dcac1ed 100644 --- a/main.py +++ b/main.py @@ -13,69 +13,75 @@ # set game name. pygame.display.set_caption("ʙᴀsᴋᴇᴛ ɢᴏ !") # define window's size. -window = pygame.display.set_mode((1024,640)) +window = pygame.display.set_mode((1024, 640)) fond = pygame.image.load('./img/main_menu/background/bg.png') -pygame.mouse.set_pos((fond.get_width()/2,fond.get_height()/2)) +pygame.mouse.set_pos((fond.get_width()/2, fond.get_height()/2)) -#load button images +# Define the police +font = pygame.font.SysFont("Arial", 24) + +# load button images start_img = pygame.image.load('./img/main_menu/buttons/start_btn.png') exit_img = pygame.image.load('./img/main_menu/buttons/exit_btn.png') start_img_hov = pygame.image.load('./img/main_menu/buttons/start_btn_hov.png') exit_img_hov = pygame.image.load('./img/main_menu/buttons/exit_btn_hov.png') Leader_img = pygame.image.load('./img/main_menu/buttons/Leaderboard_btn.png') -Leader_img_hov = pygame.image.load('./img/main_menu/buttons/Leaderboard_btn_hov.png') +Leader_img_hov = pygame.image.load( + './img/main_menu/buttons/Leaderboard_btn_hov.png') shop_img = pygame.image.load('./img/main_menu/buttons/shop_btn.png') shop_img_hov = pygame.image.load('./img/main_menu/buttons/shop_btn_hov.png') sound_img = pygame.image.load('./img/option_menu/buttons/sound_btn.png') -sound_img_hov = pygame.image.load('./img/option_menu/buttons/sound_btn_hov.png') +sound_img_hov = pygame.image.load( + './img/option_menu/buttons/sound_btn_hov.png') gear_img = pygame.image.load('./img/main_menu/buttons/gear_btn.png') gear_img_hov = pygame.image.load('./img/main_menu/buttons/gear_btn_hov.png') back_img = pygame.image.load('./img/option_menu/buttons/back_btn.png') back_img_hov = pygame.image.load('./img/option_menu/buttons/back_btn_hov.png') save_img = pygame.image.load('./img/option_menu/buttons/save_btn.png') save_img_hov = pygame.image.load('./img/option_menu/buttons/save_btn_hov.png') -#create button instances +# create button instances start_button = button.Button(397, 390, start_img, start_img_hov, 1) exit_button = button.Button(412, 500, exit_img, exit_img_hov, 1) -leader_btn = button.Button(920,540, Leader_img, Leader_img_hov,1) -shop_btn = button.Button(840,550, shop_img, shop_img_hov,1) -sound_btn = button.Button(470,500, sound_img, sound_img_hov,1) -gear_btn = button.Button(17,550, gear_img, gear_img_hov,1) -back_btn = button.Button (612,500, back_img, back_img_hov,1) -save_btn = button.Button (212,500, save_img, save_img_hov,1) +leader_btn = button.Button(920, 540, Leader_img, Leader_img_hov, 1) +shop_btn = button.Button(840, 550, shop_img, shop_img_hov, 1) +sound_btn = button.Button(470, 500, sound_img, sound_img_hov, 1) +gear_btn = button.Button(17, 550, gear_img, gear_img_hov, 1) +back_btn = button.Button(612, 500, back_img, back_img_hov, 1) +save_btn = button.Button(212, 500, save_img, save_img_hov, 1) clock = pygame.time.Clock() -#game variables +# game variables menu_state = "main" is_running = True press = False press2 = False i = 0 -#set cursor +# set cursor cursor = "img/cursor.png" cursor_init = pygame.image.load(cursor).convert_alpha() pygame.mouse.set_visible(False) while is_running: MOUSE_POS = pygame.mouse.get_pos() - x,y = pygame.mouse.get_pos() + x, y = pygame.mouse.get_pos() x -= cursor_init.get_width()/2-5 y -= cursor_init.get_height()/2-8 - #blit background - window.blit(fond,(0,0)) - #check if game is paused + # blit background + window.blit(fond, (0, 0)) + # check if game is paused if menu_state == "main": - if leader_btn.draw_and_clicked(window) : + if leader_btn.draw_and_clicked(window): menu_state = "options" Leader = True - if shop_btn.draw_and_clicked(window) : + if shop_btn.draw_and_clicked(window): pass - #if start0_button was clicked or selected and press enter - if start_button.draw_and_clicked(window) or press == True : - image = ["flame","mountains","pink","basket-ball","smile","military"] - i = random.randint(0,5) + # if start0_button was clicked or selected and press enter + if start_button.draw_and_clicked(window) or press == True: + image = ["flame", "mountains", "pink", + "basket-ball", "smile", "military"] + i = random.randint(0, 5) # create a new instance of the Game. - game = Game(window, "img/basket-ball/",image[i], None) + game = Game(window, "img/basket-ball/", image[i], None) # register dummy player. game.register_player("Yanis") # setup the game. @@ -83,7 +89,7 @@ if exit_button.draw_and_clicked(window) or press2 == True: pygame.quit() sys.exit() - if gear_btn.draw_and_clicked(window) : + if gear_btn.draw_and_clicked(window): menu_state = "options" Leader = False for event in pygame.event.get(): @@ -92,48 +98,48 @@ sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: - if start_button.rect.collidepoint(MOUSE_POS) : + if start_button.rect.collidepoint(MOUSE_POS): press = True - else : + else: press = False - if exit_button.rect.collidepoint(MOUSE_POS) : + if exit_button.rect.collidepoint(MOUSE_POS): press2 = True - else : + else: press2 = False - #button path - if event.scancode == 82 : - if MOUSE_POS != (513,438) : - pygame.mouse.set_pos((513,438)) - else : - pygame.mouse.set_pos((513,547)) - if event.scancode == 81 : - if MOUSE_POS != (513,547) : - pygame.mouse.set_pos((513,547)) - else : - pygame.mouse.set_pos((513,438)) - if event.scancode == 80 : - if MOUSE_POS != (58,593) : - pygame.mouse.set_pos((58,593)) - if MOUSE_POS == (58,593) : - pygame.mouse.set_pos((971,590)) - if MOUSE_POS == (971,590) : - pygame.mouse.set_pos((887,589)) - if event.scancode == 79 : - if MOUSE_POS != (971,590) : - pygame.mouse.set_pos((971,590)) - if MOUSE_POS == (971,590) : - pygame.mouse.set_pos((58,593)) - if MOUSE_POS == (58,593) : - pygame.mouse.set_pos((887,598)) - #check if the options menu is open + # button path + if event.scancode == 82: + if MOUSE_POS != (513, 438): + pygame.mouse.set_pos((513, 438)) + else: + pygame.mouse.set_pos((513, 547)) + if event.scancode == 81: + if MOUSE_POS != (513, 547): + pygame.mouse.set_pos((513, 547)) + else: + pygame.mouse.set_pos((513, 438)) + if event.scancode == 80: + if MOUSE_POS != (58, 593): + pygame.mouse.set_pos((58, 593)) + if MOUSE_POS == (58, 593): + pygame.mouse.set_pos((971, 590)) + if MOUSE_POS == (971, 590): + pygame.mouse.set_pos((887, 589)) + if event.scancode == 79: + if MOUSE_POS != (971, 590): + pygame.mouse.set_pos((971, 590)) + if MOUSE_POS == (971, 590): + pygame.mouse.set_pos((58, 593)) + if MOUSE_POS == (58, 593): + pygame.mouse.set_pos((887, 598)) + # check if the options menu is open if menu_state == "options": fond = pygame.image.load('./img/option_menu/background/bg.png') - #draw the different options buttons + # draw the different options buttons if back_btn.draw_and_clicked(window) or press == True: menu_state = "main" press = False fond = pygame.image.load('./img/main_menu/background/bg.png') - if save_btn.draw_and_clicked(window) or press2 == True : + if save_btn.draw_and_clicked(window) or press2 == True: menu_state = "main" press2 = False fond = pygame.image.load('./img/option_menu/background/bg.png') @@ -143,32 +149,48 @@ sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: - if back_btn.rect.collidepoint(MOUSE_POS) : + if back_btn.rect.collidepoint(MOUSE_POS): press = True - else : + else: press = False - if save_btn.rect.collidepoint(MOUSE_POS) : + if save_btn.rect.collidepoint(MOUSE_POS): press2 = True - else : + else: press2 = False - #button path - if event.scancode == 80 : - if MOUSE_POS != (320,550) : - pygame.mouse.set_pos((320,550)) - else : - pygame.mouse.set_pos((712,550)) - if event.scancode == 79 : - if MOUSE_POS != (712,550) : - pygame.mouse.set_pos((712,550)) - else : - pygame.mouse.set_pos((320,550)) - if Leader == False : - if sound_btn.draw_and_clicked(window) : + # button path + if event.scancode == 80: + if MOUSE_POS != (320, 550): + pygame.mouse.set_pos((320, 550)) + else: + pygame.mouse.set_pos((712, 550)) + if event.scancode == 79: + if MOUSE_POS != (712, 550): + pygame.mouse.set_pos((712, 550)) + else: + pygame.mouse.set_pos((320, 550)) + if Leader == False: + if sound_btn.draw_and_clicked(window): pass - else : - pass - - window.blit(cursor_init,(x,y)) + else: + # Create fake leaderboard data with player and score + leaderboard_data = {"yanis": 500, + "joe": 400, "bob": 300, "bubu": 200} + textLeader = font.render("Leaderboard", True, (255, 255, 255)) + textPlayer = font.render("Player", True, (255, 255, 255)) + textScore = font.render("Score", True, (255, 255, 255)) + + # Display all the player line by line + for i, player in enumerate(leaderboard_data): + text = font.render(player, True, (255, 255, 255)) + window.blit(text, (470, 200 + i * 50)) + text = font.render( + str(leaderboard_data[player]), True, (255, 255, 255)) + window.blit(text, (600, 200 + i * 50)) + + window.blit(textPlayer, (470, 150)) + window.blit(textScore, (600, 150)) + window.blit(textLeader, (470, 100)) + + window.blit(cursor_init, (x, y)) pygame.display.update() clock.tick(60) - \ No newline at end of file From 828ad8fb2b4321d5e37dea75edfda5157cf0226b Mon Sep 17 00:00:00 2001 From: Louis Gallet Date: Sun, 18 Dec 2022 21:27:27 +0100 Subject: [PATCH 2/8] feat: starting implementation with POO Create the leaderboard class, import it in the main, create the leaderboard.txt with sample value --- interface/leaderboard.py | 39 +++++++++++++++++++++++++++++++++++++++ main.py | 32 +++++++++++++++++++++----------- utils/leaderboard.txt | 1 + 3 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 interface/leaderboard.py create mode 100644 utils/leaderboard.txt diff --git a/interface/leaderboard.py b/interface/leaderboard.py new file mode 100644 index 0000000..d7d14e2 --- /dev/null +++ b/interface/leaderboard.py @@ -0,0 +1,39 @@ +# The Leaderboard class +class Leaderboard: + def __init__(self, path: str = None): + self.leaderboard = {} + self.path = path + + def add_player(self, player: str, score: int): + self.leaderboard[player] = score + if self.path: + with open(self.path, "a") as file: + file.write(f"{player}:{score}\n") + + def get_leaderboard(self): + return self.leaderboard + + def get_player_score(self, player): + return self.leaderboard[player] + + def get_player_rank(self, player): + return sorted(self.leaderboard.values(), reverse=True).index(self.leaderboard[player]) + 1 + + def get_leaderboard_rank(self): + return sorted(self.leaderboard.items(), key=lambda x: x[1], reverse=True) + + def import_player_from_txt(self): + if self.path: + with open(self.path, "r") as file: + for line in file: + player, score = line.split(":") + self.add_player(player, int(score)) + else: + raise ValueError("No path specified") + + def remove_player(self, player): + self.leaderboard.pop(player) + if self.path: + with open(self.path, "w") as file: + for player in self.leaderboard: + file.write(f"{player}:{self.leaderboard[player]}\n") diff --git a/main.py b/main.py index dcac1ed..f1b9728 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,10 @@ import sys from game import Game +from interface.leaderboard import Leaderboard +leaderboard = Leaderboard("./utils/leaderboard.txt") + + # init pygame. pygame.init() # load game icon. @@ -61,6 +65,10 @@ cursor_init = pygame.image.load(cursor).convert_alpha() pygame.mouse.set_visible(False) +# set leaderboard +leaderboard.import_player_from_txt() +leaderboard.add_player("Yanis", 100) + while is_running: MOUSE_POS = pygame.mouse.get_pos() x, y = pygame.mouse.get_pos() @@ -172,20 +180,22 @@ if sound_btn.draw_and_clicked(window): pass else: - # Create fake leaderboard data with player and score - leaderboard_data = {"yanis": 500, - "joe": 400, "bob": 300, "bubu": 200} + # get all player by score in a dict + leaderboard_data = leaderboard.get_leaderboard() textLeader = font.render("Leaderboard", True, (255, 255, 255)) textPlayer = font.render("Player", True, (255, 255, 255)) textScore = font.render("Score", True, (255, 255, 255)) - - # Display all the player line by line - for i, player in enumerate(leaderboard_data): - text = font.render(player, True, (255, 255, 255)) - window.blit(text, (470, 200 + i * 50)) - text = font.render( - str(leaderboard_data[player]), True, (255, 255, 255)) - window.blit(text, (600, 200 + i * 50)) + # Sort the dict by score + leaderboard_data = dict( + sorted(leaderboard_data.items(), key=lambda item: item[1], reverse=True)) + # print the leaderboard + i = 0 + for player, score in leaderboard_data.items(): + textPlayer = font.render(player, True, (255, 255, 255)) + textScore = font.render(str(score), True, (255, 255, 255)) + window.blit(textPlayer, (470, 200+i)) + window.blit(textScore, (600, 200+i)) + i += 50 window.blit(textPlayer, (470, 150)) window.blit(textScore, (600, 150)) diff --git a/utils/leaderboard.txt b/utils/leaderboard.txt new file mode 100644 index 0000000..b444a1d --- /dev/null +++ b/utils/leaderboard.txt @@ -0,0 +1 @@ +Yanis:100 From 110a7aa782aaaceddc0c2eb1be816916a844d072 Mon Sep 17 00:00:00 2001 From: Louis Gallet Date: Sun, 18 Dec 2022 21:27:44 +0100 Subject: [PATCH 3/8] feat: apply PEP8 formater to game.py --- game.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/game.py b/game.py index 5637560..8700f46 100644 --- a/game.py +++ b/game.py @@ -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. @@ -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. @@ -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) @@ -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: @@ -128,4 +133,3 @@ def get_screen(self): Return game's screen. """ return self.__screen - \ No newline at end of file From 1ea075a59c59a6f44de64d12e9ac5f6be85a5843 Mon Sep 17 00:00:00 2001 From: Louis Gallet Date: Sun, 18 Dec 2022 21:40:00 +0100 Subject: [PATCH 4/8] feat: format the leaderboard file and add specs --- interface/leaderboard.py | 99 +++++++++++++++++++++++++++++++--------- main.py | 1 - utils/leaderboard.txt | 5 +- 3 files changed, 82 insertions(+), 23 deletions(-) diff --git a/interface/leaderboard.py b/interface/leaderboard.py index d7d14e2..fb4fe31 100644 --- a/interface/leaderboard.py +++ b/interface/leaderboard.py @@ -1,39 +1,96 @@ # 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): - self.leaderboard[player] = score - if self.path: - with open(self.path, "a") as file: - file.write(f"{player}:{score}\n") - - def get_leaderboard(self): - return self.leaderboard - - def get_player_score(self, player): - return self.leaderboard[player] - - def get_player_rank(self, player): - return sorted(self.leaderboard.values(), reverse=True).index(self.leaderboard[player]) + 1 + 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") + else: + self.leaderboard[player] = score + if self.path: + with open(self.path, "a") as file: + file.write(f"{player}:{score}\n") - def get_leaderboard_rank(self): - return sorted(self.leaderboard.items(), key=lambda x: x[1], reverse=True) + 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") + else: + self.leaderboard[player] = score - def import_player_from_txt(self): + 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(":") - self.add_player(player, int(score)) + self.add_player_from_txt(player, int(score)) else: raise ValueError("No path specified") - def remove_player(self, player): + 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 player in self.leaderboard: - file.write(f"{player}:{self.leaderboard[player]}\n") + 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 + """ + + return sorted(self.leaderboard.items(), key=lambda x: x[1], reverse=True) diff --git a/main.py b/main.py index f1b9728..3467a2b 100644 --- a/main.py +++ b/main.py @@ -67,7 +67,6 @@ # set leaderboard leaderboard.import_player_from_txt() -leaderboard.add_player("Yanis", 100) while is_running: MOUSE_POS = pygame.mouse.get_pos() diff --git a/utils/leaderboard.txt b/utils/leaderboard.txt index b444a1d..9b9f1b8 100644 --- a/utils/leaderboard.txt +++ b/utils/leaderboard.txt @@ -1 +1,4 @@ -Yanis:100 +Louis:5000 +Yanis:4000 +Damien:3000 +Bubu:400 \ No newline at end of file From 7b243d0c055c77d80d64027bbff1f27f8951ada1 Mon Sep 17 00:00:00 2001 From: Louis Gallet Date: Sun, 18 Dec 2022 21:46:35 +0100 Subject: [PATCH 5/8] feat: add a condition to show 5 entry leaderboard --- main.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index 3467a2b..f5aa5e4 100644 --- a/main.py +++ b/main.py @@ -189,12 +189,17 @@ sorted(leaderboard_data.items(), key=lambda item: item[1], reverse=True)) # print the leaderboard i = 0 + g = 0 for player, score in leaderboard_data.items(): - textPlayer = font.render(player, True, (255, 255, 255)) - textScore = font.render(str(score), True, (255, 255, 255)) - window.blit(textPlayer, (470, 200+i)) - window.blit(textScore, (600, 200+i)) - i += 50 + if g == 4: + break + else: + textPlayer = font.render(player, True, (255, 255, 255)) + textScore = font.render(str(score), True, (255, 255, 255)) + window.blit(textPlayer, (470, 200+i)) + window.blit(textScore, (600, 200+i)) + i += 50 + g += 1 window.blit(textPlayer, (470, 150)) window.blit(textScore, (600, 150)) From 82d484ea865497021e4d425d5ae7ba128536d031 Mon Sep 17 00:00:00 2001 From: Louis Gallet Date: Mon, 19 Dec 2022 18:58:55 +0100 Subject: [PATCH 6/8] feat: add conditions and change methods Check if the username is too long and return a list of only the 5 first player --- interface/leaderboard.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/interface/leaderboard.py b/interface/leaderboard.py index fb4fe31..7ac6190 100644 --- a/interface/leaderboard.py +++ b/interface/leaderboard.py @@ -17,6 +17,8 @@ def add_player(self, player: str, score: int) -> None: """ if player in self.leaderboard: raise ValueError("Player already exists") + elif len(player) > 10: + raise ValueError("Player name too long") else: self.leaderboard[player] = score if self.path: @@ -32,6 +34,8 @@ def add_player_from_txt(self, player: str, score: int) -> None: """ if player in self.leaderboard: raise ValueError("Player already exists") + elif len(player) > 10: + raise ValueError("Player name too long") else: self.leaderboard[player] = score @@ -44,7 +48,10 @@ def import_player_from_txt(self) -> None: with open(self.path, "r") as file: for line in file: player, score = line.split(":") - self.add_player_from_txt(player, int(score)) + if len(player) > 10: + raise ValueError("Player name too long") + else: + self.add_player_from_txt(player, int(score)) else: raise ValueError("No path specified") @@ -93,4 +100,7 @@ def get_leaderboard_rank(self) -> list: @return: The leaderboard sorted by rank """ - return sorted(self.leaderboard.items(), key=lambda x: x[1], reverse=True) + listSorted = sorted(self.leaderboard.items(), + key=lambda x: x[1], reverse=True) + # return only the 5 first entry + return listSorted[:5] From a3f8c5ee7c9fc05057745e076800112792026171 Mon Sep 17 00:00:00 2001 From: Louis Gallet Date: Mon, 19 Dec 2022 19:00:05 +0100 Subject: [PATCH 7/8] feat: change leaderboard data variable Fetch the leaderboard sorted with the Leaderboard class --- main.py | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/main.py b/main.py index f5aa5e4..a10e28f 100644 --- a/main.py +++ b/main.py @@ -67,6 +67,8 @@ # set leaderboard leaderboard.import_player_from_txt() +leaderboard_data = leaderboard.get_leaderboard_rank() + while is_running: MOUSE_POS = pygame.mouse.get_pos() @@ -180,26 +182,17 @@ pass else: # get all player by score in a dict - leaderboard_data = leaderboard.get_leaderboard() textLeader = font.render("Leaderboard", True, (255, 255, 255)) textPlayer = font.render("Player", True, (255, 255, 255)) textScore = font.render("Score", True, (255, 255, 255)) - # Sort the dict by score - leaderboard_data = dict( - sorted(leaderboard_data.items(), key=lambda item: item[1], reverse=True)) - # print the leaderboard - i = 0 - g = 0 - for player, score in leaderboard_data.items(): - if g == 4: - break - else: - textPlayer = font.render(player, True, (255, 255, 255)) - textScore = font.render(str(score), True, (255, 255, 255)) - window.blit(textPlayer, (470, 200+i)) - window.blit(textScore, (600, 200+i)) - i += 50 - g += 1 + # Print the leaderboard from leaderboard_data + for i in range(0, len(leaderboard_data)): + text = font.render( + leaderboard_data[i][0], True, (255, 255, 255)) + window.blit(text, (470, 200+i*50)) + text = font.render( + str(leaderboard_data[i][1]), True, (255, 255, 255)) + window.blit(text, (600, 200+i*50)) window.blit(textPlayer, (470, 150)) window.blit(textScore, (600, 150)) From 42f4ccd37dedca7c8bbe3b752d69585c5741b0d0 Mon Sep 17 00:00:00 2001 From: Louis Gallet Date: Wed, 21 Dec 2022 17:59:56 +0100 Subject: [PATCH 8/8] =?UTF-8?q?feat:=20=E2=9C=A8=20Add=20condition=20to=20?= =?UTF-8?q?check=20empty=20username?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a condition to check if a username in the database is empty and raise an error if it is --- interface/leaderboard.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/interface/leaderboard.py b/interface/leaderboard.py index 7ac6190..03260c5 100644 --- a/interface/leaderboard.py +++ b/interface/leaderboard.py @@ -19,6 +19,8 @@ def add_player(self, player: str, score: int) -> None: 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: @@ -36,6 +38,8 @@ def add_player_from_txt(self, player: str, score: int) -> None: 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 @@ -50,6 +54,8 @@ def import_player_from_txt(self) -> None: 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: