大家好,我是python的新手 - 或任何编码!无论如何,我试图制作一个简单的基于文本的RPG游戏,我还没有完成,但我在创建一个治疗系统时遇到了问题。一切正常,直到我尝试使用治疗法术并返回标题中的错误。不,我有点知道它试图告诉我的错误,但我不知道如何解决它。任何帮助将不胜感激,但请记住,在编码方面,我是一个庞大的新手。
三江源。
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
人员类:
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
答案 0 :(得分:2)
我刚刚看到问题被编辑了。
问题是缩进。所有方法def
语句都应该以与__init__
相同的缩进级别开始,否则它们不属于该类。
(缩进表示您在代码行的开头放置了多少个空格。)
首先,让我将代码减少到最低限度以重现问题。
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
不应该是一个简单的属性,因为我们想要调用它。所以它应该是方法,这是一个与类相关联的函数。它可以这样定义,例如:
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
答案 1 :(得分:0)
您没有发布Person类的实现。我猜你没有声明“治愈”的属性。我认为你试图从类Person(def heal(damage = 0))
调用一个方法基本上属性是类字段。例如
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方法中看到。这是一个本地功能。