Example #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();
            }

            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);
            }

            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);
            }

            PaddleBottom.X = MathHelper.Clamp(PaddleBottom.X, 0, graphics.PreferredBackBufferWidth - PaddleBottom.Width);

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

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

            // Ball - side walls
            if (Walls.Any(w => CollisionDetector.Overlaps(Ball, w)))
            {
                Ball.Direction = new Vector2(-Ball.Direction.X, Ball.Direction.Y);
                Ball.Speed     = Ball.Speed * Ball.BumpSpeedIncreaseFactor;
            }
            // Ball - winning walls
            if (Goals.Any(w => CollisionDetector.Overlaps(Ball, w)))
            {
                Ball.X     = GraphicsDevice.Viewport.Bounds.Center.X;
                Ball.Y     = GraphicsDevice.Viewport.Bounds.Center.Y;
                Ball.Speed = GameConstants.DefaultInitialBallSpeed;
                HitSound.Play();
            }
            // Paddle - ball collision
            if (CollisionDetector.Overlaps(Ball, PaddleTop) && Ball.Direction.Y < 0 ||
                (CollisionDetector.Overlaps(Ball, PaddleBottom) && Ball.Direction.Y > 0))
            {
                Ball.Direction = new Vector2(Ball.Direction.X, -Ball.Direction.Y);
                Ball.Speed    *= Ball.BumpSpeedIncreaseFactor;
            }

            base.Update(gameTime);
        }
Example #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();
            }

            // TODO: Add your update logic here
            var touchState = Keyboard.GetState();
            var bounds     = GraphicsDevice.Viewport.Bounds;

            if (touchState.IsKeyDown(Keys.Left))
            {
                PaddleBottom.X = PaddleBottom.X - (float)(PaddleBottom.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
                PaddleBottom.X = MathHelper.Clamp(PaddleBottom.X, bounds.Left, bounds.Right - PaddleBottom.Width);
            }
            if (touchState.IsKeyDown(Keys.Right))
            {
                PaddleBottom.X = 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 = PaddleTop.X - (float)(PaddleTop.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
                PaddleTop.X = MathHelper.Clamp(PaddleTop.X, bounds.Left, bounds.Right - PaddleTop.Width);
            }
            if (touchState.IsKeyDown(Keys.D))
            {
                PaddleTop.X = PaddleTop.X + (float)(PaddleTop.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
                PaddleTop.X = MathHelper.Clamp(PaddleTop.X, bounds.Left, bounds.Right - PaddleTop.Width);
            }
            var ballPositionChange = ball.Direction * (float)(gameTime.ElapsedGameTime.TotalMilliseconds * ball.Speed);

            ball.X += ballPositionChange.X;
            ball.Y += ballPositionChange.Y;

            // Ball - side walls
            if (Walls.Any(w => CollisionDetector.Overlaps(ball, w)))
            {
                ball.BounceX();
                ball.Acceleration();
            }
            // Ball - winning walls
            if (Goals.Any(w => CollisionDetector.Overlaps(ball, w)))
            {
                ball.X     = 225;
                ball.Y     = 460;
                ball.Speed = GameConstants.DefaultInitialBallSpeed;
                HitSound.Play();
            }
            // Paddle - ball collision
            if (CollisionDetector.Overlaps(ball, PaddleTop) && ball.Direction.Y < 0 ||
                (CollisionDetector.Overlaps(ball, PaddleBottom) && ball.Direction.Y > 0))
            {
                ball.BounceY();
                ball.Acceleration();
            }

            base.Update(gameTime);
        }
Example #3
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);
            }
            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);
            }

            PaddleBottom.X = MathHelper.Clamp(PaddleBottom.X, 0, 500 - PaddleBottom.Width);
            PaddleTop.X    = MathHelper.Clamp(PaddleTop.X, 0, 500 - PaddleTop.Width);

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

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

            // return true if overlaps , false otherwise ...
            // Ball - side walls
            // If ball has collision with any of the side wall
            // Reverse X direction of the ball
            // Increase the ball speed by bump speed increase factor
            if (CollisionDetector.Overlaps(Ball, Walls[0]) || CollisionDetector.Overlaps(Ball, Walls[1]))
            {
                Ball.Direction = -Ball.Direction * new Vector2(-1, 1);
                Ball.Speed    *= Ball.BumpSpeedIncreaseFactor;
            }
            // If ball has collision with winning walls ( goals )
            // Move ball to the center
            // Reset ball speed
            // Play hit sound with : HitSound . Play ();
            else if (CollisionDetector.Overlaps(Ball, Goals[0]) || CollisionDetector.Overlaps(Ball, Goals[1]))
            {
                Ball.X     = 250;
                Ball.Y     = 450;
                Ball.Speed = GameConstants.DefaultInitialBallSpeed;
                HitSound.Play();
            }
            // If ball has collision with paddles ( with appropriate movement direction !!)
            // Reverse Y direction of the ball
            // Increase the ball speed by bump speed increase factor
            else if (CollisionDetector.Overlaps(Ball, PaddleBottom) || CollisionDetector.Overlaps(Ball, PaddleTop))
            {
                Ball.Direction = Ball.Direction * new Vector2(1, -1);
                Ball.Speed    *= Ball.BumpSpeedIncreaseFactor;
            }

            base.Update(gameTime);
        }
Example #4
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();
            var screenBounds = GraphicsDevice.Viewport.Bounds;

            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);
            }
            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);
            }

            PaddleBottom.X = MathHelper.Clamp(PaddleBottom.X, 0, screenBounds.Width -
                                              PaddleBottom.Width);
            PaddleTop.X = MathHelper.Clamp(PaddleTop.X, 0, screenBounds.Width -
                                           PaddleBottom.Width);

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

            Ball.X += ballPositionChange.X;

            Ball.Y += ballPositionChange.Y;

            foreach (var wall in Walls)
            {
                if (CollisionDetector.Overlaps(Ball, wall))
                {
                    Ball.Direction = Ball.Direction * new Vector2(-1, 1);
                    Ball.Speed     = Ball.Speed * GameConstants.DefaultBallBumpSpeedIncreaseFactor;
                    continue;
                }
            }

            foreach (var goal in Goals)
            {
                if (CollisionDetector.Overlaps(Ball, goal))
                {
                    HitSound.Play();
                    Ball.X     = screenBounds.Width / 2f - GameConstants.DefaultBallSize / 2f;
                    Ball.Y     = screenBounds.Height / 2f - GameConstants.DefaultBallSize / 2f;
                    Ball.Speed = GameConstants.DefaultInitialBallSpeed;
                }
            }

            if (CollisionDetector.Overlaps(Ball, PaddleBottom) || CollisionDetector.Overlaps(Ball, PaddleTop))
            {
                Ball.Direction = Ball.Direction * new Vector2(1, -1);
                Ball.Speed     = Ball.Speed * GameConstants.DefaultBallBumpSpeedIncreaseFactor;
            }



            base.Update(gameTime);
        }
Example #5
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);
            }
            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);
            }
            PaddleBottom.X = MathHelper.Clamp(PaddleBottom.X, GraphicsDevice.Viewport.Bounds.Left, GraphicsDevice.Viewport.Bounds.Right -
                                              PaddleBottom.Width);
            PaddleTop.X = MathHelper.Clamp(PaddleTop.X, GraphicsDevice.Viewport.Bounds.Left, GraphicsDevice.Viewport.Bounds.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, PaddleTop))
                {
                    switch (Ball.Direction.BallDirection)
                    {
                    case GameVector.Direction.UpLeft:
                        Ball.Direction = new GameVector(GameVector.Direction.DownLeft);
                        break;

                    case GameVector.Direction.UpRight:
                        Ball.Direction = new GameVector(GameVector.Direction.DownRight);
                        break;
                    }
                    Ball.Speed *= Ball.BumpSpeedIncreaseFactor;
                }
                if (CollisionDetector.Overlaps(Ball, PaddleBottom))
                {
                    switch (Ball.Direction.BallDirection)
                    {
                    case GameVector.Direction.DownLeft:
                        Ball.Direction = new GameVector(GameVector.Direction.UpLeft);
                        break;

                    case GameVector.Direction.DownRight:
                        Ball.Direction = new GameVector(GameVector.Direction.UpRight);
                        break;
                    }
                    Ball.Speed *= Ball.BumpSpeedIncreaseFactor;
                }
                if (CollisionDetector.Overlaps(wall, Ball))
                {
                    switch (Ball.Direction.BallDirection)
                    {
                    case GameVector.Direction.DownLeft:
                        Ball.Direction = new GameVector(GameVector.Direction.DownRight);
                        break;

                    case GameVector.Direction.UpLeft:
                        Ball.Direction = new GameVector(GameVector.Direction.UpRight);
                        break;

                    case GameVector.Direction.DownRight:
                        Ball.Direction = new GameVector(GameVector.Direction.DownLeft);
                        break;

                    case GameVector.Direction.UpRight:
                        Ball.Direction = new GameVector(GameVector.Direction.UpLeft);
                        break;
                    }
                    Ball.Speed *= Ball.BumpSpeedIncreaseFactor;
                }
            }
            foreach (Wall wall in Goals)
            {
                if (CollisionDetector.Overlaps(wall, Ball))
                {
                    Ball.X     = GraphicsDevice.Viewport.Bounds.Width / 2f - GameConstants.DefaultBallSize / 2f;
                    Ball.Y     = GraphicsDevice.Viewport.Bounds.Height / 2f - GameConstants.DefaultBallSize / 2;
                    Ball.Speed = GameConstants.DefaultInitialBallSpeed;
                    HitSound.Play();
                }
            }

            base.Update(gameTime);
        }