From d75f4290ffebd0f67f7dcea425f794f2d3524eb4 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Wed, 11 Nov 2020 16:09:03 +0100 Subject: [PATCH] Store the start position in a map --- dungeonbattle/interfaces.py | 13 ++++-- dungeonbattle/tests/interfaces_test.py | 2 +- resources/example_map.txt | 3 +- resources/example_map_2.txt | 63 +++++++++++++------------- 4 files changed, 45 insertions(+), 36 deletions(-) diff --git a/dungeonbattle/interfaces.py b/dungeonbattle/interfaces.py index 7b89ec6..a1ab13c 100644 --- a/dungeonbattle/interfaces.py +++ b/dungeonbattle/interfaces.py @@ -14,6 +14,8 @@ class Map: """ width: int height: int + start_y: int + start_x: int tiles: List[List["Tile"]] entities: List["Entity"] # coordinates of the point that should be @@ -21,9 +23,12 @@ class Map: currentx: int currenty: int - def __init__(self, width: int, height: int, tiles: list): + def __init__(self, width: int, height: int, tiles: list, + start_y: int, start_x: int): self.width = width self.height = height + self.start_y = start_y + self.start_x = start_x self.tiles = tiles self.entities = [] @@ -63,13 +68,15 @@ class Map: Load a map represented by its characters and build a Map object. """ lines = content.split("\n") - lines = [line for line in lines if line] + first_line = lines[0] + start_y, start_x = map(int, first_line.split(" ")) + lines = [line for line in lines[1:] if line] height = len(lines) width = len(lines[0]) tiles = [[Tile.from_ascii_char(c) for x, c in enumerate(line)] for y, line in enumerate(lines)] - return Map(width, height, tiles) + return Map(width, height, tiles, start_y, start_x) def draw_string(self, pack: TexturePack) -> str: """ diff --git a/dungeonbattle/tests/interfaces_test.py b/dungeonbattle/tests/interfaces_test.py index c82083c..b487eac 100644 --- a/dungeonbattle/tests/interfaces_test.py +++ b/dungeonbattle/tests/interfaces_test.py @@ -9,7 +9,7 @@ class TestInterfaces(unittest.TestCase): """ Create a map and check that it is well parsed. """ - m = Map.load_from_string(".#\n#.\n") + m = Map.load_from_string("0 0\n.#\n#.\n") self.assertEqual(m.width, 2) self.assertEqual(m.height, 2) self.assertEqual(m.draw_string(TexturePack.ASCII_PACK), ".#\n#.") diff --git a/resources/example_map.txt b/resources/example_map.txt index 4111fae..4b82063 100644 --- a/resources/example_map.txt +++ b/resources/example_map.txt @@ -1,4 +1,5 @@ - ####### ############# +1 6 + ####### ############# #.....# #...........# #.....# #####...........# #.....# #...............# diff --git a/resources/example_map_2.txt b/resources/example_map_2.txt index bcb4b60..8864f04 100644 --- a/resources/example_map_2.txt +++ b/resources/example_map_2.txt @@ -1,18 +1,19 @@ - ########### ######### - #.........# #.......# - #.........# ############.......# - #.........###############..........#.......############## - #.........#........................#....................# - #.........#.............#..........#.......#............# - ########.########.............#..................#............# - #.........# #.............####.#######.......#............# - #.........# #.............##.........###################### - #.........# #####.##########.........# ########### - #.........# #......# #.........# #.........# - ########.##########......# #.........# #.........# - #...........##......# #.........# #.........# - #...........##......# #.........# #.........# - #...........##......# #.........# ################.###### +1 17 + ########### ######### + #.........# #.......# + #.........# ############.......# + #.........###############..........#.......############## + #.........#........................#....................# + #.........#.............#..........#.......#............# + ########.########.............#..................#............# + #.........# #.............####.#######.......#............# + #.........# #.............##.........###################### + #.........# #####.##########.........# ########### + #.........# #......# #.........# #.........# + ########.##########......# #.........# #.........# + #...........##......# #.........# #.........# + #...........##......# #.........# #.........# + #...........##......# #.........# ################.###### #...........##......# #.........# #.................############ #...........##......# ########.########.......#.........#..........# #...........##......# #...............#.......#.........#..........# @@ -22,19 +23,19 @@ #.........# #...............###################..........# #.........############ #...............# #..........# #.........#..........# #...............# ############ - #....................#####.###########.############# - ########.#########...................# #.............# - #........# #..........#........# #.............######### - #........# ######.##########........# #.............#.......# - #........# #..........# #........# #.....................# - #........# #..........# #........# #.............#.......# - #........# #..........# #........# #.............#.......# - #........# #..........# #........# #.............#.......# - #........# #..........#########.##### #.............#.......# - #........# #..........#.........# ##########.############.####### - #........# #..........#.........# #..............# #..........# - ########## #..........#.........# #..............# #..........# - ############.........# #..............# #..........# - #.........# #..............# #..........# - ########### #..............# #..........# - ################ ############ + #....................#####.###########.############# + ########.#########...................# #.............# + #........# #..........#........# #.............######### + #........# ######.##########........# #.............#.......# + #........# #..........# #........# #.....................# + #........# #..........# #........# #.............#.......# + #........# #..........# #........# #.............#.......# + #........# #..........# #........# #.............#.......# + #........# #..........#########.##### #.............#.......# + #........# #..........#.........# ##########.############.####### + #........# #..........#.........# #..............# #..........# + ########## #..........#.........# #..............# #..........# + ############.........# #..............# #..........# + #.........# #..............# #..........# + ########### #..............# #..........# + ################ ############