在按键上做
On key pressed do
我想做一个你控制角色的游戏。
游戏将是命令行(.bat 文件)。
问题是控制角色需要玩家按 (WSAD) 然后输入才能发生动作。
那么,我可以只按 (WSAD) 使角色移动吗?
如果是这样,我必须从互联网上下载一些废话吗?
这里假设 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;
}
}
}
可以使用 '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),它将自动选择 'w',如 /d
中指定
下面是 'choice' 命令的每个属性的作用:
/c 列出您希望用户拥有的击键(即在您的情况下为 wsad)
/n 切换是否 [W,S,A,D]?显示(是的,没有 /n,没有)
/t 设置做出默认选择前的时间
/d 在 /t 秒后设置默认选择
/m 将文本打印到控制台
有关 'choice' 的更多信息,请单击 here, or here。
另外,仅供参考,问题已经回答here
来源:Robvanderwoude,经验。
@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
编辑:另一个例子,蛇形动画!
@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
我想做一个你控制角色的游戏。
游戏将是命令行(.bat 文件)。
问题是控制角色需要玩家按 (WSAD) 然后输入才能发生动作。
那么,我可以只按 (WSAD) 使角色移动吗?
如果是这样,我必须从互联网上下载一些废话吗?
这里假设 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;
}
}
}
可以使用 '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),它将自动选择 'w',如 /d
中指定下面是 'choice' 命令的每个属性的作用:
/c 列出您希望用户拥有的击键(即在您的情况下为 wsad)
/n 切换是否 [W,S,A,D]?显示(是的,没有 /n,没有)
/t 设置做出默认选择前的时间
/d 在 /t 秒后设置默认选择
/m 将文本打印到控制台
有关 'choice' 的更多信息,请单击 here, or here。
另外,仅供参考,问题已经回答here
来源:Robvanderwoude,经验。
@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
编辑:另一个例子,蛇形动画!
@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