在python类外调用方法

在python类外调用方法,python,python-2.7,Python,Python 2.7,我对Python和OOP比较陌生,我正试图编写一个带有类的迷你冒险游戏,我的BattleEngine类让我陷入困境。 这个想法是根据你的角色和对手的“力量”和“智慧”来选择与对手作战或智胜对手。 我尝试在此处调用攻击方法时出错: class Armory(Scene): def enter(self): print "This room appears to be some kind of armory. There are shelves full of weapon

我对Python和OOP比较陌生,我正试图编写一个带有类的迷你冒险游戏,我的BattleEngine类让我陷入困境。 这个想法是根据你的角色和对手的“力量”和“智慧”来选择与对手作战或智胜对手。 我尝试在此处调用攻击方法时出错:

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)    
这里是我定义战列引擎类的地方:

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'     
一旦我在程序中达到这一点,就会收到错误:“攻击全局名称未定义”
我不确定我到底做错了什么。任何帮助都会很棒

您需要在您的
战机
实例上调用
攻击
,并且不需要传入
self

battle = BattleEngine()
if decision == "fight":
    player_wins = battle.attack(Player.strength, 3)
请注意,您需要接收
.attack()
方法的返回值

这同样适用于
.outwit()
方法:

elif decision == "wit":
    player_wins = battle.outwit(Player.wit, 3)    
您可能需要修复
.attack()
.outwit()
方法中的返回值;而不是
return player\u wins
return'death'
,可能是return
True
False

self
参数由Python为您处理,并将引用特定的
BattleEngine
实例


并不是说您真的需要类,例如,您的
BattleEngine()
类当前没有每个实例的状态。您在任何方法中都不使用
self
,因为实例上没有可参考的内容。

您需要调用
BattleEngine
实例上的
attack
,并且不需要传入
self

battle = BattleEngine()
if decision == "fight":
    player_wins = battle.attack(Player.strength, 3)
请注意,您需要接收
.attack()
方法的返回值

这同样适用于
.outwit()
方法:

elif decision == "wit":
    player_wins = battle.outwit(Player.wit, 3)    
您可能需要修复
.attack()
.outwit()
方法中的返回值;而不是
return player\u wins
return'death'
,可能是return
True
False

self
参数由Python为您处理,并将引用特定的
BattleEngine
实例


并不是说您真的需要类,例如,您的
BattleEngine()
类当前没有每个实例的状态。您在任何方法中都不使用
self
,因为实例上没有可参考的内容。

修复此问题后…您的
玩家赢了
。我想在
enter
中,你想将
player\u wins
设置为调用
attack
的结果,然后检查它;在
attack
中,如果玩家赢了,则返回
True
而不是名为
player\u wins
的全局变量,否则返回
False
而不是
'death'
。此外,您几乎肯定希望
strength
wit
成为
玩家
类的实例属性,而不是类属性。如果有多个玩家,他们每个人都有不同的强度值,对吗?例如,如果我完成游戏并作为一个新角色重新开始,我就不会有上次那样的强度。我安排代码的方式是,如果用户选择玩新游戏,则
强度
wit
属性重置。这不是正确的方法。OOP的要点是每个
Player
实例代表一个播放器,而
Player
类代表所有可能的播放器的整个类。类属性仅用于所有玩家都应该共享的值。修复此问题后,
player\u wins
未正确处理。我想在
enter
中,你想将
player\u wins
设置为调用
attack
的结果,然后检查它;在
attack
中,如果玩家赢了,则返回
True
而不是名为
player\u wins
的全局变量,否则返回
False
而不是
'death'
。此外,您几乎肯定希望
strength
wit
成为
玩家
类的实例属性,而不是类属性。如果有多个玩家,他们每个人都有不同的强度值,对吗?例如,如果我完成游戏并作为一个新角色重新开始,我就不会有上次那样的强度。我安排代码的方式是,如果用户选择玩新游戏,则
强度
wit
属性重置。这不是正确的方法。OOP的要点是每个
Player
实例代表一个播放器,而
Player
类代表所有可能的播放器的整个类。类属性只用于所有玩家共享的值。太好了!我把这两件事都修好了,一切都按照我的计划进行。不需要类,你的意思是我可以定义函数攻击并在其他地方智取正确吗;没有状态的类只能作为美化的名称空间。太好了!我把这两件事都修好了,一切都按照我的计划进行。不需要类,你的意思是我可以定义函数攻击并在其他地方智取正确吗;没有状态的类只能作为美化的名称空间。