首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python面向对象编程新手

Python面向对象编程新手
EN

Stack Overflow用户
提问于 2011-06-09 22:24:41
回答 3查看 274关注 0票数 0

我想知道如何用我的第一段OOP代码来解决这个问题。问题存在于Snake类的攻击方法中。我在游戏中有两条蛇,正试图让这条蛇攻击另一条。现在我使用两个变量来记录下轮到哪条蛇,然后用这个来尝试攻击另一条蛇。然而,这是不起作用的。有谁知道怎么解决这个问题吗?非常感谢。

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-06-09 22:36:38

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

代码语言:javascript
复制
opposite="alpha"
current="beta"

=>将其更改为

代码语言:javascript
复制
opposite=alpha
current=beta

此外,类中还有一个字段和一个同名的方法:attack。我建议将该字段重命名为"attackpoints“或其他名称。

另外,您可以调用"beta.attack()“。您在那里忘记了方法参数。

票数 2
EN

Stack Overflow用户

发布于 2011-06-09 22:41:27

我看到了两个问题。第一种方法是传递变量的名称,而不是变量本身。

更改此设置:

代码语言:javascript
复制
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)

要这样做:

代码语言:javascript
复制
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类中定义了两次attack字段。

代码语言:javascript
复制
class Snake:
    attack=25

    def attack(self, opposite, current):

这是我在使用你的代码后想出来的:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2011-06-09 22:40:46

当您进行beta攻击时,您调用的是不带任何参数的attack()方法。我猜你想要beta.attack(alpha,beta)

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

代码语言:javascript
复制
def attack(self, opposite):
    opposite.hp -= self.attack-opposite.defense
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6294386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档