Mountain Gamer
Mountain Gamer

Reputation: 31

On key pressed do

I want to make a game where you control your character.

The game is going to be command line (.bat file).

The problem is that controlling the character would require the player to press (WSAD) then enter to make actions happen.

So, can I make it so the character moves by pressing just (WSAD)?

And if so, must I download some crap from the internet?

Upvotes: 2

Views: 151

Answers (3)

UnknownOctopus
UnknownOctopus

Reputation: 2277

It's possible by using the 'choice' command structured as:

choice [/c choices] [/n] [/t timeout /d choice] [/m text]

For example:

:Main
@echo off
choice /c wsad /t 100 /d w /m yourtexthere
if %ERRORLEVEL% == 1 goto w
if %ERRORLEVEL% == 2 goto s
if %ERRORLEVEL% == 3 goto a
if %ERRORLEVEL% == 4 goto d

:w
echo You pressed w!
::Your code here
pause
exit

:s
echo You pressed s!
::Your code here
pause
exit

:a
echo You pressed a!
::Your code here
pause
exit

:d
echo You pressed d!
::Your code here
pause
exit

Would print:

yourtexthere [W,S,A,D]?

And if w was pressed:

yourtexthere [W,S,A,D]?
You pressed w!
press any key to continue...

Of course instead of printing text then exiting you could add specific code to specify what to do when the key is pressed in the ::Your code here section.

Note: If this program were left for 100 seconds (/t) it would automatically choose 'w' as specified in /d

Here is what each of the attributes to the 'choice' command do:

/c list the keystrokes that you wish the user to have (i.e in your case wsad)

/n toggles whether or not [W,S,A,D]? is displayed (yes without the /n, no with it)

/t sets the time before a default choice is made

/d sets the default choice after /t seconds

/m prints out text to the console

For more on 'choice' click here, or here.

Also, just for future reference, the question was already answered here

Sources: Robvanderwoude, experience.

Upvotes: 3

Aacini
Aacini

Reputation: 67216

@echo off
setlocal EnableDelayedExpansion

set /A lines=15, cols=25

set "line="
for /L %%j in (0,1,%cols%) do set "line=!line! "
set /A linesP=lines+2, colsP=cols+2, i=lines/2, j=cols/2
mode CON: cols=%colsP% lines=%linesP%

:refresh
set "line[%i%]=!line:~0,%j%!X"
cls
for /L %%i in (0,1,%lines%) do echo/!line[%%i]!
:nextKey
choice /C WSADX /N > NUL
goto moveTo-%errorlevel%

:moveTo-1  Up
if %i% equ 0 goto nextKey
set "line[%i%]="
set /A i-=1
goto refresh

:moveTo-2  Down
if %i% equ %lines% goto nextKey
set "line[%i%]="
set /A i+=1
goto refresh

:moveTo-3  Left
if %j% equ 0 goto nextKey
set /A j-=1
goto refresh

:moveTo-4  Right
if %j% equ %cols% goto nextKey
set /A j+=1
goto refresh

:moveTo-5  eXit

EDIT: Another example, Snake-like animation!

@echo off
setlocal EnableDelayedExpansion

set /A lines=15, cols=25

set "line="
for /L %%j in (0,1,%cols%) do set "line=!line! "
for /L %%i in (0,1,%lines%) do set "line[%%i]=%line%"
set /A linesP=lines+2, colsP=cols+2, i=0, j=0, moveI=0, moveJ=1
mode CON: cols=%colsP% lines=%linesP%

:turnTo-3
:refresh
set /A i+=moveI, j+=moveJ, jP=j+1
if %i% lss 0 goto crash
if %i% gtr %lines% goto crash
if %j% lss 0 goto crash
if %j% gtr %cols% goto crash
set "line[%i%]=!line[%i%]:~0,%j%!X!line[%i%]:~%jP%!"
cls
for /L %%i in (0,1,%lines%) do echo/!line[%%i]!
:nextKey
choice /C ADSX /N /T 1 /D S > NUL
goto turnTo-%errorlevel%

:turnTo-1  Left
if %moveJ% equ 0 (
   set /A moveJ=moveI, moveI=0
) else (
   set /A moveI=-moveJ, moveJ=0
)
goto refresh

:turnTo-2  Right
if %moveJ% equ 0 (
   set /A moveJ=-moveI, moveI=0
) else (
   set /A moveI=moveJ, moveJ=0
)
goto refresh

:crash
echo Crash^!

:turnTo-4  eXit

Upvotes: 1

JabberwockyDecompiler
JabberwockyDecompiler

Reputation: 3390

Assuming C# here is an example of how to read inputs from the command line.

namespace ConsoleTesting.CSharp
{
    public class TypeSafeEnum
    {
        /// <summary>
        /// Initialize the type save enum process
        /// </summary>
        public static void TypeSafeEnumMain()
        {
            PrintChoices();
            Suit suit = ReadOption();
            PrintSuit(suit);
            Common.Pause();
        }

        /// <summary>
        /// Evaluate the line entered
        /// </summary>
        /// <returns></returns>
        private static Suit ReadOption()
        {
            var option = Console.ReadLine();
            Suit suit;
            switch (option)
            {
                case "1":
                    suit = Suit.CLUBS;
                    break;
                case "2":
                    suit = Suit.DIAMONDS;
                    break;
                case "3":
                    suit = Suit.HEARTS;
                    break;
                case "4":
                    suit = Suit.SPADES;
                    break;
                default:
                    Console.WriteLine("Please enter a number 1 - 4.");
                    Console.WriteLine("");
                    PrintChoices();
                    return ReadOption();
            }

            return suit;
        }

        /// <summary>
        /// Print a list of options to select from the enums
        /// </summary>
        private static void PrintChoices()
        {
            Console.WriteLine("1) CLUBS");
            Console.WriteLine("2) DIAMONDS");
            Console.WriteLine("3) HEARTS");
            Console.WriteLine("4) SPADES");
            Console.WriteLine("");
            Console.WriteLine("Select your suit of choice.");
        }

        /// <summary>
        /// Print the suit using the suit class enum object
        /// </summary>
        /// <param name="suit"></param>
        private static void PrintSuit(Suit suit)
        {
            Console.WriteLine("Your suit is '{0}'", suit.ToString());
        }
    }

    /// <summary>
    /// Type Safe Enum
    /// Example from but converted to C# and refactored
    /// http://www.javacamp.org/designPattern/enum.html
    /// </summary>
    public class Suit
    {
        private string name;

        public static Suit CLUBS = new Suit("da clubs");
        public static Suit DIAMONDS = new Suit("da diamonds");
        public static Suit HEARTS = new Suit("da hearts");
        public static Suit SPADES = new Suit("da spades");

        private Suit(String name)
        {
            this.name = name;
        }
        public String ToString()
        {
            return name;
        }

    }
}

Upvotes: -1

Related Questions