当前位置: 首页>>代码示例>>Python>>正文


Python Enemy.take_damage方法代码示例

本文整理汇总了Python中enemy.Enemy.take_damage方法的典型用法代码示例。如果您正苦于以下问题:Python Enemy.take_damage方法的具体用法?Python Enemy.take_damage怎么用?Python Enemy.take_damage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在enemy.Enemy的用法示例。


在下文中一共展示了Enemy.take_damage方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestEnemy

# 需要导入模块: from enemy import Enemy [as 别名]
# 或者: from enemy.Enemy import take_damage [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)
开发者ID:Martin95056,项目名称:Dungeons,代码行数:47,代码来源:test_enemy.py

示例2: __init__

# 需要导入模块: from enemy import Enemy [as 别名]
# 或者: from enemy.Enemy import take_damage [as 别名]

#.........这里部分代码省略.........
        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"
            return (ID[0], ID[1] - 1)
        if direction == "up":
            self.map[ID[0] - 1][ID[1]] = "E"
            return (ID[0] - 1, ID[1])
        if direction == "down":
            self.map[ID[0] + 1][ID[1]] = "E"
            return (ID[0] + 1, ID[1])

    def range_fight(self, enemy_position, enemy_moving_direction):
        print("A range fight was started between our {} and {}".format(
            repr(self.hero), repr(self.enemy)))
        while self.get_position() is not None:
            self.enemy.take_damage(self.hero.attack(by="magic"))
            print("Hero casts a {}, hits enemy for {} dmg. Enemy health\
 is {}".format(self.hero.spell.name, self.hero.spell.get_damage(
                         ), self.enemy.get_health()))
            enemy_position = self.move_enemy(enemy_position, enemy_moving_direction)
            print("Enemy moved one square towards Hero.")
        self.start_fight()

    def start_fight(self):
        print("A melee fight was started between our {} and {}".format(
            repr(self.hero), repr(self.enemy)))
        while True:
            if self.determine_damage_source() is not None:
                if self.determine_damage_source() == "spell":
                    self.enemy.take_damage(self.hero.attack(by="magic"))
                    print("Hero casts a {}, hits enemy for {} dmg. Enemy health\
 is {}".format(self.hero.spell.name, self.hero.spell.get_damage(
                         ), self.enemy.get_health()))
                elif self.determine_damage_source() == "weapon":
                    self.enemy.take_damage(self.hero.attack(by="weapon"))
                    print("Hero hits enemy with {} for {} dmg. Enemy health\
 is {}".format(self.hero.weapon.name, self.hero.weapon.get_damage(
                         ), self.enemy.get_health()))
            if not self.enemy.is_alive():
                print("Enemy is dead!")
                break
            self.hero.take_damage(self.enemy.attack(by="weapon"))
            print("Enemy hits hero for {} dmg. Hero health is {}".format(
                self.enemy.damage, self.hero.get_health()))
            if not self.hero.is_alive():
                y, x = self.get_position()
                self.map[y][x] = "."
                print("Hero is dead!")
                break
开发者ID:kobso1245,项目名称:Programming101-v3,代码行数:104,代码来源:dungeon.py

示例3: TestEnemy

# 需要导入模块: from enemy import Enemy [as 别名]
# 或者: from enemy.Enemy import take_damage [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)
#.........这里部分代码省略.........
开发者ID:stilyantanev,项目名称:Dungeons-And-Pythons,代码行数:103,代码来源:enemy_test.py

示例4: test_take_damage

# 需要导入模块: from enemy import Enemy [as 别名]
# 或者: from enemy.Enemy import take_damage [as 别名]
 def test_take_damage(self):
     enemy2 = Enemy(health=100, mana=100, damage=20)
     enemy2.take_damage(30)
     self.assertEqual(enemy2.get_health(), 70)
开发者ID:EmineBasheva,项目名称:Dungeons_and_Pythons,代码行数:6,代码来源:test_enemy.py


注:本文中的enemy.Enemy.take_damage方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。