Ejemplo n.º 1
0
        internal bool CollidesWith(Ball ball)
        {
            Rectangle ballRect = new Rectangle((int)ball.X, (int)ball.Y, Ball.Width, Ball.Height);
            Rectangle myRect = new Rectangle((int)this.X, (int)this.Y, Paddle.Width, Paddle.Height);

            return ballRect.Intersects(myRect);
        }
Ejemplo n.º 2
0
        internal void HandleBallCollision(Ball ball)
        {
            float distX, distY;

            if (ball.Velocity.X > 0)
            {
                if (ball.Velocity.Y > 0)
                {
                    // Collision from top left
                    distX = ball.X + Ball.Width - this.X;
                    distY = ball.Y + Ball.Height - this.Y;
                }
                else
                {
                    // Collision from bottom left
                    distX = ball.X + Ball.Width - this.X;
                    distY = this.Y + Brick.Height - ball.Y;
                }
            }
            else
            {
                if (ball.Velocity.Y > 0)
                {
                    // Collision from top right
                    distX = this.X + Brick.Width - ball.X;
                    distY = ball.Y + Ball.Height - this.Y;
                }
                else
                {
                    // Collision from bottom right
                    distX = this.X + Brick.Width - ball.X;
                    distY = this.Y + Brick.Height - ball.Y;
                }
            }

            if (distX > distY)
                ball.Velocity = new Vector2(ball.Velocity.X, -ball.Velocity.Y);
            else
                ball.Velocity = new Vector2(-ball.Velocity.X, ball.Velocity.Y);
        }
Ejemplo n.º 3
0
 internal void HandleBallCollision(Ball ball)
 {
     ball.Velocity = new Vector2(ball.Velocity.X, -ball.Velocity.Y);
 }
Ejemplo n.º 4
0
        private void CheckBallCollisions(Ball ball, GameTime gameTime)
        {
            if (this.Paddle.CollidesWith(ball))
                this.Paddle.HandleBallCollision(ball);

            for (int i = 0; i < this.Bricks.Count; i++)
            {
                Brick brick = this.Bricks[i];

                if (brick.CollidesWith(ball))
                {
                    // Rebound and remove brick
                    brick.HandleBallCollision(ball);
                    if (this.EventAggregator != null)
                        this.EventAggregator.Publish(new BrickDestroyedEvent(brick));
                    this.Bricks.Remove(brick);
                    break;
                }
            }
        }