Esempio n. 1
0
        protected override void Update(GameTime gameTime)
        {
            //handle key press
            if (snake.IsAlive)
            {
                timeSinceLastBadAppleSpawned += gameTime.ElapsedGameTime.TotalSeconds;
                if (inputManager.WasAnyKeyPressed())
                {
                    newDirection = inputManager.GetDirectionFromValidInput();
                }

                if (gameManager.Tick(gameTime.ElapsedGameTime.TotalSeconds))
                {
                    snake.Move(newDirection);

                    if (timeSinceLastBadAppleSpawned > timeToSpawnBadApple)
                    {
                        Apple newBadApple = new Apple(snake.Body.Select(x => x.Position).ToArray(), true);
                        badApples.Add(newBadApple);
                        timeToSpawnBadApple          = new Random().Next(3, 8);
                        timeSinceLastBadAppleSpawned = 0;
                    }

                    if (snake.IsAlive)
                    {
                        if (gameManager.CheckCollision(snake.Body[0].Position, apple.Position))
                        {
                            //concat the bad apple positions to the snake positions so good apple doesn't spawn on existing bad apple
                            apple.DropApple(snake.Body.Select(x => x.Position).Concat(badApples.Select(x => x.Position)).ToArray());
                            snake.Grow();
                        }
                        foreach (var badApple in badApples)
                        {
                            if (gameManager.CheckCollision(snake.Body[0].Position, badApple.Position))
                            {
                                gameManager.IncreaseSpeed();
                                badApples.Remove(badApple);
                                snake.Score -= 5;
                                break;
                            }
                        }
                    }
                }
            }
            else
            {
                if (inputManager.WasKeyPressed(Keys.Enter))
                {
                    GameManager.TICK  = 0.1f;
                    gameManager.Speed = 1;
                    snake             = new Snake();
                    badApples.Clear();
                    timeSinceLastBadAppleSpawned = 0;
                    snake.IsAlive = true;
                    startOfGame   = false;
                }
            }

            base.Update(gameTime);
        }