LoginSignup
2
5

More than 5 years have passed since last update.

【自分用】バッチファイルでRPG、ADVゲームを作成するときのテンプレ

Last updated at Posted at 2016-02-04

内部の細かい説明は各ラベル先頭に。
使用済みの変数は都度確認するのを忘れずに。

フォルダ構造
game(任意)
 これ.bat
 SaveData
  Save1.txt
  Save2.txt
   :
 DisplayText
  表示する文章置き場

現在実装しているラベルの機能は、
・ゲーム内で一度も変化しない変数の設定(:Initialize)
・データのセーブ、ロード(:DataSave :DataLoad)
・数字用のゲージ作成(:GaugeSet)
・全ステータスの列挙、書き出し(:DataDisplay)
・任意txtファイルの削除(:FileDelete)
・GUI選択肢とその上に表示するメニューや質問文の表示(:select)
・GUI選択肢用のカーソル移動計算(:Cursor :Cursor_0,1,2,3,4)

 使いたいという奇特な同志がいれば、改変含め(当然ですが)ご自由にどうぞ。
 その際、「.batでゲーム作ろうな!」と周りに言いまくってくれると嬉しいです
 質問あればコメントに是非。

Game.bat
@echo off
setlocal enabledelayedexpansion
cd /d %~dp0

:Main1
rem 起動時最初だけ行うメインプロセス
    call :Initialize
    call :DataLoad
    goto Main2

:Main2
rem データロード終了後のメインプロセス(マイページ的な)
    call :DataSave
    exit

:Initialize
rem 起動後の後の設定
    set fP_d=%cd%\DisplayText
    set fP_s=%cd%\SaveData
    exit /b

:DataLoad
rem 全セーブデータの中身を取得、プレイするデータを選ばせて決定させる
rem status[0]=セーブしているデータの個数
rem status[1~個数]=各データの名前(nameとか)
rem status[[ファイル番号][1~個数]=各データのステータスの中身
    set /a existfile+=1,k=0
    if exist %fP_s%\Save%existfile%.txt (
        for /f %%m in (%fP_s%\Save%existfile%.txt) do (
            set /a k+=1
            set status[%existfile%][!k!]=%%m
        )
        goto DataLoad
    ) else set /a existfile-=1
    for /f "delims=" %%m in (%fP_s%\template.txt) do (
        set status[!k!]=%%m
        set /a k+=1
    )
    call :Cursor 0 %existfile% 1
    for /l %%i in (1,1,%existfile%) do set command[%%i][1]=Save%%i:!status[%%i][1]!
    echo セーブデータ選択(%%cursorm%%/%arraym%)>%fP_d%\Temp.txt
    call :Select 0 0 j
    set selectdata=%cursorm%
    for /l %%i in (1,1,%status[0]%) do set !status[%%i]!=!status[%selectdata%][%%i]!
    cls
    call :Cursor 0 2 1
    set command[1][1]=はい
    set command[2][1]=いいえ
    call :FileDelete %fP_d%\Temp
    call :DataDisplay 2 0 %fP_d%\Temp
    echo このデータで始めてよろしいですか?>>%fP_d%\Temp.txt
    call :Select 0 0 j
    if %cursorm% == 2 set existfile=0& goto DataLoad
    exit /b

:DataSave
    call :DataDisplay 1 1 %fP_s%\Save%selectdata%
    exit /b

:Select
rem WASDを含む選択肢の表示
rem 呼び出し時のarrayを使用、コマンドはcommand[m][n]に格納しておく
rem 引数↓
rem 0/Temp.txtを表示するなら0、しないなら1/wasd以外の選択肢…実行
rem 1/選択肢の上に表示する文章を書き込んだtxtのファイル名…%2.txtの中身を追加
rem 2/カーソル番号によって読み込むファイルが変わるとき、その変数名の配列以前…!%2[m][n]!.txtの中身を追加
rem 返り値...選択肢のerrorlevel(出力はerrorlevel)(1~4はwasdなので、5~が帰ってくる。何番目が選ばれたのかはcursorm,n)
    if %1 == -1 del /q %fP_d%\Temp.txt
    if %1 == 1 for /f %%m in (%fP_d%\%3.txt) do call echo %%m
    if %1 == 2 for /f %%m in (%fP_d%\!%3[%cursorm%][%cursorn%]!.txt) do call echo %%m
    if %1 == 10 call :%2 %3 %4 %5 %6 %7 %8 %9
    if %1 neq 0 exit /b
    for /l %%i in (1,1,%arraym%) do (
        set commandlist[%%i]= 
        for /l %%j in (1,1,%arrayn%) do (
            set commandlist[%%i]=!commandlist[%%i]!!cursorl[%%i][%%j]!!command[%%i][%%j]!!cursorr[%%i][%%j]!
        )
    )
    cls
    if %2 == 0 for /f %%m in (%fP_d%\Temp.txt) do call echo %%m
        for /l %%i in (1,1,%arraym%) do (
        echo !commandlist[%%i]!
    )
    choice /c wasd%3 >nul
    if %errorlevel% leq 4 (
        call :Cursor %errorlevel%
        goto Select
    )
    exit /b

:DataDisplay
rem 全ステータスを、「ステータス名:ステータス」の形で列挙
rem 引数1…0なら表示、1なら書き込み(新規)、2なら書き込み(追記)
rem 引数2…0なら全部、1ならステータスだけ(ステータス名無し)
rem 引数3…書き込みの場合ファイル名
    if %1 == 0 if %2 == 0 for /l %%i in (1,1,%status[0]%) do call call echo %%%%status[%%i]%%%%:%%%%%%status[%%i]%%%%%%
    if %1 == 0 if %2 == 1 for /l %%i in (1,1,%status[0]%) do call call echo %%%%%%status[%%i]%%%%%%
    if %1 == 1 call :FileDelete %3
    if %1 geq 1 if %2 == 0 for /l %%i in (1,1,%status[0]%) do call call echo %%%%status[%%i]%%%%:%%%%%%status[%%i]%%%%%%>>%3.txt
    if %1 geq 1 if %2 == 1 for /l %%i in (1,1,%status[0]%) do call call echo %%%%%%status[%%i]%%%%%%>>%3.txt
    exit /b

:GaugeSet
rem 引数1…分子の数値or数値が入っている変数
rem 引数2…分母の数値or数値が入っている変数
rem 引数3…ゲージの文字列を代入したい変数名
rem 引数4…ゲージのサイズ(100の約数)
    set /a tempint1=%1*%4/%2
    set /a tempint2=%1*%4%%%2
    set gauge=
    set /a blank=0,division=0
    for /l %%i in (1,1,%4) do (
        if %%i leq %tempint1% (
            set tempstr1=set division=%%i
        ) else (
            if !tempint2! neq 0 (
                set tempstr1=set tempint2=0
            ) else (
                set tempstr1=)
        )
        set gauge=!gauge!!tempstr1!
    )
    set %3=%gauge% (%1/%2)
    exit /b

:FileDelete
    del /q %1.txt
    exit /b

:Cursor
rem カーソル移動関連
rem 引数...01234(/arraym/arrayn)
    if not "%3" == "" set /a arraym=%2,arrayn=%3
    set cursorl[%cursorm%][%cursorn%]= 
    set cursorr[%cursorm%][%cursorn%]= 
    call :Cursor_%1
    set cursorl[%cursorm%][%cursorn%]=set cursorr[%cursorm%][%cursorn%]=exit /b
:Cursor_0
rem カーソル初期化
    for /l %%i in (1,1,!arraym!) do (
        for /l %%j in (1,1,!arrayn!) do (
            set cursorl[%%i][%%j]= 
            set cursorr[%%i][%%j]= 
        )
    )
    set /a cursorm=1,cursorn=1
    exit /b
:Cursor_1
rem カーソル移動(上)
    set /a cursorm=(cursorm+arraym-2)%%arraym+1
    exit /b
:Cursor_2
rem カーソル移動(左)
    set /a cursorn=(cursorn+arrayn-2)%%arrayn+1
    exit /b
:Cursor_3
rem カーソル移動(下)
    set /a cursorm=cursorm%%arraym+1
    exit /b
:Cursor_4
rem カーソル移動(右)
    set /a cursorn=cursorn%%arrayn+1
    exit /b
2
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
5