I recently followed some tutorials and learnt how to create and program batch files. I started making a game meant to simulate fighting monsters. One of the mechanics of this game was to be an inventory where once programmed, you'd be able to buy certain items from a store and put them in your inventory which you can already access in battle. The only programmed level so far is the tutorial and only the start of it. What you should be able to do is select an inventory slot when asked, then the computer should run the inventory function which it doesn't seem to do. Some error shows up, then closes the window automatically.If anyone could explain in simple terms why this isn't working and how i can fix it, it would really help me out. Thanks :D Here's my code...
::SETUP
#echo off
title Platformer
color 0a
::Variables
set my_level=0
set money=0
set inventory_slot_1=
set inventory_slot_2=
set inventory_slot_3=
set inventory_slot_4=
set inventory_slot_5=
set current_level=-1
set pin=0
::MONSTER VARIABLES
set monster_level = 0
::MAIN MENU
:main_menu
cls
echo.
echo PPPPP l aaaaa ttttt fffff ooooo rrrr mmmmm eeeee rrrr
echo P P l a a t f o o r r m m m e r r
echo PPPPP l aaaaa t ffff o o rrrr m m m eeee rrrr
echo P l a a t f o o r r m m m e r r
echo P l a a t f ooooo r r m m eeeee r r
echo.
echo 1990 inc.
echo.
echo LVL.%my_level% $%money%
echo ________________________________________________________________________________
echo.
echo 1 - Play
echo 2 - Save
echo 3 - Load
echo.
set /p input=">>> "
if %input%==1 goto level_select
if %input%==2 goto save
if %input%==3 goto load
pause >nul
::LEVEL SELECT
:level_select
cls
echo LVL.%my_level% $%money%
echo ________________________________________________________________________________
echo.
echo 1 - Tutorial
echo 2 - Level 1
echo 3 - level 2
echo 4 - level 3
echo 5 - Boss
echo 6 - BACK
echo.
set /p input=">>> "
if %input%==1 goto tutorial
if %input%==2 goto level_select
if %input%==3 goto level_select
if %input%==4 goto level_select
if %input%==5 goto level_select
if %input%==6 goto main_menu
goto main_menu
pause >nul
::INVENTORY
:inventory
if %~1==rock set %damage%=2
if %~1==mercy set mercy_chance=%RANDOM%*%monster_leve%/32768+1
EXIT /b
::TUTORIAL
:tutorial
cls
echo LVL.%my_level% $%money%
echo ________________________________________________________________________________
echo.
set /a current_level = 0
call:summon_dragon
pause >nul
::Monster -- Dragon
:summon_dragon
set /a monster_level=%current_level%+1
set /a rand=%RANDOM%%%3+1
set /a monster_hp=%monster_level%*3+%rand%
echo A Lvl.%monster_level% Dragon Has Been Summoned
echo What Will You Do?
echo.
echo HP: %monster_hp%
echo.
echo 1 - [ %inventory_slot_1% ]
echo 2 - [ %inventory_slot_2% ]
echo 3 - [ %inventory_slot_3% ]
echo 4 - [ %inventory_slot_4% ]
echo 5 - [ %inventory_slot_5% ]
echo 6 - [ Mercy ]
echo.
set /p input=">>> "
if input==1 callinventory %inventory_slot_1%
if input==2 call:inventory %inventory_slot_2%
if input==3 call:inventory %inventory_slot_3%
if input==4 call:inventory %inventory_slot_4%
if input==5 call:inventory %inventory_slot_5%
if input==6 (
call:inventory mercy
if %mercy_chance%==1 (
set my_level=%my_level%+1
goto win
)
)
set monster_hp=%monster_hp%-%damage%
if monster_hp<=0(
set my_level=%my_level%+1
set current_level=%current_level%+1
goto win
)
EXIT /b
::WIN
:win
cls
echo YOU WIN!
pause >nul
::SAVE
:save
cls
echo LVL.%my_level% $%money%
echo ________________________________________________________________________________
echo.
set /p pin="Pin: "
echo.
if pin==nul(set /p pin = Change Pin: goto save)
(
echo #echo off
echo set my_level=%my_level%
echo set money=%money%
) >> game_saves\%pin%.cmd
echo SAVED
pause >nul
goto main_menu
::LOAD
:load
cls
echo LVL.%my_level% $%money%
echo ________________________________________________________________________________
echo.
set /p pin="Pin: "
echo.
if pin==nul( pause >Invalid Pin goto load)
call game_saves\%pin%.cmd
echo Loaded
pause >nul
goto main_menu
if %~1==rock set %damage%=2
Is probably incorrect. If %~1 is empty, then there will be a syntax error.
You are attempting to set the variable that is the contents of damage to 2. Probably this is incorrect. If damage is not set at this time, you will get an error as cmd attempts to parse set =2
Go to Start>Programs>Accessories and set yourself a shortcut to Command Prompt.
Clicking that will put you in to cmd and you can then navigate about and run the batch by changing to the appropriate directory with cd c:\wherever\your\batch\is and simply running nameofyourbatchfile.
To exit cmd, type exit and to get help on commands type commandname /?
All acions are terminated with Enter
Related
So i made a menu in batch and you have options to select what you would want to do, but if u enter a option thats not on the menu it selects the first option. how would i make it echo invalid input please try again.
menu:
echo Please select what you would like to do!
echo =========================================
echo 1)Example 1
echo 2)Example 2
echo 3)Example 3
echo 4)Example 4
echo 5)Example 5
echo 6)Example 6
echo =========================================
set /p ans="Please enter your selection:"
if %ans%==1 (
goto a
)
if %ans%==2 (
goto b
)
if %ans%==3 (
goto c
)
if %ans%==4 (
goto f
)
if %ans%==5 (
goto g
)
if %ans%==6 (
goto h
)
Use choice. Much simpler. Here is an example
#echo off
Choice /c 123456 /m "select choice"
If not %errorlevel% equ 0 Echo you chose %errorlevel%
You can then use it to goto by creating easy manageable labels.
#echo off
Choice /c 123 /m "select choice"
Goto opt%errorlevel%
:opt3
Echo do something here for option 3
Goto :eof
:opt2
Echo do something here for option 2
Goto :eof
:opt1
Echo do something for oprion 1
:opt0
Exho You pressed ctrl+c and selected N
....
You get the idea..
I am trying to create a program that has the user input 3 values which the program then proceeds and uses as the A, B and C respectively in the ax^2+bx+c (discriminant = D) and gives you the "solutions" it has (if D>0 then 2 solutions, if D = 0 one "double" solution and if D < 0 it will display that there are no real solutions).
#echo off
setlocal enabledelayedexpansion
:arxh
cls
echo *****************************************
echo *Theleis na trekseis to programma (Y/N)?*
echo *****************************************
set /p V=
if /I %V%==Y goto start
if /I %V%==N goto end
echo ~~~~~~~~~~~~~~~~~~~~~~~~~
echo ~Kane thn swsth epilogh!~
echo ~~~~~~~~~~~~~~~~~~~~~~~~~
pause
goto arxh
:start
echo --------------------
echo Dwse mou thn timh A:
echo --------------------
set /p A=
echo --------------------
echo Dwse mou thn timh B:
echo --------------------
set /p B=
echo --------------------
echo Dwse mou thn timh C:
echo --------------------
set /p C=
goto calculation
:calculation
set /a D=%B%*%B%-4*%A%*%C%
echo **************************************************************************
echo ----------------------
echo Timh diakrinousas: %D%
echo ----------------------
if %D% LSS 0 goto error
if %D% GTR 0 goto positive
if %D%==0 goto zero
:error
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo ~Arnhtikh timh diakrinousas! Den uparxoun pragmatikes luseis!~
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pause
goto arxh
:positive
setlocal enabledelayedexpansion
set count=0
set /p dend=Vale edw thn timh ths diakrinousas:
set num=%dend%
for /l %%i in (%dend%, -1, 1) do (
set /a sqr=%%i*%%i
if !sqr! leq %dend% (
set digit=%%i.
set root=%%i
goto out
)
)
:out
call set /a count+=1
if %count% GTR 5 goto next
set /a dend=(%dend%-%sqr%)*100
set /a div=%root%*2
for /l %%i in (9,-1,0) do (
set /a sqr=%div%%%i*%%i
if !sqr! leq %dend% (
set root=%root%%%i
goto out
)
)
:next
set root=%root:~-5%
if %dend% neq 0 set digit=%digit%%root%
goto results
:results
set /p S=%digit%
set /a Q=(-%B%+%S%)/2*%A%
set /a R=(-%B%-%S%)/2*%A%
echo ----------------------------------------------------
echo H prwth riza einai ~%Q%~ kai h deuterh einai ~%R%~ !
echo ----------------------------------------------------
echo *****************Approximate Value*******************
pause
goto arxh
:zero
set /a Z=-b/2*A
echo -------------------------
echo H diplh riza einai: %Z% !
echo -------------------------
pause
goto arxh
:end
end
I found a program that the user inputs a number and it finds the square root of said number! I converted the code, so that the user would not input that number, but instead the number would be the discriminant (the number A,B and C would make in the equation).
No matter how hard i try, I get missing operants errors or unbalanced parenthesis or it displays to me the root of the discriminant but doesn't proceed to use it correctly in the equations below. Seriously I am stuck.
Some help would be appreciated!
P.S. don't run the code snippet, I am new here, so I used that tool to display the code.
Thank you in any case
Hi I make simple batch file now what I tried to add correct answers number at the end but it keeps saying zero because the variable values are not changing when the answers are chosen. Here is my code below
#echo off
title Game One
color 1f
::#############################
:one
set correctn=0
set correctn2=0
cls
echo What is 2 + 2?
echo.
echo.
echo A) 6
echo B) 4
echo C) 49
echo D) 17
echo.
echo.
echo Type the correct answer.
set /p ch1=
echo.
echo.
if not defined ch1 (goto one)
if %ch1%==A goto no
if %ch1%==A correctn=0
if %ch1%==B goto yes
if %ch1%==B correctn=1
if %ch1%==C goto no
if %ch1%==C correctn=0
if %ch1%==D goto no
if %ch1%==D correctn=0
pause>null
::#########################################
:no
cls
echo Sorry, that answer is incorrect.
echo.
echo.
echo The correct choice was B, which was 4.
pause>null
goto two
::#########################################
:yes
cls
echo You are correct. Congratulations.
echo Press any key to continue.
pause>null
goto two
::##########################################
:two
cls
echo What is 100 divided by 2?
echo A) 45
echo B) 50
echo C) 90
echo D) 17
echo.
echo.
set/p ch2=
echo.
echo.
if not defined ch2 (goto two)
if %ch2%==A goto no2
if %ch2%==A correctn2=0
if %ch2%==B goto yes2
if %ch2%==B correctn2=1
if %ch2%==C goto no2
if %ch2%==C correctn2=0
if %ch2%==D goto no2
if %ch2%==D correctn2=0
echo Invalid Choice, Please Try Again!
pause>null
::#################################
:no2
cls
echo Sorry, that answer is incorrect.
echo.
echo.
echo The correct choice was B, which was 50.
pause>null
::########################################
:yes2
cls
echo You are correct. Congratulations.
echo Press any key to continue.
pause>null
goto end
::######################################
:end
set/a correct=%correctn% + %correctn2%
echo Number of answers correct was %correct%
pause>null
So how to change variable value in if statement if already variable exists?
You need to set a variable first and then goto. As currently written, your script goes to a label yes first. Hence, if %ch1%==B set correctn=1 line is never reached:
if %ch1%==B goto yes
if %ch1%==B set correctn=1
if %ch1%==B goto yes
Moreover, IF command string comparison is case sensitive without /I switch; try the following:
if /I %ch1%==B (set correctn=1&goto yes)
I'd suggest using (Windows native) CHOICE.EXE instead of set /p for a single key-press user input, e.g as follows:
CHOICE /C abcd
IF %errorlevel%==2 (
set correctn=1
goto yes
) else (
set correctn=0
goto no
)
Im am trying to make a simple coin game in batch. The problem I have is once the delay is completed (ping) "goto was unexpected at this time" is all I get before the window closes. Any help would be appreciated!
#echo off
color f0
title Nim In Batch
::: Main Menu
:main
set /a coins=12
set /a time=0
cls
echo Nim In Batch!
echo.
choice /n /c pr /m "Play Or See Rules?"
if %errorlevel%==1 (
goto play
)
if %errorlevel%==2 (
goto rules
)
::: Rules
:rules
cls
echo Objective: Be the one to claim the last coin
echo.
echo Rules:
echo -You Play First
echo -You Must Remove 1,2 Or 3 Coins Each Turn
echo -Turns Switch Once Coins Are Removed
echo -Coins Must Be Removed In Order
echo.
echo Ready?
pause>nul
goto main
::: Game
:play
cls
echo (%coins%) Coins
echo.
choice /n /c 123 /m "How Many Coins Do You Wish To Remove?"
if %errorlevel%==1 (
set /a Coins=%coins%-1
)
if %errorlevel%==2 (
set /a Coins=%coins%-2
)
if %errorlevel%==3 (
set /a Coins=%coins%-3
)
cls
echo (%coins%) Coins
echo.
echo Computer Is Thinking
ping 1.1.1.1 -n 1 -w 3250 >NUL
if %errorlevel%==1 (
set /a Coins=%coins%-3
)
if %errorlevel%==2 (
set /a Coins=%coins%-2
)
if %errorlevel%==3 (
set /a Coins=%coins%-1
)
if %coin%==0 (
goto end
) else (
goto play
)
:end
cls
echo Computer Wins!
echo He Collected The Final Coin!
echo.
pause
goto main
You forget an s in %coins% here :
if %coins%==0 (
goto end
) else (
goto play
)
i have looked for an answer and have not found one.
#echo off
:a
cls
set /p %a% =
if %a% == 1 goto b
goto c
:b
echo.
echo worked
pause
:C
echo.
echo dident work
pause
i set a = 1 and is said that the goto command was unexpected at this time. any help would be great
Changed
ok it has been change and a new problem occurs. now when a = 1 it always goes to c not b
#echo off
:a
cls
set /p a =
if "%a%" == "1" goto b
goto c
:b
echo.
echo worked
pause
:C
echo.
echo wierd
pause
#echo off
echo.
set /p a=
echo.
if %a%==1 goto b
if not %a%==1 goto c
:b
echo worked
echo.
pause
exit
:c
echo dident work
echo.
pause
exit
change this:
set /p %a% =
to
set /p a=
and
if %a% == 1 goto b
to
if "%a%" EQU "1" goto b
#echo off&cls
set /p a=Enter a number :
if "%a%" EQU "1" goto:b
echo dident work
exit/b
:b
echo Result = 1