简体   繁体   中英

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. 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.

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. 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)

Look at the default Queue of .net framework here: http://msdn.microsoft.com/en-us/library/7977ey2c.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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