So far I made a snake that can move properly. I want to create the
points that he eat.
here is the snake I have done:


using System;
using System.Threading;
using System.Collections.Generic;
using System.Text;

namespace ReadKey
{
    class Program
    {
        static void ShiftLeftArrays(short[] xSnake, short[] ySnake)
        {
            for (int i = 0; i < xSnake.Length - 1; i++)
            {
                xSnake[i] = xSnake[i + 1];
                ySnake[i] = ySnake[i + 1];
            }
        }
        static void OpositeArray(short[] a)
        {
            int upIndex = a.Length - 1, lowIndex = 0;
            short temp;
            for (int i = 0; upIndex > lowIndex; i++)
            {
                temp = a[upIndex];
                a[upIndex] = a[lowIndex];
                a[lowIndex] = temp;
                upIndex--;
                lowIndex++;
            }
        }
        static void ShowFirstSnake(short[] xSnake, short[] ySnake)
        {
            for (int i = 0; i < xSnake.Length; i++)
            {
                Console.SetCursorPosition(xSnake[i], ySnake[i]);
                Console.Write("*");
            }
        }

        static void Main(string[] args)
        {
            const short WINDOW_WIDTH=80;
            const short WINDOW_HEIGHT=49;
            short[] xSnake = { WINDOW_WIDTH / 2, WINDOW_WIDTH / 2 + 1,
WINDOW_WIDTH / 2 + 2,
                                                WINDOW_WIDTH / 2 + 3,
WINDOW_WIDTH / 2 + 4 };
            short[] ySnake = { WINDOW_HEIGHT / 2, WINDOW_HEIGHT / 2,
WINDOW_HEIGHT / 2,
                                                    WINDOW_HEIGHT / 2,
WINDOW_HEIGHT / 2 };

            Console.CursorVisible = false;
            Console.BackgroundColor = ConsoleColor.Blue;
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WindowHeight = WINDOW_HEIGHT;
            Console.WindowWidth = WINDOW_WIDTH;

            ShowFirstSnake(xSnake, ySnake);

            ConsoleKeyInfo KeyIn, lastKey;

            KeyIn = Console.ReadKey(true);
            lastKey = KeyIn;
            while (KeyIn.Key != ConsoleKey.Escape)
            {
                if (KeyIn.Key == ConsoleKey.RightArrow)
                {
                    if (lastKey.Key == ConsoleKey.LeftArrow)
                    {
                        OpositeArray(xSnake);
                        OpositeArray(ySnake);
                    }
                    Console.SetCursorPosition(xSnake[0], ySnake[0]);
                    Console.Write(" ");
                    ShiftLeftArrays(xSnake, ySnake);
                    xSnake[xSnake.Length - 1]++;
                }
                if (KeyIn.Key == ConsoleKey.UpArrow)
                {
                    if (lastKey.Key == ConsoleKey.DownArrow)
                    {
                        OpositeArray(xSnake);
                        OpositeArray(ySnake);
                    }
                    Console.SetCursorPosition(xSnake[0], ySnake[0]);
                    Console.Write(" ");
                    ShiftLeftArrays(xSnake, ySnake);
                    ySnake[xSnake.Length - 1]--;
                }

                if (KeyIn.Key == ConsoleKey.LeftArrow)
                {
                    if (lastKey.Key == ConsoleKey.RightArrow)
                    {
                        OpositeArray(xSnake);
                        OpositeArray(ySnake);
                    }
                    Console.SetCursorPosition(xSnake[0], ySnake[0]);
                    Console.Write(" ");
                    ShiftLeftArrays(xSnake, ySnake);
                    xSnake[xSnake.Length - 1]--;
                }
                if (KeyIn.Key == ConsoleKey.DownArrow)
                {
                    if (lastKey.Key == ConsoleKey.UpArrow)
                    {
                        OpositeArray(xSnake);
                        OpositeArray(ySnake);
                    }
                    Console.SetCursorPosition(xSnake[0], ySnake[0]);
                    Console.Write(" ");
                    ShiftLeftArrays(xSnake, ySnake);
                    ySnake[xSnake.Length - 1]++;
                }
                Console.SetCursorPosition(xSnake[xSnake.Length - 1],
ySnake[ySnake.Length - 1]);
                Console.Write("*");
                lastKey = KeyIn;
                KeyIn = Console.ReadKey(true);
                points(xSnake,ySnake);
            }
            Console.WriteLine();
        }
    }
}


I made a function to the points

        static void Points(short[] xSnake, short[] ySnake)
        {
            Random rnd = new Random();
            int potx, poty;
            potx = rnd.Next(50);
            poty = rnd.Next(50);
            Console.SetCursorPosition(rnd.Next(50), rnd.Next(50));
            Console.Write("*");
            if (xSnake[0] == potx && ySnake[0] == poty)
            {
                Console.SetCursorPosition(potx, poty);
                Console.Write("*");
            }
        }


you can see where I put this function, now when I run it everytime I
move the snake it makes more point.
can anyone help me ?
thanks

Reply via email to