为什么我的CMD / Batch游戏不起作用?

时间:2014-04-25 14:21:20

标签: batch-file cmd

好吧,如果您将此代码放在批处理文件中:

  1. 它不会选择一个随机数(或者它是否总是有相同的结果)
  2. 如果你选择战斗,它似乎失败或胜利
  3. 我一直在教自己编码,这是我第一次参加论坛,所以请放轻松我:)
  4. 这是我的代码:

    @echo off
    title template
    color 0F
    pause
    
    :menu
    cls
    echo 1.start
    echo 2.instructions
    echo 3.exit
    set /p answer=Type the number of your option and press enter
    if %answer%==1 goto start_1
    if %answer%==2 goto instructions
    if %answer%==3 goto exit
    
    :exit
    echo thanks for playing
    pause
    exit /b
    
    :instructions
    cls
    echo instructions
    echo.
    echo This game is case-sensitive!
    echo Just have fun with it!
    pause
    goto menu
    
    :start_1
    set /a s1=%random% * 3 / 32768 + 1
    if %s1%==1 goto prefight_1
    if %s1%==2 goto prefight_2
    if %s1%==3 goto prefight_3
    
    :prefight_1
    cls
    echo You have discovered 3 Turtles!
    echo They dont see you!
    set /p answer=would you like to (1)FIGHT or (2)RUN?
    if %answer%==1 goto fight_1
    if %answer%==2 goto run_1
    
    :fight_1
    set /a f1=%random% * 4 / 32768 + 1
    if %f1%==1 goto lose_fight_1
    if %f1%==2 goto win_fight_1
    if %f1%==3 goto win_fight_1
    if %f1%==4 goto win_fight_1
    
    :prefight_2
    cls
    echo You have discovered 3 Turtles!
    echo They see you!
    set /p answer=would you like to (1)FIGHT or (2)RUN?
    if %answer%==1 goto fight_2
    if %answer%==2 goto run_1
    
    :fight_2
    set /a f2=%random% * 4 / 32768 + 1
    if %f2% gtr 4 goto fight_2
    if %f2% lss 1 goto fight_2
    if %f2%==1 goto lose_fight_1
    if %f2%==2 goto lose_fight_1
    if %f2%==3 goto win_fight_1
    if %f2%==4 goto win_fight_1
    
    :prefight_3
    cls
    echo You have discovered 3 Turtles!
    echo They see you!
    echo They seem angry!
    set /p answer=would you like to (1)FIGHT or (2)RUN?
    if %answer%==1 goto fight_3
    if %answer%==2 goto run_1
    
    :fight_3
    set /a f3=%random% * 4 / 32768 + 1
    if %f3%==1 goto lose_fight_1
    if %f3%==2 goto lose_fight_1
    if %f3%==3 goto lose_fight_1
    if %f3%==4 goto win_fight_1
    
    :lose_fight_1
    cls
    echo Sorry,You LOST!
    echo Thank you for playing!
    echo made by: JEREMY
    set /p answer==(1)continue or (2)quit?
    if %answer%==1 goto start_1
    if %answer%==2 goto menu
    pause
    
    :run_1
    cls
    echo You ran away
    pause
    goto start_1
    

3 个答案:

答案 0 :(得分:1)

首先,人类在“等于”而非“==”时阅读会更好。 设置/ p VARIABLE =在清除时更容易使用,所以像这样使用echo:

:Jelly_Attack_1
cls
echo AH! There's a swarm of angry jellyfish!
echo Act fast!
echo.
echo You can (1) fight or (2) RUN
set /p VARIABLE=
if %VARIABLE% equ 1 goto Fight_Jelly_1
if %VARIABLE% equ 2 goto Run
if %VARIABLE% neq 1 goto TEST

战斗部分看起来应该是这样的:

:Fight_Jelly_1
cls
echo The jellyfish are more annoying than harmful!
echo You have a great advantage!
pause
set /a FIGHTNO=%random%
if %FIGHTNO% gtr 4 goto Fight_Jelly_1
if %FIGHTNO% lss 1 goto Fight_Jelly_1
if %FIGHTNO% equ 1 goto FAILED
if %FIGHTNO% equ 2 goto Fight_Jelly_1_Win
if %FIGHTNO% equ 3 goto Fight_Jelly_1_Win
if %FightNO% geq 4 goto Fight_Jelly_1_Win

然后,当想要获得金币或奖励时,请执行以下操作:

:Fight_Jelly_1_Win
cls
echo The fight was easy and you found some gold!
pause
set gold=0 (if you didn't have a gold variable)
set /a gold=gold+5 (to add gold)
goto FRAMENAME

我希望我帮忙!

答案 1 :(得分:0)

假随机 - 我确认%random%由系统时钟 - http://blogs.msdn.com/b/oldnewthing/archive/2010/06/17/10026183.aspx

驱动

使用时钟MS获得1到1000的随机数 这是一种获得毫秒的方法:https://answers.yahoo.com/question/index?qid=20100816021807AAz6eL3

引入随机性的另一种可能性是启动游戏,然后等待用户输入,即准备开始?"当它们启动时会在cmd.exe启动后的时间内引入一些随机性。

答案 2 :(得分:0)

我强烈怀疑您设置了变量random。如果您已完成此操作,则您在环境中设置的值将覆盖该魔术变量。

您可以按set "random="

清除它

setlocal之后使用@echo off是正常做法。这样,当批次终止时,您对环境所做的任何更改都将被撤消。如果没有它,任何所做的更改都将保留,直到它们再次被明确更改为止。

就个人而言,我更喜欢

设置/值=%随机%%%限制+ 1

生成值1..limit。如果没有其他原因,输入起来会更容易。


@echo off
setlocal
rem environment changes made after here will be backed-out when the batch finishes...
....whatever....

setlocal命令设置了本地'只有在批处理结束或遇到endlocal命令时才存在的环境。

请在提示中查看setlocal /?以获取更多信息。

我添加win_fight_1例程后,您的批处理似乎正常。

但请注意,批次将在set /p answer = = (1)continue or (2)quit?上进行限制,但删除其中一个 = 将解决它。

除此之外,只需观察您是否要转到menustart_1

if %f2% gtr 4 goto fight_2
if %f2% lss 1 goto fight_2

将无效 - 当然,如果您已使用set /a f2=%random% %% 4 + 1