我在用Python做游戏。我在代码上遇到了一些问题。我将我的mob定义为不同的类,以便以后编辑。我遇到的问题是,我不能呼吁健康对它造成损害。你知道吗
class spaceStalker(object):
def __init__(self, name, hp):
self.name = name
self.hp = hp
mobList = [spaceStalker]
mob = random.choice(mobList)
killList = ["not killed the beast!", "killed the beast!"]
kill = random.choice(killList)
def game():
if mob == spaceStalker:
fleeAtt = input("A Space Stalker has appeared! Attack or flee? ")
if fleeAtt == "Attack":
hitPass = input("Attack or Pass? ")
if hitPass == "Attack":
spaceStalker.hp -= 50
print(spaceStalker.hp)
else:
print("1")```
使用类的实例
在执行mob检查时,可以使用
isinstance(object, classname)
:所以不是:
用途:
我还建议你在课堂上使用getter和setter:
另外,也许您想使用类名约定(camelcase),所以我将其编写为
SpaceStalker
相关问题 更多 >
编程相关推荐