Example #1
0
        // move snake
        static List <Struct.DIEM> MoveSnake(List <Struct.DIEM> Snake, string DirectionMove, int score)
        {
            Struct.DIEM newPoint;
            switch (DirectionMove)
            {
            case "LeftArrow":
                newPoint.x = Snake[0].x - 1;
                newPoint.y = Snake[0].y;
                Snake.Insert(0, newPoint);
                //Check collision
                if (MiniGameOver.CheckCollision(newPoint))
                {
                    MiniGameOver.GameOver(score);
                    Snake.Clear();
                    return(Snake);
                }
                //Check collision trailer
                if (MiniGameOver.CheckCollisionTrailer(Snake, newPoint))
                {
                    MiniGameOver.GameOver(score);
                    Snake.Clear();
                    return(Snake);
                }
                //ve lại snake
                RePaintSnake(newPoint, Snake[1], Snake[Snake.Count - 1]);
                Snake.RemoveAt(Snake.Count - 1);
                return(Snake);

            case "RightArrow":
                newPoint.x = Snake[0].x + 1;
                newPoint.y = Snake[0].y;
                Snake.Insert(0, newPoint);
                //Check collision
                if (MiniGameOver.CheckCollision(newPoint))
                {
                    MiniGameOver.GameOver(score);
                    Snake.Clear();
                    return(Snake);
                }
                //Check collision trailer
                if (MiniGameOver.CheckCollisionTrailer(Snake, newPoint))
                {
                    MiniGameOver.GameOver(score);
                    Snake.Clear();
                    return(Snake);
                }
                // ve lại snake
                RePaintSnake(newPoint, Snake[1], Snake[Snake.Count - 1]);
                Snake.RemoveAt(Snake.Count - 1);
                return(Snake);

            case "UpArrow":
                newPoint.x = Snake[0].x;
                newPoint.y = Snake[0].y - 1;
                Snake.Insert(0, newPoint);
                //Check collision
                if (MiniGameOver.CheckCollision(newPoint))
                {
                    MiniGameOver.GameOver(score);
                    Snake.Clear();
                    return(Snake);
                }
                //Check collision trailer
                if (MiniGameOver.CheckCollisionTrailer(Snake, newPoint))
                {
                    MiniGameOver.GameOver(score);
                    Snake.Clear();
                    return(Snake);
                }
                //ve lại snake
                RePaintSnake(newPoint, Snake[1], Snake[Snake.Count - 1]);
                Snake.RemoveAt(Snake.Count - 1);
                return(Snake);

            case "DownArrow":
                newPoint.x = Snake[0].x;
                newPoint.y = Snake[0].y + 1;
                Snake.Insert(0, newPoint);
                //Check collision
                if (MiniGameOver.CheckCollision(newPoint))
                {
                    MiniGameOver.GameOver(score);
                    Snake.Clear();
                    return(Snake);
                }
                //Check collision trailer
                if (MiniGameOver.CheckCollisionTrailer(Snake, newPoint))
                {
                    MiniGameOver.GameOver(score);
                    Snake.Clear();
                    return(Snake);
                }
                //ve lại snake
                RePaintSnake(newPoint, Snake[1], Snake[Snake.Count - 1]);
                Snake.RemoveAt(Snake.Count - 1);
                return(Snake);
            }
            return(Snake);
        }
Example #2
0
        // control snake
        static string ControlSnake(int level, List <Struct.DIEM> Snake, string DirectionSnake, ref Struct.DIEM randomPoint, ref bool checkEat, ref int score)
        {
            Random randomXY = new Random();

            Struct.DIEM oldFood;
            oldFood.x = 0;
            oldFood.y = 0;
            ConsoleKeyInfo input;
            var            startTime = DateTime.UtcNow;
            int            count     = 1;
            bool           check     = true;

            level = (11 - level) * 25;
            int stage = randomXY.Next(6, 12);

            do
            {
                while (!Console.KeyAvailable)
                {
                    if (DateTime.UtcNow - startTime > TimeSpan.FromMilliseconds(level))
                    {
                        Snake = MoveSnake(Snake, DirectionSnake, score);
                        if (Snake.Count == 0)
                        {
                            return("GAMEOVER");
                        }
                        startTime = DateTime.UtcNow;
                        count++;
                    }

                    if (count == stage && checkEat == true)
                    {
                        randomPoint.x = randomXY.Next(40, 90);
                        randomPoint.y = randomXY.Next(2, 27);
                        checkEat      = false;
                        if (randomPoint.x != 0)
                        {
                            RandomPointPanit(randomPoint);
                        }
                        count = 1;
                    }
                    if (!checkEat && count % 9 == 0)
                    {
                        if (randomPoint.x != 0)
                        {
                            RandomPointPanit(randomPoint);
                        }
                    }

                    if (MiniGameOver.CheckEat(Snake[0], randomPoint) && !checkEat)
                    {
                        oldFood.x = randomPoint.x;
                        oldFood.y = randomPoint.y;
                        checkEat  = true;
                        Snake.Add(randomPoint);
                        score++;
                        Console.SetCursorPosition(23, 3);
                        Console.BackgroundColor = ConsoleColor.White;
                        Console.Write(score);
                        count = 1;
                    }

                    if (checkEat && randomPoint.x != 0)
                    {
                        //FragmentSnake(randomPoint);
                        HeaderSnakeEatting(Snake[0]);
                        FragmentSnake(Snake[1]);
                    }
                }
                input = Console.ReadKey(true);
                if (input.Key == ConsoleKey.UpArrow || input.Key == ConsoleKey.LeftArrow)
                {
                    check = false;
                }
                if (input.Key == ConsoleKey.DownArrow || input.Key == ConsoleKey.RightArrow)
                {
                    check = false;
                }
                if (input.Key.ToString() == DirectionSnake)
                {
                    check = true;
                }
                if (input.Key == ConsoleKey.Escape)
                {
                    check = false;
                    break;
                }
                switch (DirectionSnake)
                {
                case "UpArrow":
                    if (input.Key.ToString() == "DownArrow" || input.Key.ToString() == "UpArrow")
                    {
                        check = true;
                    }
                    break;

                case "DownArrow":
                    if (input.Key.ToString() == "UpArrow" || input.Key.ToString() == "DownArrow")
                    {
                        check = true;
                    }
                    break;

                case "LeftArrow":
                    if (input.Key.ToString() == "RightArrow" || input.Key.ToString() == "LeftArrow")
                    {
                        check = true;
                    }
                    break;

                case "RightArrow":
                    if (input.Key.ToString() == "LeftArrow" || input.Key.ToString() == "RightArrow")
                    {
                        check = true;
                    }
                    break;
                }
            } while (check);
            return(input.Key.ToString());
        }