我最近遇到了一个批处理程序的问题,它基本上是一个未完成的平台游戏:我的代码正在进行中,我遇到了我的碰撞检测尝试。我有一个矩阵M,我可以在这样的EnableDelayedExpansion块中访问!M [%% a] [%% d]! 它第一次工作,但第二次使用不再起作用: echo A!M [%xx%,%yy%]!B产生AB。
我很感激任何提示。
提前致谢!
@echo off
::creating a new line variable for multi line strings
set NLM=^
:: Two empty lines are required here
::set up initial grid
set height=9
set width=14
for /l %%a in (0,1,9) do (
for /l %%d in (0,1,14) do (
set M[%%a][%%d]=.
)
)
::create some vars at an initial value
set falling=0
set row=5
set turns=0
set x=8
set face=empty
:turn
set M[%row%][%x%]=^P
::set wall position
set x2=4
set y2=6
set M[%y2%][%x2%]=^#
::display current grid
setlocal EnableDelayedExpansion
set "grid="
for /l %%a in (0,1,9) do (
set line=!M[%%a][0]!
for /l %%d in (1,1,14) do (
set line=!line!!M[%%a][%%d]!
)
set grid=!grid! !NLM! !line!
)
cls
echo !grid!
endlocal
::prompt and make move
:: 4 = d
choice /c:wasd /n /m "" /t 1 /d s
set move=0
set /a xx=x
set /a yy=row
:move clearing Player
set M[%row%][%x%]=.
:: ^ this clears the characters trail so that there is no trail left behind after
:: moves, even when blocked. But at the start of grid Player is drawn again.
if %errorlevel%==4 ( if %x% LSS %width% ( set face=right
set /a xx=x+1) else ( set face=empty))
::if %errorlevel%==3 ( if %row% LSS %height% ( set face=down
::set /a move=0) else ( set face=empty))
if %errorlevel%==2 ( if %x% GTR 0 ( set face=left
set /a xx=x-1) else ( set face=empty))
if %errorlevel%==1 ( if %row% GTR 0 ( set face=up
set /a move=1) else ( set face=empty))
::falling!
set /a yy=yy-move
if %move%==0 (
set /a falling+=1
set face=down
) else (
set falling=0
)
set /a yy=yy+falling
::collision tests
setlocal enableDelayedExpansion
::echo !M[%xx%,%yy%]! > tmp.var
echo A!M[%xx%,%yy%]!B
endlocal
::set /P var1="" < tmp.var
::if %var1%==# (
:: set face=empty
::)
::now the actual movements
::if %face%==right set /a x+=1
::if %face%==left set /a x-=1
::if %face%==up set /a row=yy
::if %face%==down set /a row=yy
set /a x=xx
set /a row=yy
::increment turns, return to top
set /a turns+=1
goto :turn