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.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))
order_dict[order] = self.players[i]
self.players = []
for i in range(1,4):
self.players.append(order_dict[i])
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))
# 讓雙方 Pk 并展示結果
def pk_role(self):
for i in range(3):
print('\n----------------- 【第%s輪】 -----------------' % (i+1))
# 每一局開戰前加buff
self.players[i].fight_buff(self.enemies[i],'我方','敵方')
self.enemies[i].fight_buff(self.players[i],'敵方','我方')
input('\n戰斗雙方準備完畢,請按回車鍵繼續。')
print('--------------------------------------------')
while self.players[i].life >0 and self.enemies[i].life>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:
print('\n很遺憾,我方%s 掛掉了!'% (self.players[i].name))
self.score -= 1
elif self.players[i].life >0 and self.enemies[i].life<= 0:
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()
這串代碼其他都懂。就是有一行不理解:(如圖)
為什么opponent后面要加.name 這是什么意思?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/132410.html
標籤:其他開發語言
