批处理游戏中的未知错误

时间:2015-06-15 22:47:10

标签: batch-file

我不知道自己做错了什么。任何帮助,将不胜感激。 我试图运行测试以查看战斗标签是否有效,但在创建播放器后发生错误。我认为这个问题与传递给战斗标签的变量有关,但我不确定。所有评论都表示赞赏,谢谢。 这是我的批处理文件:

@echo off
CLS
GOTO :start

:createPlayer
CLS
SET playerMaxHealth=100
REM playerMaxHealth is always equall to 100.

SET playerCurHealth=100
REM playerCurHealth is between 0 and 100.

SET /A playerStrength=%RANDOM% * 10 / 32768 + 1
REM playerStrength is between 1 and 10.

SET playerWeapon=100
REM playerWeapon is between 100 and 200. It's multiplied by playerStrength to get playerAttack.

SET playerDefense=8
REM playerDefense is used to get playerTotalDefense.

SET playerArmor=100
REM playerArmor is between 100 and 200. It's multiplied with playerDefense to get playerTotalDefense.
GOTO :eof

:fight
REM %1 = playerCurHealth, %2 = playerStrength, %3 = playerWeapon, %4 = playerDefense, %5 = playerArmor
REM %6 = enemyHealth, %7 = enemyStrength, %8 = enemyDefense, %9 = enemyGold
SET playerCurHealth= %1
SET playerStrength= %2
SET playerWeapon= %3
SET playerDefense= %4
SET playerArmor= %5
SET enemyHealth= %6
SET enemyStrength= %7
SET enemyDefense= %8
SET enemyGold= %9

:repeat
CALL :enemysTurn %playerCurHealth% %playerDefense% %playerArmor% %enemyStrength%
IF %playerCurHealth% LEQ 0 (
    SET win=false
    GOTO :lost
)
CALL :playersTurn %playerStrength% %playerWeapon% %enemyHealth% %enemyDefense%
IF %enemyHealth% LEQ 0 (
    SET win=true
    GOTO :won %enemyGold%
)
PAUSE
GOTO :repeat

:enemysTurn
SET playerCurHealth= %1
SET playerDefense= %2
SET playerArmor= %3
SET enemyStrength= %4
SET /A attack= %enemyStrength% * ((100 + (%RANDOM% * 10 / 32768 + 1)) / 100)
SET /A defense= 100 - (%playerDefense% * (%playerArmor% / 100))
SET /A damage= %attack% * %defense% / 100
SET /A playerCurHealth= %playerCurHealth% - %damage%
ECHO Player health: %playerCurHealth%
GOTO :eof

:playersTurn
SET playerStrength= %1
SET playerWeapon= %2
SET enemyHealth= %3
SET enemyDefense= %4
SET /A attack= (%playerStrength% * (%playerWeapon% / 100)) * ((100 + (%RANDOM% * 10 / 32768 + 1)) / 100)
SET /A defense= 100 - %enemyDefense%
SET /A damage= %attack% * %defense% / 100
SET /A enemyHealth= %enemyHealth% - %damage%
ECHO Enemy Health: %enemyHealth%
GOTO :eof

:start
CLS
CALL :createPlayer
ECHO %playerMaxHealth%
ECHO %playerCurHealth%
ECHO %playerStrength%
ECHO %playerWeapon%
ECHO %playerDefense%
ECHO %playerArmor%
PAUSE
GOTO :fight %playerCurHealth% %playerStrength% %playerWeapon% %playerDefense% %playerArmor% 70 5 7 50

编辑:错误是缺少操作数。我希望标签重复运行,直到其中一个健康状况等于或低于零,然后会因为没有赢或输标签而抛出错误。我发现当文件尝试使用变量 GOTO:fight 时会发生问题。

1 个答案:

答案 0 :(得分:0)

而不是GOTO :fight %playerCurHealth% ...,请使用CALL :fight %playerCurHealth% ...

  

The CALL command会将控制传递给后面的语句   指定的标签以及任何指定的参数。