Add some comments

This commit is contained in:
Yohann D'ANELLO
2020-10-16 18:05:49 +02:00
committed by Nicolas Margulies
parent d8401d9920
commit d8bd500349
3 changed files with 31 additions and 1 deletions

View File

@ -4,6 +4,10 @@ from enum import Enum
class Map:
"""
Object that represents a Map with its width, height
and the whole tiles, with their custom properties.
"""
width: int
height: int
tiles: list
@ -16,12 +20,18 @@ class Map:
@staticmethod
def load(filename: str):
"""
Read a file that contains the content of a map, and build a Map object.
"""
with open(filename, "r") as f:
file = f.read()
return Map.load_from_string(file)
@staticmethod
def load_from_string(content: str):
"""
Load a map represented by its characters and build a Map object.
"""
lines = content.split("\n")
lines = [line for line in lines if line]
height = len(lines)
@ -31,6 +41,10 @@ class Map:
return Map(width, height, tiles)
def draw_string(self) -> str:
"""
Draw the current map as a string object that can be rendered
in the window.
"""
return "\n".join("".join(tile.value for tile in line)
for line in self.tiles)
@ -44,6 +58,9 @@ class Tile(Enum):
return self == Tile.WALL
def can_walk(self) -> bool:
"""
Check if an entity (player or not) can move in this tile.
"""
return not self.is_wall()