您好,登錄后才能下訂單哦!
這篇“Python如何實現高配版王者小游戲”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Python如何實現高配版王者小游戲”文章吧。
import pygame import os.path import csv import setting as set import live import game_event import gameui as gi import startupui as si
def run_game(): #初始化pygame庫 pygame.init() #創建時鐘對象(控制幀率) clock=pygame.time.Clock() #實例化設置類,用于導入游戲設置 setting=set.Setting() #設置游戲窗口 screen=pygame.display.set_mode((setting.screen_width,setting.screen_height)) pygame.display.set_caption(setting.screen_caption)
設置不同的組,用于分別處理各種物品間的關系
#玩家組 group_player=pygame.sprite.Group() #玩家的攻擊組 group_attack=pygame.sprite.Group() #敵人組 group_enemy=pygame.sprite.Group() #敵人的攻擊組 group_enemy_attack=pygame.sprite.Group()
實例化ui對象
#showinfo用于在游戲內顯示人物血條等信息 showinfo=gi.Info(setting,screen) #人物選擇按鈕 yi_button=si.MonkeyKingButton(screen,setting) monkey_button=si.YiButton(screen,setting) fox_button=si.FoxButton(screen,setting) bin_button=si.BinButton(screen,setting)
游戲開始界面的按鈕
pve_button=si.PVEButton(screen,setting) pvp_button=si.PVPButton(screen,setting) endless_button=si.EndlessButton(screen,setting) control_button=si.ControlButton(screen,setting) memory_button=si.RecordButton(screen,setting) cooling_button=si.CoolingButton(screen,setting)
游戲背景
select_button=si.SelectButton(screen,setting) win_button=si.WinButton(screen,setting) dead_button=si.DeadButton(screen,setting)
玩家當前選擇的人物標記
player_button_1=si.PlayerButton1(screen,setting) player_button_2=si.PlayerButton2(screen,setting) #空白按鈕 none_button=si.NoneButton(screen,setting) #空白圖像 none_info=gi.ExInfo(screen,none_button,setting.introduce_none)
介紹按鈕作用的圖像
pve_info=gi.ExInfo(screen,pve_button,setting.introduce_pve) pvp_info=gi.ExInfo(screen,pvp_button,setting.introduce_pvp) endless_info=gi.ExInfo(screen,endless_button,setting.introduce_endless) control_info=gi.ExInfo(screen,control_button,setting.introduce_control) record_info=gi.ExInfo(screen,memory_button,setting.introduce_record) cooling_info=gi.ExInfo(screen,cooling_button,setting.introduce_cooling)
按鈕組(繪制時,在前的按鈕會被在后的按鈕覆蓋)
buttons=[select_button,yi_button,monkey_button,fox_button,bin_button, pve_button,pvp_button,endless_button, cooling_button,control_button,memory_button, dead_button,win_button]
標簽按鈕組
choose_buttons=[player_button_1,player_button_2]
介紹按鈕作用的圖像組
button_info_dict={none_button:none_info,pve_button:pve_info,pvp_button:pvp_info, endless_button:endless_info,control_button:control_info, memory_button:record_info,cooling_button:cooling_info} #當前顯示的圖像列表 info_label=[] #存儲模擬剛體運動的列表 rigidbody_list=[] #玩家實例,初始化為戰士 player_1=live.MonkeyKing(setting,screen) player_2=live.MonkeyKing(setting,screen) if not os.path.exists(setting.record_path): #如果游戲記錄文件不存在就新創建一個 with open(setting.record_path,'w',newline="") as f: writer=csv.writer(f) header=["Time","Mode","Winner","1st Score","2st Score","Duration(s)","1st Player","2nd Player","isCooling"] writer.writerow(header)
while True: #繪制背景 screen.blit(setting.screen_surface_background,(0,0)) #設置游戲幀率 clock.tick(setting.fps) #檢測鍵盤鼠標事件 game_event.check_event(setting,screen,group_player,group_attack,group_enemy, group_enemy_attack,buttons,showinfo,button_info_dict,info_label)
更新當前選擇人物的標簽
game_event.update_choose(setting,buttons,choose_buttons)
游戲運行,非玩家對抗模式
if (setting.game_active and (setting.game_mode==0 or setting.game_mode==2)):
人物初始化
if(not setting.isinit): if setting.player_1!=None: player_1=setting.player_1 group_player.add(player_1) if setting.player_2!=None: player_2=setting.player_2 group_player.add(player_2) setting.isinit=True #游戲計時器 setting.timer+=1 #更新玩家 group_player.update() #生成敵人 game_event.generate_enemies(setting,group_enemy,screen)
更新敵人,玩家的攻擊,敵人的攻擊,玩家狀態等
game_event.update_enemies(setting,showinfo,screen,group_player,group_enemy,group_attack,group_enemy_attack) game_event.update_attacks(setting,screen,group_attack,group_enemy,rigidbody_list) game_event.update_enemy_attacks(setting,screen,group_player,group_enemy_attack,rigidbody_list) game_event.update_state(setting,showinfo) game_event.update_rigidbody(setting,rigidbody_list)
勝利條件
if setting.timer>=60*setting.fps and not group_enemy.spritedict and setting.game_mode==0: game_event.game_win(setting,showinfo,group_enemy,group_attack,group_enemy_attack) setting.timer=0
失敗條件
if setting.isinit and ((setting.player_1!=None and setting.health_1<=0) or (setting.player_2!=None and setting.health_2<=0)): game_event.game_dead(setting,showinfo,group_enemy,group_attack,group_enemy_attack) setting.timer=0
玩家對抗模式
elif setting.game_active and setting.game_mode==1:
人物初始化
if(not setting.isinit): if setting.player_1!=None and setting.player_2!=None: player_1=setting.player_1 group_player.add(player_1) player_2=setting.player_2 group_player.add(player_2) setting.isinit=True
游戲計時器
setting.timer+=1
更新玩家
player_1.update() player_2.update()
更新玩家的攻擊,信息顯示和物理模擬
game_event.update_attacks_pvp(setting,screen,group_attack,rigidbody_list) game_event.update_state(setting,showinfo) game_event.update_rigidbody(setting,rigidbody_list)
玩家1勝利條件
if setting.isinit and setting.health_2<=0: setting.score_1+=1 game_event.game_win(setting,showinfo,group_enemy,group_attack,group_enemy_attack) setting.timer=0
玩家2勝利條件
if setting.isinit and setting.health_1<=0: setting.score_2+=1 game_event.game_win(setting,showinfo,group_enemy,group_attack,group_enemy_attack) setting.timer=0
根據上述更新的結果繪制整個游戲窗口
game_event.update_screen(setting,screen,group_player,group_attack,group_enemy,group_enemy_attack, showinfo,buttons,info_label,choose_buttons) #運行游戲 run_game()
以上就是關于“Python如何實現高配版王者小游戲”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。