コード例 #1
0
ファイル: Game1.cs プロジェクト: anthonyjunakovic/raupjc-hw1
        /// <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();
            }

            var bounds = GraphicsDevice.Viewport.Bounds;

            var touchState = Keyboard.GetState();

            if (touchState.IsKeyDown(Keys.Left))
            {
                PaddleBottom.X -= (float)(PaddleBottom.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            else if (touchState.IsKeyDown(Keys.Right))
            {
                PaddleBottom.X += (float)(PaddleBottom.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            PaddleBottom.X = MathHelper.Clamp(PaddleBottom.X, bounds.Left, bounds.Right - PaddleBottom.Width);

            if (touchState.IsKeyDown(Keys.A))
            {
                PaddleTop.X -= (float)(PaddleTop.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            else if (touchState.IsKeyDown(Keys.D))
            {
                PaddleTop.X += (float)(PaddleTop.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            PaddleTop.X = MathHelper.Clamp(PaddleTop.X, bounds.Left, bounds.Right - PaddleTop.Width);

            var ballPositionChange = Ball.Direction * new Vector2((float)(gameTime.ElapsedGameTime.TotalMilliseconds * Ball.Speed));

            Ball.X += ballPositionChange.X;
            Ball.Y += ballPositionChange.Y;

            foreach (Wall wall in Walls)
            {
                if (CollisionDetector.Overlaps(Ball, wall))
                {
                    Ball.Direction.TurnHorizontal();
                    Ball.Speed *= Ball.BumpSpeedIncreaseFactor;
                }
            }

            foreach (Wall goal in Goals)
            {
                if (CollisionDetector.Overlaps(Ball, goal))
                {
                    if (goal.Player.Equals(PaddleBottom))
                    {
                        ScoreTop++;
                    }
                    else
                    {
                        ScoreBottom++;
                    }
                    UpdateTitle();
                    Ball.X     = bounds.Width / 2 - Ball.Width / 2;
                    Ball.Y     = bounds.Height / 2 - Ball.Height / 2;
                    Ball.Speed = GameConstants.DefaultInitialBallSpeed;
                    HitSound.Play();
                    break;
                }
            }

            if ((CollisionDetector.Overlaps(Ball, PaddleBottom)) || (CollisionDetector.Overlaps(Ball, PaddleTop)))
            {
                Ball.Direction.TurnVertical();
                Ball.Speed *= Ball.BumpSpeedIncreaseFactor;
            }

            base.Update(gameTime);
        }
コード例 #2
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();
            }

            var touchState = Keyboard.GetState();

            if (touchState.IsKeyDown(Keys.Left))
            {
                PaddleBottom.X = PaddleBottom.X - (float)(PaddleBottom.Speed *
                                                          gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            if (touchState.IsKeyDown(Keys.Right))
            {
                PaddleBottom.X = PaddleBottom.X + (float)(PaddleBottom.Speed *
                                                          gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            PaddleBottom.X = MathHelper.Clamp(PaddleBottom.X, screenBounds.Left, screenBounds.Right -
                                              PaddleBottom.Width);

            //Player two
            if (touchState.IsKeyDown(Keys.A))
            {
                PaddleTop.X = PaddleTop.X - (float)(PaddleTop.Speed *
                                                    gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            if (touchState.IsKeyDown(Keys.D))
            {
                PaddleTop.X = PaddleTop.X + (float)(PaddleTop.Speed *
                                                    gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            PaddleTop.X = MathHelper.Clamp(PaddleTop.X, screenBounds.Left, screenBounds.Right -
                                           PaddleTop.Width);

            var ballPositionChange = Ball.Direction *
                                     (float)(gameTime.ElapsedGameTime.TotalMilliseconds * Ball.Speed);

            Ball.X += ballPositionChange.X;
            Ball.Y += ballPositionChange.Y;

            foreach (Wall wall in Walls)
            {
                if (CollisionDetector.Overlaps(Ball, wall))
                {
                    Ball.Direction = new Vector2(Ball.Direction.X * -1, Ball.Direction.Y);
                    if (Ball.BumpSpeedIncreaseFactor * Ball.Speed > Ball.MaximumSpeed)
                    {
                        Ball.Speed = Ball.MaximumSpeed;
                    }
                    else
                    {
                        Ball.Speed *= Ball.BumpSpeedIncreaseFactor;
                    }
                }
            }
            foreach (Wall goal in Goals)
            {
                if (CollisionDetector.Overlaps(Ball, goal))
                {
                    Ball.X     = screenBounds.Width / 2f - GameConstants.DefaultBallSize / 2f;
                    Ball.Y     = screenBounds.Height / 2f - GameConstants.DefaultBallSize / 2f;
                    Ball.Speed = GameConstants.DefaultInitialBallSpeed;
                    HitSound.Play();
                }
            }

            if (CollisionDetector.Overlaps(Ball, PaddleTop) || CollisionDetector.Overlaps(Ball, PaddleBottom))
            {
                Ball.Direction = new Vector2(Ball.Direction.X, Ball.Direction.Y * -1);
                if (Ball.BumpSpeedIncreaseFactor * Ball.Speed > Ball.MaximumSpeed)
                {
                    Ball.Speed = Ball.MaximumSpeed;
                }
                else
                {
                    Ball.Speed *= Ball.BumpSpeedIncreaseFactor;
                }
            }

            base.Update(gameTime);
        }