Add some comments
This commit is contained in:
		
				
					committed by
					
						
						Nicolas Margulies
					
				
			
			
				
	
			
			
			
						parent
						
							d8401d9920
						
					
				
				
					commit
					d8bd500349
				
			@@ -4,6 +4,10 @@ from enum import Enum
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Map:
 | 
					class Map:
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    Object that represents a Map with its width, height
 | 
				
			||||||
 | 
					    and the whole tiles, with their custom properties.
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
    width: int
 | 
					    width: int
 | 
				
			||||||
    height: int
 | 
					    height: int
 | 
				
			||||||
    tiles: list
 | 
					    tiles: list
 | 
				
			||||||
@@ -16,12 +20,18 @@ class Map:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    @staticmethod
 | 
					    @staticmethod
 | 
				
			||||||
    def load(filename: str):
 | 
					    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:
 | 
					        with open(filename, "r") as f:
 | 
				
			||||||
            file = f.read()
 | 
					            file = f.read()
 | 
				
			||||||
        return Map.load_from_string(file)
 | 
					        return Map.load_from_string(file)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @staticmethod
 | 
					    @staticmethod
 | 
				
			||||||
    def load_from_string(content: str):
 | 
					    def load_from_string(content: str):
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        Load a map represented by its characters and build a Map object.
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
        lines = content.split("\n")
 | 
					        lines = content.split("\n")
 | 
				
			||||||
        lines = [line for line in lines if line]
 | 
					        lines = [line for line in lines if line]
 | 
				
			||||||
        height = len(lines)
 | 
					        height = len(lines)
 | 
				
			||||||
@@ -31,6 +41,10 @@ class Map:
 | 
				
			|||||||
        return Map(width, height, tiles)
 | 
					        return Map(width, height, tiles)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def draw_string(self) -> str:
 | 
					    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)
 | 
					        return "\n".join("".join(tile.value for tile in line)
 | 
				
			||||||
                         for line in self.tiles)
 | 
					                         for line in self.tiles)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -44,6 +58,9 @@ class Tile(Enum):
 | 
				
			|||||||
        return self == Tile.WALL
 | 
					        return self == Tile.WALL
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def can_walk(self) -> bool:
 | 
					    def can_walk(self) -> bool:
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        Check if an entity (player or not) can move in this tile.
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
        return not self.is_wall()
 | 
					        return not self.is_wall()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,6 +5,9 @@ from dungeonbattle.interfaces import Map
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
class TestInterfaces(unittest.TestCase):
 | 
					class TestInterfaces(unittest.TestCase):
 | 
				
			||||||
    def test_map(self) -> None:
 | 
					    def test_map(self) -> None:
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        Create a map and check that it is well parsed.
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
        m = Map.load_from_string(".#\n#.\n")
 | 
					        m = Map.load_from_string(".#\n#.\n")
 | 
				
			||||||
        self.assertEqual(m.width, 2)
 | 
					        self.assertEqual(m.width, 2)
 | 
				
			||||||
        self.assertEqual(m.height, 2)
 | 
					        self.assertEqual(m.height, 2)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,22 +1,29 @@
 | 
				
			|||||||
#!/usr/bin/env python
 | 
					 | 
				
			||||||
from .interfaces import Map
 | 
					from .interfaces import Map
 | 
				
			||||||
from .term_manager import TermManager
 | 
					from .term_manager import TermManager
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def proof_of_concept() -> None:
 | 
					def proof_of_concept() -> None:
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    Read an example map, parse it, then the (squirrel) player can move freely.
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    # Load the example map
 | 
				
			||||||
    m = Map.load("example_map.txt")
 | 
					    m = Map.load("example_map.txt")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Create the context manager to manipulate the screen
 | 
				
			||||||
    with TermManager() as term_manager:
 | 
					    with TermManager() as term_manager:
 | 
				
			||||||
        stdscr = term_manager.screen
 | 
					        stdscr = term_manager.screen
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Draw the full map
 | 
				
			||||||
        stdscr.addstr(0, 0, m.draw_string())
 | 
					        stdscr.addstr(0, 0, m.draw_string())
 | 
				
			||||||
        stdscr.refresh()
 | 
					        stdscr.refresh()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        cur = [1, 6]    # (y,x)
 | 
					        cur = [1, 6]    # (y,x)
 | 
				
			||||||
 | 
					        # We are a squirrel
 | 
				
			||||||
        stdscr.addstr(1, 6, '🐿️')
 | 
					        stdscr.addstr(1, 6, '🐿️')
 | 
				
			||||||
        stdscr.refresh()
 | 
					        stdscr.refresh()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        while True:
 | 
					        while True:
 | 
				
			||||||
 | 
					            # Get movements of the user
 | 
				
			||||||
            key = stdscr.getkey()
 | 
					            key = stdscr.getkey()
 | 
				
			||||||
            stdscr.addstr(cur[0], cur[1], '.')
 | 
					            stdscr.addstr(cur[0], cur[1], '.')
 | 
				
			||||||
            next_pos = cur[:]
 | 
					            next_pos = cur[:]
 | 
				
			||||||
@@ -28,8 +35,11 @@ def proof_of_concept() -> None:
 | 
				
			|||||||
                next_pos[1] = cur[1] - 1
 | 
					                next_pos[1] = cur[1] - 1
 | 
				
			||||||
            if key == 'd' or key == 'KEY_RIGHT':
 | 
					            if key == 'd' or key == 'KEY_RIGHT':
 | 
				
			||||||
                next_pos[1] = cur[1] + 1
 | 
					                next_pos[1] = cur[1] + 1
 | 
				
			||||||
 | 
					            # Check if we stay in the bounds
 | 
				
			||||||
            if 0 <= next_pos[0] < m.height and 0 <= next_pos[0] < m.width:
 | 
					            if 0 <= next_pos[0] < m.height and 0 <= next_pos[0] < m.width:
 | 
				
			||||||
                next_tile = m.tiles[next_pos[0]][next_pos[1]]
 | 
					                next_tile = m.tiles[next_pos[0]][next_pos[1]]
 | 
				
			||||||
 | 
					                # Check if the new position is valid
 | 
				
			||||||
                if next_tile.can_walk():
 | 
					                if next_tile.can_walk():
 | 
				
			||||||
                    cur = next_pos
 | 
					                    cur = next_pos
 | 
				
			||||||
 | 
					            # Draw the squirrel
 | 
				
			||||||
            stdscr.addstr(cur[0], cur[1], '🐿️')
 | 
					            stdscr.addstr(cur[0], cur[1], '🐿️')
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user