def start_match(self, wait): """Create some wait time before the actual drawing of the game.""" for i in range(wait): self.window.fill(pygame.Color(225, 225, 225)) time = ' {:d} '.format(wait - i) # Game starts in 3, 2, 1 text = [ TextBlock(text=' Game starts in ', pos=(self.screen_rect.centerx, 4 * self.screen_rect.centery / 10), canvas_size=VAR.canvas_size, font_path=self.font_path, window=self.window, scale=(1 / 12), block_type="text"), TextBlock(text=time, pos=(self.screen_rect.centerx, 12 * self.screen_rect.centery / 10), canvas_size=VAR.canvas_size, font_path=self.font_path, window=self.window, scale=(1 / 1.5), block_type="text") ] for text_block in text: text_block.draw() pygame.display.update() pygame.display.set_caption("SNAKE GAME | Game starts in " + time + " second(s) ...") pygame.time.wait(1000) LOGGER.info('EVENT: GAME START')
def menu(self): """Main menu of the game. Return ---------- selected_option: int The selected option in the main loop. """ pygame.display.set_caption("snake-on-pygme | PLAY NOW!") img = pygame.image.load(self.logo_path).convert() img = pygame.transform.scale(img, (VAR.canvas_size, int(VAR.canvas_size / 3))) img_rect = img.get_rect() img_rect.center = self.screen_rect.center list_menu = ["PLAY", "BENCHMARK", "LEADERBOARDS", "QUIT"] menu_options = [ TextBlock( text=" PLAY GAME ", pos=(self.screen_rect.centerx, 4 * self.screen_rect.centery / 10), canvas_size=VAR.canvas_size, font_path=self.font_path, window=self.window, scale=(1 / 12), block_type="menu", ), TextBlock( text=" BENCHMARK ", pos=(self.screen_rect.centerx, 6 * self.screen_rect.centery / 10), canvas_size=VAR.canvas_size, font_path=self.font_path, window=self.window, scale=(1 / 12), block_type="menu", ), TextBlock( text=" LEADERBOARDS ", pos=(self.screen_rect.centerx, 8 * self.screen_rect.centery / 10), canvas_size=VAR.canvas_size, font_path=self.font_path, window=self.window, scale=(1 / 12), block_type="menu", ), TextBlock( text=" QUIT ", pos=(self.screen_rect.centerx, 10 * self.screen_rect.centery / 10), canvas_size=VAR.canvas_size, font_path=self.font_path, window=self.window, scale=(1 / 12), block_type="menu", ), ] selected_option = self.cycle_menu( menu_options, list_menu, OPTIONS, img, img_rect ) return selected_option
def get_name(self): """See test.py in my desktop, for a textinput_box input in pygame. Return ---------- text: string Text received from handle_input. """ done = False input_box = InputBox( x=200, y=300, w=140, h=32, window=self.window, font_path=self.resource_path( "resources/fonts/product_sans_bold.ttf"), ) text_block = TextBlock( text=" YOUR NAME ", pos=(self.screen_rect.centerx, 0.9 * self.screen_rect.centery), canvas_size=VAR.canvas_size, font_path=self.font_path, window=self.window, scale=(1 / 24), block_type="text", ) while not done: pygame.event.pump() events = pygame.event.get() for event in events: if event.type == pygame.QUIT: done = True text = input_box.handle_event(event) if text is not None: done = True input_box.update() self.window.fill(VAR.bg_color) input_box.draw() text_block.draw() pygame.display.update() return text
def select_speed(self): """Speed menu, right before calling start_match. Return ---------- speed: int The selected speed in the main loop. """ list_menu = ["EASY", "MEDIUM", "HARD", "MEGA_HARDCORE"] menu_options = [ TextBlock( text=LEVELS[i], pos=( self.screen_rect.centerx, 4 * (i + 1) * self.screen_rect.centery / 10, ), canvas_size=VAR.canvas_size, font_path=self.font_path, window=self.window, scale=(1 / 10), block_type="menu", ) for i in range(len(list_menu)) ] speed = self.cycle_menu(menu_options, list_menu, SPEEDS) mega_hardcore = False if speed == SPEEDS["MEGA_HARDCORE"]: mega_hardcore = True return speed, mega_hardcore
def over(self, score, step): """If collision with wall or body, end the game and open options. Return ---------- selected_option: int The selected option in the main loop. """ score_option = None if len(score) == VAR.benchmark: score_option = TextBlock( text=" ADD TO LEADERBOARDS ", pos=(self.screen_rect.centerx, 8 * self.screen_rect.centery / 10), canvas_size=VAR.canvas_size, font_path=self.font_path, window=self.window, scale=(1 / 15), block_type="menu", ) text_score = "SCORE: " + str(int(np.mean(score))) list_menu = ["PLAY", "MENU", "ADD_TO_LEADERBOARDS", "QUIT"] menu_options = [ TextBlock( text=" PLAY AGAIN ", pos=(self.screen_rect.centerx, 4 * self.screen_rect.centery / 10), canvas_size=VAR.canvas_size, font_path=self.font_path, window=self.window, scale=(1 / 15), block_type="menu", ), TextBlock( text=" GO TO MENU ", pos=(self.screen_rect.centerx, 6 * self.screen_rect.centery / 10), canvas_size=VAR.canvas_size, font_path=self.font_path, window=self.window, scale=(1 / 15), block_type="menu", ), score_option, TextBlock( text=" QUIT ", pos=(self.screen_rect.centerx, 10 * self.screen_rect.centery / 10), canvas_size=VAR.canvas_size, font_path=self.font_path, window=self.window, scale=(1 / 15), block_type="menu", ), TextBlock( text=text_score, pos=(self.screen_rect.centerx, 15 * self.screen_rect.centery / 10), canvas_size=VAR.canvas_size, font_path=self.font_path, window=self.window, scale=(1 / 10), block_type="text", ), ] pygame.display.set_caption("snake-on-pygame | " + text_score + " | GAME OVER...") LOGGER.info("EVENT: GAME OVER | FINAL %s", text_score) selected_option = self.cycle_menu(menu_options, list_menu, OPTIONS) return selected_option
def view_leaderboards(self, page=1): file_path = self.resource_path("resources/scores.json") with open(file_path, "r") as leaderboards_file: scores_data = json.loads(leaderboards_file.read()) dataframe = pd.DataFrame.from_dict(scores_data) dataframe = pd.concat( [ dataframe.drop(["ranking_data"], axis=1), dataframe["ranking_data"].apply(pd.Series), ], axis=1, ) # Separate 'ranking_data' into 2 cols ammount_of_players = len(dataframe.index) players_per_page = 5 number_of_pages = -(-ammount_of_players // players_per_page) score_page = [] score_header = " POS NAME SCORE STEP " list_menu = ["LEADERBOARDS"] menu_options = [ TextBlock( text=" LEADERBOARDS ", pos=(self.screen_rect.centerx, 2 * self.screen_rect.centery / 10), canvas_size=VAR.canvas_size, font_path=self.font_path, window=self.window, scale=(1 / 12), block_type="text", ) ] list_menu.append("HEADER") menu_options.append( TextBlock( text=score_header, pos=(self.screen_rect.centerx, 4 * self.screen_rect.centery / 10), canvas_size=VAR.canvas_size, font_path=self.font_path, window=self.window, scale=(1 / 24), block_type="text", background_color=(152, 152, 152), )) # Adding pages to the loop for i in range(1, number_of_pages + 1): score_page.append(dataframe.loc[dataframe.index.intersection( range(5 * (i - 1), 5 * i))]) list_menu.append(("LEADERBOARDS{:d}".format(i))) menu_options.append( TextBlock( text=(" {:d} ".format(i)), pos=( (2 * self.screen_rect.centerx / (number_of_pages + 1) * i), (13 * self.screen_rect.centery / 10), ), canvas_size=VAR.canvas_size, font_path=self.font_path, window=self.window, scale=(1 / 18), block_type="menu", )) for i, row in score_page[page - 1].iterrows(): list_menu.append(("RANK{:d}".format(i))) pos = "{0: <5} ".format(1 + i) name = "{0: <25} ".format(row["name"]) score = "{0: <5} ".format(row["score"]) step = "{0: <5} ".format(row["step"]) data = pos + name + score + step menu_options.append( TextBlock( text=data, pos=( self.screen_rect.centerx, ((5 + 1.5 * (i - (page - 1) * 5)) * (self.screen_rect.centery / 10)), ), canvas_size=VAR.canvas_size, font_path=self.font_path, window=self.window, scale=(1 / 24), block_type="text", )) list_menu.append("MENU") menu_options.append( TextBlock( text=" MENU ", pos=(self.screen_rect.centerx, 16 * self.screen_rect.centery / 10), canvas_size=VAR.canvas_size, font_path=self.font_path, window=self.window, scale=(1 / 12), block_type="menu", )) selected_option, page = self.cycle_menu(menu_options, list_menu, OPTIONS, leaderboards=True) return selected_option, page