def test_equip(self): self.assertEqual(self.h.weapon, None) w = Weapon(name="w", damage=20) self.h.equip(w) self.assertEqual(self.h.weapon.damage, 20) self.assertEqual(self.h.weapon.name, "w") w1 = Weapon(name="w1", damage=30) self.h.equip(w1) self.assertEqual(self.h.weapon.damage, 30) self.assertEqual(self.h.weapon.name, "w1") w2 = Weapon(name="w2", damage=10) self.h.equip(w2) self.assertEqual(self.h.weapon.damage, 30) self.assertEqual(self.h.weapon.name, "w1")
def pick_treasure(self): t = [] with open("loot.txt", 'r') as f: for i in f.readlines(): t.append(i) treasure = t[randint(0, len(t) - 1)].split(",") if treasure[0] == "weapon": tmp = float(treasure[2]) if '.' in treasure[2] else int( treasure[2]) tmp_w = Weapon(name=treasure[1], damage=tmp) self.inst.equip(tmp_w) tr = f"the weapon \"{tmp_w.name}\" with {tmp_w.get_damage()} dmg" elif treasure[0] == "spell": tmp = float(treasure[2]) if '.' in treasure[2] else int( treasure[2]) tmp1 = float(treasure[3]) if '.' in treasure[3] else int( treasure[3]) tmp_s = Spell(name=treasure[1], damage=tmp, mana_cost=tmp1, cast_range=int(treasure[4])) self.inst.learn(tmp_s) tr = f"the spell \"{tmp_s.name}\" with {tmp_s.get_damage()} dmg, {tmp_s.get_mana_cost()} mana cost and range {tmp_s.get_cast_range()}" else: if isinstance(self.inst, Hero): if treasure[0] == "Mana potion": self.inst.take_mana(int(treasure[1])) tr = f"Mana potion with {int(treasure[1])} mana" elif treasure[0] == "Health potion": self.inst.take_healing(int(treasure[1])) tr = f"Health potion with {int(treasure[1])} health" elif isinstance(self.inst, Enemy): print("The enemy can\'t heal themself.") return tr
class TestWeapon(unittest.TestCase): def setUp(self): self.w1 = Weapon(name="The Axe of Destiny", damage=20) self.w2 = Weapon(name="The Axe", damage=20.5) def test_raise_type_error_init(self): with self.assertRaises(TypeError): self.w3 = Weapon(name="The destroyer", damage="22.5") def test_get_damage(self): self.assertEqual(self.w1.get_damage(), 20) self.assertEqual(self.w2.get_damage(), 20.5) def test_get_name(self): self.assertEqual(self.w1.get_name(), "The Axe of Destiny") self.assertEqual(self.w2.get_name(), "The Axe")
def main(): start_screen() map = get_map_names() map.reverse() print_map_names(map) m = input(f"Now pick a level: ") m = f'{m}.txt' while m not in get_map_names(): m = input(f"Wrong file name, pick again : ") m = f'{m}.txt' give_map = f"Maps/{m}" map.remove(m) global d d = Dungeon(give_map) d.spawn(h) print("Your hero is \'H\' and where you see \'E\' there is an enemy. If you reach \'T\' treasure awaits you.") w = Weapon(name="The Axe of Destiny", damage=20) s = Spell(name="Fireball", damage=40, mana_cost=80, cast_range=3) h.equip(w) h.learn(s) d.print_map() while True: if d.cleared == True: das = input('Go to next ? y/n ') if len(map) == 0: println('You won the game') elif das == 'y': hero = d.hero d = Dungeon('Maps/' + map[0]) d.spawn(hero) map.remove(map[0]) d.print_map() else: print('Game finished') break do_you_wanna_start_a_fight() if h.is_alive(): dir = get_right_direction() d.move_hero(dir) else: h.health = h.max_health h.mana = h.max_mana if d.spawn(h): inp = input("You died do you want to respawn? (y/n) ") if inp == "y": d.print_map() elif inp == "n": break else: print("You lost the game Q_Q") break
def test_attack(self): self.assertEqual(self.h.attack(by="weapon"), 0) self.assertEqual(self.h.attack(by="spell"), 0) w = Weapon(name="w", damage=20) self.h.equip(w) self.assertEqual(self.h.attack(by="weapon"), 20) s = Spell(name="s", damage=30, mana_cost=10, cast_range=2) self.h.learn(s) self.assertEqual(self.h.attack(by="spell"), 30) self.assertEqual(self.h.mana, 90) s1 = Spell(name="s1", damage=30, mana_cost=110, cast_range=2) self.h.learn(s1) self.assertEqual(self.h.attack(by="spell"), 0) self.assertEqual(self.h.mana, 90)
def generate_enemy(self): ran = randint(0, 1) enemy = Enemy(health=random.randint(80, 100), mana=random.randint(50, 80), damage=random.randint(20, 40)) if ran == 1: enemy.equip(Weapon(name='Base Sword', damage=randint(40, 60))) else: enemy.learn( Spell(name='Dark Magic', damage=randint(40, 60), mana_cost=randint(10, 20), cast_range=2)) return enemy
def setUp(self): self.w1 = Weapon(name="The Axe of Destiny", damage=20) self.w2 = Weapon(name="The Axe", damage=20.5)
def test_raise_type_error_init(self): with self.assertRaises(TypeError): self.w3 = Weapon(name="The destroyer", damage="22.5")
def setUp(self): self.enemy = Enemy(health=50, mana=56, damage=50.0) self.enemy.learn(Spell(name='light', damage=52, mana_cost=23, cast_range=2)) self.enemy.equip(Weapon(name='ligh Axe', damage=66))