本文整理汇总了Python中dungeon.Dungeon.spawn方法的典型用法代码示例。如果您正苦于以下问题:Python Dungeon.spawn方法的具体用法?Python Dungeon.spawn怎么用?Python Dungeon.spawn使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dungeon.Dungeon
的用法示例。
在下文中一共展示了Dungeon.spawn方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestDungeon
# 需要导入模块: from dungeon import Dungeon [as 别名]
# 或者: from dungeon.Dungeon import spawn [as 别名]
class TestDungeon(unittest.TestCase):
def setUp(self):
self.my_map = Dungeon("basic_dungeon.txt")
#def test_map_dungeon(self):
# self.my_map.print_map()
def test_spawn_hero(self):
good_hero = hero.Hero("Sebastian", 100, "The great")
self.assertTrue(self.my_map.spawn("The Great", good_hero))
# self.my_map.print_map()
bad_orc = orc.Orc("Mudagog", 100, 1.4)
self.assertTrue(self.my_map.spawn("The Badass", bad_orc))
# self.my_map.print_map()
def test_move_hero(self):
good_hero = hero.Hero("Sebastian", 100, "The great")
self.assertTrue(self.my_map.spawn("The Great", good_hero))
# self.my_map.print_map()
bad_orc = orc.Orc("Mudagog", 100, 1.4)
self.assertTrue(self.my_map.spawn("The Badass", bad_orc))
# self.my_map.print_map()
self.assertFalse(self.my_map.move("The Great", "up"))
self.assertFalse(self.my_map.move("The Great", "left"))
self.assertTrue(self.my_map.move("The Badass", "up"))
self.assertFalse(self.my_map.move("The Badass", "right"))
self.my_map.print_map()
示例2: main
# 需要导入模块: from dungeon import Dungeon [as 别名]
# 或者: from dungeon.Dungeon import spawn [as 别名]
def main():
my_dungeon = Dungeon("area.txt")
my_dungeon.spawn(my_dungeon.hero)
my_dungeon.hero.learn(Spell("strong", 30, 70, 6))
while my_dungeon.hero.is_alive():
print(my_dungeon.print_map())
command = input("Enter Direction: ")
my_dungeon.move_hero(command)
示例3: test_start_fight_both_alive
# 需要导入模块: from dungeon import Dungeon [as 别名]
# 或者: from dungeon.Dungeon import spawn [as 别名]
def test_start_fight_both_alive(self):
fight_map = Dungeon("fight_map.txt")
blademaster = Orc("Yurnero", 120, 1.2)
fight_map.spawn("player_1", self.paladin)
fight_map.spawn("player_2", blademaster)
fight_map.move("player_1", "right")
fight_map.move("player_2", "left")
fight_map.move("player_1", "right")
self.assertTrue(self.paladin.is_alive())
self.assertTrue(blademaster.is_alive())
示例4: test_moving_around_the_map
# 需要导入模块: from dungeon import Dungeon [as 别名]
# 或者: from dungeon.Dungeon import spawn [as 别名]
def test_moving_around_the_map(self):
dungeon = Dungeon("level2.txt")
dungeon.spawn(self.hero)
dungeon.move_hero("right")
dungeon.move_hero("down")
self.assertFalse(dungeon.move_hero("right"))
dungeon.move_hero("down")
self.assertFalse(dungeon.move_hero("left"))
# dungeon.print_map()
self.assertTrue(dungeon.move_hero("down"))
示例5: test_start_fight_paladin_dead
# 需要导入模块: from dungeon import Dungeon [as 别名]
# 或者: from dungeon.Dungeon import spawn [as 别名]
def test_start_fight_paladin_dead(self):
axe = Weapon("axe", 10, 0.5)
blademaster = Orc("Yurnero", 120, 1.2)
blademaster.equip_weapon(axe)
fight_map = Dungeon("fight_map.txt")
fight_map.spawn("player_1", self.paladin)
fight_map.spawn("player_2", blademaster)
fight_map.move("player_1", "right")
fight_map.move("player_2", "left")
fight_map.move("player_1", "right")
expected_arr = [['.', '.', 'O', '.']]
self.assertFalse(self.paladin.is_alive())
self.assertTrue(blademaster.is_alive())
#testing _make_changes_after_fight
self.assertEqual(fight_map.map_array, expected_arr)
示例6: DungeonTest
# 需要导入模块: from dungeon import Dungeon [as 别名]
# 或者: from dungeon.Dungeon import spawn [as 别名]
class DungeonTest(unittest.TestCase):
"""docstring for DungeonTest"""
def setUp(self):
self.test_m = ['..##......',
'#.##..###.',
'#.###.###.',
'#..S.S###.',
'###.#####.']
test_map_file = open('test_map.txt', 'w')
test_map_file.write('\n'.join(self.test_m))
test_map_file.close()
self.test_map = Dungeon('test_map.txt')
def test_print_map(self):
self.assertEqual('\n'.join(self.test_m), self.test_map.print_map())
def test_spawn(self):
hero = Hero('p1', 100, 'first')
orc = Orc('p2', 100, 2)
self.test_map.spawn('player1', hero)
self.assertEqual('H', self.test_map.print_map()[36])
self.assertEqual('S', self.test_map.print_map()[38])
self.test_map.spawn('player2', orc)
self.assertEqual('O', self.test_map.print_map()[38])
self.assertEqual(False, self.test_map.spawn('player3', orc))
def tearDown(self):
remove('test_map.txt')
def test_move(self):
after_move = ['..##......',
'#.##..###.',
'#.###.###.',
'#...HO###.',
'###.#####.']
hero = Hero('p1', 100, 'first')
orc = Orc('p2', 100, 2)
hero_weapon = Weapon('Axe', 25, 0.1)
hero.equip_weapon(hero_weapon)
orc_weapon = Weapon('Stick', 5, 0.01)
orc.equip_weapon(orc_weapon)
self.test_map.spawn('player1', hero)
self.test_map.spawn('player2', orc)
self.assertTrue(self.test_map.move('player1', 'right'))
self.assertEqual(False, self.test_map.move('player1', 'up'))
self.assertEqual(False, self.test_map.move('player1', 'fuckedUpDirection'))
self.assertEqual('\n'.join(after_move), self.test_map.print_map())
self.test_map.move('player1', 'right')
def test_fight(self):
pass
示例7: test_move_left_and_fight
# 需要导入模块: from dungeon import Dungeon [as 别名]
# 或者: from dungeon.Dungeon import spawn [as 别名]
def test_move_left_and_fight(self):
second_content = 'SS##......\n#.##..###.\n#.###.###.\n#.....###.\n###.#####.'
second_filename = "basic_dungeon1.txt"
second_file = open(second_filename,'w')
second_file.write(second_content)
second_file.close()
second_map = Dungeon(second_filename)
#----------------------------------------------
player1 = Hero("Bron", 10, "DragonSlayer")
player2 = Orc('karakondjul', 100 , 1.5)
second_map.spawn(player1.name, player1)
second_map.spawn(player2.name, player2)
self.assertTrue(second_map.move(player2.name, 'left'))
os.remove(second_filename)
示例8: test_the_game
# 需要导入模块: from dungeon import Dungeon [as 别名]
# 或者: from dungeon.Dungeon import spawn [as 别名]
def test_the_game(self):
h = Hero(name="Bron", title="Dragonslayer",
health=100, mana=100, mana_regeneration_rate=2)
w = Weapon(name="The Axe of Destiny", damage=20)
h.equip(w)
s = Spell(name="Lightning bolt", damage=30, mana_cost=50, cast_range=2)
h.learn(s)
dungeon = Dungeon("level1.txt")
dungeon.spawn(h)
dungeon.move_hero("right")
dungeon.move_hero("down")
dungeon.try_range_attack()
dungeon.move_hero("down")
dungeon.move_hero("down")
dungeon.try_range_attack()
# dungeon.move_hero("right")
# dungeon.move_hero("right")
dungeon.print_map()
示例9: DungeonTest
# 需要导入模块: from dungeon import Dungeon [as 别名]
# 或者: from dungeon.Dungeon import spawn [as 别名]
class DungeonTest(unittest.TestCase):
def setUp(self):
self.new_map = [
['S', '.', '#', '#', '.', '.', '.', '.', '.', 'T'],
['#', 'T', '#', '#', '.', '.', '#', '#', '#', '.'],
['#', '.', '#', '#', '#', 'E', '#', '#', '#', 'E'],
['#', '.', 'E', '.', '.', '.', '#', '#', '#', '.'],
['#', '#', '#', 'T', '#', '#', '#', '#', '#', 'G']
]
self.dungeon = Dungeon(self.new_map)
self.hero = Hero("Floki", "Builder", 100, 100, 2)
def test_create_new_instance(self):
self.assertTrue(isinstance(self.dungeon, Dungeon))
def test_valid_members(self):
self.assertEqual(self.dungeon.map, self.new_map)
self.assertEqual(self.dungeon.spawning_points, [(0, 0)])
self.assertEqual(self.dungeon.hero, None)
self.assertEqual(self.dungeon.hero_x, None)
self.assertEqual(self.dungeon.hero_y, None)
def test_spawn(self):
self.assertTrue(self.dungeon.spawn(self.hero))
self.assertEqual(self.dungeon.hero.health, 100)
self.assertEqual(self.dungeon.hero.mana, 100)
self.assertEqual(self.dungeon.hero_x, 0)
self.assertEqual(self.dungeon.hero_y, 0)
def test_move(self):
self.dungeon.spawn(self.hero)
self.assertFalse(self.dungeon.move_hero("u"))
self.assertIsNone(self.dungeon.move_hero("r"))
self.assertIsNone(self.dungeon.move_hero("d"))
self.assertEqual(self.dungeon.hero_x, 1)
self.assertEqual(self.dungeon.hero_y, 1)
self.assertIsNone(self.dungeon.move_hero("d"))
self.assertEqual(self.dungeon.hero_x, 2)
self.assertEqual(self.dungeon.hero_y, 1)
示例10: TestDungeon
# 需要导入模块: from dungeon import Dungeon [as 别名]
# 或者: from dungeon.Dungeon import spawn [as 别名]
class TestDungeon(unittest.TestCase):
def setUp(self):
self.dungeonMap = Dungeon("basic_dungeon.txt")
def test_init(self):
self.assertEqual(self.dungeonMap.mappath, "basic_dungeon.txt")
def test_print_map(self):
pass
def test_spawn(self):
bron_hero = Hero("Bron", 100, "DragonSyler")
print (self.dungeonMap.spawn("pesho", bron_hero))
示例11: TestDungeon
# 需要导入模块: from dungeon import Dungeon [as 别名]
# 或者: from dungeon.Dungeon import spawn [as 别名]
class TestDungeon(unittest.TestCase):
def setUp(self):
self.filename = "maze.txt"
self.contents = "S.##......\n#.##..###.\n#.###.###.\n#.....###.\n###.#####S"
with open(self.filename, "w+") as f:
f.write(self.contents)
f.close()
self.peshtera = Dungeon("maze.txt")
self.orc = Orc("Ivancho", 100, 1.3)
self.hero = Hero("Petarcho", 100, "Garbage")
def tearDown(self):
os.remove(self.filename)
def test_dungeon_map(self):
with open(self.filename, "r+") as f:
actual = f.read()
f.close()
self.assertEqual(self.contents, actual)
def test_print_map(self):
output = self.peshtera.print_map()
with open(self.filename, "r+") as f:
actual = f.read()
f.close()
self.assertEqual(output, actual)
def test_spawn_character(self):
self.peshtera.spawn("Player One", self.orc)
with open(self.filename, "r+") as f:
actual = f.read()
f.close()
contents = "O.##......\n#.##..###.\n#.###.###.\n#.....###.\n###.#####S"
self.assertEqual(actual, contents)
def test_spawn_already_spawned_character(self):
self.peshtera.spawnlist = {"Player One": self.orc}
result = self.peshtera.spawn("Player One", self.orc)
self.assertEqual("Character is already spawned.", result)
def test_spawn_with_no_free_slots(self):
self.filename = "maze.txt"
self.contents = "..##......\n#.##..###.\n#.###.###.\n#.....###.\n###.#####."
with open(self.filename, "w+") as f:
f.write(self.contents)
f.close()
result = self.peshtera.spawn("One", self.orc)
self.assertEqual("No free spawn slot.", result)
def test_move_to_unknown_given_direction(self):
self.peshtera.spawn("Orc", self.orc)
result = self.peshtera.move("Orc", "test")
self.assertEqual("Wrong direction given.", result)
示例12: start
# 需要导入模块: from dungeon import Dungeon [as 别名]
# 或者: from dungeon.Dungeon import spawn [as 别名]
def start():
print("Welcome. Type 'help' for available commands")
exit = False
created = False
loaded_map = False
while exit is False:
full_command = input('--> ').strip().split(' ')
if full_command[0] == 'load_map':
dungeon = Dungeon(full_command[1])
if dungeon.map:
loaded_map = True
elif full_command[0] == 'show_map' and loaded_map:
print(dungeon.print_map())
elif full_command[0] == 'create_hero' and loaded_map:
my_hero = Hero(
full_command[1], int(full_command[2]), full_command[3])
my_hero.weapon = Weapon('Ashbringer', 40, 0.8)
created = True
elif full_command[0] == 'spawn_hero' and loaded_map:
if created is True:
dungeon.spawn(full_command[1], my_hero)
dungeon.spawn_npcs()
else:
print('No created characters.')
elif full_command[0] == 'move' and loaded_map:
if full_command[1] not in dungeon.ingame:
print ('Player name is incorrect.')
elif not dungeon.ingame[full_command[1]].is_alive():
exit = True
return 'Your character is dead. Game over.'
else:
if dungeon.unlocked:
exit = True
return 'Game Over. You have won.'
print(dungeon.move_player(full_command[1], full_command[2]))
to_move = dungeon.get_random_npc()
if not dungeon.ingame[to_move].is_alive():
dungeon.ingame = {
i: dungeon.ingame[i] for i in dungeon.ingame if i != 0}
dungeon.npcs = {i: dungeon.npcs[i]
for i in dungeon.npcs if i != 0}
else:
print(dungeon.move_npc(to_move))
print(dungeon.print_map())
elif full_command[0] == 'heal':
if created and loaded_map:
my_hero.health = my_hero.max_health
else:
print('No created characters.')
elif full_command[0] == 'known_as':
if created and loaded_map:
print (my_hero.known_as())
else:
print('No created characters.')
elif full_command[0] == 'help':
print(helper)
elif full_command[0] == 'exit':
exit = True
else:
print("Invalid command. Type 'help' for available commands.")
示例13: dungeon_test
# 需要导入模块: from dungeon import Dungeon [as 别名]
# 或者: from dungeon.Dungeon import spawn [as 别名]
class dungeon_test(unittest.TestCase):
def setUp(self):
self.dungeon_map = [
['.', 'S', '#', '#', '.', '.', '.', '.', '.', 'T'],
['#', 'T', '#', '#', '.', '.', '#', '#', '#', '.'],
['#', '.', '#', '#', '#', 'E', '#', '#', '#', 'E'],
['#', '.', 'E', '.', '.', '.', '#', '#', '#', '.'],
['#', '#', '#', 'T', '#', '#', '#', '#', '#', 'G']]
f = open('test_level.txt', 'w')
for line in self.dungeon_map:
f.write(''.join(line) + '\n')
f.close()
self.dungeon = Dungeon("test_level.txt")
def tearDown(self):
os.remove("test_level.txt")
def test_init(self):
self.assertEqual(self.dungeon.dungeon_map, self.dungeon_map)
def test_find_item_coordinates(self):
self.assertEqual(self.dungeon._find_item_coordinates('S'), (0, 1))
self.assertEqual(self.dungeon._find_item_coordinates('G'), (4, 9))
def test_spawn(self):
hero = Unit(100, 100, 5)
self.dungeon.spawn(hero)
self.assertEqual(self.dungeon.dungeon_map[0][1], 'H')
def test_prin_map(self):
map_string = """.S##.....T
#T##..###.
#.###E###E
#.E...###.
###T#####G"""
self.assertEqual(self.dungeon.print_map(), map_string)
def test_move_hero(self):
hero = Unit(100, 100, 5)
self.dungeon.spawn(hero)
self.assertFalse(self.dungeon.move_hero('up'))
self.assertTrue(self.dungeon.move_hero('left'))
# self.assertTrue(self.dungeon.move_hero('rigth'))
self.assertTrue(self.dungeon.move_hero('rigth'))
self.assertTrue(self.dungeon.move_hero('down'))
self.assertTrue(self.dungeon.move_hero('down'))
self.assertEqual(self.dungeon.hero_possition, (2, 1))
self.assertEqual(self.dungeon.dungeon_map[1][1], '.')
self.dungeon_map[0][1] = '.'
self.dungeon_map[1][1] = '.'
self.dungeon_map[2][1] = 'H'
self.assertEqual(self.dungeon.dungeon_map, self.dungeon_map)
def test_found_treasure(self):
hero = Unit(100, 100, 5)
hero.health = 1
hero.mana = 1
self.dungeon.spawn(hero)
self.dungeon._found_treasure('mana')
self.assertGreater(hero.mana, 1)
self.dungeon._found_treasure('health')
self.assertGreater(hero.health, 1)
self.dungeon._found_treasure('weapon')
self.assertNotEqual(hero.weapon, None)
self.dungeon._found_treasure('spell')
self.assertNotEqual(hero.spell, None)
def test_create_enemy(self):
enemy = self.dungeon._create_enemy(100, 100, 100)
self.assertEqual(enemy.health, 100)
self.assertEqual(enemy.mana, 100)
self.assertEqual(enemy.damage, 100)
def test_find_enemy(self):
self.dungeon.hero_possition = (3, 3)
self.dungeon.dungeon_map[3][3] = 'H'
def test_hero_attack(self):
hero = Unit(100, 100, 5)
hero.learn(Spell('a', 10, 10, 2))
self.dungeon.spawn(hero)
self.dungeon.move_hero('down')
self.dungeon.move_hero('down')
self.assertEqual('Nothing in casting range 2',
self.dungeon.hero_attack(by="spell"))
self.dungeon.move_hero('down')
self.assertIsInstance(self.dungeon.hero_attack(by='spell'), Fight)
示例14: TestDungeon
# 需要导入模块: from dungeon import Dungeon [as 别名]
# 或者: from dungeon.Dungeon import spawn [as 别名]
class TestDungeon(unittest.TestCase):
def setUp(self):
self.start_map_content = 'S.##......\n#.##..###.\n#.###.###.\n#.....###.\n###.#####S'
with open('basic_dungeon.txt', 'w') as map_file:
map_file.write(self.start_map_content)
self.map = Dungeon('basic_dungeon.txt')
self.hero_player = Hero('Stoyan', 100, 'DeathBringer')
self.hero_player.equip_weapon(Weapon('Sword', 20, 0.5))
self.orc_player = Orc('Broks', 100, 1.5)
self.orc_player.equip_weapon(Weapon('Stick', 10, 0.1))
self.battle_map_content = 'S.#\n#S#'
with open('battle_map.txt', 'w') as map_file:
map_file.write(self.battle_map_content)
self.battle_map = Dungeon('battle_map.txt')
self.battle_map.spawn('human', self.hero_player)
self.battle_map.spawn('orc', self.orc_player)
def tearDown(self):
remove(self.map.filename)
remove('battle_map.txt')
def test_dungeon_init(self):
self.assertEqual(self.map.filename, 'basic_dungeon.txt')
def test_print_map(self):
self.assertEqual(self.map.print_map(), self.start_map_content)
def test_first_spawn_succesful(self):
self.assertTrue(self.map.spawn('player_1', self.hero_player))
def is_map_changed_after_spawning_entity(self, entity, symbol):
self.map.spawn('player', entity)
with open(self.map.filename, 'r') as map_file:
map_content_after_spawn = map_file.read()
return map_content_after_spawn == self.start_map_content.replace(
'S', symbol, 1)
def test_map_changed_after_spawning_hero(self):
self.assertTrue(
self.is_map_changed_after_spawning_entity(self.hero_player, 'H'))
def test_map_changed_after_spawning_orc(self):
self.assertTrue(
self.is_map_changed_after_spawning_entity(self.orc_player, 'O'))
def test_spawn_unsuccesful_on_full_map(self):
self.map.spawn('player1', self.hero_player)
self.map.spawn('player2', self.orc_player)
self.assertFalse(self.map.spawn('player3', self.orc_player))
def test_correct_move(self):
self.map.spawn('player1', self.hero_player)
self.assertTrue(self.map.move('player1', 'right'))
def test_out_of_map_move(self):
self.map.spawn('player1', self.hero_player)
self.assertFalse(self.map.move('player1', 'left'))
def test_move_through_obstacle(self):
self.map.spawn('player1', self.hero_player)
self.assertFalse(self.map.move('player1', 'down'))
def test_move_reflected_on_map(self):
self.map.spawn('player1', self.hero_player)
self.map.spawn('player2', self.orc_player)
self.map.move('player1', 'right')
self.map.move('player1', 'down')
self.map.move('player2', 'up')
self.map.move('player2', 'up')
self.assertEqual(
self.map.print_map(),
'..##......\n#H##..###.\n#.###.###O\n#.....###.\n###.#####.'
)
def test_move_into_battle(self):
self.battle_map.move('human', 'right')
self.battle_map.move('orc', 'up')
player_dead = not self.hero_player.is_alive(
) or not self.orc_player.is_alive()
self.assertTrue(player_dead)
def test_map_accurate_after_battle(self):
self.battle_map.move('human', 'right')
self.battle_map.move('orc', 'up')
self.assertEqual(self.battle_map.print_map(), '.H#\n#.#')
示例15: CommandLineInterface
# 需要导入模块: from dungeon import Dungeon [as 别名]
# 或者: from dungeon.Dungeon import spawn [as 别名]
class CommandLineInterface():
def __init__(self):
self.command_list = {
"show_map": self.display_map,
"create_hero": self.create_character,
"known_as": self.known_as,
"spawn_hero": self.spawn_character,
"move": self.move_character,
"heal": self.heal_character,
"help": self.help_message,
"exit": self.exit_game
}
self.exit = False
self.is_created = False
self.map_loaded = False
self.validator = None
self.input = None
self.help = """show_map - Displays current dungeon map
create_hero <Name> <Health> <Nickname> - Creates hero with entered attributes
spawn_hero <Username> - Spawns hero with specified username into the dungeon
known_as - Displays name and nickname of hero
move <username> <direction> - Moves the hero in the specified direction
help - Displays this message
exit - Closes the program"""
def start_game(self):
print("Welcome. Type 'help' for available commands")
while self.exit is False:
command = self.get_input_command()
if command[0] in self.command_list:
execute = self.command_list[command[0]]
output = execute()
print(output) if output is not None else None
else:
print("Invalid command. Type 'help' for available commands.")
def get_input_command(self):
self.input = input("--> ").strip().split(" ")
return self.input
def load_map(self, map_path):
self.validator = MapValidator(map_path)
if not self.validator.validate_map():
return self.validator.generate_message()
else:
self.dungeon = Dungeon(map_path)
if self.dungeon.map:
self.map_loaded = True
return self.validator.generate_message()
def display_map(self):
return self.dungeon.print_map()
def create_character(self):
self.char = Hero(self.input[1], int(self.input[2]), self.input[3])
self.char.weapon = Weapon("Ashbringer", 40, 0.8)
self.is_created = True
def known_as(self):
return self.char.known_as()
def spawn_character(self):
if self.is_created is True:
self.dungeon.spawn(self.input[1], self.char)
self.dungeon.spawn_npcs()
else:
return "No created characters."
def move_character(self):
if self.input[1] not in self.dungeon.ingame:
return "Player name is incorrect."
elif not self.dungeon.ingame[self.input[1]].is_alive():
self.exit = True
return "Your character is dead. Game over."
else:
if self.dungeon.unlocked:
self.exit = True
return "Game Over. You have won."
return self.dungeon.move_player(self.input[1], self.input[2])
to_move = self.dungeon.get_random_npc()
if not self.dungeon.ingame[to_move].is_alive():
self.dungeon.ingame = {i: self.dungeon.ingame[i]
for i in self.dungeon.ingame
if i != 0}
self.dungeon.npcs = {i: self.dungeon.npcs[i]
for i in self.dungeon.npcs if i != 0}
else:
return self.dungeon.move_npc(to_move)
return self.display_map()
def heal_character(self):
if self.is_created and self.map_loaded:
self.char.health = self.char.max_health
else:
return "No created characters."
def help_message(self):
return self.help
#.........这里部分代码省略.........