Exemple #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));
            }
        }
Exemple #2
0
        public void Update()
        {
            if (started && !paused)
            {
                bool collided = false;
                foreach (INonReactiveCollideable collideable in nonReactiveCollideables)
                {
                    if (ball.GetBoundingBox().Intersects(collideable.GetBoundingBox()))
                    {
                        ball.CollideWith(collideable);

                        if (collideable is Wall wall)
                        {
                            if (wall.side == Side.BOTTOM || wall.side == Side.TOP)
                            {
                                soundManager.PlayPointWonSound(wall.side == Side.TOP);
                                scoreDisplay.UpdateScore(wall.side == Side.TOP);
                                Reset(false);
                                Start();
                                return;
                            }
                            else
                            {
                                soundManager.PlayBallWallSound();
                            }
                        }
                        else if (collideable is Paddle)
                        {
                            soundManager.PlayBallPaddleSound();
                        }

                        collided = true;
                    }

                    if (collideable == computerPaddle)
                    {
                        computerPaddle.ReceiveIntendedPosition(ball.GetBoundingBox().X + ball.GetBoundingBox().Width / 2);
                    }
                    else if (collideable == playerPaddle)
                    {
                        playerPaddle.ReceiveIntendedPosition(lastMouseXPos);
                    }

                    collideable.Update();
                }

                if (collided && ballCollidedLastFrame)
                {
                    ball.Reset();
                }

                ballCollidedLastFrame = collided;

                ball.Update();
                scoreDisplay.Update();
            }
            else if (starting)
            {
                startTimer--;
                if (startTimer == 60 || startTimer == 120 || startTimer == 0)
                {
                    soundManager.PlayCountdownSound();
                }
                if (startTimer == 0)
                {
                    started  = true;
                    starting = false;
                }
            }
        }