您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“基于Python如何實現格斗小游戲”,內容詳細,步驟清晰,細節處理妥當,希望這篇“基于Python如何實現格斗小游戲”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
import time import random lose_player = 0 lose_enemy = 0 for i in range(3): print('-'*10+'第'+str(i+1)+'局'+'-'*10) hp_player = random.randint(100,181) attack_player = random.randint(30,51) hp_enemy = random.randint(100,181) attack_enemy = random.randint(30,51) print('【玩家血量】:',hp_player,'【玩家戰力】:',attack_player) print('【敵人血量】:',hp_enemy,'【敵人戰力】:',attack_enemy) print('-'*30) time.sleep(1.5) while hp_player > 0 and hp_enemy > 0: hp_player = hp_player - attack_enemy hp_enemy = hp_enemy - attack_player print('你發起了攻擊,【敵人】剩余血量:',hp_enemy) print('敵人向你發起了攻擊,【玩家】的血量剩余:',hp_player) print('-'*30) time.sleep(1.5) if hp_enemy <= 0 and hp_player < 0: print('玩家和敵人同歸于盡了!') elif hp_enemy < 0 and hp_player >= 0: print('歐耶!敵人被玩家干掉了!') lose_enemy += 1 elif hp_player < 0 and hp_enemy > 0: print('悲催,玩家被敵人干掉了!') lose_player += 1 print('-'*30) time.sleep(1.5) if lose_enemy == 2: print('【最終結果:玩家獲勝!】') break elif lose_player == 2: print('【最終結果:你輸了!】') break else: if lose_enemy > lose_player: print('【最終結果:玩家獲勝!】') elif lose_player > lose_enemy: print('【最終結果:玩家輸了!】') else: print('【最終結果:平局】')
import time import random class Role(): def __init__(self,name = '|角色|'): self.name = name self.life = random.randint(100,150) self.attack = random.randint(30,50) class Sains_knight(Role): def __init__(self,name = '|圣光騎士|'): Role.__init__(self,name) self.life = 5 * self.life self.attack = 3 * self.attack def fight_buff(self,enemy): if enemy == '|暗影刺客|': self.attack *= 1.5 else: self.attack = self.attack class Shadow_Assassin(Role): def __init__(self,name = '|暗影刺客|'): Role.__init__(self, name) self.life = 3 * self.life self.attack = 5 * self.ttack def fight_buff(self,enemy): if enemy == '|精靈弩手|': self.attack *= 1.5 else: self.attack = self.attack class Faerie_Bowman(Role): def __init__(self,name = '|精靈弩手|'): Role.__init__(self, name) self.life = 4 * self.life self.attack = 4 * self.attack def fight_buff(self,enemy): if enemy == '|圣光騎士|': self.attack *= 1.5 else: self.attack = self.attack class GAME(): def __init__(self): self.players = [] self.enemies = [] self.show_title() self.show_role() self.order_role() self.pk_role() # 隨機生成角色的屬性 def born_role(self): for i in range(3): self.players.append(random.choice([Sains_knight(),Shadow_Assassin(),Faerie_Bowman()])) self.enemies.append(random.choice([Sains_knight(),Shadow_Assassin(),Faerie_Bowman()])) # 生成和展示角色信息 def show_role(self): self.born_role() # 展示我方的3個角色 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('敵人隊伍:') # 展示敵方的3個角色 for i in range(3): print('|敵方|%s 血量:%s 攻擊:%s' % (self.enemies[i].name, self.enemies[i].life, self.enemies[i].attack)) print('--------------------------------------------') input('請按回車鍵繼續。') # 為了讓玩家更有控制感,可以插入類似的代碼來切分游戲進程。 # 角色排序,選擇出場順序。 def order_role(self): order_dict = {} for i in range(3): order = int(input('你想讓{}第幾個上場?(輸入數字1-3)'.format(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): round = 1 score = 0 for i in range(3): # 一共要打三局 player_name = self.players[i].name enemy_name = self.enemies[i].name #判斷敵人是否對其有克制效果,有則進行攻擊加成 self.players[i].fight_buff(enemy_name) self.enemies[i].fight_buff(player_name) player_life = self.players[i].life player_attack = self.players[i].attack enemy_life = self.enemies[i].life enemy_attack = self.enemies[i].attack # 每一局開戰前展示戰斗信息 print('\n----------------- 【第%s局】 -----------------' % round) print('玩家角色:%s vs 敵方角色:%s ' % (player_name, enemy_name)) print('%s 血量:%s 攻擊:%s' % (player_name, player_life, player_attack)) print('%s 血量:%s 攻擊:%s' % (enemy_name, enemy_life, enemy_attack)) print('--------------------------------------------') input('請按回車鍵繼續。\n') # 開始判斷血量是否都大于零,然后互扣血量。 while player_life > 0 and enemy_life > 0: enemy_life = enemy_life - player_attack player_life = player_life - enemy_attack print('%s發起了攻擊,%s剩余血量%s' % (player_name, enemy_name, enemy_life)) print('%s發起了攻擊,%s剩余血量%s' % (enemy_name, player_name, player_life)) print('--------------------------------------------') time.sleep(1) else: # 每局的戰果展示,以及分數score和局數的變化。 # 調用show_result()函數,打印返回元組中的result。 print(self.show_result(player_life,enemy_life)[1]) # 調用show_result()函數,完成計分變動。 score += int(self.show_result(player_life,enemy_life)[0]) round += 1 input('\n點擊回車,查看比賽的最終結果\n') if score > 0: print('【最終結果:你贏了!】\n') elif score < 0: print('【最終結果:你輸了!】\n') else: print('【最終結果:平局!】\n') # 返回單局戰果和計分法所加分數。 def show_result(self,player_life, enemy_life): # 注意:該函數要設定參數,才能判斷單局戰果。 if player_life > 0 and enemy_life <= 0: result = '\n敵人死翹翹了,你贏了!' return 1, result # 返回元組(1,'\n敵人死翹翹了,你贏了!'),類似角色屬性的傳遞。 elif player_life <= 0 and enemy_life > 0: result = '\n悲催,敵人把你干掉了!' return -1, result else: result = '\n哎呀,你和敵人同歸于盡了!' return 0, result #展示標題 def show_title(self): print('''--------------歡迎來到煉獄角斗場----------------- 在昔日的黃昏山脈,陸奧帝國的北境邊界上,有傳說中的‘煉獄角斗場'! 鮮血與戰斗是角斗士的歸宿,金錢與榮耀是角斗士的信仰! 今日,只要【你的隊伍】能取得勝利,你將獲得一筆夠花500的財富! 將隨機生成【你的隊伍】和【敵人隊伍】!''') input('\n狹路相逢勇者勝,話不投機半句多(按任意鍵繼續...)') GAME_START = GAME()
讀到這里,這篇“基于Python如何實現格斗小游戲”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。