简体   繁体   English

在C#中的控制台上移动

[英]Movement on the console in c#

I'm working on a snake game right now, and am having difficulty with making sure the tail follows my head. 我现在正在做蛇游戏,在确保尾巴顺着我的头有困难。 I'm currently trying to get it to work, and I've tried about a dozen different ideas, all of which either make it stall out completely (eg snake appears to be frozen in one place), or the vertical motion happens at all points of the tail at once, instead of one movement following another. 我目前正在尝试使其工作,并且已经尝试了大约十二种不同的想法,所有这些想法要么使其完全消失(例如,蛇似乎被冻结在一个地方),要么就发生了垂直运动。一次尾巴点,而不是一个接一个的动作。 I'm also having some trouble with the Console.Clear() method that seems inescapable. 我也似乎无法避免的Console.Clear()方法遇到了一些麻烦。 Either I do it too many times and it deletes everything but the first point of my snake, or I don't and the old positions don't get erased. 我执行了太多次后,它删除了我蛇的第一点以外的所有内容,或者我没有删除,并且旧位置也没有删除。 Here's the code (it's a test code, split from the actual game as I need to make sure the code works): 这是代码(这是测试代码,根据需要从实际游戏中拆分出来,以确保代码可以正常工作):

class Program
{
    const int size = 10;
    struct Sprite
    {
        public char[] ch;
        public int[,] posXY;
        public int directionX;
        public int directionY;
    }
    static void Main(string[] args)
    {
        int startX = 10;
        int startY;
        Sprite player = new Sprite();
        player.ch = new char[7];
        player.posXY = new int[7,2];
        for (int i = 0; i < player.ch.Length; i++)
        {
            player.ch[i] = '*';
        }
        for (int i = 0; i < 7; i++)
        {
            startY = 10;
            player.posXY[i, 0] = startX;
            player.posXY[i, 1] = startY;
            startX--;
        }
        ConsoleKeyInfo cki = new ConsoleKeyInfo();
        while (true)
        {
            update(cki, ref player);
            draw(player);
            Thread.Sleep(200);
        }//end while
    }//end main
    static void update(ConsoleKeyInfo cki, ref Sprite player)
    {
        if (Console.KeyAvailable)
        {
            cki = Console.ReadKey(true);
            if (cki.Key == ConsoleKey.LeftArrow || cki.Key == ConsoleKey.A)
            {
                player.directionX = -1;
                player.directionY = 0;
            }
            if (cki.Key == ConsoleKey.RightArrow || cki.Key == ConsoleKey.D)
            {
                player.directionX = 1;
                player.directionY = 0;
            }
            if (cki.Key == ConsoleKey.UpArrow || cki.Key == ConsoleKey.W)
            {
                player.directionX = 0;
                player.directionY = -1;
            }
            if (cki.Key == ConsoleKey.DownArrow || cki.Key == ConsoleKey.S)
            {
                player.directionX = 0;
                player.directionY = 1;
            }
        }//endif
        for (int i = 0; i < 7; i++)
        {
            player.posXY[i, 0] = player.posXY[i, 0] + player.directionX;
            player.posXY[i, 1] = player.posXY[i, 1] + player.directionY;
        }
    }//end update
    static void draw(Sprite player)
    {
        Console.Clear();
        for (int i = 0; i < 7; i++)
        {
            Console.SetCursorPosition(player.posXY[i, 0], player.posXY[i, 1]);
            Console.Write(player.ch[i]);
        }
    }//end draw

} }

PS I need to use a struct for my snake, using a Class isn't an option. PS我需要为蛇使用一个结构,使用类不是一个选择。

IMHO the best datastructure to describe the "snake" game is a queue. 恕我直言,描述“蛇”游戏的最佳数据结构是队列。 So that you can dequeue ("undraw") 1 "item" from the tail and enqueue ("draw") a new one with the new coordinates as the head. 这样您就可以从尾部出队(“取消绘制”)1个“项目”,并以新坐标为头入队(“绘制”)一个新的项目。 If it happens to be on the "apple" you just skip one dequeue operation or enqueue twice. 如果碰巧在“苹果”上,则只需跳过一个出队操作或两次入队。

If you are not familiar with the Queue data structure take a look at this fist: http://en.wikipedia.org/wiki/Queue_(abstract_data_type) 如果您不熟悉Queue数据结构,请看以下拳头: http : //en.wikipedia.org/wiki/Queue_(abstract_data_type)

Look at the default Queue of .net framework here: http://msdn.microsoft.com/en-us/library/7977ey2c.aspx 在此处查看.net框架的默认队列: http : //msdn.microsoft.com/zh-cn/library/7977ey2c.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM