Ejemplo n.º 1
0
        static void DrawMove()
        {
            while (true)
            {
                if (!Game.pause)
                {
                    Game.snake.Move(Game.snake.direction);

                    Thread.Sleep(Game.snake.speed);

                    if (Game.snake.body[0].x > 69)
                    {
                        Game.snake.body[0].x = 0;
                    }
                    else if (Game.snake.body[0].x < 0)
                    {
                        Game.snake.body[0].x = 69;
                    }

                    if (Game.snake.body[0].y > 19)
                    {
                        Game.snake.body[0].y = 0;
                    }
                    else if (Game.snake.body[0].y < 0)
                    {
                        Game.snake.body[0].y = 19;
                    }

                    if (Game.snake.AteFood())
                    {
                        Game.food.SetRandomPosition();
                    }

                    Game.CheckGameOver();
                    if (Game.Over)
                    {
                        break;
                    }

                    Game.Draw();
                }
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.SetWindowSize(120, 30);

            while (true)
            {
                Console.Clear();
                Console.CursorVisible = false;
                Console.Clear();

                Game.Init();
                if (Game.exit)
                {
                    break;
                }
                Console.Clear();

                DrawFrame();
                Game.wall.Draw();

                Game.snake.direction = 0;
                bool MainMenu = false;

                while (!Game.exit && !MainMenu)
                {
                    if (Game.pause)
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.SetCursorPosition(0, 25);
                        Console.WriteLine("Your game is paused.");
                    }
                    else
                    {
                        Game.food.Draw();
                        Game.snake.Draw();
                        ShowScore();
                    }
                    Thread.Sleep(100);

                    while (true)
                    {
                        bool tryagain = false;
                        if (Console.KeyAvailable)
                        {
                            ConsoleKeyInfo btn = Console.ReadKey(true);

                            if (btn.Key != ConsoleKey.P)
                            {
                                Game.pause = false;
                                switch (btn.Key)
                                {
                                case ConsoleKey.W:
                                    if (Game.snake.direction != 1 && Game.snake.direction != 2)
                                    {
                                        Game.snake.direction = 1;
                                    }
                                    else
                                    {
                                        tryagain = true;
                                    }
                                    break;

                                case ConsoleKey.S:
                                    if (Game.snake.direction != 1 && Game.snake.direction != 2)
                                    {
                                        Game.snake.direction = 2;
                                    }
                                    else
                                    {
                                        tryagain = true;
                                    }
                                    break;

                                case ConsoleKey.A:
                                    if (Game.snake.direction != 3 && Game.snake.direction != 4)
                                    {
                                        Game.snake.direction = 3;
                                    }
                                    else
                                    {
                                        tryagain = true;
                                    }
                                    break;

                                case ConsoleKey.D:
                                    if (Game.snake.direction != 3 && Game.snake.direction != 4)
                                    {
                                        Game.snake.direction = 4;
                                    }
                                    else
                                    {
                                        tryagain = true;
                                    }
                                    break;

                                case ConsoleKey.Escape:

                                    Console.Clear();
                                    if (!Game.pause)
                                    {
                                        Game.pause = true;
                                        Console.ForegroundColor = ConsoleColor.Red;
                                        Console.WriteLine("GAME PAUSE");
                                        int      option  = 0;
                                        string[] options = { "Resume", "Go to main menu", "Exit the game" };

                                        while (true)
                                        {
                                            Console.Clear();
                                            Console.WriteLine("GAME PAUSE");
                                            Game.pause = true;

                                            for (int i = 0; i < 3; i++)
                                            {
                                                if (option == i)
                                                {
                                                    Console.BackgroundColor = ConsoleColor.White;
                                                }
                                                else
                                                {
                                                    Console.BackgroundColor = ConsoleColor.Black;
                                                }

                                                Console.WriteLine(options[i]);
                                            }
                                            ConsoleKey key = Console.ReadKey(true).Key;
                                            switch (key)
                                            {
                                            case ConsoleKey.UpArrow:
                                                if (option > 0)
                                                {
                                                    option--;
                                                }
                                                break;

                                            case ConsoleKey.DownArrow:
                                                if (option < 2)
                                                {
                                                    option++;
                                                }
                                                break;
                                            }
                                            if (key == ConsoleKey.Enter)
                                            {
                                                if (option == 0)
                                                {
                                                    Console.Clear();
                                                    DrawFrame();
                                                    Game.snake.Draw();
                                                    Game.wall.Draw();
                                                    Game.food.Draw();
                                                    ShowScore();
                                                }

                                                else if (option == 1)
                                                {
                                                    MainMenu = true;
                                                }

                                                else
                                                {
                                                    Game.exit = true;
                                                }
                                                break;
                                            }

                                            if (key == ConsoleKey.Escape)
                                            {
                                                Console.Clear();
                                                DrawFrame();
                                                Game.snake.Draw();
                                                Game.wall.Draw();
                                                Game.food.Draw();
                                                ShowScore();
                                                break;
                                            }
                                        }
                                    }

                                    break;
                                }
                            }
                            else
                            {
                                Game.pause = true;
                            }
                        }


                        if (!tryagain)
                        {
                            break;
                        }
                    }

                    if (!Game.pause)
                    {
                        Game.snake.Move(Game.snake.direction);

                        if (Game.snake.body[0].x > 69)
                        {
                            Game.snake.body[0].x = 0;
                        }
                        else if (Game.snake.body[0].x < 0)
                        {
                            Game.snake.body[0].x = 69;
                        }

                        if (Game.snake.body[0].y > 19)
                        {
                            Game.snake.body[0].y = 0;
                        }
                        else if (Game.snake.body[0].y < 0)
                        {
                            Game.snake.body[0].y = 19;
                        }

                        if (Game.snake.AteFood())
                        {
                            Game.food.SetRandomPosition();
                        }

                        if (Game.CheckGameOver())
                        {
                            break;
                        }
                    }
                }
                if (Game.exit)
                {
                    break;
                }
            }
        }