小白求助

weixin_51923417 2021-05-01 12:15:39
代码中第111行的cooperat_role函数运行时报错提示Game中没有life属性或者attack属性。。但是Game其他函数也调用了这两个类属性呀。。求大佬解惑。祝大佬51快乐~
import random
import time

# 创建一个类,可实例化成具体的游戏角色
class Role:
def __init__(self, name='【角色】'): # 把角色名作为默认参数
self.name = name
self.life = random.randint(100,150)#血量
self.attack = random.randint(30,50)#攻击力

# 创建3个子类,可实例化为3个不同的职业

class Knight(Role):
def __init__(self, name='【圣光骑士】'): # 把子类角色名作为默认参数
Role.__init__(self,name) # 利用了父类的初始化函数
self.life = self.life*5 # 骑士有5份血量
self.attack = self.attack*3 # 骑士有3份攻击力

# 职业克制关系
def fight_buff(self, opponent,str1,str2):
if opponent.name == '【暗影刺客】':
self.attack = int(self.attack * 1.5)
print('『%s』【圣光骑士】对 『%s』【暗影刺客】说:“让无尽光芒制裁你的堕落!”'%(str1, str2))

class Assassin(Role):
def __init__(self, name='【暗影刺客】'):
Role.__init__(self,name)
self.life = self.life*3
self.attack = self.attack*5

# 职业克制关系
def fight_buff(self, opponent,str1,str2):
if opponent.name == '【精灵弩手】':
self.attack = int(self.attack * 1.5)
print('『%s』【暗影刺客】对 『%s』【精灵弩手】说:“主动找死,就别怪我心狠手辣。”'%(str1, str2))

class Bowman(Role):
def __init__(self, name='【精灵弩手】'):
Role.__init__(self,name)
self.life = self.life*4
self.attack = self.attack*4

# 职业克制关系
def fight_buff(self, opponent,str1,str2):
if opponent.name == '【圣光骑士】':
self.attack = int(self.attack * 1.5)
print('『%s』【精灵弩手】对 『%s』【圣光骑士】说:“骑着倔驴又如何?你都碰不到我衣服。”'%(str1, str2))

# 创建一个类,可生成3V3并展示:可分为:欢迎语→随机生成→展示角色
class Game():
def __init__(self):
self.players = [] # 存玩家顺序
self.enemies = [] # 存敌人顺序
self.score = 0 # 比赛积分
self.i = 0 # 记轮次
# 依次执行以下函数
self.game_start() # 欢迎语
self.born_role() # 随机生成6个角色
self.show_role() # 展示角色
self.order_role() # 排序并展示
self.cooperat_role()#检测双方阵型是否形成羁绊
self.pk_role() # 让双方 Pk 并展示结果
self.show_result() # 展示最终结局

# 欢迎语
def game_start(self):
print('------------ 欢迎来到“炼狱角斗场” ------------')
print('在昔日的黄昏山脉,奥卢帝国的北境边界上,有传说中的“炼狱角斗场”。')
print('鲜血与战斗是角斗士的归宿,金钱与荣耀是角斗士的信仰!')
print('今日,只要【你的队伍】能取得胜利,你将获得一笔够花500年的财富。')
time.sleep(2)
print('将随机生成【你的队伍】和【敌人队伍】!')
input('\n狭路相逢勇者胜,请按任意键继续。\n')

# 随机生成6个角色
def born_role(self):#每一次随机生成一个一个角色,角色允许重复。
for i in range(3):
self.players.append(random.choice([Knight(),Assassin(),Bowman()]))
self.enemies.append(random.choice([Knight(),Assassin(),Bowman()]))

# 展示角色
def show_role(self):
print('----------------- 角色信息 -----------------')
print('你的队伍:')
for i in range(3):
print( '『我方』%s 血量:%s 攻击:%s'%
(self.players[i].name,self.players[i].life,self.players[i].attack))
print('--------------------------------------------')

print('敌人队伍:')
for i in range(3):
print('『敌方』%s 血量:%s 攻击:%s'%
(self.enemies[i].name,self.enemies[i].life,self.enemies[i].attack))
print('--------------------------------------------')
input('请按回车键继续。\n')

# 排序并展示
def order_role(self):
order_dict = {}
for i in range(3):
order = int(input('你想将 %s 放在第几个上场?(输入数字1~3)'% self.players[i].name))#(从born_role函数中Knight(),Assassin(),Bowman()中的name类属性)
order_dict[order] = self.players[i]#以字典的方式存储(存储的是born_role函数中生成的各个类Knight(),Assassin(),Bowman()并生成用户输入的次数数据)
self.players = []#重置players列表
for i in range(1,4):
self.players.append(order_dict[i]) #(按照一次的键从order_dict字典中提出值,)
print('\n你的队伍出场顺序是:%s、%s、%s'
%(self.players[0].name,self.players[1].name,self.players[2].name))
print('敌人队伍出场顺序是:%s、%s、%s'
%(self.enemies[0].name,self.enemies[1].name,self.enemies[2].name))
#检测双方是否形成团队buff
def cooperat_role(self):
if self.players[0].name==self.players[1].name==self.players[2].name:
for i in range(3):
self.players[i].life = int(self.life * 1.25)
print('恭喜您获得团队buff,血量上升25%')
if self.players[0].name!=self.players[1].name!=self.players[2].name:
for i in range(3):
self.players[i].attack = int(self.attack * 1.25)
print('恭喜您获得团队buff,攻击力上升25%')
if self.enemies[0].name==self.enemies[1].name==self.enemies[2].name:
for i in range(3):
self.enemies[i].life = int(self.life * 1.25)
print('敌方获得团队buff,血量上升25%')
if self.enemies[0].name!=self.enemies[1].name!=self.enemies[2].name:
for i in range(3):
self.enemies[i].attack = int(self.attack * 1.25)
print('敌方获得团队buff,攻击力上升25%')
else:
print('大家都没有获得团队buff呢')
# 让双方 Pk 并展示结果
def pk_role(self):
for i in range(3):
print('\n----------------- 【第%s轮】 -----------------' % (i+1))
# 每一局开战前加buff
self.players[i].fight_buff(self.enemies[i],'我方','敌方')#这里的我方,敌方对应了以上类方法fight_buff的敌对玩家属性还有str1,str2
self.enemies[i].fight_buff(self.players[i],'敌方','我方')
input('\n战斗双方准备完毕,请按回车键继续。')
print('--------------------------------------------')

while self.players[i].life >0 and self.enemies[i].life>0:#敌我双方血量都不低于0的时候执行循环
self.enemies[i].life -= self.players[i].attack#血量判断的的依据是我方血量减去敌方攻击力
self.players[i].life -= self.enemies[i].attack
print('我方%s 发起了攻击,敌方%s 剩余血量 %s'%
(self.players[i].name,self.enemies[i].name,self.enemies[i].life))
print('敌方%s 发起了攻击,我方%s 剩余血量 %s'%
(self.enemies[i].name,self.players[i].name,self.players[i].life))
print('--------------------------------------------')
time.sleep(1)
if self.players[i].life <= 0 and self.enemies[i].life> 0:#我方血量小于0并且敌方血量大于0的时候score-1分,我方该回合失败。
print('\n很遗憾,我方%s 挂掉了!'% (self.players[i].name))
self.score -= 1
elif self.players[i].life >0 and self.enemies[i].life<= 0: #我方血量大于0并且敌方血量小于0的时候score+1分,我方该回合胜利。
print('\n恭喜,我方%s 活下来了。'% (self.players[i].name))
self.score += 1
else:
print('\n我的天,他们俩都死了啊!')

# 展示最终结局
def show_result(self):
input('\n请按回车查看最终结果。\n')
if self.score >0:
print('【最终结果】\n你赢了,最终的财宝都归你了!')
elif self.score == 0:
print('【最终结果】\n你没有胜利,但也没有失败,在夜色中灰溜溜离开了奥卢帝国。')
else:
print('【最终结果】\n你输了。炼狱角斗场又多了几具枯骨。')

game = Game()
...全文
48 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧