Python面向对象编程新手

时间:2011-06-09 14:24:41

标签: python oop object

我想知道如何使用我的第一个OOP代码解决这个问题。问题在于Snake类的攻击方法。我在比赛中有两条蛇,我试图让蛇攻击另一条蛇。目前我正在使用两个变量来记录它的转向,然后使用它来尝试攻击另一条蛇。但这不起作用。任何人都知道如何解决这个问题?非常感谢你。

class Snake:
    hp=100
    attack=25
    defense=1
    def set_name(self, name):
        self.name=name

    def shed(self):
        self.defense=self.defense+1

    def attack(self, opposite, current):
        opposite.hp=opposite.hp-(current.attack-opposite.defense)

    def eat(self):
        self.attack=self.attack+5
        print(str(self.name) + " eats a rat!")
        print(str(self.name) + "'s attack dmg is now " + str(self.attack))

    def sleep(self):
        print (str(self.name) + " goes to sleep")
        self.hp=self.hp+10
        if self.hp>100:
            self.hp=100
        print (str(self.name) + " wakes up with " + str(self.hp) + "hp")

##initialises the snakes
alpha=Snake()
beta=Snake()
## gives the snakes names of the user's choice
alpha_name=raw_input("What would you like to name your snake? ")
alpha.set_name(alpha_name)
beta_name=raw_input("What would you like to name the other snake? ")
beta.set_name(beta_name)
##starts the game
turn=True
while alpha.hp>0 and beta.hp>0:
    while turn==True:
        opposite="beta"
        current="alpha"
        action=raw_input("attack, sleep, eat or shed? ")
        try:
            if action=="attack":
                alpha.attack(opposite, current)
            if action=="sleep":
                alpha.sleep()
            if action=="eat":
                alpha.eat()
            if action=="shed":
                alpha.shed()

            turn=False
        except IOError:
            print("Please chose only one action, exaclty how it is typed")
    while turn==False:
        opposite="alpha"
        current="beta"
        if beta.hp<15:
            beta.sleep()
        elif alpha.hp>75:
            beta.attack()
        else:
            index=random.randint(1, 3)
            if index==1:
                beta.shed()
            elif index==2:
                beta.eat()
            else:
                beta.attack(opposite, current)    
        turn=True

3 个答案:

答案 0 :(得分:2)

在“攻击”中你试图访问“opposite.hp”,但这个方法是用字符串而不是对象调用的:

opposite="alpha"
current="beta"

=&GT;将此更改为

opposite=alpha
current=beta

此外,课程中还有一个名称相同的字段和方法:攻击。我建议将该字段重命名为“攻击点”或其他内容。

另外,你称之为“beta.attack()”。你忘了那里的方法论点。

答案 1 :(得分:2)

我看到两个问题。第一个是你传递变量的名称而不是变量本身。

改变这个:

while alpha.hp>0 and beta.hp>0:
    while turn==True:
        opposite="beta"
        current="alpha"
        action=raw_input("attack, sleep, eat or shed? ")
        try:
            if action=="attack":
                alpha.attack(opposite, current)

到此:

while alpha.hp>0 and beta.hp>0:
    while turn==True:
        opposite=beta
        current=alpha
        action=raw_input("attack, sleep, eat or shed? ")
        try:
            if action=="attack":
                alpha.attack(opposite, current)

此外,您在Snake类中定义了两次攻击字段。

class Snake:
    attack=25

    def attack(self, opposite, current):

以下是我在使用您的代码后想出的内容:

import random

class Snake:
    hp=100
    attack_skill=25
    defense=1

    def set_name(self, name):
        self.name=name

    def shed(self):
        self.defense=self.defense+1

    def attack(self, opposite):
        opposite.hp = opposite.hp - (self.attack_skill - opposite.defense)

    def eat(self):
        self.attack_skill += 5
        print(str(self.name) + " eats a rat!")
        print(str(self.name) + "'s attack dmg is now " + str(self.attack_skill))

    def sleep(self):
        print (str(self.name) + " goes to sleep")
        self.hp=self.hp+10
        if self.hp>100:
            self.hp=100
        print (str(self.name) + " wakes up with " + str(self.hp) + "hp")


##initialises the snakes
alpha=Snake()
beta=Snake()
## gives the snakes names of the user's choice
alpha_name=raw_input("What would you like to name your snake? ")
alpha.set_name(alpha_name)
beta_name=raw_input("What would you like to name the other snake? ")
beta.set_name(beta_name)
##starts the game
turn=True
while alpha.hp>0 and beta.hp>0:
    while turn==True:
        opposite="beta"
        current="alpha"
        action=raw_input("attack, sleep, eat or shed? ")
        try:
            if action=="attack":
                alpha.attack(beta)
            if action=="sleep":
                alpha.sleep()
            if action=="eat":
                alpha.eat()
            if action=="shed":
                alpha.shed()

            turn=False
        except IOError:
            print("Please chose only one action, exaclty how it is typed")
    while turn==False:
        opposite="alpha"
        current="beta"
        if beta.hp<15:
            beta.sleep()
        elif alpha.hp>75:
            beta.attack(alpha)
        else:
            index=random.randint(1, 3)
            if index==1:
                beta.shed()
            elif index==2:
                beta.eat()
            else:
                beta.attack(alpha)
        turn=True

答案 2 :(得分:1)

当你beta攻击时,你正在调用没有任何参数的attack()方法。我假设你想要beta.attack(alpha,beta)

但你可以重构方法只需要对手作为参数(因为你知道谁在攻击(它是调用攻击方法的对象))

def attack(self, opposite):
    opposite.hp -= self.attack-opposite.defense