Batch file 批处理文件RPG游戏错误

Batch file 批处理文件RPG游戏错误,batch-file,cmd,Batch File,Cmd,这就是我的代码。我想做的基本上是,如果我是1级或0级,它会让我去其他地方。 例如,如果我是0级 后藤遭遇1 如果我是三级 后藤遭遇4 但我不知道怎么做。我想我在这一行很接近: @echo off TITLE Zombie Warrior setlocal enabledelayerdexpansion :new set playerdmg= 23 set zombiedmg= 24 set coin= 0 set rewards= 10 set level= 0 goto refresh :

这就是我的代码。我想做的基本上是,如果我是1级或0级,它会让我去其他地方。 例如,如果我是0级 后藤遭遇1 如果我是三级 后藤遭遇4

但我不知道怎么做。我想我在这一行很接近:

@echo off
TITLE Zombie Warrior
setlocal enabledelayerdexpansion


:new
set playerdmg= 23
set zombiedmg= 24
set coin= 0
set rewards= 10
set level= 0
goto refresh
:refresh
set health=100
set zombiehealth=200
set zombie2health=400
goto menu
:menu
cls
echo.
echo Zombie Warrior
echo Coins = %coin%
echo.
echo 1) Play!
echo 2) Exit.
echo 3) Shop
echo.
set /p c=C:\

if "%c%" == "1" goto home
if "%c%" == "2" exit
if "%c%" == "3" goto shop
goto menu
set health=100
set zombiehealth=200
:home
cls
echo Welcome to the game!
echo -+-+-+-+-+-+-+-+-+-+-+-+-
echo Coins: %coin% 
echo Level: %level%
echo.
echo 1) FIGHT!
echo 2) Quit
set /p c=C:
if "%level%" == "1" (
if "%c%" == "1" goto encounter2
)
if "%level%" == "0" (
if "%c%" == "1" goto encounter1
)
if "%c%" == "2" goto menu
goto home
set health=100
set zombiehealth=200
:encounter1
cls
echo You: %health%
echo Zombies Level 1: %zombiehealth%
echo Your damage: %playerdmg%
echo.
echo 1) Attack
echo 2) Run!
echo.
set /p c=C:\
if "%c%" == "1" goto attack1
if "%c%" == "2" goto refresh
goto encounter1
:encounter2
cls
echo You: %health%
echo Zombies Level 2: %zombie2health%
echo Your damage: %playerdmg%
echo.
echo 1) Attack
echo 2) Run!
echo.
set /p c=C:\

if "%c%" == "1" goto attack2
if "%c%" == "2" goto refresh
goto encounter2

:attack2
set /a zombie2health-=playerdmg
set /a health-=zombiedmg
if %zombie2health% lss 0 goto win1
if %health% lss 0 goto lose
if !zombiehealth! lss 30 set /a coin+=5
goto encounter1
:attack1
set /a zombiehealth-=playerdmg
set /a health-=zombiedmg
if %zombiehealth% lss 0 goto win1
if %health% lss 0 goto lose
if !zombiehealth! lss 30 set /a coin+=5
goto encounter1
:win1
set /a coin+=rewards
set /a level+=1
if level == 1 set /a coin+=10
goto refresh
:lose
cls
echo You lost :(
pause
goto refresh

:shop
cls
echo What would you like to buy? (type q to quit)
echo Coins: %coin%
echo 1) Baseball bat
echo.
set /p c=C:\
if "%c%" == "1" goto bat1
if "%c%" == "q" goto menu


:bat1
if %coin% lss 30 goto nope
set /a playerdmg+=5
set /a coin-=30
goto menu

:nope
echo You don't have enough coins!
pause
goto menu

您的问题源于以下几行:

if "%level%" == "1" (
    if "%c%" == "1" goto encounter2
    )
    if "%level%" == "0" (
    if "%c%" == "1" goto encounter1
    )
在SET语句中,所有空格都是重要的,因此您的值包含一个前导空格。if%level%==0语句扩展为if 0==0,这当然是错误的

从SET语句中删除前导空格,立即出现的问题就会得到解决。我还没有评估您的整个代码库,看看是否还有其他问题

构造SET语句的更好方法是将整个表达式括在引号中。引号表示赋值的结束,但不包括在值中。这可防止值中包含意外的尾随间距

set playerdmg= 23
set zombiedmg= 24
set coin= 0
set rewards= 10
set level= 0

去掉if语句条件部分中的所有空格。将if%level%==1更改为if%level%==1,依此类推。批处理if语句按字面意思使用空格,因此您将%level%与'1'进行比较,而不是将%level%与1进行比较,并且它将始终返回false。另一个错误:命令的语法不正确。您可以在任何地方按字面意思获得该错误。至少在故障排除上下点功夫,还是不行!
set "playerdmg=23"
set "zombiedmg=24"
set "coin=0"
set "rewards=10"
set "level=0"