所以我之前做了这个代码,但是在调用时它不会使用check_level命令或者award命令:
from random import randint
class RPG:
#Defines parts of your character
def __init__(self, character_name):
self.chrn = character_name
self.exp = 0 #Total Experince points
self.level = 1 #Total Level
self.h = 25 #Player Health
self.expn = 10 #Experience Needed
self.eh = 10 #Base Enemy Health
self.set = False #Used to chest enemy status
self.win = False #Used to check for win
def set_basic(self): # Sets up the basic enemy.
if self.eh == 0:
self.eh = 10
def check_level(self): # Checks to see if you have enough EXP to level up.
if self.exp >= self.expn:
self.level = self.level + 1
self.exp = 0
self.expn = self.level * 10
print('Level has increased to {0}'.format(self.level))
def check_enemy(self): # Checks to see if enemy's health is equal to 0.
if self.eh == 0:
print('You Win')
self.set == True
RPG.set_basic(self)
else:
print('')
def award(self): # Awards EXP if enemy is dead.
if self.set == True:
self.exp = self.exp + 5
print ('You gained 5 EXP!')
def attack(self): #The main character's attack.
x = randint(1,100)
if x > 20:
y = randint(1, self.level * 3)
self.eh = self.eh - y
final = ('Enemy took {0} damage'.format(y))
print(final)
if self.eh < 0:
self.eh = 0
print('Enemy has {0} health left'.format(self.eh))
print('')
RPG.check_enemy(self)
RPG.award(self)
RPG.check_level(self)
else:
print('Miss!')
答案 0 :(得分:1)
此:
RPG.check_enemy(self)
RPG.award(self)
RPG.check_level(self)
应该是:
self.check_enemy()
self.award()
self.check_level()
暗示self
param。
编辑:实际上您的任何RPG.*(self)
函数都应该是self.*()
- 我看到其中有一些函数散布在整个课程中。
答案 1 :(得分:1)
Samsquanch将您的所有RPG.(self)
方法调用更改为self.*()
是正确的。当您调用方法时,python将始终将实例对象作为第一个参数传递。这样您就可以避免像以前一样显式调用该方法。您可以查看this link以获取有关self
。
我测试了您的代码,看起来您的问题在check_enemy()
内。您需要将self.set == True
更改为self.set = True
。
新方法应如下所示
def check_enemy(self):
if self.eh == 0:
print('You Win')
self.set = True
self.set_basic()
else:
print('')