/// <summary> /// ��齫Ҫ�����߽�����б�Ҫ��ʱ��ת��ķ��� /// </summary> /// <param name="ball">�ж��Ƿ���ײ���������</param> private void DetectWallCollision(Ball ball) { bool hit = false; if ((ball.Right + ball.XSpeed >this.Width-12) //�ұ߽� || (ball.Left + ball.XSpeed < borderWidth)) //��߽� { hit = true; ball.ReverseX(); } if (ball.Top + ball.YSpeed < borderWidth)//�ϱ߽� { hit = true; ball.ReverseY(); if (ball.FromPaddle) ball.ReverseY();//����ɳ�ȥ } if (hit && !ball.FromPaddle) { wallSound.Play(); } }
/// <summary> /// Checks if the ball is about to hit the side or top walls, and reverses it's direction if necessary /// </summary> /// <param name="ball">The ball to check for collision</param> private void DetectWallCollision(Ball ball) { bool hit = false; if ((ball.Right + ball.XSpeed > this.Width - borderWidth) //right border || (ball.Left + ball.XSpeed < borderWidth)) //left border { hit = true; ball.ReverseX(); } //is the ball about to hit the top border? if (ball.Top + ball.YSpeed < borderWidth) { hit = true; ball.ReverseY(); } if (hit) { wallSound.Play(); } }
/// <summary> /// �����������ש�����ײ�Ƿ���������������ж����ש������û��������� /// </summary> /// <param name="ball">Ҫ������</param> /// <param name="bricks">Ҫ�����Ƿ��е��ߵ�ש��</param> private void CheckBricks(Ball ball, Brick[,] bricks) { bool ultimatelyReverseX = false; //����������ȷ����ֻ����һ��ʱ���ڱ���תһ�� bool ultimatelyReverseY = false; //���������ֱ���ת����ֱ�Ӵ���ש������ foreach (Brick brick in bricks) { bool hit, reverseX, reverseY; if (brick.Strength > 0) //ש��û������ʱ����Ƿ�����ײ { //ִ����ײ��� DetectBrickCollision(ball, brick, out hit, out reverseX, out reverseY); if (hit) { brick.Hit(); //��ש���һ��Ѫ if (brick.Strength == 0)//ש��Ѫ��Ϊ0��ʱ��Ӧ����ʧ { CheckBrickForPowerUps(brick); //���ש�����Ƿ���������� brickCount--; //��ש������1 } score += level * brick.CalculateScore(); brickSound.Play(); if (reverseX) ultimatelyReverseX = true; if (reverseY) ultimatelyReverseY = true; } } } if (ultimatelyReverseY) ball.ReverseY(); if (ultimatelyReverseX) ball.ReverseX(); }
/// <summary> /// Given a ball and brick, checks them for collision /// if they collide, checks if the brick had powerups inside it /// </summary> /// <param name="ball">Ball object to check for collision</param> /// <param name="bricks">Brick object to check for powerups</param> private void CheckBricks(Ball ball, Brick[,] bricks) { bool ultimatelyReverseX = false; //these variables exist so that the ball can only be reversed bool ultimatelyReverseY = false; //once per timer tick, otherwise sometimes the ball can be reversed twice //and plough right through bricks foreach (Brick brick in bricks) { bool hit, reverseX, reverseY; if (brick.Strength > 0) //only detect collision if the brick hasn't been hit { //perform collision detection DetectBrickCollision(ball, brick, out hit, out reverseX, out reverseY); if (hit) { brick.Hit(); //subtract the strength if (brick.Strength == 0) { CheckBrickForPowerUps(brick); //check if the brick had any powerups brickCount--; //update the new brickcount } score += level * brick.CalculateScore(); brickSound.Play(); if (reverseX) ultimatelyReverseX = true; if (reverseY) ultimatelyReverseY = true; } } } if (ultimatelyReverseY) ball.ReverseY(); if (ultimatelyReverseX) ball.ReverseX(); }