所以我正在制作一款文字冒险游戏,目前我正在制作敌人。我的班级 random_enemies 为你的角色制作垃圾小怪来战斗,我在其中有一个称为弱、正常、强等的功能……它会根据你的角色而定,具体取决于它是哪一个。当我调用 random_enemies.weak 它说(名称错误:未定义全局变量“p”),即使它应该是。
import random
from character import *
from player import *
class random_enemies(character):
def __init__(self,name,hp,maxhp,attack_damage,ability_power,exp):
super(random_enemies,self).__init__(name,hp,maxhp)
self.attack_damage = attack_damage
self.ability_power = ability_power
self.exp = exp
def weak():
self.hp = random.randint(p.maxhp/10, p.maxhp/5)
self.attack_damage = None
self.ability_power = None
self.exp = None
from character import*
class player(character):
def __init__(self,name,hp,maxhp,attack_damage,ability_power):
super(player,self).__init__(name, hp, maxhp)
self.attack_damage = attack_damage
self.ability_power = ability_power
这是我的玩家类,下面是玩家从中获得“maxhp”的类。
class character(object):
def __init__(self,name,hp,maxhp):
self.name = name
self.hp = hp
self.maxhp = maxhp
def attack(self,other):
pass
p=player(Players_name, 100, 100, 10, 5,)
while (p.hp>0):
a=input("What do you want to do?")
if a=="Instructions":
Instructions()
elif a=="Commands":
Commands()
elif a=="Fight":
print("Level",wave,"Wave Begins")
if wave < 6:
b = random_enemies.weak()
print("A",b,"Appeared!")
print("Stats of",b, ": \n Health=", b.hp,"Attack Damage=",b.attack_damage)
continue
我只是很快就做了这个,只是为了测试我所拥有的一切是否正常工作,直到我得到错误。这也是调用 random_enemies.weak() 的地方。在这也是我定义“p”是什么的地方。