Exemplo n.º 1
0
        private void checkBallCollisions()
        {
            SizeF      windowSize   = renderTarget.Size;
            float      windowWidth  = windowSize.Width;
            float      windowHeight = windowSize.Height;
            PointF     ballPos      = ball.GetPosition();
            PointF     ballVel      = ball.GetVelocity();
            RectangleF ballBounds   = ball.GetBoundingBox();

            // check collision with left and right screen bounds
            if (ballBounds.Right < 0 && ballVel.X < 0)
            {
                rightScore.IncScore();
                ball.SetPosition(new PointF(windowHeight / 2, windowWidth / 2));
            }
            else if (ballBounds.Left > windowWidth && ballVel.X > 0)
            {
                leftScore.IncScore();
                ball.SetPosition(new PointF(windowHeight / 2, windowWidth / 2));
            }

            // check collision with top and bottom screen bounds
            if ((ballBounds.Top < 0 && ballVel.Y < 0) || (ballBounds.Bottom > windowHeight && ballVel.Y > 0))
            {
                soundMgr.PlayWallHit();
                ball.SetVelocity(new PointF(ballVel.X, ballVel.Y * -1));
            }


            RectangleF leftPaddleBounds  = leftPaddle.GetBoundingBox();
            RectangleF rightPaddleBounds = rightPaddle.GetBoundingBox();

            if ((ballBounds.IntersectsWith(leftPaddleBounds) && ball.GetVelocity().X < 0) || (ballBounds.IntersectsWith(rightPaddleBounds) && ball.GetVelocity().X > 0))
            {
                soundMgr.PlayPaddleHit();
                ball.SetVelocity(new PointF(ballVel.X * -1, ballVel.Y));
            }
        }