Exemplo n.º 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)
        {
            var screenBounds = GraphicsDevice.Viewport.Bounds;

            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);
                PaddleBottom.X = MathHelper.Clamp(PaddleBottom.X, screenBounds.Left, screenBounds.Right - PaddleBottom.Width);
            }
            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);
            }
            if (touchState.IsKeyDown(Keys.A))
            {
                PaddleTop.X = PaddleTop.X - (float)(PaddleTop.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
                PaddleTop.X = MathHelper.Clamp(PaddleTop.X, screenBounds.Left, screenBounds.Right - PaddleTop.Width);
            }
            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;

            // Ball - side walls
            if (Walls.Any(w => CollisionDetector.Overlaps(Ball, w)))
            {
                Ball.Direction = Ball.Direction * new Vector2(-1, 1);
                Ball.Speed     = Ball.Speed * Ball.BumpSpeedIncreaseFactor;
            }
            // Ball - winning walls
            if (Goals.Any(w => CollisionDetector.Overlaps(Ball, w)))
            {
                Ball.X     = screenBounds.Center.ToVector2().X;
                Ball.Y     = screenBounds.Center.ToVector2().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 = Ball.Direction * new Vector2(1, -1);
                Ball.Speed    *= Ball.BumpSpeedIncreaseFactor;
            }

            base.Update(gameTime);
        }
Exemplo n.º 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);
            }

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

            if (Walls.Any(w => CollisionDetector.Overlaps(Ball, w)))
            {
                Ball.Direction *= Direction.SW;
                Ball.incrementSpeed();
            }

            if (Goals.Any(w => CollisionDetector.Overlaps(Ball, w)))
            {
                Ball.X     = 250;
                Ball.Y     = 450;
                Ball.Speed = GameConstants.DefaultInitialBallSpeed;
                HitSound.Play();
            }

            if ((CollisionDetector.Overlaps(Ball, PaddleTop) && Ball.Direction.Y < 0) || (CollisionDetector.Overlaps(Ball, PaddleBottom) && Ball.Direction.Y > 0))
            {
                Ball.Direction *= Direction.NE;
                Ball.incrementSpeed();
            }

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

            Ball.X += ballPositionState.X;
            Ball.Y += ballPositionState.Y;


            base.Update(gameTime);
        }
Exemplo n.º 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)
        {
            base.Update(gameTime);

            var TouchState = Keyboard.GetState();

            if (TouchState.IsKeyDown(Keys.Left))
            {
                PaddleBottom.Position.X -= (float)(PaddleBottom.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            if (TouchState.IsKeyDown(Keys.Right))
            {
                PaddleBottom.Position.X += (float)(PaddleBottom.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            if (TouchState.IsKeyDown(Keys.A))
            {
                PaddleTop.Position.X -= (float)(PaddleTop.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            if (TouchState.IsKeyDown(Keys.D))
            {
                PaddleTop.Position.X += (float)(PaddleTop.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
            }

            PaddleBottom.Position.X = MathHelper.Clamp(PaddleBottom.Position.X, graphics.GraphicsDevice.Viewport.Bounds.Left, graphics.GraphicsDevice.Viewport.Bounds.Right - PaddleBottom.Size.Width);
            PaddleTop.Position.X    = MathHelper.Clamp(PaddleTop.Position.X, graphics.GraphicsDevice.Viewport.Bounds.Left, graphics.GraphicsDevice.Viewport.Bounds.Right - PaddleTop.Size.Width);

            // Ball movement
            Ball.Position += Ball.Direction * (float)(gameTime.ElapsedGameTime.TotalMilliseconds * Ball.Speed);

            var bounds = graphics.GraphicsDevice.Viewport.Bounds;

            if (Ball.Position.X < bounds.Left || Ball.Position.X > bounds.Right - Ball.Size.Width)
            {
                Ball.Direction.X = -Ball.Direction.X;
                Ball.Speed       = Ball.Speed * Ball.BumpSpeedIncreaseFactor;
                HitSound.Play();
            }

            if (Ball.Position.Y > bounds.Bottom || Ball.Position.Y < bounds.Top)
            {
                Ball.Position = bounds.Center.ToVector2();
                Ball.Speed    = Ball.InitialSpeed;
            }

            if ((CollisionDetector.Overlaps(Ball, PaddleTop) && Ball.Direction.Y < 0) ||
                CollisionDetector.Overlaps(Ball, PaddleBottom) && Ball.Direction.Y > 0)
            {
                Ball.Direction.Y = -Ball.Direction.Y;
                Ball.Speed      *= Ball.BumpSpeedIncreaseFactor;
            }
        }
Exemplo n.º 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();

            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 -
                                           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     = (graphics.PreferredBackBufferWidth - GameConstants.DefaultBallSize) / 2;
                Ball.Y     = (graphics.PreferredBackBufferHeight - GameConstants.DefaultBallSize) / 2;
                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;
            }

            if (Ball.Speed > GameConstants.MaxBallSpeed)
            {
                Ball.Speed = GameConstants.MaxBallSpeed;
            }

            base.Update(gameTime);
        }
Exemplo n.º 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();
            }

            PaddleBottom.X = MathHelper.Clamp(PaddleBottom.X, _screenBounds.Left,
                                              _screenBounds.Right - PaddleBottom.Width);
            PaddleTop.X = MathHelper.Clamp(PaddleTop.X, _screenBounds.Left,
                                           _screenBounds.Right - PaddleTop.Width);

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

            Ball.Speed = MathHelper.Clamp(Ball.Speed, Ball.MinSpeed, Ball.MaxSpeed);

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

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

            if (CollisionDetector.Overlaps(Ball, Walls.GetElement(0)) ||
                CollisionDetector.Overlaps(Ball, Walls.GetElement(1)))
            {
                Ball.Direction.X = -Ball.Direction.X;
                Ball.Speed      *= Ball.BumpSpeedIncreaseFactor;
            }

            else if (CollisionDetector.Overlaps(Ball, Goals.GetElement(0)) ||
                     CollisionDetector.Overlaps(Ball, Goals.GetElement(1)))
            {
                Ball.X     = _screenBounds.Width / 2f - Ball.Width / 2f;
                Ball.Y     = _screenBounds.Bottom / 2f - Ball.Height / 2f;
                Ball.Speed = GameConstants.DefaultInitialBallSpeed;
                //HitSound.Play();
            }

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

            base.Update(gameTime);
        }
Exemplo n.º 6
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 -= (float)(PaddleTop.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            if (touchState.IsKeyDown(Keys.D))
            {
                PaddleTop.X += (float)(PaddleTop.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            PaddleBottom.X = MathHelper.Clamp(PaddleBottom.X, 0, GameConstants.WindowWidth - PaddleBottom.Width);
            PaddleTop.X    = MathHelper.Clamp(PaddleTop.X, 0, GameConstants.WindowWidth - PaddleTop.Width);

            //Check if the Ball collided with Walls
            foreach (Wall wall in Walls)
            {
                if (CollisionDetector.Overlaps(Ball, wall))
                {
                    Ball.Direction.X = -Ball.Direction.X;
                    Ball.Speed      *= Ball.BumpSpeedIncreaseFactor;
                }
            }

            //Check if the Ball collided with Goals
            foreach (Wall goal in Goals)
            {
                if (CollisionDetector.Overlaps(Ball, goal))
                {
                    Ball.X     = GameConstants.WindowWidth / 2f;
                    Ball.Y     = GameConstants.WindowHeight / 2f;
                    Ball.Speed = GameConstants.DefaultInitialBallSpeed;
                    HitSound.Play();
                }
            }

            //Check if the Ball collided with Paddles
            if (CollisionDetector.Overlaps(Ball, PaddleTop))
            {
                Ball.Direction.Y = -Ball.Direction.Y;
                Ball.Speed      *= Ball.BumpSpeedIncreaseFactor;
            }
            else if (CollisionDetector.Overlaps(Ball, PaddleBottom))
            {
                Ball.Direction.Y = -Ball.Direction.Y;
                Ball.Speed      *= Ball.BumpSpeedIncreaseFactor;
            }

            //Limit the Ball speed
            Ball.Speed = MathHelper.Clamp(Ball.Speed, GameConstants.DefaultInitialBallSpeed, Ball.MaximumBallSpeed);

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

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

            base.Update(gameTime);
        }
Exemplo n.º 7
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, GraphicsDevice.Viewport.Bounds.Width - PaddleBottom.Width);
            PaddleTop.X = MathHelper.Clamp(PaddleTop.X, 0, GraphicsDevice.Viewport.Bounds.Width - 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(wall, Ball))
                {
                    Ball.Direction = new Vector2(-Ball.Direction.X, Ball.Direction.Y);
                    Ball.Speed    *= Ball.BumpSpeedIncreaseFactor;
                }
            }

            foreach (Wall goal in Goals)
            {
                if (CollisionDetector.Overlaps(goal, Ball))
                {
                    Ball.X     = GraphicsDevice.Viewport.Bounds.Width / 2f;
                    Ball.Y     = GraphicsDevice.Viewport.Bounds.Bottom / 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);
                Ball.Speed    *= Ball.BumpSpeedIncreaseFactor;
            }

            if (Ball.Speed > GameConstants.MaxBallSpeed)
            {
                Ball.Speed = GameConstants.MaxBallSpeed;
            }


            base.Update(gameTime);
        }
Exemplo n.º 8
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();

            if (touchState.IsKeyDown(Keys.Left))
            {
                PaddleBottom.X = PaddleBottom.X - (float)(PaddleBottom.Speed *
                                                          gameTime.ElapsedGameTime.TotalMilliseconds);

                PaddleBottom.X = MathHelper.Clamp(PaddleBottom.X, 0, graphics.GraphicsDevice.Viewport.Width -
                                                  PaddleBottom.Width);
            }
            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.GraphicsDevice.Viewport.Width -
                                              PaddleBottom.Width);
            PaddleTop.X = MathHelper.Clamp(PaddleTop.X, 0, graphics.GraphicsDevice.Viewport.Width -
                                           PaddleTop.Width);


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

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


            //checking Collision

            // 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
            if (CollisionDetector.Overlaps(Ball, PaddleTop) || CollisionDetector.Overlaps(Ball, PaddleBottom))
            {
                Ball.Direction = Ball.Direction * (new Vector2(1, -1));
                Ball.Speed    *= Ball.BumpSpeedIncreaseFactor;
                if (Ball.Speed > Ball.MaxSpeed)
                {
                    Ball.Speed = Ball.MaxSpeed;
                }
            }


            // 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
            foreach (Wall w in Walls)
            {
                if (CollisionDetector.Overlaps(Ball, w))
                {
                    Ball.Direction = Ball.Direction * (new Vector2(-1, 1));
                    Ball.Speed    *= Ball.BumpSpeedIncreaseFactor;
                    if (Ball.Speed > Ball.MaxSpeed)
                    {
                        Ball.Speed = Ball.MaxSpeed;
                    }
                    break;
                }
            }


            // If ball has collision with winning walls ( goals )
            // Move ball to the center
            // Reset ball speed
            // Play hit sound with : HitSound . Play ();
            foreach (Wall g in Goals)
            {
                if (CollisionDetector.Overlaps(Ball, g))
                {
                    Ball.X     = graphics.GraphicsDevice.Viewport.Width / 2f;
                    Ball.Y     = graphics.GraphicsDevice.Viewport.Height / 2f;
                    Ball.Speed = GameConstants.DefaultInitialBallSpeed;
                    HitSound.Play();

                    break;
                }
            }



            base.Update(gameTime);
        }
Exemplo n.º 9
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))
            {
                movePaddleLeft(PaddleBottom, gameTime);
            }
            if (touchState.IsKeyDown(Keys.Right))
            {
                movePaddleRight(PaddleBottom, gameTime);
            }

            if (touchState.IsKeyDown(Keys.A))
            {
                movePaddleLeft(PaddleTop, gameTime);
            }
            if (touchState.IsKeyDown(Keys.D))
            {
                movePaddleRight(PaddleTop, gameTime);
            }

            var screenBounds = GraphicsDevice.Viewport.Bounds;
            // D∈[-1 , 1]
            var ballPositionChange = Ball.Direction * MathHelper.Clamp((float)(gameTime.ElapsedGameTime.TotalMilliseconds * Ball.Speed), -5, 5);

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

            // Ball - side walls
            if (Walls.Any(w => CollisionDetector.Overlaps(Ball, w)))
            {
                Vector2 vector;
                vector         = Ball.Direction;
                vector.X       = -vector.X;
                Ball.Direction = vector;
                Ball.Direction = vector;
                if (Ball.Speed * Ball.BumpSpeedIncreaseFactor <= GameConstants.MaxBallSpeed)
                {
                    Ball.Speed = Ball.Speed * Ball.BumpSpeedIncreaseFactor;
                }
            }
            // Ball - winning walls
            if (Goals.Any(w => CollisionDetector.Overlaps(Ball, w)))
            {
                Ball.X     = screenBounds.Center.ToVector2().X;
                Ball.Y     = screenBounds.Center.ToVector2().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))
            {
                Vector2 vector;
                vector   = Ball.Direction;
                vector.Y = -vector.Y;

                Ball.Direction = vector;
                if (Ball.Speed * Ball.BumpSpeedIncreaseFactor <= GameConstants.MaxBallSpeed)
                {
                    Ball.Speed = Ball.Speed * Ball.BumpSpeedIncreaseFactor;
                }
            }

            base.Update(gameTime);
        }
Exemplo n.º 10
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 bounds = GraphicsDevice.Viewport.Bounds;

            PaddleBottom.X = MathHelper.Clamp(PaddleBottom.X, bounds.Left, bounds.Right - PaddleBottom.Width);
            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;

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

            foreach (Wall wall in Walls)
            {
                if (CollisionDetector.Overlaps(Ball, wall))
                {
                    switch (Ball.Direction.Course)
                    {
                    case Ball.MyVector.Direction.SouthEast:
                    {
                        Ball.Direction = new Ball.MyVector(Ball.MyVector.Direction.SouthWest);
                        break;
                    }

                    case Ball.MyVector.Direction.SouthWest:
                    {
                        Ball.Direction = new Ball.MyVector(Ball.MyVector.Direction.SouthEast);
                        break;
                    }

                    case Ball.MyVector.Direction.NorthEast:
                    {
                        Ball.Direction = new Ball.MyVector(Ball.MyVector.Direction.NorthWest);
                        break;
                    }

                    case Ball.MyVector.Direction.NorthWest:
                    {
                        Ball.Direction = new Ball.MyVector(Ball.MyVector.Direction.NorthEast);
                        break;
                    }
                    }
                    if (Ball.Speed < GameConstants.DefaultBallMaxSpeed)
                    {
                        Ball.Speed *= GameConstants.DefaultIBallBumpSpeedIncreaseFactor;
                    }
                }
            }

            if ((Ball.Direction.Course.Equals(Ball.MyVector.Direction.SouthEast) ||
                 Ball.Direction.Course.Equals(Ball.MyVector.Direction.SouthWest)) &&
                CollisionDetector.Overlaps(Ball, PaddleBottom))
            {
                if (Ball.Direction.Course.Equals(Ball.MyVector.Direction.SouthEast))
                {
                    Ball.Direction = new Ball.MyVector(Ball.MyVector.Direction.NorthEast);
                }
                else
                {
                    Ball.Direction = new Ball.MyVector(Ball.MyVector.Direction.NorthWest);
                }
                if (Ball.Speed < GameConstants.DefaultBallMaxSpeed)
                {
                    Ball.Speed *= GameConstants.DefaultIBallBumpSpeedIncreaseFactor;
                }
            }
            else if ((Ball.Direction.Course.Equals(Ball.MyVector.Direction.NorthEast) ||
                      Ball.Direction.Course.Equals(Ball.MyVector.Direction.NorthWest)) &&
                     CollisionDetector.Overlaps(Ball, PaddleTop))
            {
                if (Ball.Direction.Course.Equals(Ball.MyVector.Direction.NorthEast))
                {
                    Ball.Direction = new Ball.MyVector(Ball.MyVector.Direction.SouthEast);
                }
                else
                {
                    Ball.Direction = new Ball.MyVector(Ball.MyVector.Direction.SouthWest);
                }
                if (Ball.Speed < GameConstants.DefaultBallMaxSpeed)
                {
                    Ball.Speed *= GameConstants.DefaultIBallBumpSpeedIncreaseFactor;
                }
            }

            foreach (Wall goal in Goals)
            {
                if (CollisionDetector.Overlaps(Ball, goal))
                {
                    if (Goals.IndexOf(goal) == 0)
                    {
                        scoreTop++;
                    }
                    else
                    {
                        scoreBottom++;
                    }
                    Ball.X     = bounds.Width / 2f;
                    Ball.Y     = bounds.Height / 2f;
                    Ball.Speed = GameConstants.DefaultInitialBallSpeed;
                    HitSound.Play();
                }
            }
            base.Update(gameTime);
        }
Exemplo n.º 11
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)
        {
            var screenBounds = GraphicsDevice.Viewport.Bounds;

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
                Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

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

            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);
            }
            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);
            }
            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 * (float)(gameTime.ElapsedGameTime.TotalMilliseconds * Ball.Speed);

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

            foreach (Wall w in Walls)
            {
                if (CollisionDetector.Overlaps(w, Ball))
                {
                    Ball.Direction *= new Vector2(-1, 1);
                    Ball.Speed     *= Ball.BumpSpeedIncreaseFactor;
                    Ball.Speed      = Math.Min(Ball.Speed, Ball.MaxSpeed);
                }
            }

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

            if (CollisionDetector.Overlaps(PaddleBottom, Ball) && Math.Abs(Ball.Direction.Y - 1) < 0.001)
            {
                Ball.Direction *= new Vector2(1, -1);
                Ball.Speed     *= Ball.BumpSpeedIncreaseFactor;
                Ball.Speed      = Math.Min(Ball.Speed, Ball.MaxSpeed);
            }

            if (CollisionDetector.Overlaps(PaddleTop, Ball) && Math.Abs(Ball.Direction.Y + 1) < 0.001)
            {
                Ball.Direction *= new Vector2(1, -1);
                Ball.Speed     *= Ball.BumpSpeedIncreaseFactor;
                Ball.Speed      = Math.Min(Ball.Speed, Ball.MaxSpeed);
            }

            base.Update(gameTime);
        }
Exemplo n.º 12
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)
        {
            var touchState = Keyboard.GetState();

            var bounds = GraphicsDevice.Viewport.Bounds;

            PaddleBottom.X = MathHelper.Clamp(PaddleBottom.X, bounds.Left, bounds.Right - PaddleBottom.Width);
            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;

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

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            if (CollisionDetector.Overlaps(Ball, Walls[0]) || CollisionDetector.Overlaps(Ball, Walls[1]))
            {
                if (Ball.Direction.X > 0)
                {
                    Ball.Direction = new Vector2(-1, Ball.Direction.Y);
                }
                else
                {
                    Ball.Direction = new Vector2(1, Ball.Direction.Y);
                }
                IncreaseBallSpeed();
            }

            if (CollisionDetector.Overlaps(Ball, Goals[0]) || CollisionDetector.Overlaps(Ball, Goals[1]))
            {
                Ball.Speed = GameConstants.DefaultInitialBallSpeed;
                Ball.X     = bounds.Center.X;
                Ball.Y     = bounds.Center.Y;
                HitSound.Play();
            }

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

            base.Update(gameTime);
        }
Exemplo n.º 13
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();

            // 1st player
            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);
            }
            // 2nd player
            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);
            }

            // check if coordinates fit right into the game screen
            PaddleBottom.X = MathHelper.Clamp(PaddleBottom.X, 0, 500 -
                                              PaddleBottom.Width);

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

            // ball movement
            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);
                SetNewBallSpeed();
            }
            // Ball - winning walls
            if (Goals.Any(w => CollisionDetector.Overlaps(Ball, w)))
            {
                Ball.X     = 230;
                Ball.Y     = 430;
                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);
                SetNewBallSpeed();
            }
            base.Update(gameTime);
        }