简体   繁体   中英

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. 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:

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 :

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.

The same applies to the .outwit() method further on:

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

You probably need to fix the return values in the .attack() and .outwit() methods; instead of return player_wins and return 'death' , return True or False perhaps.

The self parameter is taken care of for you by Python, and will refer to the specific BattleEngine instance.

Not that you really need classes here, your BattleEngine() class currently has no per-instance state, for example. You don't use self in any of the methods, because there is nothing on the instance to refer to .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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