首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AttributeError:“人”对象没有属性“治愈”

AttributeError:“人”对象没有属性“治愈”
EN

Stack Overflow用户
提问于 2017-04-04 11:55:50
回答 2查看 3.2K关注 0票数 0

你好,伙计们,我对python非常陌生--或者任何编码!无论如何,我试图做一个简单的文本为基础的RPG游戏,我还远没有完成,但我有问题,创建一个愈合系统。一切正常工作,直到我尝试使用治疗咒语,它会返回标题中的错误。不,我知道它想告诉我什么是错的,但我不知道如何补救。任何帮助都会感谢,但请记住,我是一个伟大的新手,当谈到编码。

谢谢你。

代码语言:javascript
复制
from classes.game import Person, bcolors
from classes.magic import Spell

# Create Black Magic
fire = Spell("Fire", 10, 100, "black")
thunder = Spell("Thunder", 10, 100, "black")
blizzard = Spell("Blizzard", 10, 100, "black")
meteor = Spell("Meteor", 20, 200, "black")
quake = Spell("Quake", 14, 140, "black")

# Create White Magic
cure = Spell("Cure", 12, 120, "white")
cura = Spell("Cura", 18, 200, "white")

# Instantiate People
player = Person(460, 65, 60, 34, [fire, thunder, blizzard, meteor, cure,          cura])
enemy = Person(1200, 65, 45, 25, [])

running = True
i = 0

print(bcolors.FAIL + bcolors.BOLD + "AN ENEMY ATTACKS!" + bcolors.ENDC)

while running:
print("=================")
player.choose_action()
choice = input("Choose action")
index = int(choice) - 1

if index == 0:
    dmg = player.generate_damage()
    enemy.take_damage(dmg)
    print("You attacked for", dmg, "points of damage.")
elif index == 1:
    player.choose_magic()
    magic_choice = int(input("Choose magic:")) - 1

    spell = player.magic[magic_choice]
    magic_dmg = spell.generate_damage()

    current_mp = player.get_mp()

    if spell.cost > current_mp:
        print(bcolors.FAIL + "\nNot enough MP\n" + bcolors.ENDC)
        continue

    player.reduce_mp(spell.cost)

    if spell.type == "white":
        player.heal(magic_dmg)
        print(bcolors.OKBLUE + "\n" + spell.name + " heals for", str(magic_dmg), "HP." + bcolors.ENDC)
    elif spell.type == "black":
        enemy.take_damage(magic_dmg)
        print(bcolors.OKBLUE + "\n" + spell.name + " deals", str(magic_dmg), "points of damage." + bcolors.ENDC)


enemy_choice = 1

enemy_dmg = enemy.generate_damage()
player.take_damage(enemy_dmg)
print("Enemy attacks for", enemy_dmg)

print("-----------------------")
print("Enemy HP:", bcolors.FAIL + str(enemy.get_hp()) + "/" + str(enemy.get_max_hp()) + bcolors.ENDC + "\n")

print("Your HP:", bcolors.OKGREEN + str(player.get_hp()) + "/" + str(player.get_max_hp()) + bcolors.ENDC)
print("Your MP:", bcolors.OKBLUE + str(player.get_mp()) + "/" + str(player.get_max_mp()) + bcolors.ENDC + "\n")

if enemy.get_hp() == 0:
    print(bcolors.OKGREEN + "You win!" + bcolors.ENDC)
    running = False
elif player.get_hp() == 0:
    print(bcolors.FAIL + "Your enemy has defeated you!" + bcolors.ENDC)
    running = False

个人班:

代码语言:javascript
复制
class Person:
    def __init__(self, hp, mp, atk, df, magic):
        self.maxhp = hp
        self.hp = hp
        self.maxmp = mp
        self.mp = mp
        self.atkl = atk - 10
        self.atkh = atk + 10
        self.df = df
        self.magic = magic
        self.actions = ["Attack", "Magic"]

            def generate_damage(self):
    return random.randrange(self.atkl, self.atkh)


def take_damage(self, dmg):
    self.hp -= dmg
    if self.hp < 0:
        self.hp = 0


    def heal(self, dmg):
        self.hp += dmg
        if self.hp > self.maxhp:
            self.hp = self.maxhp

def get_hp(self):
    return self.hp

def get_max_hp(self):
    return self.maxhp

def get_mp(self):
    return self.mp

def get_max_mp(self):
    return self.maxmp

def reduce_mp(self, cost):
    self.mp -= cost


def choose_action(self):
    i = 1
    print(bcolors.OKBLUE + bcolors.BOLD + "Actions" + bcolors.ENDC)
    for item in self.actions:
        print(str(i) + ":", item)
        i += 1

def choose_magic(self):
    i = 1

    print(bcolors.OKBLUE + bcolors.BOLD + "Magic" + bcolors.ENDC)
    for spell in self.magic:
        print(str(i) + ":", spell.name, "(cost:", str(spell.cost) + ")")
        i += 1
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-04-04 12:14:44

我刚看到这个问题是经过编辑的。问题是缩进。 All方法def语句应该从与__init__相同的缩进级别开始,否则它们不是类的一部分。

(缩进表示在代码行的开头放置多少空格。)

首先,让我将您的代码减少到最低限度来重现问题。

代码语言:javascript
复制
class Person:
    def __init__(self, hp):
        self.maxhp = hp
        self.hp = hp

player = Person(460)
player.hp = 455  # forcefully reduce player's hp
player.heal(10)  # AttributeError: 'Person' object has no attribute 'heal'

很明显,player这个人并不真正知道如何被治愈。heal从未被定义过。请注意,可以无问题地访问.hp,因为它是在__init__中定义的。

heal不应该是一个简单的属性,因为我们想要调用它。所以它应该是一个方法,它是一个与类相关联的函数。它可以这样定义,例如:

代码语言:javascript
复制
class Person:
    def __init__(self, hp):
        self.maxhp = hp
        self.hp = hp

    # This is part of the Person class
    def heal(self, amount):
        self.hp += amount

# This is NOT part of the Person class
def something():
    pass
票数 3
EN

Stack Overflow用户

发布于 2017-04-04 12:14:39

你没有发布Person类的植入。我猜你没有一个属性声明为“治愈”。我认为您正在尝试从类Person ( def (damage=0))调用一个方法。

基本上,属性是类字段。例如

代码语言:javascript
复制
class Example():
    field1 = [] # this is an attribute
    health = 1000 # this is an attribute

    def __init__(): # this is a method
        field1 = ['a', 'b']

    def heal(value=0): #this is a method
         if self.health < 1000:
             if value > 1000 - self.health:
                 self.health = 1000 - self.health
             else:
                 self.health += value

我写了一个简单的治愈方法,这样你就能明白我在说什么。

因此,如果要调用属性,则不使用“(.)”在类的实例上。你只要叫"my_instance.attribute1“。但是,如果您想调用一个方法,您应该使用“(.)”比如"my_instance.my_method()“。(同样,我们不是在讨论高级python编程,例如,可以使用@property作为属性调用方法)。

编辑:只有在take_damage方法中才能看到恢复方法。这是一个局部函数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43206556

复制
相关文章

相似问题

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