简体   繁体   English

python类外部的调用方法

[英]calling method outside class python

I'm relatively new to Python and OOP, I'm trying to write a mini adventure game with classes and have gotten stuck with my BattleEngine class. 我是Python和OOP的新手,我试图用类编写一个迷你冒险游戏,并被我的BattleEngine类所困扰。 The idea is to have options to fight or outwit an opponent based on your characters and the opponents 'strength' and 'wit'. 这个想法是根据您的角色以及对手的“力量”和“机智”来选择与对手战斗或胜过对手。 I get an error when I try to call my attack method here: 当我尝试在此处调用攻击方法时出现错误:

class Armory(Scene):

    def enter(self):
        print "This room appears to be some kind of armory. There are shelves full of weaponry lining"
        print "the walls. You walk around admiring the shiny weapons, you reach out to pick up a"
        print "massive battleaxe. Right as your fingers touch it you hear voices from behind the"
        print "next door. They sound like they're getting closer. As the voices grow nearer you must make"
        print "a decision. Will you stand and fight (enter 'fight') or will you use your wit to outsmart"
        print "your opponent(enter 'wit')?"
        decision = raw_input("> ")
        battle = BattleEngine()
        if decision == "fight":
            attack(self, Player.strength, 3)
            if player_wins:
                print "A man in light armour walks in and sees you with your sword drawn. A look of"
                print "shock and disbelief is on his face. You act quickly and lunge at him."
                print "The soldier struggles to unsheath his sword as you run him through."
                print "He collapses to the ground wearing the same look of disbelief."
                print "Your strength has increased by 1."
                Player.strength += 1
        elif decision == "wit":
            outwit(self, Player.wit, 3)    

Here is where I defined my BattleEngine class: 这是我定义BattleEngine类的地方:

class BattleEngine(object):

    def attack(self, player_strength, nonplayer_strength):
        player_roll = randint(1,6)
        nonplayer_roll = randint(1,6)
        if (player_roll + player_strength) >= (nonplayer_roll + nonplayer_strength):
            return player_wins
        else: 
            return 'death'

    def outwit(self, player_wit, nonplayer_wit):
        player_roll = randint(1,6)
        nonplayer_roll = randint(1,6)
        if (player_roll + player_wit) >= (nonplayer_roll + nonplayer_wit):
            return player_wins
        else: 
            return 'death'     

Once I get to this point in my program is receive the error that : 'attack global name is not defined' I'm not sure what I'm doing wrong exactly. 一旦到达程序中的这一点,就会收到错误消息:“未定义攻击全局名称”,我不确定我到底在做什么错。 Any help would be fantastic! 任何帮助都太棒了!

You need to call attack on your BattleEngine instance, and you do not need to pass in self : 你需要调用attack BattleEngine实例,你不需要通过self

battle = BattleEngine()
if decision == "fight":
    player_wins = battle.attack(Player.strength, 3)

Note that you need to receive the return value of the .attack() method. 请注意,您需要接收.attack()方法的返回值。

The same applies to the .outwit() method further on: 进一步在.outwit()方法上也是如此:

elif decision == "wit":
    player_wins = battle.outwit(Player.wit, 3)    

You probably need to fix the return values in the .attack() and .outwit() methods; 您可能需要在.attack().outwit()方法中修复返回值; instead of return player_wins and return 'death' , return True or False perhaps. 而不是return player_winsreturn 'death' ,也许返回TrueFalse

The self parameter is taken care of for you by Python, and will refer to the specific BattleEngine instance. Python会照顾好self参数,并将引用特定的BattleEngine实例。

Not that you really need classes here, your BattleEngine() class currently has no per-instance state, for example. 并不是说您这里确实需要类,例如, BattleEngine()类当前没有按实例的状态。 You don't use self in any of the methods, because there is nothing on the instance to refer to . 您不会在任何方法中使用self ,因为实例上没有要引用的东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM