def test_can_cast(self): s = Spell(name="s", damage=40, mana_cost=120, cast_range=2) self.h.learn(s) self.assertFalse(self.h.can_cast()) s1 = Spell(name="s1", damage=40, mana_cost=20, cast_range=2) self.h.learn(s1) self.assertTrue(self.h.can_cast())
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 test_learn(self): self.assertEqual(self.h.weapon, None) s = Spell(name="s", damage=30, mana_cost=50, cast_range=2) self.h.learn(s) self.assertEqual(self.h.spell.damage, 30) self.assertEqual(self.h.spell.name, "s") s1 = Spell(name="s1", damage=40, mana_cost=50, cast_range=2) self.h.learn(s1) self.assertEqual(self.h.spell.damage, 40) self.assertEqual(self.h.spell.name, "s1") s2 = Spell(name="s1", damage=20, mana_cost=50, cast_range=2) self.h.learn(s2) self.assertEqual(self.h.spell.damage, 40) self.assertEqual(self.h.spell.name, "s1")
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
def test_attack(self): self.assertRaises(AssertionError, self.enemy.attack, by=55) self.assertEqual(52, self.enemy.attack(by='spell')) self.assertEqual(66, self.enemy.attack(by='weapon')) self.enemy.learn(Spell(name='light', damage=99, mana_cost=150, cast_range=2)) self.assertRaises(ValueError, self.enemy.attack, by='spell')
class TestSpell(unittest.TestCase): def setUp(self): self.s1 = Spell(name="Fireball", damage=30, mana_cost=50, cast_range=2) self.s2 = Spell(name="Lightning", damage=30.5, mana_cost=50.5, cast_range=2) def test_raise_type_error_cast_range(self): with self.assertRaises(TypeError): self.s3 = Spell(name="Lightning", damage=30.5, mana_cost=50.5, cast_range=2.3) def test_raise_type_error_damage(self): with self.assertRaises(TypeError): self.s3 = Spell(name="Lightning", damage="jjj", mana_cost=50.5, cast_range=2) def test_raise_type_error_mana_cost(self): with self.assertRaises(TypeError): self.s3 = Spell(name="Lightning", damage=50, mana_cost="ss", cast_range=2) def test_get_name(self): self.assertEqual(self.s1.get_name(), "Fireball") self.assertEqual(self.s2.get_name(), "Lightning") def test_get_damage(self): self.assertEqual(self.s1.get_damage(), 30) self.assertEqual(self.s2.get_damage(), 30.5) def test_get_mana_cost(self): self.assertEqual(self.s1.get_mana_cost(), 50) self.assertEqual(self.s2.get_mana_cost(), 50.5) def test_get_cast_range(self): self.assertEqual(self.s1.get_cast_range(), 2)
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 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 test_raise_type_error_mana_cost(self): with self.assertRaises(TypeError): self.s3 = Spell(name="Lightning", damage=50, mana_cost="ss", cast_range=2)
def test_raise_type_error_damage(self): with self.assertRaises(TypeError): self.s3 = Spell(name="Lightning", damage="jjj", mana_cost=50.5, cast_range=2)
def setUp(self): self.s1 = Spell(name="Fireball", damage=30, mana_cost=50, cast_range=2) self.s2 = Spell(name="Lightning", damage=30.5, mana_cost=50.5, cast_range=2)
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))