示例#1
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            // TODO: Add your update logic here
            state = Keyboard.GetState();
            score = "Score: " + player.Score;

            fontSize = font.MeasureString(score);

            lives = "Lives: " + player.Lives;
            if (player.Lives > 0 && !gameOver)
            {
                if (!ball.NewBall)
                {
                    paddle.Update(state, GraphicsDevice.Viewport.Width);
                    ball.Update(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, paddle, player);

                    for (int i = 0; i < levelBricks.Count; i++)
                    {
                        for (int j = 0; j < levelBricks[i].Count; j++)
                        {
                            if (ball.CheckCollision(levelBricks[i][j]))
                            {
                                hit = true;
                                levelBricks[i].Remove(levelBricks[i][j]);
                                player.Score++;
                                break;
                            }
                        }

                        if (levelBricks[i].Count == 0)
                        {
                            levelBricks.Remove(levelBricks[i]);
                        }

                        if (hit)
                        {
                            hit = false;
                            break;
                        }

                        if (levelBricks.Count == 0)
                        {
                            levelBricks.Remove(levelBricks[i]);
                        }
                    }

                    if (levelBricks.Count == 0)
                    {
                        gameOver = true;
                    }
                }
                else if (ball.NewBall)
                {
                    if (state.IsKeyDown(Keys.Space))
                    {
                        ball.NewBall = false;
                    }
                }
            }

            else
            {
                if (player.Lives > 0)
                {
                    gameMessage = "You WIN!";
                }

                else
                {
                    gameMessage = "You LOSE";
                }

                gameFontSize = endGameFont.MeasureString(gameMessage);
                //You lose
                if (state.IsKeyDown(Keys.Enter))
                {
                    ball.NewBall = true;
                    player.Lives = 3;
                    player.Score = 0;
                    gameOver     = false;
                    levelBricks  = new List <List <Brick> >();
                    ball.ResetPosition(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, paddle);
                    generateBricks();
                }
            }
            base.Update(gameTime);
        }