0

Basically, what I'm interested in doing, is allow the user playing my game, through a settings menu, switch the colors of the text in game ( and in the menu ). If you run my code, and you select a color via settings/color settings/ from the main menu, it will select the color correctly, however once you exit the color settings menu. It will revert the color to purple or some odd reason, and not the color you selected. How do I fix this? Here is the code...

    @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

The color selector thingy is located in :ColorS and the code I'm using to make it so that wherever you are in the program, is selected to the color you wanted is "color %color%" but it doesn't seem to work...

  • this is occuring because you are setting the value of color to 5 when the user goes back – T3RR0R Apr 20 '20 at 15:55
  • @T3RR0R Where am I setting the value of 5? I literally did ctrl+f to see if I actually did that in my code, to no avail... :( –  Apr 20 '20 at 15:56
  • when you take input to return, you do so with the same variable - color – T3RR0R Apr 20 '20 at 16:16
  • @T3RR0R I actually discovered that just now, what would I replace it with though? –  Apr 20 '20 at 16:24
  • use a different variable name to take the input, then set the value of color depending on the input using if conditions. See my answer for an alternate method. – T3RR0R Apr 20 '20 at 16:34
  • Well, this does not appear to be the *shorted code to reproduce the problem* -- please read [mcve]... – aschipfl Apr 20 '20 at 17:17

1 Answers1

0

This can be done a little better with Choice, the use of Ansi codes and a For loop array.

@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

I constructed The array setup for color changes for the program exampled here. The script I've posted here has been scaled back to simplify color selection via the use of numbers 0-9 using Choice and the errorlevel returned upon selection.

T3RR0R
  • 2,747
  • 3
  • 10
  • 25