代碼中第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()

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/282027.html
上一篇:gis中合并記憶體不足問題求助
下一篇:希望大佬能解決下
