コード例 #1
0
        /// <summary>
        ///     Allows the game to run logic such as updating the world.
        /// </summary>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
                Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            var screenBounds = GraphicsDevice.Viewport.Bounds;
            var touchState   = Keyboard.GetState();

            //Down paddle control
            if (touchState.IsKeyDown(Keys.Left))
            {
                PaddleBottom.X -= (float)(PaddleBottom.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            if (touchState.IsKeyDown(Keys.Right))
            {
                PaddleBottom.X += (float)(PaddleBottom.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
            }

            //Top paddle control
            if (touchState.IsKeyDown(Keys.A))
            {
                PaddleTop.X -= (float)(PaddleTop.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
                _paddleAi    = false;
            }
            if (touchState.IsKeyDown(Keys.D))
            {
                PaddleTop.X += (float)(PaddleTop.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
                _paddleAi    = false;
            }

            if (_paddleAi)
            {
                PaddleTop.X = MathHelper.Lerp(PaddleTop.X, PaddleTop.X + PaddleAI.Move(PaddleTop, Ball) * (float)(PaddleTop.Speed * gameTime.ElapsedGameTime.TotalMilliseconds), Ball.GetElement(0).Speed > 1 ? 1 : (Ball.GetElement(0).Speed / 1.2f));
            }

            //Limit paddle position
            PaddleBottom.X =
                MathHelper.Clamp(PaddleBottom.X, screenBounds.Left, screenBounds.Right - PaddleBottom.Width);
            PaddleTop.X = MathHelper.Clamp(PaddleTop.X, screenBounds.Left, screenBounds.Right - PaddleTop.Width);

            foreach (var ball in Ball)
            {
                //Ball movement
                var ballPositionChange =
                    ball.Direction * (float)(gameTime.ElapsedGameTime.TotalMilliseconds * ball.Speed);
                ball.X += ballPositionChange.X;
                ball.Y += ballPositionChange.Y;

                // Ball hitting side walls
                if (CollisionDetector.Overlaps(ball, Walls.GetElement(0)) ||
                    CollisionDetector.Overlaps(ball, Walls.GetElement(1)))
                {
                    HitSound.Play(0.2f, 0, 0);
                    ball.Direction.invertX();
                    if (ball.Speed < GameConstants.DefaultMaxBallSpeed)
                    {
                        ball.Speed *= ball.BumpSpeedIncreaseFactor;
                    }
                }

                // Ball hitting goals
                if (CollisionDetector.Overlaps(ball, Goals.GetElement(0)))
                {
                    Score.Player1++;
                    if (Ball.Count == 1)
                    {
                        ResetBall();
                    }
                    else
                    {
                        Ball.Remove(ball);
                    }
                }
                if (CollisionDetector.Overlaps(ball, Goals.GetElement(1)))
                {
                    Score.Player2++;
                    if (Ball.Count == 1)
                    {
                        ResetBall();
                    }
                    else
                    {
                        Ball.Remove(ball);
                    }
                }

                // Ball hitting paddles
                if (CollisionDetector.Overlaps(ball, PaddleTop) || CollisionDetector.Overlaps(ball, PaddleBottom))
                {
                    HitSound.Play(0.2f, 0, 0);
                    ball.Direction.invertY();
                    if (ball.Speed < GameConstants.DefaultMaxBallSpeed)
                    {
                        ball.Speed *= ball.BumpSpeedIncreaseFactor;
                    }

                    if (ball == Ball.GetElement(0))
                    {
                        SpawnPickups();
                    }
                }

                if (Timer.Equals("0"))
                {
                    foreach (var pickup in Pickups)
                    {
                        if (!CollisionDetector.Overlaps(ball, pickup))
                        {
                            continue;
                        }
                        pickup.Activate(this);
                        Pickups.Remove(pickup);
                    }
                }
            }

            //Ball outside of the screen
            if (!new Rectangle(-GameConstants.WallDefaultSize, -GameConstants.WallDefaultSize,
                               screenBounds.Width + GameConstants.WallDefaultSize,
                               screenBounds.Height + GameConstants.WallDefaultSize)
                .Intersects(new Rectangle((int)Ball.GetElement(0).X, (int)Ball.GetElement(0).Y,
                                          Ball.GetElement(0).Width, Ball.GetElement(0).Height)))
            {
                ResetBall();
            }

            base.Update(gameTime);
        }