def main(): hero_name = input("Enter hero's name: ") hero_title = input("Enter hero's title: ") hero = Hero(hero_name, hero_title, 150, 150, 5) hero.equip(Weapon("Sword", 30)) hero.learn(Spell("KillALL", 35, 30, 3)) dungeon = Dungeon("map.txt", "basic_loot_list_example.json") dungeon.spawn(hero) dict_commands = {"mu": "up", "md": "down", "ml": "left", "mr": "right", "au": "up", "ad": "down", "al": "left", "ar": "right", "h": "help"} dict_commands_move = {"mu": "up", "md": "down", "ml": "left", "mr": "right"} dict_commands_attack = {"au": "up", "ad": "down", "al": "left", "ar": "right"} index_of_hero = [1, 1] print_commands() while not dungeon.end_of_game: dungeon.print_map() print() player_input = "" while player_input not in dict_commands.keys(): player_input = str(input(">>> ")) if player_input == "h": print_commands() if player_input in dict_commands_move.keys(): dungeon.move_hero(dict_commands_move[player_input]) if player_input in dict_commands_attack.keys(): dungeon.hero_atack(dict_commands_attack[player_input]) if dungeon.end_of_game: break
def level_readers(path): print("----Welcome to Dungeons and Pythons!!!----") print("Move your hero with a, w, s, d.") print("Press X to exit.") name = input("Enter name for your hero: ") title = input("Enter your hero's nickname: ") h = Hero(name, title, health=100, mana=100, mana_regeneration_rate=2) w = Weapon(name='The Axe of Destiny', damage=10) h.equip(w) #there is no hero without weapon. Start game with some levels = os.listdir(path) levels.sort() cls() print_logo() sleep(3) #wait for 3 seconds for idx, lvl in enumerate(levels): map = Dungeon(path + '/' + lvl, h, idx + 1) map.spawn() key = '' exited = False legit_keys = {'a', 'w', 's', 'd', 'x'} while map.hero.is_alive(): #does every move print('level {}'.format(idx + 1)) map.print_map() print("||||HEALTH: {}||||".format(map.hero.get_health())) print("||||MANA: {}||||\n".format(map.hero.get_mana())) key = getch() cls() key = key.lower() if key in legit_keys: if key == 'a': map.move_hero('left') if key == 'd': map.move_hero('right') if key == 's': map.move_hero('down') if key == 'w': map.move_hero('up') if key == 'x': exited = True break else: print("Invalid command") if map.endpoint == (map.hero_position_X, map.hero_position_Y): break #we're done here, go to next level if exited: break if map.hero.is_alive() and not exited: print("Congratulations! You win!") else: print("Bye!")
def lets_play(): command_list = [{ 'w': 'up', 's': 'down', 'a': 'left', 'd': 'right' }, 'f', 'spawn', 'status', 'u', 'p'] print('\nLets play a game!\n ') name = input('Write a name for your hero: ') title = input('How is your hero known as: ') h = Hero(name=name, title=title, health=100, mana=100, mana_regeneration_rate=2) name = input('Give a name to your weapon: ') w = Weapon(name=name, damage=20) h.equip(w) print('\n{} will fight with {} to save The Princess!\n'.format( h.known_as(), w.name)) input('Press enter to start the game!') map = Dungeon("level1.txt") map.spawn(h) map.print_map() input_data = input( "\nType w/s/a/d to move your hero up/down/left/right\ , f for fight, spawn for spawn, status to show hero's status, u for update spell/weapon or e for exitd: " ) while (input_data != 'e'): if input_data in command_list[0].keys(): map.move_hero(command_list[0][input_data]) map.print_map() elif input_data == command_list[1]: map.hero_attack() map.print_map() elif input_data == command_list[2]: map.spawn(h) map.print_map() elif input_data == command_list[3]: print('\n', h, h.weapon, h.spell) elif input_data == command_list[4]: which = input('Type spell/weapon: ') print( 'Are you sure. You have {} and the update is 20. Type y or n:' .format(map.hero.money)) result = input() if result == 'y': map.hero.update(which) else: print('Update not successful') elif input_data == 'm': print("\nType w/s/a/d to move your hero up/down/left/right\ , f for fight, spawn for spawn, status to show hero's status, u for update spell/weapon or e for exit" ) else: print('Comming soon!') print('Type m to see the curret command menu.') print('\n') input_data = input('Command: ')
class Test_Dungeon(unittest.TestCase): def setUp(self): self.map = Dungeon("level1.txt") self.h = Hero(name="Bron", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=2) def test_init(self): self.assertEqual(self.map.get_map(), [['S', '.', '#', '#', '.', '.', 'S', '.', '.', 'T'], ['#', 'T', '#', '#', '.', '.', '#', '#', '#', '.'], [ '#', '.', '#', '#', '#', 'E', '#', '#', '#', 'E'], ['#', '.', 'E', '.', '.', '.', '#', '#', '#', '.'], ['#', '#', '#', 'T', '#', '#', '#', '#', '#', 'G']]) self.assertEqual(self.map.get_enemies(), [[2, 5], [2, 9], [3, 2]]) def test_spawn(self): self.map.spawn(self.h) self.assertEqual(self.h.mana, self.h.starting_mana) self.assertEqual(self.h.health, self.h.starting_health) self.assertEqual(self.map.get_hero_x(), 0) self.assertEqual(self.map.get_hero_y(), 0) self.assertTrue(self.map.spawn(self.h)) def test_move(self): self.map.spawn(self.h) self.h.take_damage(90) self.assertFalse(self.map.move_hero(self.h, 'up')) self.assertTrue(self.map.move_hero(self.h, 'right')) with self.assertRaises(Wrong_direction): self.assertTrue(self.map.move_hero(self.h, 'hihihi')) self.assertEqual(1, self.map.get_hero_x()) self.assertEqual(0, self.map.get_hero_y()) self.assertFalse(self.map.move_hero(self.h, 'right')) self.assertEqual(1, self.map.get_hero_x()) self.assertEqual(0, self.map.get_hero_y()) # self.map.move_hero(self.h, 'down') def test_spell_or_weapon(self): self.h.equip(Weapon(name="The Axe of Destiny", damage=20)) self.h.learn( Spell(name="Fireball", damage=30, mana_cost=50, cast_range=2)) self.assertTrue(isinstance(self.map.spell_or_weapon(self.h), Spell)) self.h.equip(Weapon(name="The Axe of Destiny", damage=120)) self.assertTrue(isinstance(self.map.spell_or_weapon(self.h), Weapon)) def test_hero_attack(self): self.h.equip(Weapon(name="The Axe of Destiny", damage=20)) self.h.learn( Spell(name="Fireball", damage=30, mana_cost=50, cast_range=2)) self.map.spawn(self.h) self.map.print_map() self.map.move_hero(self.h, 'right') self.map.print_map() self.map.move_hero(self.h, 'down') self.map.print_map() self.map.move_hero(self.h, 'down') self.map.print_map() self.map.move_hero(self.h, 'down') self.map.print_map() # self.assertEqual(self.map.move_hero(self.h, 'right'), True) self.map.hero_attack(self.h) self.map.print_map() for x in range(5): self.map.move_hero(self.h, 'right') self.map.print_map() self.map.move_hero(self.h, 'up') self.map.print_map() self.map.spawn(self.h) self.map.print_map() self.map.move_hero(self.h, 'left') self.map.move_hero(self.h, 'down') self.map.move_hero(self.h, 'down') self.map.print_map()