[英]why i can't move the player while I am trying to make space invader in c#
[英]C# console game space invader
嘿,我用太空飞船写了一个代码,但是我不能将它向左或向右移动,我想问问是否有人可以帮助我,因为我不理解。 我尝试了一些其他方法,例如以其他方式编写它,但是它不起作用,对于编程来说是新手,也许您可以通过编写我的代码的方式看到它。
string player = @"
^
o
| o |
/\\\o///\
</o\>";
char laser = '|';
int moves = 0;
int xlaser = rnd.Next(width), ylaser = rnd.Next(hight);
WriteHere(xlaser, ylaser, laser);
WriteHere(x, y, player);
// game loop
do
{
Console.SetCursorPosition(0, 0);
if (Console.KeyAvailable)
{
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.UpArrow:
{
if (y > 0)
{
//y -= 1;
y--; // ist //y = y - 1;
}
moves++;
}
break;
case ConsoleKey.DownArrow:
{
if (y < hight - 1)
{
y++; // ist y = y + 1;
}
moves++;
}
break;
case ConsoleKey.LeftArrow:
{
if (x > 0)
{
x--;
}
moves++;
}
break;
case ConsoleKey.RightArrow:
{
if (x < width - 1)
{
x++;
}
moves++;
}
break;
}
Console.Clear();
Writehere(x, y, player);
如果您定义不带空格的播放器字符串,并将其逐行写入控制台,它将起作用。 我确定有更好的解决方案,但是作为快速解决方案,这可能没问题。
用此代码替换WriteHere
static void WriteHere(int x, int y, string s)
{
foreach (var line in s.Split('\n'))
{
Console.SetCursorPosition(x, y);
Console.Write(line);
y++;
}
}
对于玩家而言,
string player = @"
^
o
| o |
/\\\o///\
</o\>";
这样做(注意左侧空白的不同)。
string player = @"
^
o
| o |
/\\\o///\
</o\>";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.