Пример #1
0
        public static void Body()                               //main loop of program, moves body
        {
            movement[1] = 0;                                    //always start going in the right direction
            while (!Game.end)                                   //main loop
            {
                Thread.Sleep(Game.speed);                       //refreshes at difficulty speed
                Game.EndCheck();                                //ends game at any end conditions

                if (appleX == snakeX[1] && appleY == snakeY[1]) //check if the apple was got
                {
                    appleGot = true;
                    BodyGrow();
                }

                for (int i = snakeLength; i > 0; i--) //index movement array
                {
                    movement[i + 1] = movement[i];
                }

                Snake();                  //draws snake every scan
                Apple();                  //draws apple every scan

                if (Console.KeyAvailable) //key input
                {
                    while (true)
                    {
                        ConsoleKeyInfo turn = Console.ReadKey(true);
                        if (turn.Key.Equals(ConsoleKey.RightArrow) && movement[1] != 1)
                        {
                            movement[1] = 0;
                        }

                        if (turn.Key.Equals(ConsoleKey.LeftArrow) && movement[1] != 0)
                        {
                            movement[1] = 1;
                        }

                        if (turn.Key.Equals(ConsoleKey.UpArrow) && movement[1] != 2)
                        {
                            movement[1] = 3;
                        }

                        if (turn.Key.Equals(ConsoleKey.DownArrow) && movement[1] != 3)
                        {
                            movement[1] = 2;
                        }
                        break;
                    }
                }

                for (int i = 1; i <= snakeLength; i++) //changes direction of snake based on key input
                {
                    switch (movement[i])
                    {
                    case 0:
                        snakeX[i]++;
                        break;

                    case 1:
                        snakeX[i]--;
                        break;

                    case 2:
                        snakeY[i]++;
                        break;

                    case 3:
                        snakeY[i]--;
                        break;
                    }
                }
            }
        }
Пример #2
0
        public static void Body() //main loop of program, moves body
        {
            while (!Game.end)     //main loop
            {
                Thread.Sleep(Game.speed);
                Game.EndCheck();

                if (appleX == snakeX[1] && appleY == snakeY[1])
                {
                    appleGot = true;
                    BodyGrow();
                    //Console.ReadLine();                             //used for debug
                }

                else
                {
                    for (int i = snakeLength; i > 0; i--)
                    {
                        movement[i + 1] = movement[i];
                    }
                }

                Draw.Snake();             //draws the snake every scan of loop, moves the body

                if (Console.KeyAvailable) //key input
                {
                    while (true)
                    {
                        ConsoleKeyInfo turn = Console.ReadKey(true);
                        if (turn.Key.Equals(ConsoleKey.RightArrow) && movement[1] != 1)
                        {
                            movement[1] = 0;
                        }

                        if (turn.Key.Equals(ConsoleKey.LeftArrow) && movement[1] != 0)
                        {
                            movement[1] = 1;
                        }

                        if (turn.Key.Equals(ConsoleKey.UpArrow) && movement[1] != 2)
                        {
                            movement[1] = 3;
                        }

                        if (turn.Key.Equals(ConsoleKey.DownArrow) && movement[1] != 3)
                        {
                            movement[1] = 2;
                        }
                        break;
                    }
                }

                for (int i = 1; i <= snakeLength; i++) //changes direction of snake based on key input
                {
                    switch (movement[i])
                    {
                    case 0:
                        snakeX[i]++;
                        break;

                    case 1:
                        snakeX[i]--;
                        break;

                    case 2:
                        snakeY[i]++;
                        break;

                    case 3:
                        snakeY[i]--;
                        break;
                    }
                }
            }
        }