Ejemplo n.º 1
0
        public bool Run()
        {
            while (GameRunning)
            {
                if (GameMode == GameMode.SinglePlayer)
                {
                    if (Snake.CheckBoardCollision(Board))
                    {
                        GameRunning = false;
                        this.Winner = "Someone Else";
                        break;
                    }
                    if (Snake.CheckTailCollision())
                    {
                        GameRunning = false;
                        this.Winner = "Someone Else";
                        break;
                    }
                }
                //--------------------------------------------
                // Check for collision with tail and board
                //--------------------------------------------
                if (GameMode == GameMode.SnakeVsApple)
                {
                    if (Snake.CheckBoardCollision(Board))
                    {
                        GameRunning = false;
                        this.Winner = "Apple";
                        break;
                    }
                    if (Snake.CheckTailCollision())
                    {
                        GameRunning = false;
                        this.Winner = "Apple";
                        break;
                    }
                }
                else if (GameMode == GameMode.SnakeVsSnake)
                {
                    //--------------------------------------------
                    // Check for collision on Snake Player Two
                    //--------------------------------------------
                    if (SnakePlayerTwo.CheckBoardCollision(Board))
                    {
                        GameRunning = false;
                        this.Winner = "Player 1";
                        break;
                    }
                    if (SnakePlayerTwo.CheckTailCollision())
                    {
                        GameRunning = false;
                        this.Winner = "Player 1";
                        break;
                    }
                    if (SnakePlayerTwo.CheckSnakeCollision(Snake))
                    {
                        GameRunning = false;
                        this.Winner = "Player 1";
                        break;
                    }
                    //--------------------------------------------
                    // Check for collision with other Snake
                    //--------------------------------------------
                    //if (Snake.CheckSnakeCollision(SnakePlayerTwo))
                    //{
                    //    GameRunning = false;
                    //    this.Winner = "Player 2";
                    //    break;
                    //}
                }



                //--------------------------------------------
                // Getting movement direction
                //--------------------------------------------
                if (Console.KeyAvailable)
                {
                    Direction = Joystick.SetKeyDirection(SnakeDirection);
                    if (Direction == KeyDirection.Up || Direction == KeyDirection.Down || Direction == KeyDirection.Left || Direction == KeyDirection.Right)
                    {
                        SnakeDirection = Direction;
                    }
                    else
                    {
                        AppleDirection = Direction;
                    }
                }

                //--------------------------------------------
                // Eating of the apple
                //--------------------------------------------
                if (GameMode == GameMode.SnakeVsApple)
                {
                    SnakeEnergy = Snake.GetEnergy();
                    IsFoodEaten = Snake.Eat(Apple);
                }
                else if (GameMode == GameMode.SinglePlayer)
                {
                    IsFoodEaten = Snake.Eat(Food);
                }
                else if (GameMode == GameMode.SnakeVsSnake)
                {
                    OnePlayerFoodIsEaten = Snake.Eat(Food);
                    TwoPlayerFoodIsEaten = SnakePlayerTwo.Eat(Food);
                    if (OnePlayerFoodIsEaten)
                    {
                        Score++;
                        IsFoodEaten = true;
                    }
                    if (TwoPlayerFoodIsEaten)
                    {
                        TwoPlayerScore++;
                        IsFoodEaten = true;
                    }
                }

                Apple.EraseOldApple();
                if (GameMode == GameMode.SnakeVsApple)
                {
                    Snake.Move(SnakeDirection, IsFoodEaten);
                    AppleDirection = Apple.Move(AppleDirection);
                }
                else if (GameMode == GameMode.SnakeVsSnake)
                {
                    SnakeDirectionPlayerTwo = SnakePlayerTwo.TranslateAppleDirectionToSnake(AppleDirection, SnakeDirectionPlayerTwo);
                    Snake.Move(SnakeDirection, OnePlayerFoodIsEaten);
                    SnakePlayerTwo.Move(SnakeDirectionPlayerTwo, TwoPlayerFoodIsEaten);
                }
                else if (GameMode == GameMode.SinglePlayer)
                {
                    Snake.Move(SnakeDirection, IsFoodEaten);
                }



                if (IsFoodEaten)
                {
                    if (GameMode == GameMode.SnakeVsApple)
                    {
                        Apple.LoseLife();
                        Apple.MakeFood(BoardWidth, BoardHeight);
                        Energy = Apple.GetEnergy();
                        Snake.GetEnergyFromApple(Energy);
                    }
                    else if (GameMode == GameMode.SnakeVsSnake)
                    {
                        Food.MakeFood(BoardWidth, BoardHeight);
                        IsFoodEaten = false;
                    }
                    else
                    {
                        Score++;
                        Food.MakeFood(BoardWidth, BoardHeight);
                    }
                }

                //--------------------------------------------
                // Drawing the graphics in both modes
                //--------------------------------------------
                Snake.Draw("Green");



                //--------------------------------------------
                // Drawing different for Two vs One Player mode
                //--------------------------------------------
                if (GameMode == GameMode.SnakeVsApple)
                {
                    Apple.RottTheApple();
                    Energybar.Draw(BoardHeight, SnakeEnergy);
                    Snake.LoseEnergy();
                    Apple.Draw();
                    Apple.DrawLifes(BoardHeight);
                }
                else if (GameMode == GameMode.SnakeVsSnake)
                {
                    SnakePlayerTwo.Draw("Magenta");
                    Food.Draw();
                }
                else
                {
                    Food.Draw();
                    ShowScore(Score, BoardHeight);
                }



                if (SnakeEnergy < 1 && GameMode == GameMode.SnakeVsApple)
                {
                    this.GameRunning = false;
                    this.Winner      = "Apple";
                }
                if (Apple.GetLifes() < 1 && GameMode == GameMode.SnakeVsApple)
                {
                    this.GameRunning = false;
                    this.Winner      = "Snake";
                }

                //--------------------------------------------
                // Timer for loop
                //--------------------------------------------
                Thread.Sleep(100 - Speed);
            }

            Console.Clear();
            Console.ForegroundColor = ConsoleColor.White;
            GameOver(this.Winner);
            bool playAgain = AskIfPlayAgain();

            return(playAgain);
        }