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: ')
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 test_check_for_enemy_down(self): d = Dungeon('map.txt', 'basic_loot_list_example.json') d.spawn(Hero("nz", "nz", 200, 120, 2)) d.hero.learn(Spell("idk", 20, 20, 2)) d.hero_place = [0, 5] d.change_map("H") result = d.check_for_enemy("down") self.assertEqual(result, 2)
def test_move_hero_(self): d = Dungeon('map.txt', 'basic_loot_list_example.json') d.spawn(Hero("nz", "nz", 200, 120, 2)) d.move_hero("right") d.move_hero("down") d.move_hero("right") d.move_hero("down") self.assertEqual(d.hero_place, [2, 1])
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!")
class MyGame(arcade.Window): def __init__(self, width, height, title): super().__init__(width, height, title) self.dungeon = None self.hero = None self.left_pressed = False self.right_pressed = False self.up_pressed = False self.down_pressed = False self.move_char = (".", [0, 0]) self.fight = Fight() arcade.set_background_color((59, 68, 75)) def setup(self): self.dungeon = Dungeon("map.txt") self.hero = Hero("Geralt", "white wolf", 150, 150, 5) self.hero.equip(Weapon("Sword", 30)) self.hero.learn(Spell("wolf's attack", 20, 20, 2)) self.dungeon.spawn(self.hero) self.command = None def on_draw(self): arcade.start_render() x = self.dungeon.borders[1] * 20 + LEFT_MARGIN - 20 y = 260 arcade.draw_rectangle_filled(x, y, 410, 410, (135, 169, 107)) arcade.draw_rectangle_outline(x, y, 420, 420, (105, 53, 156), 10) arcade.draw_text("To move pres the keybord arrows.\nTo attack first you should choose\ndirection. Press on of the keys:\n 'u' for up, 'd' for down,\n 'l' for left, 'r' for right\n......................................",\ x - 200, y + 100, (55, 3, 106), 14) # Prints the map for row in range(self.dungeon.borders[0]): # Loop for each column for column in range(self.dungeon.borders[1]): # Calculate our location x = column * COLUMN_SPACING + LEFT_MARGIN y = (self.dungeon.borders[0] - row - 1) * ROW_SPACING + BOTTOM_MARGIN # Draw the item if (self.dungeon.matrix[row][column] == "#"): arcade.draw_rectangle_filled(x, y, 30, 30, (191, 79, 81)) elif (self.dungeon.matrix[row][column] == "-"): if row == 0 or row == self.dungeon.borders[0] - 1: if column == 0 or column == self.dungeon.borders[1] - 1: arcade.draw_rectangle_filled( x, y, 30, 30, (59, 68, 75)) else: arcade.draw_rectangle_filled( x, y, 50, 30, (59, 68, 75)) if column == 0 or column == self.dungeon.borders[1] - 1: if row == 0 or row == self.dungeon.borders[0] - 1: arcade.draw_rectangle_filled( x, y, 30, 30, (59, 68, 75)) else: arcade.draw_rectangle_filled( x, y, 30, 50, (59, 68, 75)) elif (self.dungeon.matrix[row][column] == "H"): arcade.draw_rectangle_filled(x, y, 30, 30, (79, 134, 247)) arcade.draw_text("H", x - 10, y - 20, arcade.color.BLUE, 28) elif (self.dungeon.matrix[row][column] == "E"): arcade.draw_rectangle_filled(x, y, 30, 30, (255, 117, 24)) arcade.draw_text("E", x - 10, y - 20, arcade.color.RED, 28) elif (self.dungeon.matrix[row][column] == "T"): arcade.draw_rectangle_filled(x, y, 30, 30, (150, 120, 182)) arcade.draw_text("T", x - 10, y - 20, (55, 3, 106), 28) elif (self.dungeon.matrix[row][column] == "G"): arcade.draw_rectangle_filled(x, y, 30, 30, (153, 230, 179)) arcade.draw_text("G", x - 10, y - 20, (0, 130, 127), 28) else: arcade.draw_rectangle_filled(x, y, 30, 30, (135, 169, 107)) x = self.dungeon.borders[1] * 20 + LEFT_MARGIN - 20 y = self.dungeon.borders[1] * 10 + BOTTOM_MARGIN arcade.draw_rectangle_outline(x, y, 420, 220, (105, 53, 156), 10) def on_update(self, delta_time): """ if self.up_pressed and not self.down_pressed: self.dungeon.move_hero("up") elif self.down_pressed and not self.up_pressed: self.dungeon.move_hero("down") if self.left_pressed and not self.right_pressed: self.dungeon.move_hero("left") elif self.right_pressed and not self.left_pressed: self.dungeon.move_hero("left") """ def on_key_press(self, key, modifiers): """Called whenever a key is pressed. """ if key == arcade.key.UP: self.move_char = self.dungeon.move_hero("up") print("HERREEEEERERERER") #, self.move_hero) self.check_move() #self.up_pressed = True elif key == arcade.key.DOWN: self.move_char = self.dungeon.move_hero("down") self.check_move() #self.down_pressed = True elif key == arcade.key.LEFT: self.move_char = self.dungeon.move_hero("left") self.check_move() # self.left_pressed = True elif key == arcade.key.RIGHT: self.move_char = self.dungeon.move_hero("right") self.check_move() # self.right_pressed = True elif key == arcade.key.NUM_1 or key == arcade.key.KEY_1: self.command = self.fight.get_hero_command(1) elif key == arcade.key.NUM_2 or key == arcade.key.KEY_2: print('hhhhhhh') self.command = self.fight.get_hero_command(2) print(self.command) elif key == arcade.key.NUM_3 or key == arcade.key.KEY_3: self.command = self.fight.get_hero_command(3) elif key == arcade.key.NUM_4 or key == arcade.key.KEY_4: self.command = self.fight.get_hero_command(4) def on_key_release(self, key, modifiers): """Called when the user releases a key. """ if key == arcade.key.UP: self.up_pressed = False elif key == arcade.key.DOWN: self.down_pressed = False elif key == arcade.key.LEFT: self.left_pressed = False elif key == arcade.key.RIGHT: self.right_pressed = False def check_move(self): print("nznznz", self.move_char) if self.move_char[0] == "E": print("WTF???") #fight = Fight() self.dungeon.hero = self.battle(1) if self.dungeon.hero.is_alive(): self.dungeon.change_map(".") self.dungeon.hero_place[self.move_char[1]] = self.move_char[2] self.dungeon.change_map("H") else: return "Hero made move." def battle(self, distance): enemy = self.dungeon.enemies.pop() self.fight.start_fight(self.dungeon.hero, enemy, distance) while self.dungeon.hero.is_alive() and enemy.is_alive(): # the desition of the player # self.fight.distance = distance if self.dungeon.hero.is_alive(): print(self.command) if self.command is not None: if enemy.is_alive(): print(self.fight.hero_attack()) if not enemy.is_alive(): print("Enemy is dead.") if self.enemy.is_alive(): if self.hero.is_alive(): print(self.fight.enemy_attack()) if not self.dungeon.hero.is_alive(): print("Hero is dead") return self.hero
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()
class Test_dungeon(unittest.TestCase): def setUp(self): path = open("path.txt", "w") self.dung_map = """S.##...... #.##..### #.###.###. #.....### ###.####S""" path.write(self.dung_map) path.close() self.dungeon = Dungeon("path.txt") self.bron = Hero("Bron", 100, "DragonSlayer") self.orc = Orc("orc", 90, 1.4) self.bron.equip_weapon(Weapon("Mighty Axe", 25, 0.2)) self.orc.equip_weapon(Weapon("basher", 16, 0.75)) def test_dungeon_init(self): self.assertEqual(self.dungeon.map, self.dung_map) def test_spawn_hero(self): self.assertTrue(self.dungeon.spawn("hero", self.bron)) self.dung_map = """H.##...... #.##..### #.###.###. #.....### ###.####S""" self.assertEqual(self.dungeon.map, self.dung_map) def test_spawn_hero_and_orc(self): self.assertTrue(self.dungeon.spawn("hero", self.bron)) self.assertTrue(self.dungeon.spawn("orc", self.orc)) self.dung_map = """H.##...... #.##..### #.###.###. #.....### ###.####O""" self.assertEqual(self.dungeon.map, self.dung_map) def test_spawn_no_free_start_postion(self): self.dungeon.spawn("hero", self.bron) self.dungeon.spawn("hero2", self.bron) self.assertFalse(self.dungeon.spawn("heroo", self.orc)) def test_move_possible(self): self.dungeon.spawn("hero", self.bron) self.assertTrue(self.dungeon.move("hero", "right")) self.dung_map = """.H##...... #.##..### #.###.###. #.....### ###.####S""" self.assertEqual(self.dungeon.map, self.dung_map) def test_move_imposible(self): self.dungeon.spawn("hero", self.bron) self.assertFalse(self.dungeon.move("hero", "left")) self.dung_map = """H.##...... #.##..### #.###.###. #.....### ###.####S""" self.assertEqual(self.dungeon.map, self.dung_map) def test_move_fight(self): self.dungeon.map = """SS##...... #.##..### #.###.###. #.....### ###.####S""" self.dungeon.spawn("hero", self.bron) self.dungeon.spawn("orc", self.orc) self.dung_map = """.O##...... #.##..### #.###.###. #.....### ###.####S""" self.dungeon.move("hero", "right") self.assertEqual(self.dungeon.map, self.dung_map) def tearDown(self): os.remove("path.txt")
def test_move_hero_wrong_direction(self): d = Dungeon('map.txt', 'basic_loot_list_example.json') d.spawn(Hero("nz", "nz", 200, 120, 2)) self.assertFalse(d.move_hero("up"))
class Test_dungeon(unittest.TestCase): def setUp(self): path = open("path.txt", "w") self.dung_map = """S.##...... #.##..### #.###.###. #.....### ###.####S""" path.write(self.dung_map) path.close() self.dungeon = Dungeon("path.txt") self.bron = Hero("Bron", 100, "DragonSlayer") self.orc = Orc("orc", 90, 1.4) self.bron.equip_weapon(Weapon("Mighty Axe", 25, 0.2)) self.orc.equip_weapon(Weapon("basher", 16, 0.75)) def test_dungeon_init(self): self.assertEqual(self.dungeon.map, self.dung_map) def test_spawn_hero(self): self.assertTrue(self.dungeon.spawn("hero", self.bron)) self.dung_map = """H.##...... #.##..### #.###.###. #.....### ###.####S""" self.assertEqual(self.dungeon.map, self.dung_map) def test_spawn_hero_and_orc(self): self.assertTrue(self.dungeon.spawn("hero", self.bron)) self.assertTrue(self.dungeon.spawn("orc", self.orc)) self.dung_map = """H.##...... #.##..### #.###.###. #.....### ###.####O""" self.assertEqual(self.dungeon.map, self.dung_map) def test_spawn_no_free_start_postion(self): self.dungeon.spawn("hero", self.bron) self.dungeon.spawn("hero2", self.bron) self.assertFalse(self.dungeon.spawn("heroo", self.orc)) def test_move_possible(self): self.dungeon.spawn("hero", self.bron) self.assertTrue(self.dungeon.move("hero", "right")) self.dung_map = """.H##...... #.##..### #.###.###. #.....### ###.####S""" self.assertEqual(self.dungeon.map, self.dung_map) def test_move_imposible(self): self.dungeon.spawn("hero", self.bron) self.assertFalse(self.dungeon.move("hero", "left")) self.dung_map = """H.##...... #.##..### #.###.###. #.....### ###.####S""" self.assertEqual(self.dungeon.map, self.dung_map) def test_move_fight(self): self.dungeon.map = """SS##...... #.##..### #.###.###. #.....### ###.####S""" self.dungeon.spawn("hero", self.bron) self.dungeon.spawn("orc", self.orc) self.dung_map = """.O##...... #.##..### #.###.###. #.....### ###.####S""" self.dungeon.move("hero", "right") self.assertEqual(self.dungeon.map, self.dung_map) def tearDown(self): os.remove("path.txt")
class TestDungeon(unittest.TestCase): def setUp(self): self.my_hero = Hero("Bron", "Dragonslayer", 100, 50, 2) self.my_map = [['S', '.', '#', '#', '.', '.', '.', '.', '.', 'T'], ['#', 'T', '#', '#', '.', '.', '#', '#', '#', '.'], ['#', '.', '#', '#', '#', 'E', '#', '#', '#', 'E'], ['#', '.', 'E', '.', '.', '.', '#', '#', '#', '.'], ['#', '#', '#', 'T', '#', '#', '#', '#', '#', 'G']] self.my_dungeon = Dungeon(self.my_hero, self.my_map) def test_map_reading(self): self.assertEqual(Dungeon.map_reading("game_map.txt"), self.my_map) def test_initialisation(self): self.assertTrue(isinstance(self.my_dungeon, Dungeon)) def test_spawn(self): with self.assertRaises(NotAHero): self.my_dungeon.spawn(10) self.my_dungeon.map_reading("game_map.txt") self.assertTrue(self.my_dungeon.spawn(self.my_hero)) self.assertEqual(self.my_dungeon.hero_position_x, 0) self.assertEqual(self.my_dungeon.hero_position_y, 0) def test_move_hero_falses(self): self.my_dungeon.map_reading("game_map.txt") self.my_dungeon.spawn(self.my_hero) self.assertFalse(self.my_dungeon.move_hero("up")) self.assertFalse(self.my_dungeon.move_hero("left")) self.my_dungeon.hero_position_x = 4 self.my_dungeon.hero_position_y = 9 self.assertFalse(self.my_dungeon.move_hero("down")) self.assertFalse(self.my_dungeon.move_hero("right")) def test_move_hero(self): self.my_dungeon.map_reading("game_map.txt") self.my_dungeon.hero_position_y = 1 self.my_dungeon.hero_position_x = 0 self.assertTrue(self.my_dungeon.move_hero("left")) self.my_dungeon.hero_position_x = 1 self.my_dungeon.hero_position_y = 1 self.assertTrue(self.my_dungeon.move_hero("up")) self.my_dungeon.hero_position_x = 0 self.my_dungeon.hero_position_y = 1 self.assertTrue(self.my_dungeon.move_hero("down")) def test_changing_pos(self): self.my_dungeon.changing_pos_func(0, 1) self.assertEqual(self.my_dungeon.hero_position_x, 0) self.assertEqual(self.my_dungeon.hero_position_y, 1) def test_pick_treasure(self): my_dict = Dungeon.load_treasure_file("treasures.json") x = self.my_dungeon.pick_treasure(my_dict) x = x[:15] needed = "Bron's treasure" self.assertEqual(x, needed) def test_print_map(self): pass