Store the start position in a map

This commit is contained in:
Yohann D'ANELLO 2020-11-11 16:09:03 +01:00
parent 279ef2439d
commit d75f4290ff
4 changed files with 45 additions and 36 deletions

View File

@ -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:
"""

View File

@ -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#.")

View File

@ -1,3 +1,4 @@
1 6
####### #############
#.....# #...........#
#.....# #####...........#

View File

@ -1,3 +1,4 @@
1 17
########### #########
#.........# #.......#
#.........# ############.......#