• Welcome to the new forums! Server IP: smp.hometownmc.com
Hello There, Guest! Login Register


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I need help with a game I'm making
#1
So I am making a text based game with Windows batch coding. Here is the code so far:

@echo off 
title Game
color 02
echo Once upon a time a girl walked into a cave.
echo Upon entering she found a portal. Its surface was a deep purple, spirals all over its surface
pause
echo.
:enter/run
echo --------------------------------------------------------------------------------
echo [e] to enter the portal
echo [r] to run away
echo.
set /p enter/run=
cls
if %enter/run%==e (
echo Your character takes a breath, closes her eyes, and steps forward.
echo.
echo Your characters body feels as if its being ripped apart. 
echo.
echo Your character has lost consciousness.
pause
cls
goto jail
)

if %enter/run%==r (
echo Your character turns around and begins to run.
echo.
echo Your character feels a demon snatching at her feet.
echo.
echo Your character trips.
echo.
echo Your character has been dragged into the portal.
pause
cls
goto jail
)

if not %enter/run%==r (
echo ERROR
echo INVALID ASNWER
echo.
echo PLEASE TRY AGAIN
pause
cls
goto enter/run
)
:jail
cls
echo Your character wakes up bleary-eyed in a cell.
echo.
echo Your character peers around, trying to see in the semi-darkness.
pause
cls
:attack/go
echo Your character hears a small voice saying,
echo "Take this, use it to escape"
echo.
echo A small dagger drops into your character's lap.
echo.
echo Your character looks around for whoever has given you this weapon
pause
cls
echo A guard flings the door open and shouts,
echo "COME WITH ME"
pause
echo --------------------------------------------------------------------------------
echo [a] to attack the guard
echo [g] to go with the guard
echo.
set /p attack/go=
cls
if %attack/go%==a (
cls
color 0C
set /a enemyHP = 25
echo YOUR CHARACTER HAS ENTERED BATTLE!!!
echo. 
echo YOUR WEAPON IS A DAGGER.
echo.
echo YOUR CHARACTER HAS 100HP
echo.
echo YOUR ENEMY HAS %enemyHP%HP
pause
)

if %attack/go%==g (

)

if not %attack/go%==a (
echo ERROR
echo INVALID ASNWER
echo.
echo PLEASE TRY AGAIN
pause
cls
goto attack/go
)

As you may have noticed I have highlighted some parts. These are the parts I need help with. 
It's meant to say that the enemy has (however large the variable is)HP
Instead it says the enemy has HP

So, can anyone tell me how this works.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

For anyone looking to run the game this what you do:
  1. Copy and paste the code into notepad
  2. Save the file on your desktop. 
  3. While saving it save it as Game.bat and have the file type set to 'all types'
  4. Finish saving it and double-click the file
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Please point out any bugs in the game.
I will update this thread from time to time with updates on the game.
 
Reply
#2
Why use batch though? You could do the same thing far more easily in a higher level language, like python.
[Image: tumblr_nfz4m14xkj1toipevo1_400.gif]
 
Reply
#3
Batch is pretty easy to learn.
Not many commands.

Though I've never tried to learn any other language.
 
Reply
#4
Batch may have somewhat simple commands, but it can sometimes be far off from plain English. That, and it's also very limited, which I'm about to show you.

The reason why it doesn't work is because an if statement counts as a single command, and variables are expanded only when they're parsed. This is a common thing people run into once they start using for loops in batch, since this applies to any block of code. To get around this, you have to put "setlocal enabledelayedexpansion" at the top of your script and "! enemyHP!HP" instead of "%enemyHP%HP". The ! denotes a delayed expansion variable.

I guess to put it in simpler terms, you can't set a variable and then read it in the same command, so either set the variable outside of the if statement, or enable delayed expansion and use !'s for any variables set inside the same code block rather than %'s.

Or option 3: learn python lol
[Image: tumblr_nfz4m14xkj1toipevo1_400.gif]
 
Reply
#5
It's been a long time since I did this stuff but I agree with Saiyaka. Define the variable before the IF and it will work. Good luck with the game. I enclose the corrected code below.

@echo off
title Game
color 02
echo Once upon a time a girl walked into a cave.
echo Upon entering she found a portal. Its surface was a deep purple, spirals all over its surface
pause
echo.
:enter/run
echo --------------------------------------------------------------------------------
echo [e] to enter the portal
echo [r] to run away
echo.
set /p enter/run=
cls
if %enter/run%==e (
echo Your character takes a breath, closes her eyes, and steps forward.
echo.
echo Your characters body feels as if its being ripped apart.
echo.
echo Your character has lost consciousness.
pause
cls
goto jail
)
if %enter/run%==r (
echo Your character turns around and begins to run.
echo.
echo Your character feels a demon snatching at her feet.
echo.
echo Your character trips.
echo.
echo Your character has been dragged into the portal.
pause
cls
goto jail
)
if not %enter/run%==r (
echo ERROR
echo INVALID ASNWER
echo.
echo PLEASE TRY AGAIN
pause
cls
goto enter/run
)
:jail
cls
echo Your character wakes up bleary-eyed in a cell.
echo.
echo Your character peers around, trying to see in the semi-darkness.
pause
cls
:attack/go
echo Your character hears a small voice saying,
echo "Take this, use it to escape"
echo.
echo A small dagger drops into your character's lap.
echo.
echo Your character looks around for whoever has given you this weapon
pause
cls
echo A guard flings the door open and shouts,
echo "COME WITH ME"
pause
echo --------------------------------------------------------------------------------
echo [a] to attack the guard
echo [g] to go with the guard
echo.
set /p attack/go=
cls
set /a enemyHP = 25
if %attack/go%==a (
cls
color 0C
echo YOUR CHARACTER HAS ENTERED BATTLE!!!
echo.
echo YOUR WEAPON IS A DAGGER.
echo.
echo YOUR CHARACTER HAS 100HP
echo.
echo YOUR ENEMY HAS %enemyHP%HP
pause
)
if %attack/go%==g (
)
if not %attack/go%==a (
echo ERROR
echo INVALID ASNWER
echo.
echo PLEASE TRY AGAIN
pause
cls
goto attack/go
)
 
Reply
#6
Thanks clee!!
 
Reply
#7
(03-29-2017, 04:33 AM)Saiyaka Wrote: Or option 3: learn python lol

Python is great for beginners learning a third gen language, and it's object orientated!
[Image: P4HqY8y.png]
 
Reply
  


Forum Jump:


Browsing: 8 Guest(s)