本文整理汇总了Python中enemy.Enemy.attack方法的典型用法代码示例。如果您正苦于以下问题:Python Enemy.attack方法的具体用法?Python Enemy.attack怎么用?Python Enemy.attack使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类enemy.Enemy
的用法示例。
在下文中一共展示了Enemy.attack方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestEnemy
# 需要导入模块: from enemy import Enemy [as 别名]
# 或者: from enemy.Enemy import attack [as 别名]
class TestEnemy(unittest.TestCase):
def setUp(self):
self.enemy = Enemy(health=100, mana=100, damage=20)
def test_damage_negative(self):
with self.assertRaises(ValueError):
enemy2 = Enemy(health=100, mana=100, damage=-20)
def test_take_damage(self):
enemy2 = Enemy(health=100, mana=100, damage=20)
enemy2.take_damage(30)
self.assertEqual(enemy2.get_health(), 70)
def test_attack_with_weapon(self):
w = Weapon('Axe', 30)
self.enemy.equip(w)
self.assertEqual(self.enemy.attack('weapon'), 30)
def test_attack(self):
self.assertEqual(self.enemy.attack(), 20)
示例2: TestEnemy
# 需要导入模块: from enemy import Enemy [as 别名]
# 或者: from enemy.Enemy import attack [as 别名]
class TestEnemy(unittest.TestCase):
def setUp(self):
self.enemy = Enemy(200, 300, 50)
def test_enemy_init(self):
self.assertEqual(self.enemy.health, 200)
self.assertEqual(self.enemy.mana, 300)
self.assertEqual(self.enemy.curr_mana, self.enemy.mana)
self.assertEqual(self.enemy.curr_health, self.enemy.health)
def test_enemy_is_alive(self):
self.enemy.curr_health = 30
self.assertTrue(self.enemy.is_alive())
self.enemy.curr_health = 0
self.assertFalse(self.enemy.is_alive())
def test_enemy_can_cast(self):
s = Spell("BatkaAttack", 30, 50, 2)
self.assertTrue(self.enemy.can_cast(s))
self.assertEqual(self.enemy.curr_mana, 250)
s1 = Spell("MegaBatkaAttack", 30, 350, 2)
self.assertRaises(Exception)
def test_enemy_take_damage(self):
self.enemy.take_damage(50)
self.assertEqual(self.enemy.curr_health, 150)
self.enemy.take_damage(1000)
self.assertEqual(self.enemy.curr_health, 0)
def test_enemy_take_healing(self):
self.assertFalse(self.enemy.take_healing(20))
self.enemy.take_damage(100)
self.assertTrue(self.enemy.take_healing(20))
self.assertEqual(self.enemy.curr_health, 120)
self.enemy.curr_health = 0
self.assertFalse(self.enemy.take_healing(20))
def test_enemy_attack(self):
self.assertEqual(self.enemy.attack(), self.enemy.damage)
示例3: __init__
# 需要导入模块: from enemy import Enemy [as 别名]
# 或者: from enemy.Enemy import attack [as 别名]
#.........这里部分代码省略.........
for move_direction in ["left", "right", "up", "down"]:
if direction == move_direction:
dx, dy = self.directions[direction]
new_x = x + dx
new_y = y + dy
if self.check_out_of_range(new_x, new_y):
if not self.move_to_next_field(new_x, new_y):
return False
self.map[y][x] = "."
else:
return False
self.hero.take_mana(self.hero.mana_regeneration_rate)
return True
def determine_damage_source(self):
if self.hero.has_spell() and self.hero.has_weapon():
if self.hero.spell.get_damage() >= self.hero.weapon.get_damage():
if self.hero.get_mana() >= self.hero.spell.get_mana_cost():
return "spell"
return "weapon"
elif self.hero.has_spell():
if self.hero.get_mana() >= self.hero.spell.get_mana_cost():
return "spell"
elif self.hero.has_weapon():
return "weapon"
directions = {
"left": (-1, 0),
"right": (1, 0),
"up": (0, -1),
"down": (0, +1),
}
def check_range_attack(self, direction):
y, x = self.get_position()
dx, dy = self.directions[direction]
x += dx
y += dy
can_pass_freely = True
for i in range(1, self.hero.spell.get_cast_range() + 1):
if can_pass_freely:
if self.map[y][x] == "E":
return (y, x)
if self.map[y][x - i] != ".":
can_pass_freely = False
def try_range_attack(self):
y, x = self.get_position()
for i in ["left", "right", "up", "down"]:
if self.check_range_attack("right") is not None:
self.enemy = Enemy(100, 100, 20)
self.range_fight(self.check_range_attack("right"), i)
if self.hero.is_alive():
self.map[y][x] = "H"
else:
self.map[y][x] = "."
else:
print("No enemy in casting range!")
def move_enemy(self, ID, direction):
self.map[ID[0]][ID[1]] = "."
if direction == "right":
self.map[ID[0]][ID[1] + 1] = "E"
return (ID[0], ID[1] + 1)
if direction == "left":
self.map[ID[0]][ID[1] - 1] = "E"
示例4: TestEnemy
# 需要导入模块: from enemy import Enemy [as 别名]
# 或者: from enemy.Enemy import attack [as 别名]
class TestEnemy(unittest.TestCase):
def setUp(self):
self.enemy = Enemy(health=110, mana=110, damage=35)
def test_create_new_instance(self):
self.assertTrue(isinstance(self.enemy, Enemy))
def test_valid_members(self):
self.assertEqual(self.enemy.health, 110)
self.assertEqual(self.enemy.mana, 110)
self.assertEqual(self.enemy.damage, 35)
self.assertEqual(self.enemy.max_health, 110)
self.assertEqual(self.enemy.max_mana, 110)
def test_is_alive(self):
self.assertTrue(self.enemy.is_alive())
self.enemy.health = 0
self.assertFalse(self.enemy.is_alive())
def test_can_cast_without_spell(self):
self.assertFalse(self.enemy.can_cast())
def test_can_cast_with_more_mana(self):
self.enemy.spell = Spell("Chilling Poison", 20, 20, 2)
self.assertTrue(self.enemy.can_cast())
def test_can_cast_with_less_mana(self):
self.enemy.spell = Spell("Chilling Poison", 20, 20, 2)
self.enemy.mana = 0
self.assertFalse(self.enemy.can_cast())
def test_get_health(self):
self.assertEqual(self.enemy.get_health(), 110)
def test_get_mana(self):
self.assertEqual(self.enemy.get_mana(), 110)
def test_take_damage_more_than_health(self):
self.enemy.take_damage(150)
self.assertEqual(self.enemy.health, 0)
def test_take_damage_less_than_health(self):
self.enemy.take_damage(100)
self.assertEqual(self.enemy.health, 10)
def test_take_healing_dead_enemy(self):
self.enemy.health = 0
self.assertFalse(self.enemy.take_healing(200))
def test_take_healing_with_more_points_than_max_health(self):
self.enemy.health = 80
self.assertTrue(self.enemy.take_healing(200))
self.assertEqual(self.enemy.health, 110)
def test_take_healing_with_less_points_than_max_health(self):
self.enemy.health = 90
self.assertTrue(self.enemy.take_healing(10))
self.assertEqual(self.enemy.health, 100)
def test_take_mana_with_more_points_than_max_mana(self):
self.enemy.mana = 80
self.enemy.take_mana(200)
self.assertEqual(self.enemy.mana, 110)
def test_take_mana_with_negative_mana(self):
self.enemy.mana = 50
self.enemy.take_mana(-60)
self.assertEqual(self.enemy.mana, 0)
def test_take_mana_with_less_points_than_max_mana(self):
self.enemy.mana = 65
self.enemy.take_mana(20)
self.assertEqual(self.enemy.mana, 85)
def test_equip(self):
knife = Weapon("Knife", 10)
self.enemy.equip(knife)
self.assertEqual(self.enemy.weapon, knife)
def test_learn(self):
mind_blast = Spell("Mind Blast", 20, 55, 2)
self.enemy.learn(mind_blast)
self.assertEqual(self.enemy.spell, mind_blast)
def test_attack_without_equiped_weapon(self):
self.assertEqual(self.enemy.attack(by="weapon"), 0)
def test_attack_with_equiped_weapon(self):
knife = Weapon("Sword", 50)
self.enemy.equip(knife)
self.assertEqual(self.enemy.attack(by="weapon"), 50)
def test_attack_without_learned_spell(self):
self.assertEqual(self.enemy.attack(by="spell"), 0)
def test_attack_with_learned_spell(self):
fire_blast = Spell("Fire Blast", 40, 30, 1)
self.enemy.learn(fire_blast)
#.........这里部分代码省略.........
示例5: TestEnemy
# 需要导入模块: from enemy import Enemy [as 别名]
# 或者: from enemy.Enemy import attack [as 别名]
class TestEnemy(unittest.TestCase):
def setUp(self):
self.enemy_alive = Enemy(100, 0, 20)
self.enemy_1fourthalive = Enemy(25, 0, 20)
self.enemy_dead = Enemy(0, 50, 20)
self.enemy_halfalive = Enemy(50, 0, 20)
self.enemy_3fourthsalive = Enemy(75, 5, 60)
self.knife = Weapon("knife", 20)
self.spell = Spell("Fireball", 30, 50, 2)
def test_init(self):
self.assertTrue(isinstance(self.enemy_alive, Enemy))
self.assertTrue(isinstance(self.enemy_dead, Enemy))
self.assertTrue(isinstance(self.enemy_halfalive, Enemy))
def test_get_health(self):
self.assertEqual(self.enemy_alive.get_health(), 100)
self.assertEqual(self.enemy_dead.get_health(), 0)
def test_get_mana(self):
self.assertEqual(self.enemy_alive.get_mana(), 0)
self.assertEqual(self.enemy_dead.get_mana(), 50)
def test_is_alive(self):
self.assertTrue(self.enemy_alive.is_alive())
self.assertFalse(self.enemy_dead.is_alive())
def test_can_cast(self):
self.assertTrue(self.enemy_dead.can_cast())
self.assertFalse(self.enemy_alive.can_cast())
def test_take_healing(self):
with self.assertRaises(CannotHealWithNonPositivePoints):
self.enemy_1fourthalive.take_healing(-10)
self.assertFalse(self.enemy_dead.take_healing(20))
with self.assertRaises(EnemyHasMaxHealth):
self.enemy_alive.take_healing(10)
self.assertTrue(self.enemy_halfalive.take_healing(60))
self.enemy_1fourthalive.take_healing(75)
self.assertEqual(self.enemy_1fourthalive.get_health(), 100)
self.enemy_3fourthsalive.take_healing(10)
self.assertEqual(self.enemy_3fourthsalive.get_health(), 85)
def test_take_mana(self):
self.assertFalse(self.enemy_1fourthalive.take_mana())
self.assertFalse(self.enemy_3fourthsalive.take_mana())
self.assertFalse(self.enemy_alive.take_mana())
self.assertFalse(self.enemy_dead.take_mana())
self.assertFalse(self.enemy_halfalive.take_mana())
def test_equip(self):
self.enemy_alive.equip(self.knife)
self.assertEqual(self.enemy_alive.weapon, self.knife)
def test_learn(self):
self.enemy_halfalive.learn(self.spell)
self.assertEqual(self.enemy_halfalive.spell, self.spell)
def test_attack(self):
self.assertEqual(self.enemy_halfalive.attack(by="weapon"), 0)
self.assertEqual(self.enemy_1fourthalive.attack(by="spell"), None)
self.enemy_alive.equip(self.knife)
self.assertEqual(self.enemy_alive.attack(by="weapon"), 20)
self.enemy_3fourthsalive.learn(self.spell)
with self.assertRaises(Exception):
self.enemy_3fourthsalive.attack(by="spell")
示例6: HeroTests
# 需要导入模块: from enemy import Enemy [as 别名]
# 或者: from enemy.Enemy import attack [as 别名]
class HeroTests(unittest.TestCase):
def setUp(self):
self.hero = Hero("Ahri", "Kingslayer", 100, 100, 2)
self.enemy = Enemy(100, 100, 20)
self.dungeon = Dungeon("level1.txt")
self.weapon = Weapon("The Axe of Destiny", damage=20)
self.spell = Spell(
name="Fireball", damage=30, mana_cost=50, cast_range=1)
def test_known_as(self):
self.assertEqual(self.hero.known_as(), "Ahri the Kingslayer")
def test_take_damage(self):
self.hero.take_damage(150)
self.assertEqual(self.hero.get_health(), 0)
def test_take_healing(self):
self.hero.take_damage(50)
self.assertTrue(self.hero.take_healing(55))
self.assertEqual(self.hero.get_health(), 100)
def test_take_mana_with_potion(self):
pass
# Test enemy who can not regenerate mana
def test_mana_regeneration_while_moving(self):
pass
def test_can_cast(self):
self.hero.learn(self.spell)
self.assertTrue(self.hero.can_cast())
def test_can_cast_with_no_spells_learned(self):
self.assertFalse(self.hero.can_cast())
def test_attack_not_being_specified(self):
with self.assertRaises(ValueError):
self.hero.attack(by="gun")
def test_attack_without_weapon_or_spell(self):
self.assertEqual(self.hero.attack(by="weapon"), 0)
self.assertEqual(self.hero.attack(by="magic"), 0)
# Test for enemy (base damage= = 20)
self.assertEqual(self.enemy.attack(by="weapon"), 20)
self.assertEqual(self.enemy.attack(by="magic"), 20)
def test_attack(self):
self.hero.equip(self.weapon)
self.assertEqual(self.hero.attack(by="weapon"), 20)
self.hero.learn(self.spell)
self.assertEqual(self.hero.attack(by="magic"), 30)
def test_dungeon_map(self):
self.assertEqual(self.dungeon.map, [['S', '.', '#', '#', '.', '.', '.', '.', '.', 'T'], ['#', 'T', '#', '#', '.', '.', '#', '#', '#', '.'], ['#', '.', '#', '#', '#', 'E', '#', '#', '#', 'E'], ['#', '.', 'E', '.', '.', '.', '#', '#', '#', '.'], ['#', '#', '#', 'T', '#', '#', '#', '#', '#', 'G']])
# self.dungeon.print_map()
def test_spawn_hero(self):
self.dungeon.spawn(self.hero)
self.dungeon.spawn(self.hero)
self.dungeon.spawn(self.hero)
# self.dungeon.print_map()
def test_get_position(self):
self.dungeon.spawn(self.hero)
self.assertEqual(self.dungeon.get_position(), (0, 0))
def test_move_right(self):
self.dungeon.spawn(self.hero)
self.dungeon.move_hero("right")
# self.dungeon.print_map()
self.assertFalse(self.dungeon.move_hero("right"))
def test_move_left(self):
self.dungeon.spawn(self.hero)
self.dungeon.move_hero("right")
self.assertTrue(self.dungeon.move_hero("left"))
# self.dungeon.print_map()
def test_move_up(self):
self.dungeon.spawn(self.hero)
self.assertFalse(self.dungeon.move_hero("up"))
def test_move_down(self):
self.dungeon.spawn(self.hero)
self.assertFalse(self.dungeon.move_hero("down"))
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"))
def test_the_game(self):
h = Hero(name="Bron", title="Dragonslayer",
health=100, mana=100, mana_regeneration_rate=2)
#.........这里部分代码省略.........