/// <summary> /// a check if the bat has hit the ball or missed it /// </summary> /// <param name="ball">the ball</param> /// <param name="canvas">the canvas</param> /// <param name="bat">'t' if its the top bat. 'b' if its the bottom bat</param> /// <returns>1: the bat hit the ball. /// -1: the bat missed the ball. /// 0: the ball wasn't near the bat</returns> public int IsBallHit(Ball ball, Canvas canvas) { if (batType == BatType.Top) { if (ball.Position.Y - Ball.Radius * 2 > canvas.Height) { return(-1); } } else { if (ball.Position.Y + Ball.Radius * 2 < 0) { return(-1); } } if (HasHitBall(ball, Size)) { if (batType == BatType.Bottom) { ball.Position.Y = this.Position.Y - Ball.Radius; } else { ball.Position.Y = this.Position.Y + Size.Y + Ball.Radius; } ball.ChangeVelocity(); ball.Velocity.Y = -ball.Velocity.Y; if (ball.Position.X > this.Position.X + Size.X / 2) { ball.Velocity.X = Math.Abs(ball.Velocity.X); } else { ball.Velocity.X = -1 * Math.Abs(ball.Velocity.X); } return(1); } return(0); }
/// <summary> /// were the bricks hit by the ball or not. if so then make the brick invisible. /// </summary> /// <param name="ball">the ball</param> /// <returns>whether the bricks were hit or not</returns> public bool HasBallHit(Ball ball) { if (IsBallNearBricks(ball)) { //if the ball is near the bricks - check hit on every brick for (int i = 0; i < bricks.GetLength(0); i++) { for (int j = 0; j < bricks.GetLength(1); j++) { if (bricks[i, j].IsHit(ball)) { bricks[i, j].IsVisible = false; ball.Velocity.Y = -ball.Velocity.Y; ball.ChangeVelocity(); visibleBricks--; return(true); } } } } return(false); }