批脚本错误:如何允许玩我的游戏的用户通过菜单切换游戏本身的颜色



基本上,我感兴趣的是允许用户玩我的游戏,通过设置菜单,切换游戏中(和菜单中(文本的颜色。如果你运行我的代码,并通过设置/颜色设置/从主菜单中选择一种颜色,它将正确选择颜色,但一旦你退出颜色设置菜单。它会将颜色恢复为紫色或某些奇怪的原因,而不是您选择的颜色。我该如何解决这个问题?这是代码。。。

@echo off
title KnightsOval 
cls
title RPG Game
color e
mode 80,25
goto titlescreen
:titlescreen
if "%color%"=="1" ( set "%color%"="e")
if "%color%"=="2" ( set "%color%"="07")
if "%color%"=="3" ( set "%color%"="03")
if "%color%"=="4" ( set "%color%"="0c")
color %color%
cls
echo.
echo                                       ======
echo                                     Welcome To
echo                                     KnightRealm
echo                                       ======
echo.
echo                                     [1] =Play=
echo                                     [2] =Settings=
echo                                     [3] =Exit=
echo.
echo   Made by: MS-DOS Guy
echo  ===============================================================================
set /p selection1=">" 
if /i "%selection1%"=="1" goto StartGame
if /i "%selection1%"=="2" goto Settings
if /i "%selection1%"=="3" exit
:StartGame
cls
echo.
echo.
echo.
echo.
echo                            Far away...
color 01
ping 127.0.0.1 -n 4 > nul 
cls
echo.
echo    In a Land...
color 02
ping 127.0.0.1 -n 4 > nul      
cls    
echo.
echo.
echo                                        Considered to be one of the most 
color 03
ping 127.0.0.1 -n 5 > nul
cls   
echo.
echo.
echo.
echo.
echo.
echo.
echo.     
echo            Dangerous, and Bandit filled Lands
color 04
ping 127.0.0.1 -n 4 > nul
echo Lies a man, a man seeking to fix humanity's evil doers, and regain peace 
echo and order in the world...
ping 127.0.0.1 -n 4 > nul 
cls
echo             This man's name is
ping 127.0.0.1 -n 2 > nul
cls
echo             This man's name is.
ping 127.0.0.1 -n 3 > nul
cls
echo             This man's name is..
ping 127.0.0.1 -n 3 > nul
cls
ping 127.0.0.1 -n 3 > nul
cls 
echo             This man's name is...
ping 127.0.0.1 -n 2 > nul
cls
echo Ehhh, what is your name young boy? 
echo.
echo.
set /p name="Enter Your Name:"
cls
echo  Ah, good! I see your name is %name%, very nice name, has a nice 
echo  ring to it I suppose. Now! If I may ask, haha, it looks like
echo  I have forgotten your class! 
echo.
echo.
echo ===============================================================
echo  SELECT YOUR CLASS --------------------------------------------
echo ===============================================================
echo  = WARLOCK =
echo.
echo.
echo  = ROGUE =
echo.
echo.
echo  = TITAN =
echo.
set /p class="Enter Your Class:"
if "%class%"=="Warlock" goto next1 
)
if "%class%"=="Titan" goto next1  
)
if "%class%"=="Rogue" goto next1 
)
goto exit 
:color c0
echo PICK A CORRECT CHOICE/COMMAND! 
echo.
echo Redo Y/N? 
goto startgame 
exit 
:next1
if "%color%"=="1" ( set "%color%"="e")
if "%color%"=="2" ( set "%color%"="07")
if "%color%"=="3" ( set "%color%"="03")
if "%color%"=="4" ( set "%color%"="0c")
color %color%
cls
echo  I could have sworn your class was %class%! Sounds about right!
ping 127.0.0.1 -n 4 > nul
cls
echo  Unknown: Eh, anyways, I suppose now its time I introduce who I may be...
echo ================================================================
echo  1) Yeah, who are you?
echo.
echo  2) No, let me embark on my quest! 
echo.
echo ================================================================
set /p choice1=">"
if "%choice1%"=="1" goto continuestory1
if "%choice1%"=="2" goto continuestory2 
:continuestory1
if "%color%"=="1" ( set "%color%"="e")
if "%color%"=="2" ( set "%color%"="07")
if "%color%"=="3" ( set "%color%"="03")
if "%color%"=="4" ( set "%color%"="0c")
color %color%
cls
echo Unknown: Well... I'm glad you asked
ping 127.0.0.1 -n 4 > nul 
goto continuestory3
:continuestory3
color %color%
echo.
echo.
echo.
echo                                      ....
echo                                .'' .'''
echo                              .'   :
echo   \                          .:    :
echo    \                        _:    :       ..----.._
echo     \                    .:::.....:::.. .'         ''.
echo      \                 .'  #-. .-######'     #        '.
echo       \                 '.##'/ ' ################       :
echo        \                  #####################         :
echo         \               ..##.-.#### .''''###'.._        :
echo          \             :--:########:            '.    .' :
echo           \..__...--.. :--:#######.'   '.         '.     :
echo           :     :  : : '':'-:'':'::        .         '.  .'
echo           '---'''..: :    ':    '..'''.      '.        :'
echo              \  :: : :     '      ''''''.     '.      .:
echo               \ ::  : :     '            '.      '      :
echo                \::   : :           ....' ..:       '     '.
echo                 \::  : :    .....####\ .~~.:.             :
echo                  \':.:.:.:'#########.  =. ~ .'-.   . '''.. :
echo                   \    .'  ##########   _.' '. '-.       '''.
echo                   :\  :     ########          '.  '-.        :
echo                  :  \'    '   #### :           :.    '-.      :
echo                 :  .'\   :'  :     :             :      '-.    :
echo                : .'  .\  '  :      :     :        :        '.   :
echo                ::   :  \'  :.      :     :        :          '. :
echo                ::. :    \  : :      :    ;        :           '.:
echo                 : ':    '\ :  :     :     :  :     :        ..'
echo                    :    ' \ :        :     ;         :   .'''
echo                    '.   '  \:                         :.''
echo                     .:..... \:       :            ..''
echo                    '._____ '.\......'''''''.:..'''
echo                               \
echo.
echo ==============================================================
echo  Ralph: My name is Ralph, how rude of me, to now inform you 
echo  at such a time, %name%! Now, let me show you outside!
echo ==============================================================
echo  [1] - Sure, Ralph. 
echo  [2] - Where am I, right now? 
echo  []
echo.
echo.
echo.
pause


:continuestory2
cls
echo  Unknown: Well too bad! I was going to tell you anyways!
ping 127.0.0.1 -n 5 > nul
goto continuestory3


:Settings
if "%color%"=="1" ( set "%color%"="e")
if "%color%"=="2" ( set "%color%"="07")
if "%color%"=="3" ( set "%color%"="03")
if "%color%"=="4" ( set "%color%"="0c")
cls
color %color%
echo                                    ======
echo                                   Settings 
echo                                    ======
echo.
echo                                  [1] =Display Settings=
echo                                  [2] =Color Settings=
echo                                  
echo.
echo                                  [3] =Go Back=
echo.
echo ===============================================================================
set /p settings=">"
if /i "%settings%"=="1" goto DisplaySeti
if /i "%settings%"=="2" goto ColorS
if /i "%settings%"=="3" goto titlescreen 


:DisplaySeti
if "%color%"=="1" ( set "%color%"="e")
if "%color%"=="2" ( set "%color%"="07")
if "%color%"=="3" ( set "%color%"="03")
if "%color%"=="4" ( set "%color%"="0c")
color %color%
cls
echo ====================
echo Display Settings
echo ====================
echo 1) Default
echo 2) Fullscreen
echo 3) Back
set /p displayseti=">"
if "%displayseti%"=="1" mode 80,25
if "%displayseti%"=="2" mode con: cols=160 lines=78
if "%displayseti%"=="3" goto settings 
goto DisplaySeti
:ColorS
if "%color%"=="1" ( set "%color%"="e")
if "%color%"=="2" ( set "%color%"="07")
if "%color%"=="3" ( set "%color%"="03")
if "%color%"=="4" ( set "%color%"="0c")
color %color%
cls
echo ====================
echo Color Settings
echo ====================
echo 1) Yellow ( Default )
echo 2) White 
echo 3) Blue
echo 4) Red
echo ===================
echo 5) Go Back
echo ===================
set /p color=">"
if "%color%"=="1" color 0e
if "%color%"=="2" color 07
if "%color%"=="3" color 03
if "%color%"=="4" color 0c
if "%color%"=="5" goto settings
rem switching variables to match color code
if "%color%"=="1" ( set "%color%"="e")
if "%color%"=="2" ( set "%color%"="07")
if "%color%"=="3" ( set "%color%"="03")
if "%color%"=="4" ( set "%color%"="0c")


goto ColorS

颜色选择器位于:ColorS和我用来制作它的代码,这样无论你在程序中的哪个位置,都可以选择你想要的颜色是"color%color%",但它似乎不起作用。。。

使用Choice、Ansi代码和For循环数组可以做得更好。

@Echo off & Setlocal EnableDelayedExpansion
::: / Creates variable /AE = Ascii-27 escape code.
::: - http://www.dostips.com/forum/viewtopic.php?t=1733
::: - https://stackoverflow.com/a/34923514/12343998
:::
::: - /AE can be used  with and without DelayedExpansion.
Setlocal
For /F "tokens=2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
Endlocal
Set "/AE=%%a"
)
::: 

::: / Assigns ANSI color code values to each color, then builds an Array containing those color values to be accessed using the index number.
Set /A Red=31,Green=32,Yellow=33,Blue=34,Cyan=36,White=37,Grey=90,Pink=91,Magenta=95,Teal=96,CI#=0
For %%A in (Red,Yellow,Pink,Grey,Green,Cyan,White,Magenta,Blue,Teal) do (
REM build an array in which each color name is stored the Ansi code for that color, and each color name is stored in the Indexed Array C#[1-10]
Set "%%A=%/AE%[!%%A!m"
Set /A "CI#+=1"
Set "C#[!CI#!]=%%A"
)
::: 
:menu
Echo Menu:
Echo 1. Color Settings
Echo 2. Exit
Choice /N /C 12 /M ""
If Errorlevel 2 (exit)
If Errorlevel 1 Call :Colorselect
Goto :menu
:Colorselect
For /L %%A in (1,1,!CI#!) Do (
REM display a list of color options alongside the selection number.
If Not "%%A"=="10" (Echo %%A^) !C#[%%A]!) Else (Echo 0^) !C#[%%A]!)
)
REM Errorlevel for choice begins at 1. '0' in the above is actually errorlevel 10
Choice /N /C:1234567890 /M ""
REM Access the values of the color and Color codes from the Array variable C#[~n] with n being determined by the Choice errorlevel
For %%A in (!C#[%Errorlevel%]!) do Echo.!%%A! %%A selected.
Exit /b

我为这里示例的程序构建了颜色变化的数组设置。我在这里发布的脚本已经缩小,通过使用Choice和选择时返回的错误级别使用数字0-9来简化颜色选择。

最新更新