There is some other way to give body to the snake in c #

1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;






namespace Snake
{
    struct Position
    {
        public  Position (int x, int y)
    { 
        X = x;
        Y = y;
    }

        public int X;
        public int Y;



    }
    class Program
    {
        static void Main(string[] args)
        {
            string SnakeHead = ">";
            int Queuelenght = 10;
            Position Posicion = new Position(1, 3);
            List<Position> Snake  = new List<Position>();

            string direccion = "E";
            Console.CursorVisible = false;


            try
            {



                for (; ; )
                {
                    Console.SetCursorPosition(Posicion.X, Posicion.Y);
                    Console.Write(SnakeHead);

                    Console.SetCursorPosition(Posicion.X, Posicion.Y);
                    Console.Write(SnakeHead);
                    Snake.Add(Posicion);

                    if (Snake.Count > Queuelenght)
                    {
                        Position primero = Snake.First();
                       Snake.RemoveAt(0);
                        Console.SetCursorPosition(primero.X, primero.Y);
                        Console.Write(" ");
                    }

                    switch (direccion)
                    {
                        case "S":
                            Posicion.Y++;
                            break;
                        case "N":
                            Posicion.Y--;
                            break;
                        case "E":
                            Posicion.X++;
                            break;
                        case "O":
                            Posicion.X--;
                            break;
                    }


                    Thread.Sleep(50);
                    Console.ForegroundColor = ConsoleColor.Red;



                    if (Console.KeyAvailable)
                    {
                        ConsoleKey Key = Console.ReadKey(true).Key;
                        switch (Key)
                        {
                            case ConsoleKey.DownArrow:
                                if ((direccion == "E") || (direccion == "O"))
                                    direccion = "S";
                                break;
                            case ConsoleKey.UpArrow:
                                if ((direccion == "E") || (direccion == "O"))
                                    direccion = "N";
                                break;
                            case ConsoleKey.LeftArrow:
                                if ((direccion == "N") || (direccion == "S"))
                                    direccion = "O";
                                break;
                            case ConsoleKey.RightArrow:
                                if ((direccion == "N") || (direccion == "S"))
                                    direccion = "E";
                                break;


                        }
                    }
                }




            }
            catch (Exception )
            {
                Console.WriteLine("Game Over");
                Console.ReadKey();
                return;


            }
        }
    }
}
    
asked by Wolf117 01.06.2016 в 19:20
source

1 answer

1

What you can do is, instead of writing characters of a certain color, write spaces with different background color, giving a color to the head and another different from the rest of the snake.

When you write the head in a new position, you should first change the color of the previous position of the head so that it becomes part of the tail:

    static void Main(string[] args)
    {
        ConsoleColor headColor = ConsoleColor.Red;
        ConsoleColor queueColor = ConsoleColor.Green;
        int Queuelenght = 10;
        Position Posicion = new Position(1, 3);
        List<Position> Snake = new List<Position>();

        string direccion = "E";
        Console.CursorVisible = false;

        try
        {
            for (;;)
            {
                if (Snake.Any())
                {
                    Position ultimo = Snake.Last();
                    Console.BackgroundColor = queueColor;
                    Console.SetCursorPosition(ultimo.X, ultimo.Y);
                    Console.Write(" ");
                }

                Console.BackgroundColor = headColor;
                Console.SetCursorPosition(Posicion.X, Posicion.Y);
                Console.Write(" ");
                Snake.Add(Posicion);

                if (Snake.Count > Queuelenght)
                {
                    Position primero = Snake.First();
                    Snake.RemoveAt(0);
                    Console.BackgroundColor = ConsoleColor.Black;
                    Console.SetCursorPosition(primero.X, primero.Y);
                    Console.Write(" ");
                }

                switch (direccion)
                {
                    case "S":
                        Posicion.Y++;
                        break;
                    case "N":
                        Posicion.Y--;
                        break;
                    case "E":
                        Posicion.X++;
                        break;
                    case "O":
                        Posicion.X--;
                        break;
                }

                Thread.Sleep(50);

                if (Console.KeyAvailable)
                {
                    ConsoleKey Key = Console.ReadKey(true).Key;
                    switch (Key)
                    {
                        case ConsoleKey.DownArrow:
                            if ((direccion == "E") || (direccion == "O"))
                                direccion = "S";
                            break;
                        case ConsoleKey.UpArrow:
                            if ((direccion == "E") || (direccion == "O"))
                                direccion = "N";
                            break;
                        case ConsoleKey.LeftArrow:
                            if ((direccion == "N") || (direccion == "S"))
                                direccion = "O";
                            break;
                        case ConsoleKey.RightArrow:
                            if ((direccion == "N") || (direccion == "S"))
                                direccion = "E";
                            break;
                    }
                }
            }
        }
        catch (Exception)
        {
            Console.BackgroundColor = ConsoleColor.Black;
            Console.WriteLine("Game Over");
            Console.ReadKey();
            return;
        }
    }
    
answered by 01.06.2016 в 19:45