public void CheckCollision(Ball ball) { if (alive && ball.Bounds.Intersects(location)) { alive = false; ball.Deflection(this); } }
/// <summary> /// ���������˵���ʱ����ã�����Ϸ������������Ч��. /// </summary> /// <param name="type">��������</param> private void ApplyPowerUp(string type) { if (type == "����")//�Ѱ����̣����Dz�����̫�� { small.Play(); if (newPaddle .Size .Width == 66) { paddle1.Width = 46; newPaddle.Width = 46; newPaddle.Image = pad[0]; } else if (newPaddle.Size.Width == 86) { paddle1.Width = 66; newPaddle.Width = 66; newPaddle.Image = pad[1]; } } if (type == "�쳤") //�쳤 { avatar.Play(); if (newPaddle.Size.Width == 66) { paddle1.Width = 86; newPaddle.Width = 86; newPaddle.Image = pad[2]; } else if (newPaddle.Size.Width == 46) { paddle1.Width = 66; newPaddle.Width = 66; newPaddle.Image = pad[1]; } } if (type == "������") //������� { Ball ball = ballList[0]; double angle = Math.Atan2(-ball.YSpeed, ball.XSpeed); double speed = Math.Sqrt(ball.XSpeed * ball.XSpeed + ball.YSpeed * ball.YSpeed); double angle1 = angle + 2 * Math.PI / 3; //��ʼ�Ƕȼ�120�� double angle2 = angle - 2 * Math.PI / 3; //��ʼ�Ƕȼ�120�� //���ٶȴӼ�����ת����ֱ������ϵ float dX1 = (float)(Math.Cos(angle1) * speed); float dY1 = (float)(-Math.Sin(angle1) * speed); float dX2 = (float)(Math.Cos(angle2) * speed); float dY2 = (float)(-Math.Sin(angle2) * speed); //�����һ�����λ�������������ӵ�в�ͬ���ٶ� //����Ϸ�л���һ������������Ч���ῴ�����ܻ������۹����� ballList.Add(new Ball(ball.Brush, ball.Radius, ball.X, ball.Y, dX1, dY1)); ballList.Add(new Ball(ball.Brush, ball.Radius, ball.X, ball.Y, dX2, dY2)); } if (type == "�ӵ�")//�ӵ� { Ball ballLeft = new Ball(Brushes.Red, 3, paddle1.X, paddle1.Y + 5, true, true); Ball ballRight = new Ball(Brushes.Red, 3, paddle1.X + paddle1.Width, paddle1.Y + 5, true, true); bulletList.Add(ballLeft); bulletList.Add(ballRight); bullet_comeSound.Play(); } }
/// <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> /// �����Ͱ��Ƿ�����ײ����������͵���Collision()���� /// </summary> /// <param name="ball">Ҫ������</param> /// <param name="paddle">Ҫ���İ�</param> private void DetectPaddleCollision(Ball ball, Paddle paddle) { if ((ball.Right + ball.XSpeed > paddle.Left) && (ball.Left + ball.XSpeed < paddle.Right) && (ball.Bottom + ball.YSpeed > paddle.Top) && (ball.Top + ball.YSpeed < paddle.Bottom) && ball.YSpeed >= 0) //ͬ�ϱ��жϣ�������ײ��� { ball.Collide(paddle); paddleSound.Play(); } }
/// <summary> /// �����������ש���Ƿ�����ײ�������ж���ķ����Ƿ���Ҫ��ת /// </summary> /// <param name="ball">��Ҫ������</param> /// <param name="brick">��Ҫ����ש��</param> /// <param name="hit">true���������ײ</param> /// <param name="reverseX">��ֵΪtrueʱ��X�����ٶ���Ҫ����ת</param> /// <param name="reverseY">��ֵΪtrueʱ��Y�����ٶ���Ҫ����ת</param> private void DetectBrickCollision(Ball ball, Brick brick, out bool hit, out bool reverseX, out bool reverseY) { reverseX = false; //ÿ��ש��ֻ�ܷ�����һ�� reverseY = false; hit = false; //����ٶȲ��ܳ�����ֱ���������������������������ֱ�Ӵ�Խש�� //�����ƶ�ʱ����ײ�ж� if ((ball.YSpeed < 0) //�ж����Ƿ��������ƶ� && (ball.Top + ball.YSpeed < brick.Bottom) //�����ƶ�ʱ������ϱ߽�����ٶ����С��ש���±߽������±߽�����ٶȴ���ש���±߽磬��Y��������ײ && (ball.Bottom + ball.YSpeed > brick.Bottom) && (ball.Right + ball.XSpeed >= brick.Left) //�� && (ball.Left + ball.XSpeed <= brick.Right)) //�� { hit = true; reverseY = true; } //�����ƶ�ʱ����ײ�ж� if ((ball.YSpeed > 0) //�ж����Ƿ��������ƶ� && (ball.Bottom + ball.YSpeed > brick.Top) //ͬ�ϣ�������ײ�ж� && (ball.Top + ball.YSpeed < brick.Top) && (ball.Right + ball.XSpeed >= brick.Left) && (ball.Left + ball.XSpeed <= brick.Right)) { hit = true; reverseY = true; } //�����ƶ�ʱ����ײ�ж� if ((ball.XSpeed > 0) //�ж����Ƿ��������ƶ� && (ball.Left + ball.XSpeed < brick.Left) //ͬ�ϣ�������ײ�ж� && (ball.Right + ball.XSpeed > brick.Left) && (ball.Bottom + ball.YSpeed >= brick.Top) && (ball.Top + ball.YSpeed <= brick.Bottom)) { hit = true; reverseX = true; } //�����ƶ�ʱ����ײ�ж� if ((ball.XSpeed < 0) //�ж����Ƿ��������ƶ� && (ball.Right + ball.XSpeed > brick.Right) //ͬ�ϣ�������ײ�ж� && (ball.Left + ball.XSpeed < brick.Right) && (ball.Bottom + ball.YSpeed >= brick.Top) && (ball.Top + ball.YSpeed <= brick.Bottom)) { hit = true; reverseX = true; } if (hit && ball.FromPaddle) bulletList.RemoveAt(bulletList.IndexOf(ball)); }
/// <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(); }
//����BOSS����ײ private void CheckBoss(Ball ball, Label boss, out bool hit, out bool reverseX, out bool reverseY) { reverseX = false; reverseY = false; hit = false; if ((ball.YSpeed < 0) && (ball.Top + ball.YSpeed < boss.Bottom ) && (ball.Bottom + ball.YSpeed > boss .Bottom) && (ball.Right + ball.XSpeed >= boss.Left) && (ball.Left + ball.XSpeed <= boss.Right)) { hit = true; reverseY = true; } if ((ball.YSpeed > 0) && (ball.Bottom + ball.YSpeed > boss.Top) && (ball.Top + ball.YSpeed < boss.Top) && (ball.Right + ball.XSpeed >= boss.Left) && (ball.Left + ball.XSpeed <= boss.Right)) { hit = true; reverseY = true; } if ((ball.XSpeed > 0) && (ball.Left + ball.XSpeed < boss.Left) && (ball.Right + ball.XSpeed > boss.Left) && (ball.Bottom + ball.YSpeed >= boss.Top) && (ball.Top + ball.YSpeed <= boss.Bottom)) { hit = true; reverseX = true; } if ((ball.XSpeed < 0) && (ball.Right + ball.XSpeed > boss.Right) && (ball.Left + ball.XSpeed < boss.Right) && (ball.Bottom + ball.YSpeed >= boss.Top) && (ball.Top + ball.YSpeed <= boss.Bottom)) { hit = true; reverseX = true; } }
/// <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> /// Checks if a ball is about to hit the paddle and calls the Collide() method of the ball if it is /// </summary> /// <param name="ball">Ball object to check for collision</param> /// <param name="paddle">Paddle object to check for collision</param> private void DetectPaddleCollision(Ball ball, Paddle paddle) { if ((ball.Right + ball.XSpeed > paddle.Left) && (ball.Left + ball.XSpeed < paddle.Right) && (ball.Bottom + ball.YSpeed > paddle.Top) && (ball.Top + ball.YSpeed < paddle.Bottom) && ball.YSpeed >= 0) //is the ball about to hit the paddle { ball.Collide(paddle); paddleSound.Play(); } }
/// <summary> /// Given a ball and brick, checks if they collide, and whether the direction of the ball should be reversed /// </summary> /// <param name="ball">Ball object to check for collision</param> /// <param name="brick">Brick object to check for collision</param> /// <param name="hit">True if the ball collides with the brick</param> /// <param name="reverseX">True if the ball's x-direction should be reversed</param> /// <param name="reverseY">True if the ball's y-direction should be reversed</param> private void DetectBrickCollision(Ball ball, Brick brick, out bool hit, out bool reverseX, out bool reverseY) { reverseX = false; //each brick can only reverse the ball once reverseY = false; hit = false; //note the velocity of the ball cannot exceed its diameter - or this will not work //the ball could sail right through the brick //upwards collision if ((ball.YSpeed < 0) //is the ball travelling upwards? && (ball.Top + ball.YSpeed < brick.Bottom) //is the top of the ball inside the brick? && (ball.Bottom + ball.YSpeed > brick.Bottom) //is the bottom of the ball under the brick? && (ball.Right + ball.XSpeed >= brick.Left) //is the right of the ball inside the brick && (ball.Left + ball.XSpeed <= brick.Right)) //is the left of the ball inside the brick { hit = true; reverseY = true; } //downwards collision if ((ball.YSpeed > 0) //is the ball travelling downwards? && (ball.Bottom + ball.YSpeed > brick.Top) //is the bottom of the ball inside the brick (y) && (ball.Top + ball.YSpeed < brick.Top) // is the top of the ball about the brick? (y) && (ball.Right + ball.XSpeed >= brick.Left) && (ball.Left + ball.XSpeed <= brick.Right))// do the x-coords match? { hit = true; reverseY = true; } //rightwards collision if ((ball.XSpeed > 0) //is the ball moving right && (ball.Left + ball.XSpeed < brick.Left) //is the left side of the ball to the left of the brick? && (ball.Right + ball.XSpeed > brick.Left) //is the right side of the ball inside the brick? && (ball.Bottom + ball.YSpeed >= brick.Top) && (ball.Top + ball.YSpeed <= brick.Bottom)) // do the y-coords match? { hit = true; reverseX = true; } //leftwards collision if ((ball.XSpeed < 0) //is the ball moving Left? && (ball.Right + ball.XSpeed > brick.Right) //is the right side of the ball to the right of the brick? && (ball.Left + ball.XSpeed < brick.Right) //is the left side of the ball inside the brick && (ball.Bottom + ball.YSpeed >= brick.Top) && (ball.Top + ball.YSpeed <= brick.Bottom))// do the y-coords match? { hit = true; reverseX = true; } }
/// <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(); }
private void CollideWithBoss(Ball ball) { }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); Texture2D tempTexture = Content.Load<Texture2D>("paddle"); paddle = new Paddle(tempTexture, screenRectangle); tempTexture = Content.Load<Texture2D>("ball"); ball = new Ball(tempTexture, screenRectangle); brickImage = Content.Load<Texture2D>("brick"); StartGame(); }