【发布时间】:2015-01-29 16:25:45
【问题描述】:
我正在尝试在控制台中制作一条蛇,以让我在 C# 中进行培训。我的运动有问题。为了测试,我想通过按字母“a”来移动 X 上的“食物”,我注意到就图形而言,但不会通过 Console.Write 移动。Write 会增加。你能帮我个忙吗?我附上主课,世界美食
主要:
namespace Snake
{
class Program
{
static void Main(string[] args)
{
char s;
World mondo = new World(20, 10);
do
{
s = char.Parse(Console.ReadLine());
mondo.update();
mondo.drawWorld();
}
while (s == 'a');
Console.ReadLine();
}
}
}
世界:
namespace Snake
{
class World
{
public int larghezza;
public int altezza;
public int sizeSnake = 3;
public Food food;
public Snake[] snake;
public Snake s;
public enum Box { FREE , FOOD, SNAKE };//0,1,2
public Box[,] size;
public World(int larghezza, int altezza)
{
this.larghezza = larghezza;
this.altezza = altezza;
size = new Box[larghezza, altezza];
food = new Food();
snake = new Snake[sizeSnake];
s = new Snake();
food.InstaceFood(altezza, larghezza);
size[food.pos.x,food.pos.y] = Box.FOOD;
for (int i = 0; i < sizeSnake; i++)
{
snake[i] = s;
snake[0].startSnake(altezza, larghezza);
size[snake[i].pos.x-i, snake[i].pos.y] = Box.SNAKE;
}
}
public void update()
{
// snake[0].move(snake);
food.moveee();
}
public void control (Box type)
{
switch (type)
{
case Box.FOOD:
Console.ForegroundColor = ConsoleColor.Red;
Console.Write('X');
Console.ResetColor();
break;
case Box.FREE:
Console.Write(' ');
break;
case Box.SNAKE:
Console.ForegroundColor = ConsoleColor.Green;
Console.Write('█');
Console.ResetColor();
break;
}
}
public void drawWorld()
{
Console.Write('╔');
for (int i = 0; i < larghezza; i++)
{
Console.Write('═');
}
Console.Write('╗');
for (int i = 0; i < altezza; i++)
{
Console.Write('\n');
for (int j = 0; j < larghezza; j++)
{
if (j == 0)
{
Console.Write('║');
}
control(size[j,i]);
if (j == larghezza - 1)
Console.Write('║');
}
}
Console.Write('\n');
Console.Write('╚');
for (int i = 0; i < larghezza; i++)
{
Console.Write('═');
}
Console.WriteLine('╝');
}
}
}
食物:
namespace Snake
{
class Food
{
Random rnd = new Random();
public Vector2 pos;
public Food()
{
}
public void InstaceFood(int _Altezza, int _Larghezza)
{
int rndY = rnd.Next(0, _Altezza);
int rndX = rnd.Next(0, _Larghezza);
pos.x = rndX;
pos.y = rndY;
}
public void moveee()
{
this.pos.x += 1;
}
}
}
矢量2:
namespace Snake
{
struct Vector2
{
public int x;
public int y;
public Vector2(int xx, int yy)
{
x = xx;
y = yy;
}
}
}
【问题讨论】:
-
我很难真正弄清楚问题的措辞是什么。您能否尝试澄清一下您的问题到底是什么?
-
问题是“food”类中的函数 move 不能正常工作。我应该移动它的食物(测试),但在图形方面没有移动
-
您是否已经有一条正在工作的蛇在您的世界中穿行,或者您只是从食物开始?
-
您提供了代码很好,但在提问时请注意MCVE 的minimal 部分。最小化的一部分是“从头开始”......我发现当我执行这一步时,我经常可以自己找出问题。
-
现在我已经初始化了它。我想移动食物看看结果,然后我会移动蛇