批处理文件RPG游戏错误

时间:2015-02-18 13:03:08

标签: batch-file cmd

@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

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

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

if "%level%" == "1" (
    if "%c%" == "1" goto encounter2
    )
    if "%level%" == "0" (
    if "%c%" == "1" goto encounter1
    )

1 个答案:

答案 0 :(得分:0)

你的问题源自这些方面:

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

SET语句中的所有空格都很重要,因此您的值包含前导空格。您的if "%level%" == "0" (语句会扩展为if " 0" == "0" (,这当然是错误的。

从SET语句中删除前导空格,应立即解决您的问题。 (我还没有评估你的整个代码库,看看是否还有其他问题)

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

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