def main(): online = False rest_client = RESTClient(address="http://localhost", port=8000) colors_data = [] scores_data = [] if rest_client.check_connection(): colors_data = rest_client.get_color_schemes() scores_data = rest_client.get_high_scores() online = True if colors_data: for enum, element in enumerate(colors_data, 1): color_schemes[enum] = { "name": element["name"], "snake_color": (element["snake_R"], element["snake_G"], element["snake_B"]), "apple_color": (element["apple_R"], element["apple_G"], element["apple_B"]), "walls_color": (element["walls_R"], element["walls_G"], element["walls_B"]), "background_color": (element["background_R"], element["background_G"], element["background_B"]), } pg.init() pg.display.set_caption("PySnake") display_resolution = width, height = 800, 600 screen = pg.display.set_mode(display_resolution) clock = pg.time.Clock() fps = 15 block_size = 20 game = Game(display_resolution, screen, fps, clock, block_size, colors, color_schemes, online) menu = Menu(game) menu.main_menu()
def high_scores_menu(self): # Font definitions game_title_font = pg.font.SysFont(None, 100) score_font = pg.font.SysFont(None, 32) back_font = pg.font.SysFont(None, 64) # Rendering text with fonts (args: Text, Antyaliasing, Text Color) game_title_txt = game_title_font.render("PySnake", 1, (0, 255, 0)) back_txt = back_font.render("Back", 1, self.game.black) offline_text = score_font.render( "This option in unavailable in offline mode...", 1, self.game.black) no_scores_text = score_font.render("Nothing to show here...", 1, self.game.black) # Saving text width game_title_width = game_title_txt.get_width() back_width = back_txt.get_width() offline_width = offline_text.get_width() no_scores_width = no_scores_text.get_width() rest_client = RESTClient("http://localhost", 8000) if rest_client.check_connection() and self.game.online: scores = rest_client.get_high_scores() while True: self.game.screen.fill(self.game.white) self.game.screen.blit(game_title_txt, (self.game.display_resolution[0] / 2 - game_title_width / 2, 50)) if rest_client.check_connection() and self.game.online: if scores: position = 0 for enum, score in enumerate(scores, 1): position += 30 score_txt = score_font.render( '{0}. {1} - {2}'.format(enum, score["score"], score["name"]), 1, self.game.black) self.game.screen.blit(score_txt, (100, 150 + position)) elif not scores: self.game.screen.blit( no_scores_text, (self.game.display_resolution[0] / 2 - no_scores_width / 2, self.game.display_resolution[1] / 2)) self.game.screen.blit( back_txt, (self.game.display_resolution[0] / 2 - back_width / 2, self.game.display_resolution[1] - 100)) pg.draw.rect(self.game.screen, (255, 0, 0), (self.game.display_resolution[0] / 2 - back_width / 2 - 10, 490, 125, 60), 2) pg.display.update() else: self.game.screen.blit( offline_text, (self.game.display_resolution[0] / 2 - offline_width / 2, self.game.display_resolution[1] / 2)) self.game.screen.blit( back_txt, (self.game.display_resolution[0] / 2 - back_width / 2, self.game.display_resolution[1] - 100)) pg.draw.rect(self.game.screen, (255, 0, 0), (self.game.display_resolution[0] / 2 - back_width / 2 - 10, 490, 125, 60), 2) pg.display.update() for event in pg.event.get(): if event.type == pg.QUIT: sys.exit() if event.type == pg.K_ESCAPE: pg.quit() sys.exit() if event.type == pg.KEYDOWN: if event.key == pg.K_RETURN: self.main_menu() self.game.clock.tick(self.game.fps) pg.time.wait(0)