Batch File Game Accounts - batch-file

I want to make a batch adventure game (not text-based adventure) that gives you options and lets you choose to progress the story.
Problem is I don't know how to save or load progress in the game and I want to make (possibly) multiple accounts you can access to start where you left off.
I know it's possible, since I've seen it in a batch game (who's file I lost). So, anyone know to make it possible?

#echo off
:menu
cls
echo 1.Sign in
echo 2.Register
echo 3.Exit
echo.
set /p input=What would you like to do:
if %input%==1 goto log
if %input%==2 goto reg
if %input%==3 goto exit
goto menu
:reg
cls
set /p user=Enter your desired username:
set /p pass="Enter your desired password:
echo %pass% >> %user%.txt
:: This will prompt for a username and password and then
:: output the user variable as the name of a .txt file that contains the
:: password
goto menu2
:log
set /p user="Enter your username: "
set /p pass="Enter your password: "
set /p password=<%user%.txt
pause >nul
:: This will check if the password entered is equal to the password in
:: the .txt file
if %pass% equ %password% goto menu2
goto menu
:menu2
::Enter your script here
The system is much shorter when you get rid of the comments. :)
Later on in your game you could use the same system as used in the :reg label to create a batch file with player stats and maybe an inventory in it such as set /a armor=2 and set /a sword=1 then call on that file when you want to show the player their statistics/inventory. Just an idea... Anyway, hope this helps at least a little bit.

Im making a game and this is the login signup this is part of the code
#echo off
color f
:logsign
title whattodo?
cls
echo What will you do?
echo.
echo 1) Login
echo 2) Sign in
echo 3) Exit
echo.
set /p web=Type 1 or 2?
if "%web%"=="1" goto login
if "%web%"=="2" goto signup
if "%web%"=="3" exit
:notvalid
echo you're username is not valid.
echo.
echo please try again or sign up.
pause
cls
goto logsign
:notvalid1
echo you're password is not valid.
echo.
echo please try again or sign up.
pause
cls
goto logsign
:signup
title Sign Up
cls
echo What will be your username?
echo.
set /p username=Username:
echo %username% >%username%.bat
cls
echo What will be your password?
echo.
set /p password=Password:
echo %password%>>%username%.bat
cls
echo Go back to log in menu then log in.
pause
cls
goto logsign
:login
title login
cls
echo Let's start with name.What is it?
echo.
set /p username=Type username:
if not exist %username%.bat (goto notvalid)
pause
cls
echo Now what is your password?
echo.
set /p password=Type password:
if not exist %username%.bat (goto notvalid1)
pause
goto home
The sign up saves the user and pass in a .bat file

possibly you could save stats to a file named after the player profile and read off of that file to see what level they where on look around over here.How to read file contents into a variable in a batch file?

Related

trying to make an "OS" in batch

#echo off
:load
rem imitation of loading the os
color 70
ver
title boot
echo please wait...
ping localhost -n 3 >nul
cls
systeminfo
rem in here user types name he wants to be his account name
:login
title login
cls
date
cls
echo welcome to windows 71
echo before we bigen please type your name
set /P_name=here:
if %name%=admin goto admin
if not goto ms
rem ms=menu start
:ms
echo %time%
echo hello %name%
echo type HELP for list to commands
set /P_command=here:
if %command%=help goto help
if %command%=exit goto exit
if %command%=calendar goto cal
if not goto wc
rem wc=wrong command
:admin
echo hello %name% to the admin panel
echo type HELP for list to commands
set /P_command=here:
if %command%=help goto help
if %command%=exit goto exit
if %command%=calendar goto cal
So the problem is that it crashes after the :LOGIN part and I don't know what to do!
I'm trying to make an OS batch (something like MS-DOS), but it crashes after the "login" part.
I tried everything I could think of and it didn't work, also I want to make a save file so users can set a password for their "account".
As mentioned in above comments, you need to correctly use your variables, you can however use choice instead of set /p for your commands.
#echo off
:load
rem imitation of loading the os
color 70
ver
title boot
echo please wait...
timeout /t 3 >nul
cls
systeminfo
rem in here user types name he wants to be his account name
:login
title login
cls
date /t
timeout /t 2>nul
cls
echo welcome to windows 71
echo Before we begin please type your name
set /P "_name=here:"
if /i "_%name%"=="admin" goto admin
rem ms=menu start
:ms
echo %time%
echo hello %_name%
echo type HELP for list to commands
CHOICE /C HEC /M "Press H for Help, E to exit C for Calender."
goto opt%errorlevel%
:admin
echo hello %_name% to the admin panel
echo type HELP for list to commands
CHOICE /C HEC /M "Press H for Help, E to exit C for Calender."
goto opt%errorlevel%
:opt1
echo Help stuff goes here
goto :eof
:opt2
exit
:opt3
echo Calenders stuff goes here
Some things to note. You do not require to goto ms is the user is not admin as the statement for not being admin will not be met, we will automatically faal through into the ms label.
Notice where the problems were in your code. i.e if %name%=admin should be if "%_name%"=="admin" with double equal sign and the underscore in the name. It is also double quoted to ensure that we do a match without unwanted whitespace. Lastly /I option to catch ADMIN in any case.
See if /?, choice /? from command line for more help around these functions.
Okay this code is quite wrong.
I fixed it.
#echo off
:load
rem imitation of loading the os
color 70
title boot
echo please wait...
ping localhost -n 3 >nul
cls
rem in here user types name he wants to be his account name
:login
title login
cls
echo Welcome to Microsoft Windows 7!
echo Before we begin, please type your name.
set /p name=here:
if "%name%"=="admin" goto admin
if not "%name%"=="admin" goto ms
rem ms=menu start
:ms
echo %time%
echo Hello %name%
echo Type HELP for list to commands
set /p command=here:
if "%command%"=="help" goto help
if "%command%"=="exit" goto exit
if "%command%"=="calendar" goto cal
goto ms
rem wc=wrong command
:admin
CLS
echo hello %name% to the admin panel
echo type HELP for list to commands
set /P command=here:
if "%command%"=="help" goto help
if "%command%"=="exit" goto exit
if "%command%"=="calendar" goto cal
GOTO :ADMIN
Okay but you don't need to start systeminfo and all that.

Confusion with Batch-file code

So recently I started learning Batch-file, and today I decided to try and make a game with it. I have a few errors, and can't figure out what is wrong with the code.
Heres the code:
#echo off
title Batch Rpg
color 07
echo Welcome to Batch Rpg!
echo.
echo.
pause
:menu
cls
echo.
echo -Menu-
echo.
echo.
echo.
echo 1) Begin
echo.
echo 2) How to play
echo.
echo 3) Exit
echo.
echo.
echo.
echo ---------
echo.
set /p c=Choice:
if %c%==1 goto prestart1
if %c%==2 goto howtoplay
if %c%==3 goto cfr_exit
if NOT %c%==1 if NOT %c%==2 if NOT %c%==3 goto menu
:cfr_exit
cls
echo.
echo Are you sure you want to exit?
echo.
set /p c=(Y/N):
if %c%=="Y" exit
if %c%=="N" goto menu
if NOT %c%=="Y" if NOT %c%=="N" goto cfr_exit2
:cfr_exit2
cls
echo.
echo You must enter a valid option.
echo.
pause
goto cfr_exit
:howtoplay
cls
echo.
echo -How to play-
echo.
echo.
echo.
echo This game is very simple. There will be a number with an option after it, type the option in and it will perform an action(As the option would say).
echo.
pause
goto menu
:prestart1
cls
echo.
echo Welcome to land of Fageryth!
echo.
echo What is your name, adventurer?
echo.
set /p playername=Name:
goto prestart2
:prestart2
cls
echo.
echo What would be your more valued statistic, Attack damage, or Hit points?
echo.
echo.
echo.
echo 1)Attack damage(Atk)
echo.
echo 2)Hit points(Hp)
echo.
echo.
echo.
echo ---------
echo.
set /p playermorevaluedstat=Choice:
if %playermorevaluedstat%==1
set playeratk=6
set playerhp=25
if %playermorevaluedstat%==2
set playeratk=4
set playerhp=30
if NOT %playermorevaluedstat%==1 if NOT %playermorevaluedstat%==2 goto prestart2
cls
echo playeratk
echo playerhp
pause
I'm having trouble with the :prestart2 section of my code. With the end of it, I tried to make it where if the variable wasn't equal to 1 or 2, which were the options, then it send the player back up to the start of the section again, and also, when it finishes checking, I'm trying to make it display the two variables playeratk and playerhp but instead it just closes out. I am really lost here and would appreciate the help!
A couple things before we start:
First, when troubleshooting your batch scripts, get rid of (comment out) your echo off and cls lines so that you can see where it's going wrong.
Second, you should always double-quote your variables to make sure you're not inadvertently including spaces when setting and comparing them. It doesn't seem to actually be causing problems in your code, but it's a good habit to get into:
set "var1=something"
set "var2=a string with spaces"
if "%var1%"=="something" echo %var1%
Now, the problem in your code is with the two if statements stretching over multiple lines. If you're going to do that, you have to put them inside of parentheses.
set /p playermorevaluedstat=Choice:
if %playermorevaluedstat%==1 (
set playeratk=6
set playerhp=25
)
if %playermorevaluedstat%==2 (
set playeratk=4
set playerhp=30
)

How to detect invalid user input in a Batch File?

I want to use a batch file to ask for a password to continue, i have very simple code that works.
#echo off
:Begin
cls
echo.
echo Enter Password
set /p pass=
if %pass%==Password goto Start
:Start
cls
echo What would you like me to do? (Date/Chrome/Lock/Shutdown/Close)
set /p task=
if %task%==Date goto Task=Date
if %task%==Chrome goto Task=Chrome
if %task%==Lock goto Task=Lock
if %task%==Shutdown goto Task=Shutdown
if %task%==Close goto Task=Close
I need to detect when the user entered an invalid password, i have spent an hour researching but i found nothing. I'm not advanced in any way so try and keep it very simple like the code above.
Please help me.
You were missing exactly one line of code!
#echo off
cls
:Begin
echo.
echo Enter Password
set /p pass=
if %pass%==Password goto Start
cls
Echo %pass% is not the PASSWORD
goto :Begin
:Start
cls
echo What would you like me to do? (Date/Chrome/Lock/Shutdown/Close)
set /p task=
if %task%==Date goto Task=Date
if %task%==Chrome goto Task=Chrome
if %task%==Lock goto Task=Lock
if %task%==Shutdown goto Task=Shutdown
if %task%==Close goto Task=Close
If the password is invalid the code will simply continue to the next line which now is goto :Begin. This will restart the sequence. I changed the order around a bit so that the screen was cleared as well and the error was printed.
Similar to Monacraft's answer, but using if-else statements:
#echo off
cls
:Begin
echo.
echo Enter Password
set /p pass=
if %pass%==Password (goto Start) else *echo Invalid password. && goto :Begin)
:Start
cls
echo What would you like me to do? (Date/Chrome/Lock/Shutdown/Close)
set /p task=
if %task%==Date goto Task=Date
if %task%==Chrome goto Task=Chrome
if %task%==Lock goto Task=Lock
if %task%==Shutdown goto Task=Shutdown
if %task%==Close goto Task=Close
What this does is it checks for the correct password. If it isn't the correct password, it will display an error message and go back to :Begin, which restarts the sequence.

How to use the call command to validate a password in batch

So I have this code
#echo off
set /p pass=Input Pass:
call rig.txt
if %pass%==rig.txt goto right
goto wrong
:wrong
echo Authentication Failed!
pause
exit
:right
echo correct
pause
So instead of having something like this
#echo off
set /p pass=Input Pass:
if %pass%==randompassword goto right
goto wrong
:wrong
echo Authentication Failed!
pause
exit
:right
echo correct
pause
Im trying to get it so it calls rig.txt which has my password in it and checks if i inputed the right password. Please help!
#echo off
rem Read right password saved in rig.txt file
set /P rightPass=< rig.txt
set /p pass=Input Pass:
if %pass% == %rightPass% goto right
echo Authentication Failed!
pause
exit
:right
echo correct
pause

Using CALL command with an IF condition in batch

I'm trying to set up a code with passwords, where a different password will take you to a different directory. All the directories are in the same .bat file, but i want to use another .bat to call the passwords and take me to the directory. I'm not sure how to do it, and am finding it difficult to locate in the topic searches.
My code looks like this:
#echo off
:takemeto
cls
echo Enter Password
set /p name=
call C:\Users\Username\Desktop\Batch files\passwords.bat
:password1
cls
echo You have chosen Password 1
:password2
cls
echo You have chosen Password 2
The CALL is meant to locate and use my file with the passwords. the files has the passwords and the directories, which looks like this:
:Passwords
if "%name%" == "password1" goto password1
if "%name%" == "password2" goto password2
/end
Hope this is clear,
Thanks-Rob
Have your external batch script echo its output. Capture it with a for /f loop.
#echo off
setlocal
set extbat=C:\Users\Username\Desktop\Batch files\passwords.bat
:takemeto
cls
set /p name="Enter Password: "
for /f %%I in ('"%extbat%" %name%') do goto %%I
:invalid
echo Invalid password entered.
set name=
pause
goto takemeto
:password1
cls
echo You have chosen Password 1
goto :EOF
:password2
cls
echo You have chosen Password 2
goto :EOF
example C:\Users\Username\Desktop\Batch files\passwords.bat:
#echo off
if #%1==# (
echo takemeto
) else if #%1==#correct1 (
echo password1
) else if #%1==#correct2 (
echo password2
) else echo invalid
... where correct1 and correct2 are the passwords you expect your user to enter.

Resources