def show_start_menu(self): # TODO: add mouse menu control def set_color_active_menu_item(index_active_menu_item, color, font_size): if index_active_menu_item == 0: menu_items['New Game'] = [color, font_size] if index_active_menu_item == 1: menu_items['Load Game'] = [color, font_size] if index_active_menu_item == 2: menu_items['Settings'] = [color, font_size] if index_active_menu_item == 3: menu_items['Exit'] = [color, font_size] menu_background = pygame.image.load( f'resources/images/backgrounds/background_{Constants.GAME_WINDOW_WIDTH}_{Constants.GAME_WINDOW_HEIGHT}.jpg' ) menu_items = { 'New Game': [Constants.DARK_ORANGE_HIGHLIGHTED_MENU_ITEM, 80], 'Load Game': [Constants.WHITE_COLOR_TITLE_BLOCKS, 50], 'Settings': [Constants.WHITE_COLOR_TITLE_BLOCKS, 50], 'Exit': [Constants.WHITE_COLOR_TITLE_BLOCKS, 50] } index_selected_menu_item = 0 is_menu_show = True clock = pygame.time.Clock() while is_menu_show: title_size = 150 Drawer.draw_text(self._main_game_window, Constants.GAME_WINDOW_TITLE, title_size, Constants.WHITE_COLOR_TITLE_BLOCKS, Constants.GAME_WINDOW_WIDTH // 2, 100) x_to_paste_menu_item = Constants.GAME_WINDOW_HEIGHT // 3 for menu_item, color_and_size in menu_items.items(): Drawer.draw_text(self._main_game_window, menu_item, color_and_size[1], color_and_size[0], Constants.GAME_WINDOW_WIDTH // 2, x_to_paste_menu_item) x_to_paste_menu_item += Constants.GAME_WINDOW_HEIGHT // 15 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_DOWN or event.key == pygame.K_s: index_previous_selected_item = index_selected_menu_item index_selected_menu_item += 1 if index_selected_menu_item > 3: index_selected_menu_item = 0 index_previous_selected_item = 3 set_color_active_menu_item( index_previous_selected_item, Constants.WHITE_COLOR_TITLE_BLOCKS, 50) set_color_active_menu_item( index_selected_menu_item, Constants.DARK_ORANGE_HIGHLIGHTED_MENU_ITEM, 80) if event.key == pygame.K_UP or event.key == pygame.K_w: index_previous_selected_item = index_selected_menu_item index_selected_menu_item -= 1 if index_selected_menu_item < 0: index_selected_menu_item = 3 set_color_active_menu_item( index_previous_selected_item, Constants.WHITE_COLOR_TITLE_BLOCKS, 50) set_color_active_menu_item( index_selected_menu_item, Constants.DARK_ORANGE_HIGHLIGHTED_MENU_ITEM, 80) if event.key == pygame.K_RETURN: if index_selected_menu_item == 0: is_menu_show = False self.run_game() if index_selected_menu_item == 1: self.show_game_loads() if index_selected_menu_item == 2: pass if index_selected_menu_item == 3: pygame.quit() sys.exit() pygame.display.update() self._main_game_window.blit(menu_background, (0, 0)) clock.tick(Constants.FPS_LOCKING)
def game_over(self): items = ['Last Save', 'Load', 'Exit'] handlers = ['load_game()', 'show_game_loads()', 'exit(0)'] Drawer.draw_text( self._main_game_window, 'You died. Press <Enter> To Restart Or <Escape> To Exit', 30, Constants.WHITE_COLOR_TITLE_BLOCKS, Constants.GAME_WINDOW_WIDTH // 2, Constants.GAME_WINDOW_HEIGHT // 4) def set_color_active_menu_item(index_active_menu_item, color, font_size): filename_to_highlight = items[index_active_menu_item] menu_items[filename_to_highlight] = [color, font_size] menu_background = pygame.image.load( f'resources/images/backgrounds/background_{Constants.GAME_WINDOW_WIDTH}_{Constants.GAME_WINDOW_HEIGHT}.jpg' ) menu_items = {} for save_file_name in items: menu_items[save_file_name] = [ Constants.WHITE_COLOR_TITLE_BLOCKS, 50 ] menu_items[items[0]] = [ Constants.DARK_ORANGE_HIGHLIGHTED_MENU_ITEM, 80 ] amount_files = len(items) index_selected_menu_item = 0 is_menu_show = True clock = pygame.time.Clock() while is_menu_show: x_to_paste_menu_item = Constants.GAME_WINDOW_HEIGHT // 3 for menu_item, color_and_size in menu_items.items(): Drawer.draw_text(self._main_game_window, menu_item, color_and_size[1], color_and_size[0], Constants.GAME_WINDOW_WIDTH // 2, x_to_paste_menu_item) x_to_paste_menu_item += Constants.GAME_WINDOW_HEIGHT // 15 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_DOWN or event.key == pygame.K_s: index_previous_selected_item = index_selected_menu_item index_selected_menu_item += 1 if index_selected_menu_item > amount_files - 1: index_selected_menu_item = 0 index_previous_selected_item = amount_files - 1 set_color_active_menu_item( index_previous_selected_item, Constants.WHITE_COLOR_TITLE_BLOCKS, 50) set_color_active_menu_item( index_selected_menu_item, Constants.DARK_ORANGE_HIGHLIGHTED_MENU_ITEM, 80) if event.key == pygame.K_UP or event.key == pygame.K_w: index_previous_selected_item = index_selected_menu_item index_selected_menu_item -= 1 if index_selected_menu_item < 0: index_selected_menu_item = amount_files - 1 set_color_active_menu_item( index_previous_selected_item, Constants.WHITE_COLOR_TITLE_BLOCKS, 50) set_color_active_menu_item( index_selected_menu_item, Constants.DARK_ORANGE_HIGHLIGHTED_MENU_ITEM, 80) if event.key == pygame.K_RETURN: eval(handlers[index_selected_menu_item]) pygame.display.flip() self._main_game_window.blit(menu_background, (0, 0)) clock.tick(Constants.FPS_LOCKING)
def show_game_loads(self): savings = os.listdir(Constants.DIRECTORY_WITH_SAVINGS) if not savings: self.run_game() def set_color_active_menu_item(index_active_menu_item, color, font_size): filename_to_highlight = savings[index_active_menu_item] menu_items[filename_to_highlight] = [color, font_size] menu_background = pygame.image.load( f'resources/images/backgrounds/background_{Constants.GAME_WINDOW_WIDTH}_{Constants.GAME_WINDOW_HEIGHT}.jpg' ) menu_items = {} for save_file_name in savings: menu_items[save_file_name] = [ Constants.WHITE_COLOR_TITLE_BLOCKS, 50 ] menu_items[savings[0]] = [ Constants.DARK_ORANGE_HIGHLIGHTED_MENU_ITEM, 80 ] amount_files = len(savings) index_selected_menu_item = 0 is_menu_show = True clock = pygame.time.Clock() while is_menu_show: title_size = 150 Drawer.draw_text(self._main_game_window, Constants.GAME_WINDOW_TITLE, title_size, Constants.WHITE_COLOR_TITLE_BLOCKS, Constants.GAME_WINDOW_WIDTH // 2, 100) x_to_paste_menu_item = Constants.GAME_WINDOW_HEIGHT // 3 for menu_item, color_and_size in menu_items.items(): Drawer.draw_text(self._main_game_window, menu_item, color_and_size[1], color_and_size[0], Constants.GAME_WINDOW_WIDTH // 2, x_to_paste_menu_item) x_to_paste_menu_item += Constants.GAME_WINDOW_HEIGHT // 15 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_DOWN or event.key == pygame.K_s: index_previous_selected_item = index_selected_menu_item index_selected_menu_item += 1 if index_selected_menu_item > amount_files - 1: index_selected_menu_item = 0 index_previous_selected_item = amount_files - 1 set_color_active_menu_item( index_previous_selected_item, Constants.WHITE_COLOR_TITLE_BLOCKS, 50) set_color_active_menu_item( index_selected_menu_item, Constants.DARK_ORANGE_HIGHLIGHTED_MENU_ITEM, 80) if event.key == pygame.K_UP or event.key == pygame.K_w: index_previous_selected_item = index_selected_menu_item index_selected_menu_item -= 1 if index_selected_menu_item < 0: index_selected_menu_item = amount_files - 1 set_color_active_menu_item( index_previous_selected_item, Constants.WHITE_COLOR_TITLE_BLOCKS, 50) set_color_active_menu_item( index_selected_menu_item, Constants.DARK_ORANGE_HIGHLIGHTED_MENU_ITEM, 80) if event.key == pygame.K_RETURN: self.load_game(savings[index_selected_menu_item]) pygame.display.flip() self._main_game_window.blit(menu_background, (0, 0)) clock.tick(Constants.FPS_LOCKING)
def run_game(self, data=None): self._player = Player(Constants.GAME_WINDOW_WIDTH // 2, Constants.GAME_WINDOW_HEIGHT // 2, animation_images=Textures.PLAYER, saved_params=data) text = None test_cur = None test_max = None clock = pygame.time.Clock() is_game_exit = False while not is_game_exit: pygame.event.pump() for enemy in self._enemies: if pygame.sprite.collide_rect(self._player, enemy): now = pygame.time.get_ticks() if now - enemy._last_attack_time > enemy._attack_interval: enemy._last_attack_time = now self._player.current_health -= enemy.amount_damage if self._player.current_health < 1: return self.game_over() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: self.show_pause_menu() if event.key == pygame.K_F5: self.save_game([self._player]) if event.key == pygame.K_F9: self.load_game() if event.key == pygame.K_SPACE: shells = self._player.attack() self._shells.extend(shells) if event.key == pygame.K_i: self._player.show_inventory(self._main_game_window) if event.key == pygame.K_c: self._player.change_weapon() self._main_game_window.fill(int()) self._map.update(self._main_game_window) for tile in self._map.map_tiles: offset = self._camera.offset tile.rect.centerx -= offset[0] tile.rect.centery -= offset[1] for chest in self._chests: offset = self._camera.offset chest.rect.centerx -= offset[0] chest.rect.centery -= offset[1] chest.update(self._main_game_window) if pygame.sprite.collide_rect( self._player, chest) and pygame.key.get_pressed()[pygame.K_e]: additional_potions = chest.open( self._player.is_key_in_inventory()) if additional_potions is not None: for additional_thing in additional_potions: if additional_thing != 0: self._things.append(additional_thing) for potion in self._things: if potion: potion.update(self._main_game_window) offset = self._camera.offset potion.rect.centerx -= offset[0] potion.rect.centery -= offset[1] if pygame.sprite.collide_rect( self._player, potion) and pygame.key.get_pressed()[pygame.K_e]: self._player.append_thing_to_inventory(potion) self._things.remove(potion) for neutral in self._neutrals: if pygame.sprite.collide_rect( self._player, neutral) and pygame.key.get_pressed()[pygame.K_e]: self._quests.append(neutral.quest) neutral.vision_quest_mark = False offset = self._camera.offset for neutral in self._neutrals: neutral.update(self._main_game_window) neutral.rect.centerx -= offset[0] neutral.rect.centery -= offset[1] for tree in self._trees: tree.update(self._main_game_window) offset = self._camera.offset tree.rect.centerx -= offset[0] tree.rect.centery -= offset[1] for shell in self._shells: if shell: shell.update(self._main_game_window) offset = self._camera.offset shell.rect.centerx -= offset[0] shell.rect.centery -= offset[1] for enemy in self._enemies: enemy.attack(self._player.rect.centerx, self._player.rect.centery) offset = self._camera.offset enemy.rect.centerx -= offset[0] enemy.rect.centery -= offset[1] enemy.update(self._main_game_window) enemy.attack(self._player.rect.centerx, self._player.rect.centery) for enemy in self._enemies: for shell in self._shells: if shell: if pygame.sprite.collide_rect(enemy, shell): self._shells.remove(shell) enemy.current_health -= self._player.amount_damage enemy.current_health -= shell.amount_additional_damage dx = float(self._player.rect.centerx - enemy.rect.centerx) dy = float(self._player.rect.centery - enemy.rect.centery) length = math.sqrt(dx**2 + dy**2) enemy.is_enemy_angry = True enemy.show_aggression_to_attack_player( dx, dy, length) text = enemy.name test_cur = enemy.current_health test_max = enemy.max_amount_health if enemy.current_health < 0: self._player.current_experience += enemy.experience_for_killing if enemy in self._enemies: self._enemies.remove(enemy) text = None test_cur = None test_max = None self._camera.update(self._player) self._player.update(self._main_game_window) for quest in self._quests: if not self._enemies: quest.completed = True self._player.current_experience += quest.experience if quest not in self._names_done_quests: self._names_done_quests.append(quest.title) self._quests.remove(quest) if text is not None: Drawer.draw_bar(self._main_game_window, Constants.GAME_WINDOW_WIDTH // 2, 10, Constants.UNNAMED_COLOR_HEALTH_BAR, test_cur, test_max, 150, 15) Drawer.draw_text(self._main_game_window, text, 25, Constants.WHITE_COLOR_TITLE_BLOCKS, Constants.GAME_WINDOW_WIDTH // 2, 9) Drawer.draw_text( self._main_game_window, self._player.name, 15, Constants.WHITE_COLOR_TITLE_BLOCKS, self._player.rect.centerx, self._player.rect.centery + self._player.rect.height // 2 + 5) Drawer.draw_bar(self._main_game_window, Constants.GAME_WINDOW_WIDTH // 2, Constants.GAME_WINDOW_HEIGHT * 0.86, Constants.WHITE_COLOR_TITLE_BLOCKS, self._player.current_experience, self._player.experience_up_level, 310, 5, True) Drawer.draw_bar(self._main_game_window, Constants.GAME_WINDOW_WIDTH // 2 - 70, Constants.GAME_WINDOW_HEIGHT - 70, Constants.STAMINA_BAR_COLOR, self._player.current_stamina, self._player.max_stamina, 90, 14, True) pygame.display.flip() # for double buffering clock.tick(Constants.FPS_LOCKING) pygame.quit()
def draw_inventory(self, surface): def draw_outer_frame(): pygame.draw.rect(surface, Constants.DARK_ORANGE_HIGHLIGHTED_MENU_ITEM, self._inventory_rect, self._frame_thickness) def draw_inventory_cells(): for cell in self._cells: x, y = cell.left, cell.top pygame.draw.rect(surface, Constants.TEST_COLOR, (x, y, 40, 40)) pygame.draw.rect(surface, (0, 0, 0), (x, y, 40, 40), self._frame_thickness) draw_outer_frame() draw_inventory_cells() clock = pygame.time.Clock() is_inventory_showed = True is_left_mouse_button_hold = False while is_inventory_showed: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_i: is_inventory_showed = False clicked = pygame.mouse.get_pressed(3) if clicked[0] and not is_left_mouse_button_hold: start_coordinates = pygame.mouse.get_pos() is_left_mouse_button_hold = True for i in range(len(self._cells)): if (self._cells[i].left < start_coordinates[0] < self._cells[i].right and self._cells[i].top < start_coordinates[1] < self._cells[i].bottom): self._start_cell_to_move_item = i if is_left_mouse_button_hold and not clicked[0]: end_coordinates = pygame.mouse.get_pos() is_left_mouse_button_hold = False for i in range(len(self._cells)): if (self._cells[i].left < end_coordinates[0] < self._cells[i].right and self._cells[i].top < end_coordinates[1] < self._cells[i].bottom): self._end_cell_to_move_item = i for resource in self._resources.values(): if resource[2] == self._start_cell_to_move_item: pygame.draw.rect( surface, Constants.TEST_COLOR, (self._cells[resource[2]].left + 3, self._cells[resource[2]].top + 3, 34, 34)) resource[2] = self._end_cell_to_move_item self._resources['gold_coin'][0].update(surface) self._resources['silver_coin'][0].update(surface) self._resources['bronze_coin'][0].update(surface) Drawer.draw_text(surface, str(self._resources['gold_coin'][1]), 20, Constants.WHITE_COLOR_TITLE_BLOCKS, self._resources['gold_coin'][0].rect.right + 10, self._resources['gold_coin'][0].rect.top + 10) Drawer.draw_text(surface, str(self._resources['silver_coin'][1]), 20, Constants.WHITE_COLOR_TITLE_BLOCKS, self._resources['silver_coin'][0].rect.right + 10, self._resources['silver_coin'][0].rect.top + 10) Drawer.draw_text(surface, str(self._resources['bronze_coin'][1]), 20, Constants.WHITE_COLOR_TITLE_BLOCKS, self._resources['bronze_coin'][0].rect.right + 10, self._resources['bronze_coin'][0].rect.top + 10) pygame.display.flip() for name, resource in self._resources.items(): if name != 'gold_coin' and name != 'silver_coin' and name != 'bronze_coin': resource[0].rect.centerx = self._cells[resource[2]].centerx resource[0].rect.centery = self._cells[resource[2]].centery surface.blit(resource[0].image, resource[0].rect) Drawer.draw_text(surface, str(resource[1]), 20, Constants.WHITE_COLOR_TITLE_BLOCKS, self._cells[resource[2]].centerx + 10, self._cells[resource[2]].centery + 5) pygame.display.flip() clock.tick(Constants.FPS_LOCKING)