我如何在函数中调用某个参数?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我如何在函数中调用某个参数?相关的知识,希望对你有一定的参考价值。

所以我刚开始学习python,我想为什么不做一个基于文本的游戏来提高我的技能。因此,在这个游戏中,我想制作一个战斗场景,其中您有一个职业(骑士,盗贼或巫师),并且该职业存储在player_class中。每个类都有三个我要说的属性。每个班级都有速度,伤害和健康。我想做到的是,如果您的速度高于或等于敌人,则首先攻击,但是如果敌人的速度高于敌人,则首先攻击。因此,我想我可以通过调用player_class = userKnight然后在战斗中做到这一点,前提是player_class(s​​peed)> =敌人类型(espeed),然后敌人类型(health)-player_class(伤害)。这不起作用,如果有人有更好的方法,或者可以解释我如何更好地实现这一点,那就太棒了!

def userRogue(damage,speed,health):
    health = 100;
    damage = 50;
    speed = 150;
    return

def userKnight(damage,speed,health):
    health = 100;
    damage = 100;
    speed = 50;
    return

def userWizard(damage,speed,health):
    health = 100;
    damage = 75;
    speed = 75;
    return

def enemyRat(ehealth, espeed, edamage):
    ehealth = 100;
    edamage = 10;
    espeed = 70;
    return

def enemyDeath():
    if ehealth <= 0:
        print("Enemy is dead!")
    return

def playerDeath():
    if health <= 0:
        print("You have died!")
    return

user_name = input("What is your name: ")

print("1:)Rogue")
print("2:)Knight")
print("3:)Wizard")

player_class_select = int(input("Choose your class: "))

player_class = 0
enemy_type = 0

if player_class_select == 1:
    print("You chose Rogue!")
    player_class = userRogue

elif player_class_select == 2:
    print("You chose Knight!")
    player_class = userKnight

elif player_class_select == 3:
    print("You chose Wizard!")
    player_class = userWizard

else:
    print("That is not a choice!")


print("Time for your first battle...")
print("A enemy has appeared!")

enemy_type = enemyRat

if player_class(speed) >= enemy_type(espeed):
    enemy_type(ehealth) - player_class(damage)
    enemyDeath()
elif player_class(speed) < enemy_type(espeed):
    player_class(health) - enemy_type(edamage);
    playerDeath()
    ```
答案

哦,您似乎有动力,但还没有足够的技能。从概念上讲,您几乎已经准备就绪,但您应该(如其他人所述)花一些时间阅读文档。

这里是您似乎想要的实现:

class UserRogue:
 def __init__(self,damage=100,speed=50,health=150):
        self.health = damage
        self.damage = speed
        self.speed = health

class UserKnight:
    def __init__(self,damage=100,speed=100,health=50):
        self.health = damage
        self.damage = speed
        self.speed = health

class UserWizard:
    def __init__(self,damage=100,speed=75,health=75):
        self.health = damage
        self.damage = speed
        self.speed = health

class EnemyRat:
    def __init__(self,damage=100,speed=100,health=70):
        self.health = damage
        self.damage = speed
        self.speed = health

def is_dead(entity):
    return entity.health <= 0


user_name = input("What is your name: ")

print("1:)Rogue")
print("2:)Knight")
print("3:)Wizard")


player_class_select = int(input("Choose your class: "))

class_dict = {
    1: ('Rogue', UserRogue),
    2: ('Knight', UserKnight),
    3: ('Wizard', UserWizard)
             }
class_string, player_class = class_dict.get(player_class_select, ('That is not a choice!', None))
print(f"You chose {class_string}!")

player = player_class()
print("Time for your first battle...")
print("A enemy has appeared!")

enemy = EnemyRat()

while not(is_dead(player) or is_dead(enemy)):
    if player.speed >= enemy.speed:
        enemy.health -= player.damage

    else:
        player.health -= enemy.damage

if is_dead(player):
    print('You died!')

else:
    print('You defeated your first enemy!')

我更改了代码以使其正常工作。这不是我要实现的很多方法,但是它很简单并且接近您已经提供的内容。希望这可以消除您的误解,并为您提供一个起点。

以上是关于我如何在函数中调用某个参数?的主要内容,如果未能解决你的问题,请参考以下文章

我如何在函数中调用某个参数?

Swift 学习- 07 -- 函数

如何知道何时调用了`navController.popBackStack()`?

Swift 官方文档中文版: 函数

如何测量代码片段的调用次数和经过时间

调用模板化成员函数:帮助我理解另一个 *** 帖子中的代码片段