本文整理汇总了Python中dungeon.Dungeon.load_map方法的典型用法代码示例。如果您正苦于以下问题:Python Dungeon.load_map方法的具体用法?Python Dungeon.load_map怎么用?Python Dungeon.load_map使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dungeon.Dungeon
的用法示例。
在下文中一共展示了Dungeon.load_map方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_smtg_test
# 需要导入模块: from dungeon import Dungeon [as 别名]
# 或者: from dungeon.Dungeon import load_map [as 别名]
class test_smtg_test(unittest.TestCase):
"""Testing the unit"""
def setUp(self):
self.filename = 'test_dng_map.txt'
f = open(self.filename, "w")
content_map = 'S.##......\n#.##..###.\n#.###.###.\n#.....###.\n###.#####S'
f.write(content_map)
f.close()
self.new_map = Dungeon(self.filename)
def test_load_map(self):
expected = 'S.##......\n#.##..###.\n#.###.###.\n#.....###.\n###.#####S'
self.assertEqual(expected, self.new_map.load_map())
def test_map_to_matrix(self):
expected = [['S', '.', '#', '#', '.', '.', '.', '.', '.', '.'], ['#', '.', '#', '#', '.', '.', '#', '#', '#', '.'], ['#', '.', '#', '#', '#', '.', '#', '#', '#', '.'], ['#', '.', '.', '.', '.', '.', '#', '#', '#', '.'], ['#', '#', '#', '.', '#', '#', '#', '#', '#', 'S']]
self.assertEqual(expected, self.new_map.map_to_matrix())
def tearDown(self):
# fpath = os.getcwd()
try:
os.remove(self.filename)
except OSError:
print('No such file')
示例2: DungeonTest
# 需要导入模块: from dungeon import Dungeon [as 别名]
# 或者: from dungeon.Dungeon import load_map [as 别名]
class DungeonTest(unittest.TestCase):
def setUp(self):
self.mapfile = "testmap.txt"
self.mapcontents = """ZZZZZZZZZZZZZZZZZZZ\nZ#...#.##.########Z
Z#.S.#.....N....K.Z\nZ#...#..#.....####Z
Z#...#..#.....N..#Z\nZ....#..#......#.#Z
Z#......#..N...#.#Z\nZ..............#C#Z
Z..###########....Z\nZZZZZZZZZZZZZZZZZZZ"""
with open(self.mapfile, "w+") as f:
f.write(self.mapcontents)
self.dungeon = Dungeon("testmap.txt")
self.hero = Hero("Arthas", 500, "Lich King")
self.orc = Orc("Thrall", 500, 1.6)
def tearDown(self):
remove(self.mapfile)
def test_init(self):
self.assertEqual(self.dungeon.map, self.mapcontents)
def test_load_map_with_valid_file(self):
result = self.dungeon.load_map("testmap.txt")
self.assertEqual(result, self.mapcontents)
def test_load_map_with_invalid_file(self):
result = self.dungeon.load_map("not_exists.txt")
self.assertEqual(result, "")
def test_map_with_empty_file(self):
with open(self.mapfile, "w") as f:
f.write("")
result = self.dungeon.load_map("testmap.txt")
self.assertEqual(result, "")
def test_print_map_with_valid_file(self):
result = self.dungeon.print_map()
self.assertEqual(result, self.mapcontents)
def test_print_map_with_empty_file(self):
self.dungeon.map = ""
result = self.dungeon.print_map()
self.assertEqual(result, "No valid map loaded.")
def test_print_map_after_battle(self):
hero = Hero("Alan", 1, "Turing")
hero.weapon = Weapon("Stick", 1, 0.1)
self.dungeon.map = "ZSNZ"
self.dungeon.spawn("1", hero)
self.dungeon.spawn_npcs()
self.dungeon.move_player("1", "right")
self.assertEqual(self.dungeon.map, "Z.OZ")
def test_valid_spawn(self):
self.dungeon.spawn("Player 1", self.hero)
contents = """ZZZZZZZZZZZZZZZZZZZ\nZ#...#.##.########Z
Z#.H.#.....N....K.Z\nZ#...#..#.....####Z
Z#...#..#.....N..#Z\nZ....#..#......#.#Z
Z#......#..N...#.#Z\nZ..............#C#Z
Z..###########....Z\nZZZZZZZZZZZZZZZZZZZ"""
self.assertEqual(self.dungeon.map, contents)
def test_spawn_when_char_is_ingame(self):
self.dungeon.spawn("Player 1", self.hero)
result = self.dungeon.spawn("Player 1", self.hero)
self.assertEqual(result, "Character is already spawned.")
def test_spawn_when_no_free_slots(self):
self.dungeon.spawn("Player 1", self.hero)
self.dungeon.spawn("Player 2", self.orc)
testchar = Hero("Arthas", 200, "Lich")
result = self.dungeon.spawn("Player 3", testchar)
self.assertEqual(result, "No free spawn slot.")
def test_map_conversion(self):
actual = [["Z", "Z", "Z", "Z", "Z", "Z", "Z", "Z", "Z", "Z", "Z", "Z",
"Z", "Z", "Z", "Z", "Z", "Z", "Z"],
["Z", "#", ".", ".", ".", "#", ".", "#", "#", ".", "#", "#",
"#", "#", "#", "#", "#", "#", "Z"],
["Z", "#", ".", "S", ".", "#", ".", ".", ".", ".", ".", "N",
".", ".", ".", ".", "K", ".", "Z"],
["Z", "#", ".", ".", ".", "#", ".", ".", "#", ".", ".", ".",
".", ".", "#", "#", "#", "#", "Z"],
["Z", "#", ".", ".", ".", "#", ".", ".", "#", ".", ".", ".",
".", ".", "N", ".", ".", "#", "Z"],
["Z", ".", ".", ".", ".", "#", ".", ".", "#", ".", ".", ".",
".", ".", ".", "#", ".", "#", "Z"],
["Z", "#", ".", ".", ".", ".", ".", ".", "#", ".", ".", "N",
".", ".", ".", "#", ".", "#", "Z"],
["Z", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".",
".", ".", ".", "#", "C", "#", "Z"],
["Z", ".", ".", "#", "#", "#", "#", "#", "#", "#", "#", "#",
"#", "#", ".", ".", ".", ".", "Z"],
["Z", "Z", "Z", "Z", "Z", "Z", "Z", "Z", "Z", "Z", "Z", "Z",
"Z", "Z", "Z", "Z", "Z", "Z", "Z"]]
result = self.dungeon.convert_map_to_changeable_tiles()
self.assertEqual(result, actual)
#.........这里部分代码省略.........
示例3: DungeonTests
# 需要导入模块: from dungeon import Dungeon [as 别名]
# 或者: from dungeon.Dungeon import load_map [as 别名]
class DungeonTests(unittest.TestCase):
def setUp(self):
file = open("test_file.txt", "w")
content = ["S...S", "", "Spoon 4 0.2", "RustyFork 6 0.7"]
file.write("\n".join(content))
file.close()
self.dungeon = Dungeon("test_file.txt")
self.hero = Hero("Don Quixote", 20, "The Common Sense")
self.orc = Orc("Oplik", 20, 1.5)
def test_init(self):
self.assertEqual([['S', '.', '.', '.', 'S']], self.dungeon.dungeon)
weapons = list(map(lambda x: x.type, deepcopy(self.dungeon.weapons)))
self.assertEqual(["Spoon", "RustyFork"], weapons)
self.assertEqual({}, self.dungeon.players)
def test_load_map(self):
self.assertEqual([['S', '.', '.', '.', 'S']],
self.dungeon.load_map("test_file.txt"))
def test_is_map_row(self):
self.assertTrue(self.dungeon.is_map_row("#..S..#"))
self.assertFalse(self.dungeon.is_map_row("##Not.really.a.row"))
def test_set_weapons(self):
self.dungeon.weapons = []
self.dungeon.set_weapons(["Knife 7 0.3"])
self.assertEqual("Knife", self.dungeon.weapons[0].type)
self.assertEqual(7.0, self.dungeon.weapons[0].damage)
self.assertEqual(0.3, self.dungeon.weapons[0].critical_strike_percent)
def test_print_map(self):
self.assertTrue(self.dungeon.print_map())
def test_free_spawning_points(self): # help function
self.dungeon.dungeon = [['T'], ['T']]
self.assertFalse(self.dungeon.free_spawning_points())
self.dungeon.dungeon = [['T'], ['S']]
self.assertTrue(self.dungeon.free_spawning_points())
def test_spawn(self):
self.assertTrue(self.dungeon.spawn("player 1", self.hero))
self.assertTrue(self.dungeon.spawn("player 2", self.orc))
self.assertEqual({"player 1": (self.hero, 0, 0),
"player 2": (self.orc, 4, 0)}, self.dungeon.players)
def test_spawn_player_not_unique_value_error(self):
self.dungeon.spawn("player 1", self.hero)
with self.assertRaises(ValueError):
self.dungeon.spawn("player 1", self.hero)
def test_spawn_no_spawning_points(self):
self.dungeon.dungeon = [['.', '.']]
self.assertFalse(self.dungeon.spawn("player 1", self.hero))
def test_entity_kind(self):
self.assertEqual('H', self.dungeon.entity_kind(self.hero))
self.assertEqual('O', self.dungeon.entity_kind(self.orc))
def test_entity_kind_wrong_instance(self):
with self.assertRaises(ValueError):
self.dungeon.entity_kind("tomatoe")
def test_move(self):
self.dungeon.dungeon = [['S', '.']]
self.dungeon.spawn("player", self.hero)
self.dungeon.move("player", "right")
self.assertEqual([['.', 'H']], self.dungeon.dungeon)
def test_move_obstacle(self):
self.dungeon.dungeon = [['S', '#']]
self.dungeon.spawn("player", self.hero)
self.assertFalse(self.dungeon.move("player", "right"))
def test_move_out_of_bounds(self):
self.dungeon.spawn("player", self.hero)
self.assertFalse(self.dungeon.move("player", "up"))
def test_move_weapon_found(self):
self.dungeon.dungeon = [['S', '.']]
weapon = Weapon("Knife", 7, 0.3)
self.dungeon.weapons = [weapon]
self.dungeon.spawn_weapons()
self.dungeon.spawn("player", self.hero)
self.dungeon.move("player", "right")
self.assertEqual(weapon, self.dungeon.players["player"][0].weapon)
self.assertEqual([['.', 'H']], self.dungeon.dungeon)
def test_move_start_fight(self):
self.dungeon.spawn("player 1", self.hero)
self.dungeon.spawn("player 2", self.orc)
self.dungeon.spawn_weapons()
self.dungeon.move("player 1", "right")
self.dungeon.move("player 1", "right")
self.dungeon.move("player 2", "left")
self.dungeon.move("player 2", "left")
self.assertTrue([['.', '.', 'H', '.', '.']] == self.dungeon.dungeon or
#.........这里部分代码省略.........
示例4: SimulateGame
# 需要导入模块: from dungeon import Dungeon [as 别名]
# 或者: from dungeon.Dungeon import load_map [as 别名]
class SimulateGame():
def __init__(self, map_name):
self.dugneon = Dungeon(map_name)
self.command1 = ""
self.command2 = ""
self.command3 = ""
self.command4 = ""
def main():
game = SimulateGame("map.txt")
game.execute_necessary_inits()
while game.dungeon.EXIT is False:
game.dungeon.show_map()
game.dungeon.input_comand()
game.recognize_commands()
game.execute_command()
def execute_necessary_inits(self):
thanes_hummer = Weapon("Thanes hummer", 15, 0.4)
yurnero_blade = Weapon("Yurneros blade", 10, 0.35)
the_hero = Hero("Thanes", 150, 150, "Mountain King")
the_orc = Orc("Yurnero the Blademaster", 150, 150, 1.3)
small_potion = Potion("Small potion", 15)
midium_potion = Potion("Midium potion", 25)
the_hero.equip_weapon(thanes_hummer)
the_orc.equip_weapon(yurnero_blade)
self.dungeon.dict_of_items[yurnero_blade] = []
self.dungeon.dict_of_items[thanes_hummer] = []
self.dungeon.dict_of_items[small_potion] = []
self.dungeon.dict_of_items[midium_potion] = []
self.dungeon.dict_of_herous[the_orc] = []
self.dungeon.dict_of_herous[the_hero] = []
def recognize_commands(self):
if len(self.dungeon.list_of_commands) == 1:
self.command1 = self.dungeon.list_of_commands[0]
elif len(self.dungeon.list_of_commands) == 2:
self.command1 = self.dungeon.list_of_commands[0]
self.command2 = self.dungeon.list_of_commands[1]
elif len(self.dungeon.list_of_commands) == 3:
self.command1 = self.dungeon.list_of_commands[0]
self.command2 = self.dungeon.list_of_commands[1]
self.command3 = self.dungeon.list_of_commands[2]
elif len(self.dungeon.list_of_commands) == 4:
self.command1 = self.dungeon.list_of_commands[0]
self.command2 = self.dungeon.list_of_commands[1]
self.command3 = self.dungeon.list_of_commands[2]
self.command4 = self.dungeon.list_of_commands[3]
else:
print("wrong command")
self.dungeon.input_comand()
def execute_command(self):
if self.dungeon.list_of_commands[0] == "load_map":
self.dugneon.load_map(self.dungeon.list_of_commands[1])
elif self.dungeon.list_of_commands[0] == "spawn_weapons":
self.dungeon.spawn_all_items()
elif self.dungeon.list_of_commands[0] == "make_ner_hero":
self.dungeon.make_new_hero(self.command2, self.command3, self.command3, self.command4)
elif self.dungeon.list_of_commands[0] == "make_ner_orc":
self.dungeon.make_new_hero(self.command2, self.command3, self.command3, self.command4)
elif self.dungeon.list_of_commands[0] == "move":
self.dungeon.move(self.command2, self.command3)
elif self.dungeon.list_of_commands[0] == "exit":
self.dungeon.ask_to_quit()
else:
print("wrong command")
self.dungeon.input_comand()