forked from UNN/2026-rff_mp
[2] add player rejim
This commit is contained in:
parent
539dba32f9
commit
dfd34a3974
|
|
@ -207,6 +207,130 @@ class LabyrinthSolver:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class EventListener:
|
||||||
|
def notify(self, event, data):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
|
class ConsoleRenderer(EventListener):
|
||||||
|
def __init__(self, walker=None):
|
||||||
|
self.last_path = None
|
||||||
|
self.walker = walker
|
||||||
|
|
||||||
|
def notify(self, event, data):
|
||||||
|
if event == "maze_loaded":
|
||||||
|
self._draw_maze(data)
|
||||||
|
elif event == "path_found":
|
||||||
|
self.last_path = data
|
||||||
|
self._show_path_info(data)
|
||||||
|
elif event == "player_moved":
|
||||||
|
self._draw_maze_with_player(data)
|
||||||
|
|
||||||
|
def _draw_maze(self, lab):
|
||||||
|
os.system('cls' if os.name == 'nt' else 'clear')
|
||||||
|
print("=" * (lab.width * 2 + 4))
|
||||||
|
print(" ЛАБИРИНТ")
|
||||||
|
print("=" * (lab.width * 2 + 4))
|
||||||
|
for y in range(lab.height):
|
||||||
|
print(" ", end='')
|
||||||
|
for x in range(lab.width):
|
||||||
|
t = lab.get_tile(x, y)
|
||||||
|
if t == lab.start_tile:
|
||||||
|
print('S', end=' ')
|
||||||
|
elif t == lab.exit_tile:
|
||||||
|
print('E', end=' ')
|
||||||
|
elif t.wall:
|
||||||
|
print('#', end=' ')
|
||||||
|
else:
|
||||||
|
print('.', end=' ')
|
||||||
|
print()
|
||||||
|
print("=" * (lab.width * 2 + 4))
|
||||||
|
print(" S - старт E - выход # - стена . - проход")
|
||||||
|
|
||||||
|
def _draw_maze_with_player(self, lab):
|
||||||
|
os.system('cls' if os.name == 'nt' else 'clear')
|
||||||
|
print("=" * (lab.width * 2 + 4))
|
||||||
|
print(" ЛАБИРИНТ (игрок = P)")
|
||||||
|
print("=" * (lab.width * 2 + 4))
|
||||||
|
for y in range(lab.height):
|
||||||
|
print(" ", end='')
|
||||||
|
for x in range(lab.width):
|
||||||
|
t = lab.get_tile(x, y)
|
||||||
|
if self.walker and t == self.walker.current:
|
||||||
|
print('P', end=' ')
|
||||||
|
elif t == lab.start_tile:
|
||||||
|
print('S', end=' ')
|
||||||
|
elif t == lab.exit_tile:
|
||||||
|
print('E', end=' ')
|
||||||
|
elif t.wall:
|
||||||
|
print('#', end=' ')
|
||||||
|
else:
|
||||||
|
print('.', end=' ')
|
||||||
|
print()
|
||||||
|
print("=" * (lab.width * 2 + 4))
|
||||||
|
if self.walker:
|
||||||
|
print(f" Позиция игрока: ({self.walker.current.x}, {self.walker.current.y})")
|
||||||
|
|
||||||
|
def _show_path_info(self, path):
|
||||||
|
if not path:
|
||||||
|
print("\n Путь не найден!")
|
||||||
|
else:
|
||||||
|
print(f"\n Путь найден! Длина = {len(path)}")
|
||||||
|
|
||||||
|
|
||||||
|
class Walker:
|
||||||
|
def __init__(self, start_tile, lab):
|
||||||
|
self.current = start_tile
|
||||||
|
self.prev = None
|
||||||
|
self.lab = lab
|
||||||
|
|
||||||
|
def move(self, target_tile):
|
||||||
|
if target_tile and target_tile.passable():
|
||||||
|
self.prev = self.current
|
||||||
|
self.current = target_tile
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def undo(self):
|
||||||
|
if self.prev:
|
||||||
|
self.current, self.prev = self.prev, None
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
class Action:
|
||||||
|
def do(self):
|
||||||
|
raise NotImplementedError
|
||||||
|
def undo(self):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
|
class MoveAction(Action):
|
||||||
|
def __init__(self, walker, dx, dy, lab):
|
||||||
|
self.walker = walker
|
||||||
|
self.dx = dx
|
||||||
|
self.dy = dy
|
||||||
|
self.lab = lab
|
||||||
|
self.done = False
|
||||||
|
|
||||||
|
def do(self):
|
||||||
|
new_x = self.walker.current.x + self.dx
|
||||||
|
new_y = self.walker.current.y + self.dy
|
||||||
|
target = self.lab.get_tile(new_x, new_y)
|
||||||
|
if target and target.passable():
|
||||||
|
self.walker.move(target)
|
||||||
|
self.done = True
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def undo(self):
|
||||||
|
if self.done:
|
||||||
|
self.walker.undo()
|
||||||
|
self.done = False
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
loader = TextLabyrinthLoader()
|
loader = TextLabyrinthLoader()
|
||||||
lab = loader.load("maze/maze1.txt")
|
lab = loader.load("maze/maze1.txt")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user