按下按键做

时间:2015-06-30 21:36:21

标签: batch-file cmd key

我想制作一个控制角色的游戏。

游戏将是命令行(.bat文件)。

问题在于控制角色需要玩家按下(WSAD)然后输入以使动作发生。

那么,我可以通过按下(WSAD)来使角色移动吗?

如果是这样,我必须从互联网上下载一些废话吗?

3 个答案:

答案 0 :(得分:3)

可以使用'choice'命令结构化为:

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

例如:

: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

会打印:

yourtexthere [W,S,A,D]?

如果w被按下了:

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

当然,不是打印文本然后退出,您可以添加特定代码来指定在::Your code here部分按下键时要执行的操作。

注意:如果此程序保留100秒(/ t),则会自动选择/ d中指定的“w”

以下是'choice'命令的每个属性:

/ c列出您希望用户拥有的击键(即在您的情况下为wsad)

/ n切换是否[W,S,A,D]?显示(是没有/ n,没有它)

/ t设置默认选择之前的时间

/ d设置/ t秒后的默认选项

/ m将文本打印到控制台

有关“选择”的更多信息,请点击p-normhere

另外,仅供将来参考,问题已经回答here

来源:here,经验。

答案 1 :(得分:1)

@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

编辑:另一个例子,类似Snake的动画!

@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

答案 2 :(得分:-1)

假设C#是一个如何从命令行读取输入的示例。

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;
        }

    }
}