コード例 #1
0
class AStarDemo(object):
  KeyMap = {
    "h": direction.W, "j": direction.S, "k": direction.N, "l": direction.E,
    "y": direction.NW, "u": direction.NE, "b": direction.SW, "n": direction.SE
  }

  def __init__(self):
    self._cursor = (1, 1)
    self._start = (1, 1)
    self._goal = (33, 8)
    self._console = Console()
    self._map = Map()
    self._astar = AStar(self._map.cost)

  def run(self):
    while True:
      self.update()

  def update(self):
    self.render()
    key = self._console.getKey()
    if key in self.KeyMap:
      self._cursor = coord.sum(self._cursor, self.KeyMap.get(key))
    else:
      self.putTerrain(key)

  def putTerrain(self, key):
    if key not in (' ', '"', '=', '#') : return
    self._map.put(self._cursor, key)

  def render(self):
    self._console.clear()
    self._map.render(self._console)
    self._renderRoute()
    self._console.move(self._cursor)

  def _renderRoute(self):
    for c in self._astar.compute(self._start, self._goal):
      self._console.move(c).write('*')