def move_towards(self, who): m = Move(who) if isinstance(who, Enemy): x_coord = self.enemy_coords[0] y_coord = self.enemy_coords[1] distance = self.distance_to_enemy() elif isinstance(who, Hero): x_coord = self.hero_coord[0] y_coord = self.hero_coord[1] distance = self.distance_to_hero() if distance[0] == 0: if distance[1] < 0: tmp = m.move(self.dungeon, x_coord, y_coord, "left") x_coord = tmp[0] y_coord = tmp[1] self.dungeon = tmp[2] else: tmp = m.move(self.dungeon, x_coord, y_coord, "right") x_coord = tmp[0] y_coord = tmp[1] self.dungeon = tmp[2] elif distance[1] == 0: if distance[0] < 0: tmp = m.move(self.dungeon, x_coord, y_coord, "up") x_coord = tmp[0] y_coord = tmp[1] self.dungeon = tmp[2] else: tmp = m.move(self.dungeon, x_coord, y_coord, "down") x_coord = tmp[0] y_coord = tmp[1] self.dungeon = tmp[2] if isinstance(who, Enemy): self.enemy_coords = (tmp[0], tmp[1]) print( f"The enemy has moved one square in order to get to the hero. This is his move." ) elif isinstance(who, Hero): self.hero_coord = (tmp[0], tmp[1]) print( f"The hero has moved one square in order to get to the enemy. This is his move." ) self.print_dungeon() return self.dungeon
class Test(unittest.TestCase): def setUp(self): self.m = [] with open('test_map.txt', 'r') as f: for i in f.readlines(): i = i.rstrip() self.m.append(list(i)) self.hero = Hero(name='ivan', title='Dragon Slayer', health=100, mana=100, mana_regeneration_rate=2, spell=None, weapon=None) self.mover = Move(self.hero) def test_check_for_sttuff(self): self.assertRaises(AssertionError, check_for_stuff, '1', 1, 1, 'H', 1) self.assertRaises(AssertionError, check_for_stuff, self.m, '1', 1, 'H', 1) self.assertRaises(AssertionError, check_for_stuff, self.m, 1, '1', 'H', 1) self.assertRaises(AssertionError, check_for_stuff, self.m, 1, 1, 1, 1) self.assertRaises(AssertionError, check_for_stuff, self.m, 1, 1, 'B', 1) self.assertRaises(AssertionError, check_for_stuff, self.m, 1, 1, 'H', '1') def test_move(self): self.assertRaises(AssertionError, self.mover.move, self.m, 3, 1, 'wrong') self.assertRaises(AssertionError, self.mover.move, self.m, 3, '1', 'up') self.assertRaises(AssertionError, self.mover.move, self.m, '3', 1, 'wrong') self.assertRaises(AssertionError, self.mover.move, 'self.m', 3, 1, 'wrong') self.assertFalse(self.mover.move(self.m, 3, 1, 'down'))
def move_hero(self, direction): m = Move(self.hero) tmp = m.move(self.dungeon_map, self.x, self.y, direction) if m.cleared: self.cleared = True return if tmp: self.x = tmp[0] self.y = tmp[1] self.print_map() else: if not self.hero.is_alive(): des = input("Do you want to respawn? (y/n) ") if des == "y": self.hero.health = self.hero.max_health self.hero.mana = self.hero.max_mana self.spawn(self.hero) self.print_map() elif des == "n": return dir = input("You can\'t move that way! Pick another direction! ") self.move_hero(dir)