ホームページ  >  記事  >  バックエンド開発  >  King of Games のハイエンド バージョンを Python で実装する方法

King of Games のハイエンド バージョンを Python で実装する方法

王林
王林転載
2023-04-27 19:07:041572ブラウズ

エフェクト表示

King of Games のハイエンド バージョンを Python で実装する方法

必須素材

King of Games のハイエンド バージョンを Python で実装する方法

King of Games のハイエンド バージョンを Python で実装する方法

King of Games のハイエンド バージョンを Python で実装する方法

King of Games のハイエンド バージョンを Python で実装する方法

King of Games のハイエンド バージョンを Python で実装する方法

King of Games のハイエンド バージョンを Python で実装する方法

King of Games のハイエンド バージョンを Python で実装する方法

King of Games のハイエンド バージョンを Python で実装する方法

King of Games のハイエンド バージョンを 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()

以上がKing of Games のハイエンド バージョンを Python で実装する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はyisu.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。