Okay, so, I'm making a crude text-based batch game demo, its a sword-fighting game.... I've never used any sort of coding before, and I taught myself as best I could... I know this is a pretty primitive and easy to learn language, but I don't quite understand what I did wrong... It doesn't process damage to you OR the enemy, and the stat randomizer sometimes give out numbers equal to or below 0... Ive been tweaking, and changing code in ways I don't really understand, trying to fix it, so the code may be a little weird at some parts, feel free the constructively criticize, improve parts that MAY work, and PLEASE find the problems stated... (PS. its also programmed to recognize me as the "creator" and my girlfriend, and calls her by her nickname, but that works fine, so Ill remove it from this code.)
the code:
#echo off
color 02
cls
setlocal enabledelayedexpansion
set file=AdventureTextMusic.mp3
( echo Set Sound = CreateObject("WMPlayer.OCX.7"^)
echo Sound.URL = "%file%"
echo Sound.Controls.play
echo do while Sound.currentmedia.duration = 0
echo wscript.sleep 100
echo loop
echo wscript.sleep (int(Sound.currentmedia.duration^)+1^)*1000) >sound.vbs
start /min sound.vbs
title New Adventurer
echo.
echo Hello Adventurer!
echo.
echo may I ask your name?
echo.
set /p name=
echo.
if %name% equ Michelle goto bunni
if %name% equ Stuart goto kitty
if %name% neq Stuart goto random
:bunni
cls
echo.
echo Bunni? Welcome to my game! I worked hard on it, I don't know if it will be good... We'll see I guess!!
set /a gold=%random% %% 25-1
echo.
echo Here's %gold% gold to get you started! I hope it helps!
echo.
set name=Bunni
echo Good luck %name%!
pause
goto continue
:kitty
cls
echo.
echo Creator? Bug fixing I assume? I hope all is well in the land of Adventure Text! Good luck!
set /a gold=%random% %% 25-1
echo.
echo Here's %gold% gold to get you started! I hope it helps!
pause
goto continue
:random
cls
echo.
echo That name suits you! Welcome %name%! To the land of Adventure Text!
set /a gold=%random% %% 15-1
echo.
echo Here's %gold% gold to get you started! I hope it helps!
echo.
echo Good luck %name%!
pause
goto continue
:continue
cls
echo The controls are simple, when it asks for your choice of attack, pick an option.
echo.
echo Slash does your weapons default damage, minus enemy armor strength, and takes no stamina
echo.
echo Slice does your weapons default damage, ignoring armor strength, and takes 2 stamina
echo.
echo whereas Stab does 1.5 damage, ignoring armor strength, but takes 4 stamina
echo.
pause
cls
echo.
echo Would you like to begin, %name%?
pause
title %name%'s Quest
cls
set /a health=%random% %% 15-1 +2
set /a armor=%random% %% 2-1 +2
set /a damage=%random% %% 4-2 +2
set /a stamina=%random% %% 15-8 +2
echo.
echo Health: %health% Gold: %gold% Armor Strength: %armor% Damage per Hit: %damage% Stamina: %stamina%
pause
cls
echo.
set /a number=%random% %% 2-1
if %number% equ 1 goto Grunt
if %number% equ 2 goto Guard
:Guard
set enemy=Guard
set EHealth=12
set EArmor=1
set EDamage=1
goto battle1
:Grunt
set enemy=Grunt
set EHealth=10
set EArmor=0
set EDamage=2
goto battle1
:battle1
echo An enemy %enemy% has spotted you!
echo Enemy Health: %EHealth% Enemy Armor Strength: %EArmor% Enemy Damage: %EDamage%
pause
cls
echo.
goto turn1
:turn1
echo What would you like to do?
echo 1) Slash
echo 2) Slice
echo 3) Stab
set /p attack=
if %attack% equ 1 goto slash
if %attack% equ 2 goto slice
if %attack% equ 3 goto stab
:turn2
echo Health: !health! Stamina: !stamina!
if !EHealth! leq 0 goto end
echo Enemy Health: !EHealth!
echo Enemy turn!
pause
cls
goto enemy
:enemy
set /a EAttack=%random% %% 3-1
if %EAttack% equ 1 goto Eslash
if %EAttack% equ 2 goto Eslice
if %EAttack% equ 3 goto Estab
if %EAttack% neq 3 goto enemy
:Eslash
set health-=!EDamage!-!armor!
if !health1 leq 0 goto lose
goto turn1
:Eslice
set health-=!EDamage!
if !health! leq 0 goto lose
goto turn1
:Estab
set EDamage*=1.5
set health-=!EDamage!
set EDamage/=1.5
if !health! leq 0 goto lose
goto turn1
:slash
set EHealth-=!damage!-!EArmor!
goto turn2
:slice
if %stamina% lss 2 cls
if %stamina% lss 2 echo.
if %stamina% lss 2 echo You do not have enough stamina to Slice
if %stamina% lss 2 goto turn1
set EHealth-=!damage!
set stamina-=2
goto turn2
:stab
if %stamina% lss 4 cls
if %stamina% lss 4 echo.
if %stamina% lss 4 echo You do not have enough stamina to Stab
if %stamina% lss 4 goto turn1
set damage*=1.5
set EHealth-=!damage!
set damage/=1.5
set stamina-=4-2
goto turn2
:lose
cls
echo.
echo Im sorry %name%... but you lost...
pause
cls
echo This game is in pre-alpha, I will likely be updating it to add the full story.
echo Created by: SteweeBee
pause
taskkill /f /im "wscript.exe"
exit
:end
cls
echo.
echo ---- ---- ---- [----] ------- ----
echo \ \ / \ / / I I I \ I I
echo \ \ / \ / / I I I I\ \I I
echo \ \_/ /\ \_/ / I I I I \ I I
echo \ / \ / I I I I \ I
echo ------ ------ [----] I___I \___I
pause
cls
echo This game is in pre-alpha, I will likely be updating it to add the full story.
echo Created by: SteweeBee
pause
taskkill /f /im "wscript.exe"
exit
set /a health=%random% %% 15-1 +2
The line above is the same as
set /a health=%random% %% 16
and I think you want this:
set /a health=(%random% %% 15-1) +2
The line below uses delayed expansion but you don't use code that requires it (like in loops) - You could just use normal expansion like %health%
!health!
This code will work, unless another character is added when typing and then it will fail. The variable attack should also be initialised before the set /p because set /p will remember what it was the last time through if enter is pressed without input. Input like 1 2 with the space will cause it to fail too (ditto with &).
set /p attack=
if %attack% equ 1 goto slash
if %attack% equ 2 goto slice
if %attack% equ 3 goto stab
It should really have a check for invalid input at the end - just a goto :get_input to loop back for input will do.
Related
I am trying to make an "admin" login and it isn't working.
#echo off
title RPG BETA
color 0b
set /p player=Welcome to Xero, what's your name?:
if %player% == Admin goto admin
:intro
echo Hello %player%!
pause
goto start
:admin
set /p password:What is the admin password?:
if %password% == Insertanypassword goto true
if %password not insertanypassword goto false
:true
cls
echo Welcome, Admin!
set /p af=Would you like to enable admin features?[y/n]:
if %af% == y goto true1
if %af% == n goto start
:true1
cls
set CPU=20
set CPUN=Scorpion
set CPUD=3
set gun=9999999
set gund=9999999
set playerh=9999999
goto fight
:start
cls
echo In the distant future, the world was on the brink of destrucion.
timeout 3 >nul
echo In the midst of the wasteland, a single man, named %player%, will overcome the odds
pause
goto BFight
:BFight
cls
set CPU=20
set CPUN=Scorpion
set CPUD=3
set gun=1
set gund=10
set playerh=20
:Fight
cls
if %CPU% leq 0 goto win
if %playerh% == 0 goto lose
cls
echo You encounter a %CPUN%!
echo %CPUN% Health: %CPU%
echo.
echo Your Health: %playerh%
echo [1]Shoot (%gund% damage) (%gun%)
echo [2]Punch (3 damage)
echo [3]Flee
set /p fp=What do you do?
if %fp% == 1 goto gun
if %fp% == 2 goto punch
if %fp% == 3 goto flee
:gun
cls
if '%gun%'=='0' goto egun
set /a gun = gun - 1
echo You fire at the %CPUN%
timeout 4 >nul
echo It hits!
set /a CPU = CPU - gund
pause
goto cpufightp
:punch
cls
echo You punch the %CPUN%
timeout 4 >nul
echo It hits!
set /a CPU = CPU - 3
pause
goto cpufightp
:egun
echo You have no bullets!
pause
goto Fight
:cpufightp
if %CPU% leq 0 goto win
cls
echo %CPUN% Health: %CPU%
echo.
echo Your Health: %playerh%
pause
cls
goto cpufight
:cpufight
cls
echo The %CPUN% Attacks!
timeout 4 >nul
echo It hits!
set /a playerh = playerh - CPUD
pause
goto fight
:flee
goto losef
:losef
cls echo you have fled
pause
goto suggestion
:lose
cls
echo You died
pause
cls
:win
cls
echo Congradulations, %player%! You win!
pause
cls
:suggestion
set /p suggest=What should I add to Xero?:
echo %player%: %suggest% >> xerosuggest.word
Whenever I type in "Admin" instead of going to the password screen, it just closes cmd, can anyone tell me why? i have experimented with ''s and other commands but I just can't figure it out.
The problem with respect to your specific issue is a typo.
You have:
set /p password:What is the admin password?:
Instead of:
Set /P "password=What is the admin password?: "
The doublequotes are simply good practice, your error was quite clearly that you used a : instead of a =.
So I am having a problem with changing a text assinged using the set /a
here is my script
:wep
cls
if %gold% lss 50 (
echo not enough gold
pause >nul
goto shop
)
if %weap%==Stick (
echo too low on level
pause >nul
goto shop
)
if %wepugrd% lss 1 (
echo You don't have any upgrades
pause
goto shop
)
cls
echo Your Weapon is Being Upgraded
echo.
echo WAIT A FEW SECONDS
ping localhost -n 3 >nul
set /a weapdmg=%weapdmg% + 5
set /a weap=Stick
set /a wepugrd=%wepugrd% - 1
set /a gold=%gold% - 50
echo weapon upgraded
pause
goto shop
the last part is set /a weap=Stick
it was earlier fist
but instead it just changes fist to 0 instead of changing it to stick
can I get some help with that
thanx
Use this code:
:wep
cls
if %gold% lss 50 (
echo not enough gold
pause >nul
goto shop
)
if "%weap%"=="Stick" (
echo too low on level
pause >nul
goto shop
)
if %wepugrd% lss 1 (
echo You don't have any upgrades
pause
goto shop
)
cls
echo Your Weapon is Being Upgraded
echo.
echo WAIT A FEW SECONDS
%SystemRoot%\System32\ping.exe localhost -n 3 >nul
set /a weapdmg+=5
set "weap=Stick"
set /a wepugrd-=1
set /a gold-=50
echo weapon upgraded
pause
goto shop
As npocmaka wrote, switch /A is only for arithmetic operations and can't be used if just a string should be assigned to a variable.
Adding or subtracting an integer to an existing value can be also done using the short form as demonstrated above.
And read this answer for reason enclosing weap=Stick in double quotes.
I was creating a simple random number guesser in batch but I got an error in my code and don't know what it is please help. Here is the code:
#echo off
title Number Guesser
:menu
echo ------------------
echo Number Guesser
echo ------------------
echo 1. Easy
echo 2. Medium
echo 3. Hard
echo 4. Exit
set /p dif=Select difficulty number:
if %dif% == 1 goto easygen
if %dif% == 2 goto medgen
if %dif% == 3 goto hardgen
if %dif% == 4 exit
goto menu
:easygen
set /a num=%random%
if %num% gtr 20 goto gen
cls
goto play
:medgen
set /a num=%random%
if %num% gtr 50 goto gen
cls
goto play
:hardgen
set /a num=%random%
if %num% gtr 100 goto gen
cls
goto play
:play
set /p guess=Guess:
if %guess% == %num% goto win
if %guess% gtr %num% echo Lower!
if %guess% lss %num% echo Higher!
:win
cls
echo Well Done
echo 1. Play again!
echo 2. Quit
set /p cmd=What do you want to do:
if %cmd% == 1 goto menu
if %cmd% == 2 exit
I get the error once I choose the difficulty (dif) I have no idea what isn't working. It just closes.
If you open up cmd then navigate to the file and run it, instead of double clicking the batch file, you'll see an error.
The system cannot find the batch label specified - gen
Like the other commenter said, you don't have :gen defined. It says goto gen, but gen doesn't exist, so it freaks out and closes.
Well if you were to put this code onto a batch file:
it doesn't choose a random number (or if it does it always has the same result)
it does't seem to lose or win if you choose to fight
i've been teaching myself coding and this is my first time on a forum so please go easy on me :)
here is my code:
#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
First off it's better for humans to read when it's "equ" instead of "==".
The set /p VARIABLE= is easier to use when clear so use echo like so:
: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
The fight part should look something like this:
: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
Then when wanting to give gold or a reward on winning do this:
: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
I hope I helped!
I strongly suspect that you have set a variable random. If you've done that, then the value that you set in the environment overrides the magic variable.
You can clear it by set "random="
It's normal practice to use a setlocal after your #echo off. That way, any changes you make to the envirnment are backed out when the batch terminates. Without it, any changes made will remain until they are explicitly changed again.
Personally, I prefer
set /a value=%random% %% limit + 1
to generate a value 1..limit. If for no other reason, it's easier to type.
#echo off
setlocal
rem environment changes made after here will be backed-out when the batch finishes...
....whatever....
The setlocal command sets up a 'local' environment which exists only until the batch ends or an endlocal command is encountered.
see setlocal /? from the prompt for more info.
Your batch appears to work fine once I'd added a win_fight_1 routine.
Note however that batch will barf on set /p answer==(1)continue or (2)quit? but removing one of those = will fix it.
Beyond that, just watch whether you want to go to menu or start_1.
And the
if %f2% gtr 4 goto fight_2
if %f2% lss 1 goto fight_2
will be ineffective - certainly if you've used set /a f2=%random% %% 4 + 1
Fake random--
I confirmed that %random% is driven by system clock-- http://blogs.msdn.com/b/oldnewthing/archive/2010/06/17/10026183.aspx
use the clock MS to get a random number from 1 to 1000
here is one way to get milliseconds: https://answers.yahoo.com/question/index?qid=20100816021807AAz6eL3
Another possibility to introduce randomness is to start the game and then wait for user input i.e. "ready to start?" and when they start will introduce some randomness into the amount of time since cmd.exe started.
I'm trying to make a Batch file that will increment a variable by 1 each time it loops, and then check if the variable is equal to 5, and if it isn't, it loops again. I know there's probably a while loop for this, but I didn't know how to do that, and I'm just enjoying learning Batch for fun right now
Here's the code, it doesn't work the way it should, it just displays a 0: and then does nothing else. So how would I go about fixing it? I have a feeling I'm setting and incrementing the variable wrong, and maybe it's confused about the 2 if statements? (Does it have an else if....?) Anyways, thanks for the help
#echo off
set /p i=0:
goto A
:A
set /p i=i+1:
if i != 5 goto C
if i == 5 goto B
:C
echo Test :D
:B
pause>nul
Note: I don't know a lot of Batch and I'm not a pro, but I like to learn and I'm just doing this for future reference, and because I enjoy it. So, this code probably isn't good, but I want to know how I can accomplish this.
This is a way to simulate the while loop you are trying to accomplish. Only one goto is needed:
#echo off
set /a x=0
:while
if %x% lss 5 (
echo %x%
pause>nul
set /a x+=1
goto :while
)
echo Test :D
You can do that with a simple FOR command :
for /l %%x in (0,1,100) do (
echo %%x
)
You can replace 100 by the number you want
To set a numerical value to a variable, you may use the /a switch:
The /A switch specifies that the string to the right of the equal sign
is a numerical expression that is evaluated.
(Type SET /? for all the help).
Second, check your goto flow - this never loops back to A.
Third, check the syntax of the if expression (!= doesn't exist in batch).
This should work:
#echo off
set var1=0
:loop
set /a var1=%var1%+1
echo %var1%
if %var1% EQU 5 (
goto :end
) else (
goto :loop
)
:end
pause
#echo off
set a=0
:Count
set /a a=a+1
echo %a%
goto Count
try this:
#if (#CodeSection == #Batch) #then
#echo off
:a
cls
color c
echo --------------
echo start:
echo --------------
set /p start=
cls
echo --------------
echo start: %start%
echo --------------
echo --------------
echo stop:
echo --------------
set /p stop=
cls
echo --------------
echo start: %start%
echo --------------
echo --------------
echo stop: %stop%
echo --------------
echo.
echo.
echo Start in:
timeout /t 2 /nobreak >nul
echo. 5
timeout /t 1 /nobreak >nul
echo. 4
timeout /t 1 /nobreak >nul
echo. 3
timeout /t 1 /nobreak >nul
echo. 2
timeout /t 1 /nobreak >nul
echo. 1
timeout /t 1 /nobreak >nul
cls
echo --------------
echo start: %start%
echo --------------
echo --------------
echo stop: %stop%
echo --------------
echo.
echo.
echo.
echo ============================================
set SendKeys=CScript //nologo //E:JScript "%~F0"
%SendKeys% ">----"
%SendKeys% "{enter}"
:while
echo %start%
%SendKeys% "%start%"
%SendKeys% "{enter}"
set /a start=start+1
timeout /t 1 /nobreak >nul
if %start% leq %stop% goto :while
goto :end
:end
echo ============================================
%SendKeys% ">----"
%SendKeys% "{enter}"
:c
echo count again? Y/N
set /p return=
if %return% == Y goto :a
if %return% == N goto :b
goto :c
:b
#end
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));