需要关于构建更大规模批处理文件游戏的建议



在过去的一年里,我开始熟悉Batch,所有的都是自学的,我已经做了一些小的项目,但我想找到最好的方法来扩展我的代码。从其他程序中获得灵感,我学到的只是"for循环"的冰山一角。并使用函数和小宏进程。我想知道在架构和规划方面我是否应该做一些不同的事情,这样我就可以有一天做出像T3RROR这样令人惊叹的批处理程序(有点奇怪,但我是一个大粉丝,你是我的批处理偶像)。

以下是我编写的最复杂的批处理代码,也是未来游戏的模板,请添加任何提示或建议。

@echo off
SETLOCAL EnableDelayedExpansion
title crafting micro
color 2
:://///////////////////////////////////////////////////////////////////////
:pre_start
::initializes at the start of the program
set /a display_message=0
set /a inv_fiber=1
set /a fiber_qty=100
set /a inv_rope=0
set /a rope_qty=0
set /a inv_stick=1
set /a stick_qty=100
set /a inv_stone=1
set /a stone_qty=100
set /a inv_sharpened_stone=0
set /a sharpened_stone_qty=0
set /a inv_spear=0
set /a spear_qty=0
set /a inv_handaxe=0
set /a handaxe_qty=0
set /a inv_hammer=0
set /a hammer_qty=0
:://///////////////////////////////////////////////////////////////////////
:start
cls
echo ----------------------------------------------------
echo         Crafting Menu
echo ----------------------------------------------------
echo.
echo Raw materials
echo -------------
if %inv_fiber%==1 echo Fiber: x%fiber_qty%
if %inv_stick%==1 echo Stick: x%stick_qty%
if %inv_stone%==1 echo Stone: x%stone_qty%
echo.
echo Refined Materials
echo -----------------
if %inv_rope%==1 echo Rope: x%rope_qty%
if %inv_sharpened_stone%==1 echo Sharpened Stone: x%sharpened_stone_qty%
echo.
echo Tools
echo -----
if %inv_spear%==1 echo Spear: x%spear_qty%
if %inv_handaxe%==1 echo Handaxe: x%handaxe_qty%
if %inv_hammer%==1 echo Hammer: x%hammer_qty%
::sets visibility
echo.
echo ----------------------------------------------------
echo.
echo What do you wanna craft?
echo 1. Rope (x3 Fiber)
echo 2. Sharpened Stone (x2 Stones)
echo 3. Spear (x1 Rope, x1 Stick, x1 Sharpened Stone )
echo 4. Handaxe (x2 Rope, x2 Stick, x1 Sharpened Stone)
echo 5. Hammer (x2 Rope, x2 Stick, x1 Stone)
echo.
choice /c 12345
echo.
echo.
if %errorlevel%==1 (
set item=Rope
::item being crafted
set /a req1=%fiber_qty%
::1st required item
set /a min1=3
:: minimum amount needed to craft
set /a req2=0
set /a min2=0
set /a req3=0
set /a min3=0
set var1=fiber
::passes a string to be use for a vavriable
set var2=
set var3=
set var4=rope
)
if %errorlevel%==2 (
set item='Sharpened Stone'
set /a req1=%stone_qty%
set /a min1=2
set /a req2=0
set /a min2=0
set /a req3=0
set /a min3=0
set var1=stone
set var2=
set var3=
set var4=sharpened_stone
)
if %errorlevel%==3 (
set item=Spear
set /a req1=%rope_qty%
set /a min1=1
set /a req2=%stick_qty%
set /a min2=1
set /a req3=%sharpened_stone_qty%
set /a min3=1
set var1=rope
set var2=stick
set var3=sharpened_stone
set var4=spear
)
if %errorlevel%==4 (
set item=Handaxe
set /a req1=%rope_qty%
set /a min1=2
set /a req2=%stick_qty%
set /a min2=2
set /a req3=%sharpened_stone_qty%
set /a min3=1
set var1=rope
set var2=stick
set var3=sharpened_stone
set var4=handaxe
)
if %errorlevel%==5 (
set item=Hammer
set /a req1=%rope_qty%
set /a min1=2
set /a req2=%stick_qty%
set /a min2=2
set /a req3=%stone_qty%
set /a min3=1
set var1=rope
set var2=stick
set var3=stone
set var4=hammer
)
::////////////////////////////////////////////////////////////////////////////////////
:craftable_check
if %req1% GEQ %min1% (  
if %req2% GEQ %min2% (
if %req3% GEQ %min3% (
::checks minimum requried material inventory for crafting
set /a %var1%_qty=!%var1%_qty!-%min1%
set /a %var2%_qty=!%var2%_qty!-%min2%
set /a %var3%_qty=!%var3%_qty!-%min3%
::uses all the required inventory for crafting
set /a %var4%_qty=!%var4%_qty!+1
::creates the crafted item
set /a inv_%var4%=1
::makes the new item is visible
if %req1% LEQ 0 (
set inv_%var1%=0
)
if %req2% LEQ 0 (
set inv_%var2%=0
)
if %req3% LEQ 0 (
set inv_%var3%=0
)
::makes the items ivisible if there is no inventory
set /a display_message=1
set message='You have successfully created a %item%!'
goto message
)
)
)
set /a display_message=1
set message=You dont have enough material for this
goto message
:://///////////////////////////////////////////////////////////////////////////////////
:message
cls
if %display_message%==1 (
echo %message%
pause
set /a display_message=0
goto start
)
::General Settings ------------------------------------------------------------------------------
@echo off
title Game Template
color 02
cd "%userprofile%desktop"
::Game Settings ---------------------------------------------------------------------------------
:game_settings
cls
goto welcome
::Welcome Screen --------------------------------------------------------------------------------
:welcome
cls
echo Welcome to this Game!
echo/
echo Please select one of the options below
echo 1. New Game
echo 2. Load Game
echo 3. Credits
choice /c 123
if %errorlevel%==1 goto back_story
if %errorlevel%==2 goto load_game
if %errorlevel%==3 goto credits
goto welcome
::Save Game -------------------------------------------------------------------------------------
:save_game
cls
(
echo %location%
echo %player_name%
)>"GameSave.txt"
echo Game saved...
pause
goto :eof
::Load Game -------------------------------------------------------------------------------------
:load_game
cls
(
set /p location=
set /p player_name=
)<"GameSave.txt"
pause
goto %location%
::Back Story ------------------------------------------------------------------------------------
:back_story
cls
echo Backstory...
pause >nul
goto character_creation
::Character Creation ----------------------------------------------------------------------------
:character_creation
cls
echo What is your name?
set /p player_name=
if /i "%player_name%"=="" (
echo Player name can not be blank.
pause >nul
goto character_creation
)
goto start
::Game Start ------------------------------------------------------------------------------------
:start
cls
set location=start
echo %player_name%
echo %location%
echo/
echo 1. Pause Menu
choice /c 1
if %errorlevel%==1 goto pause_menu
goto start
::Pause Menu ------------------------------------------------------------------------------------
:pause_menu
cls
echo 1. Save Game
echo 2. Return
echo 3. Quit
choice /c 123
if %errorlevel%==1 call :save_game
if %errorlevel%==2 goto %location%
if %errorlevel%==3 goto exit
goto pause_menu
::Credits ---------------------------------------------------------------------------------------
:credits
cls
echo credits...
pause >nul
goto welcome
if %req3% GEQ %min3% (
::checks minimum requried material inventory for crafting
set /a %var1%_qty=!%var1%_qty!-%min1%

不能正常工作-::注释实际上是破碎的标签(不能用goto到达的标签)和破碎的代码块(括号内的一系列语句)

为您可能希望保存到文件中以便稍后重新加载的任何变量保留前缀字符。EG#

set #>filename

将启动#的所有变量保存到#whatever=something格式的文件中。

与重新加载

for /f "delims=" %%e in (filename) do set "%%e"

同样,您可以通过选择另一个前缀字符来设置场景文件。

清除变量的当前值-开始-# is

for/f "delims==" %%e in ('set # 2^>nul') do set "%%e="

在从文件加载之前执行。


使用set "var=value"设置字符串值-这避免了尾随空格引起的问题。不要给终端分配、Space或"——从元素构建路径名——与直觉相反,这可能会使过程更容易。如果使用语法set var="value",则引号成为赋值的一部分。


使用choice设置菜单子程序

:menu
set "choices="
set /a choicecount=0
cls
:menu_next
set /a choicecount+=1
if defined choicetext[%choicecount%] echo %choicecount%. choicetext[%choicecount%]&set "choices=%choices%%choicecount%"
if %choicecount% lss 9 goto menunext
echo Q. Quit
choice /c q%choices% /N /M "%~1"
goto :eof
这样,您可以在choicetext[?]中设置可用的选项(并使用 清除它们)。
for/f "delims==" %%e in ('set choicetext[ 2^>nul') do set "%%e="

),然后通过执行

显示菜单
call :menu "Some message"

当:menu"返回,然后可以

GOTO Someplace%errorlevel%

goto someplace2等。请注意,因为q总是choices的第一个字符,那么q将总是返回errorlevel 1,因此标签:whatever1将意味着q was pressed。这样,当您扩展菜单时,就不需要修改quit代码。

(X for eXit也可以)

最新更新