/// <summary> /// Repositions the ball on the racket. /// </summary> /// <param name="racket">The racket.</param> public void RepositionOnRacket(Racket racket) { try { area.Y = racket.Area.Y - Area.Width; onPropertyChanged("Area"); } catch { } }
/// <summary> /// Move the ball by key if it's on the racket. /// </summary> /// <param name="racketSpeed">The racketSpeed.</param> /// <param name="canvasWidth">The canvasWidth.</param> /// <param name="racket">The racket.</param> public void KeyMove(double racketSpeed, double canvasWidth, Racket racket) { try { // The racket is at the edge of the canvas. if (racket.Area.X <= 0) { // The ball sticks out to the left side. if (Area.X <= racket.Area.X) { area.X = 0; } } // The racket is at the edge of the canvas. else if (racket.Area.X + racket.Area.Width >= canvasWidth) { // The ball sticks out to the right side. if (Area.X >= racket.Area.X + racket.Area.Width - Area.Width) { area.X = canvasWidth - Area.Width; } } // There is space to move. else { // The left key is pushed. if (racket.Direction == Racket.Directions.Left) { area.X -= racketSpeed; } // The right key is pushed. else if (racket.Direction == Racket.Directions.Right) { area.X += racketSpeed; } } onPropertyChanged("Area"); } catch { } }
/// <summary> /// Handles the event when the ball falls down. /// </summary> private void DisposeOfBall() { try { // Dispose balls, rackets, bonuses. racketList.Clear(); ballList.Clear(); bonusList.Clear(); // Add a new racket. Racket racket = new Racket(canvasWidth / 2 - racketWidth / 2, canvasHeight - racketHeight, racketWidth, racketHeight, @"..\..\Resources\Media\Racket\normalracket.jpg"); racket.Direction = Racket.Directions.Stay; racket.StickyRacket = false; racket.IsDeleted = false; canvas.Children.Add(racket.GetRectangle()); racketList.Add(racket); // Add a new ball. Ball ball = new Ball((canvasWidth / 2) - ballRadius, canvasHeight - racketHeight - (ballRadius * 2), ballRadius * 2, ballRadius * 2, @"..\..\Resources\Media\Ball\normalball.jpg", ballHorizontalMovement, ballVerticalMovement, Ball.BallsType.Normal); ball.VerticalMovement = ballVerticalMovement > 0 ? ballVerticalMovement : -ballVerticalMovement; ball.HorizontalMovement = ballHorizontalMovement < 0 ? ballHorizontalMovement : -ballHorizontalMovement; ball.BallInMove = false; ball.RelativePosition = ball.Area.X + ball.Area.Width / 2 - racketList[0].Area.X; ball.IsDeleted = false; canvas.Children.Add(ball.GetEllipse()); ballList.Add(ball); } catch (Exception e) { errorLogViewModel.LogError(e); } }
/// <summary> /// Move the ball by mouse if it's on the racket. /// </summary> /// <param name="racketSpeed">The racketSpeed.</param> /// <param name="canvasWidth">The canvasWidth.</param> /// <param name="mousePositionX">The mousePositionX.</param> /// <param name="racket">The racket.</param> public void MouseMove(double racketSpeed, double canvasWidth, double mousePositionX, Racket racket) { try { // The mouse is left to the racket. if (mousePositionX < racket.Area.X + racket.Area.Width / 2) { // The racket is at the edge of the canvas. if (racket.Area.X <= 0) { // The ball sticks out to the left side. if (Area.X <= racket.Area.X) { area.X = 0; } } // There is space to move. else { // The differance is greater than the length the racket can move in a tick. if (racket.Area.X + racket.Area.Width / 2 - mousePositionX >= racketSpeed) { area.X -= racketSpeed; } // The differance is smaller than the length the racket can move in a tick. else { area.X -= racket.Area.X + racket.Area.Width / 2 - mousePositionX; } } } // The mouse is right to the racket. else if (mousePositionX > racket.Area.X + racket.Area.Width / 2) { // The racket is at the edge of the canvas. if (racket.Area.X + racket.Area.Width >= canvasWidth) { // The ball sticks out to the right side. if (Area.X >= racket.Area.X + racket.Area.Width - Area.Width) { area.X = canvasWidth - Area.Width; } } // There is space to move. else { // The differance is greater than the length the racket can move in a tick. if (mousePositionX - racket.Area.X + racket.Area.Width / 2 >= racketSpeed) { area.X += racketSpeed; } // The differance is smaller than the length the racket can move in a tick. else { area.X += mousePositionX - racket.Area.X + racket.Area.Width / 2; } } } onPropertyChanged("Area"); } catch { } }
/// <summary> /// Repositions the ball if it's not right on the racket. /// </summary> /// <param name="racket">The racket.</param> public void RepositionBallOnRacket(Racket racket) { try { // The ball is falling of the racket, move it back on it. if (Area.X < racket.Area.X - Area.Width / 2) { area.X = racket.Area.X - Area.Width / 2; } else if (Area.X > racket.Area.X + racket.Area.Width - Area.Width / 2) { area.X = racket.Area.X + racket.Area.Width - Area.Width / 2; } } catch { } }
/// <summary> /// Repositions the ball if it's not right on the racket. /// </summary> /// <param name="racket">The racket.</param> public void RepositionBallAtRacket(Racket racket) { try { // The ball's inside the racket. if (Area.Y > racket.Area.Y - Area.Height) { // Move the ball higher. area.Y = racket.Area.Y - Area.Height; onPropertyChanged("Area"); } } catch { } }
/// <summary> /// Move the ball by mouse if it's on the racket. /// </summary> /// <param name="racket">The racket.</param> /// <param name="canvasWidth">The canvasWidth.</param> /// <param name="mousePositionX">The mousePositionX.</param> public void MouseMove(Racket racket, double canvasWidth, double mousePositionX) { try { // The mouse is left to the racket. if (mousePositionX < racket.Area.X + racket.Area.Width / 2 + 3) { // The ball is at the edge of the canvas. if (Area.X <= 0) { area.X = 0; } // There is space to move. else { area.X = racket.Area.X + RelativePosition - Area.Width / 2; } RepositionBallOnRacket(racket); onPropertyChanged("Area"); } // The mouse is right to the racket. else if (mousePositionX > racket.Area.X + racket.Area.Width / 2 + 3) { // The ball is at the edge of the canvas. if (Area.X + Area.Width >= canvasWidth) { area.X = canvasWidth - Area.Width; } // There is space to move. else { area.X = racket.Area.X + RelativePosition - Area.Width / 2; } RepositionBallOnRacket(racket); onPropertyChanged("Area"); } } catch { } }
/// <summary> /// Move the ball by key if it's on the racket. /// </summary> /// <param name="canvasWidth">The canvasWidth.</param> /// <param name="racket">The racket.</param> public void KeyMove(double canvasWidth, Racket racket) { try { // Move the racket left. if (racket.Direction == Racket.Directions.Left) { // The ball is at the edge of the canvas. if (Area.X <= 0) { area.X = 0; } // There is space to move. else { area.X = racket.Area.X + RelativePosition - Area.Width / 2; } RepositionBallOnRacket(racket); onPropertyChanged("Area"); } // Move the racket right. else if (racket.Direction == Racket.Directions.Right) { // The ball is at the edge of the canvas. if (Area.X + Area.Width >= canvasWidth) { area.X = canvasWidth - Area.Width; } // There is space to move. else { area.X = racket.Area.X + RelativePosition - Area.Width / 2; } RepositionBallOnRacket(racket); onPropertyChanged("Area"); } } catch { } }
/// <summary> /// Handles the event when the ball falls down. /// </summary> private void DisposeOfBall() { try { // Dispose balls, rackets, bonuses. racketList.Clear(); ballList.Clear(); bonusList.Clear(); gameObjectList.Clear(); if (brickList.Count > 0) { for (int i = 0; i < brickList.Count; i++) { gameObjectList.Add(brickList[i]); } } // Add new racket. Racket racket = new Racket((canvasWidth / 2) - (racketWidth / 2), canvasHeight - racketHeight, racketHeight, racketWidth, @"..\..\Resources\Media\Racket\normalracket.jpg"); racket.Direction = Racket.Directions.Stay; racket.StickyRacket = false; racketList.Add(racket); gameObjectList.Add(racket); // Add new ball. Ball ball = new Ball((canvasWidth / 2) - ballRadius, canvasHeight - racketHeight - (ballRadius * 2), ballRadius * 2, ballRadius * 2, @"..\..\Resources\Media\Ball\normalball.jpg", ballHorizontalMovement, ballVerticalMovement, Ball.BallsType.Normal); ball.VerticalMovement = ballVerticalMovement > 0 ? ballVerticalMovement : -ballVerticalMovement; ball.HorizontalMovement = ballHorizontalMovement < 0 ? ballHorizontalMovement : -ballHorizontalMovement; ball.BallInMove = false; ballList.Add(ball); gameObjectList.Add(ball); } catch (Exception e) { errorLogViewModel.LogError(e); } }