From 9a56b4d7e99cf43404e30973b2784f5a969d0503 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Sat, 26 Dec 2020 01:08:43 +0100 Subject: [PATCH] Navigate through different maps while climbing ladders --- squirrelbattle/assets/example_map.txt | 4 ++-- squirrelbattle/assets/example_map_2.txt | 4 ++-- squirrelbattle/game.py | 20 +++++++++++++++++++- squirrelbattle/interfaces.py | 11 +++++++---- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/squirrelbattle/assets/example_map.txt b/squirrelbattle/assets/example_map.txt index e52172c..be2e798 100644 --- a/squirrelbattle/assets/example_map.txt +++ b/squirrelbattle/assets/example_map.txt @@ -1,8 +1,8 @@ 1 6 ####### ############# - #.H...# #...........# + #.H...# #...........# #.....# #####...........# - #.....# #............H..# + #.....# #............H..# #.##### #.###...........# #.# #.# #...........# #.# #.# ############# diff --git a/squirrelbattle/assets/example_map_2.txt b/squirrelbattle/assets/example_map_2.txt index c959659..b9c751f 100644 --- a/squirrelbattle/assets/example_map_2.txt +++ b/squirrelbattle/assets/example_map_2.txt @@ -1,6 +1,6 @@ 1 17 ########### ######### - #....H....# #.......# + #....H....# #.......# #.........# ############.......# #.........###############..........#.......############## #.........#........................#....................# @@ -13,7 +13,7 @@ ########.##########......# #.........# #.........# #...........##......# #.........# #.........# #...........##......# #.........# #.........# - #...........##..H...# #.........# ################.###### + #...........##..H...# #.........# ################.###### #...........##......# #.........# #.................############ #...........##......# ########.########.......#.........#..........# #...........##......# #...............#.......#.........#..........# diff --git a/squirrelbattle/game.py b/squirrelbattle/game.py index 7ef8fc9..7807882 100644 --- a/squirrelbattle/game.py +++ b/squirrelbattle/game.py @@ -55,7 +55,7 @@ class Game: # TODO generate a new map procedurally self.maps = [] self.map_index = 0 - self.map = Map.load(ResourceManager.get_asset_path("example_map_2.txt")) + self.map = Map.load(ResourceManager.get_asset_path("example_map.txt")) self.map.logs = self.logs self.logs.clear() self.player = Player() @@ -102,6 +102,24 @@ class Game: self.handle_key_pressed( KeyValues.translate_key(key, self.settings), key) + # FIXME This code should not be there, but rather in Map.tick + y, x = self.player.y, self.player.x + if self.map.tiles[y][x].is_ladder(): + # We move up on the ladder of the beginning, + # down at the end of the stage + move_down = y != self.map.start_y and x != self.map.start_x + old_map = self.map + self.map_index += 1 if move_down else -1 + self.map_index = max(0, self.map_index) + while self.map_index >= len(self.maps): + self.maps.append(Map.load(ResourceManager.get_asset_path( + "example_map_2.txt"))) + new_map = self.map + old_map.remove_entity(self.player) + new_map.add_entity(self.player) + self.player.move(self.map.start_y, self.map.start_x) + self.display_actions(DisplayActions.UPDATE) + def handle_key_pressed(self, key: Optional[KeyValues], raw_key: str = '')\ -> None: """ diff --git a/squirrelbattle/interfaces.py b/squirrelbattle/interfaces.py index 3394af8..788edaa 100644 --- a/squirrelbattle/interfaces.py +++ b/squirrelbattle/interfaces.py @@ -140,12 +140,15 @@ class Map: Put randomly {count} entities on the map, where it is available. """ for ignored in range(count): - y, x = 0, 0 while True: y, x = randint(0, self.height - 1), randint(0, self.width - 1) - tile = self.tiles[y][x] - if tile.can_walk(): - break + try: + tile = self.tiles[y][x] + except Exception as e: + raise Exception(y, x, len(self.tiles)) + else: + if tile.can_walk(): + break entity = choice(Entity.get_all_entity_classes())() entity.move(y, x) self.add_entity(entity)