Hai

I have one doubt when end your game program i.e. (run program
continuously till user press escape)
Ok I give some suggestion
        1)Increase snack length when eat  food
        2)Add score(based on maximum food eat with in a time )  to
your program and print to user
Any way
        I made small changes in your program see the following
code .Now it will run as per your desire(now it show one food  at a
time ), I add comment line in your program where I made change and I
remove some unwanted statements.



using System;

namespace SnakeConsoleApplication
{
    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 potx=0, poty=0;
            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);
            //show first food
            ShowFood( ref potx,ref poty);
            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);
                if (xSnake[0] == potx && ySnake[0] == poty)
                    //when user can reached next food will appear
                    ShowFood(ref potx, ref poty);
            }
            Console.WriteLine();
        }
        //your Point function replaced with following function
        static void ShowFood( ref short potx,ref  short poty)
        {
            Random rnd = new Random();
            potx = (short) rnd.Next(50);
            poty = (short) rnd.Next(50);
            Console.SetCursorPosition(potx, poty);
            Console.Write("*");
        }
    }
 }

I hope this code will  satisfy you


On Dec 13, 7:07 pm, "[email protected]" <[email protected]>
wrote:
> 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