private void onTick(object sender, EventArgs e) { if (!ball.GetBlnIsGlued()) { ball.ChangePosition((int)(ball.GetRecPosition().X + ball.GetfltXVel()), (int)(ball.GetRecPosition().Y + ball.GetfltYVel())); } if (ball.GetRecPosition().Y < 0) { ball.ChangePosition(ball.GetRecPosition().X, 0); ball.SetFltYVel(-ball.GetfltYVel()); } if (ball.GetRecPosition().Y - ball.GetRecPosition().Height > this.Height) { ball.Glue(); ball.ChangePosition(paddle.GetRecPosition().X + 72 / 2 - (ball.GetRecPosition().Width / 2), 432 - 16); } if (ball.GetRecPosition().X < 0) { ball.ChangePosition(0, ball.GetRecPosition().Y); ball.SetFltXVel(-ball.GetfltXVel()); } if (ball.GetRecPosition().X + ball.GetRecPosition().Width > this.Width) { ball.ChangePosition(this.Width - ball.GetRecPosition().Width, ball.GetRecPosition().Y); ball.SetFltXVel(-ball.GetfltXVel()); } if (ball.CheckForIntersect(paddle.GetRecPosition())) { ball.ChangePosition(ball.GetRecPosition().X, ball.GetRecPosition().Y - ball.GetRecPosition().Height); ball.SetFltYVel(-ball.GetfltYVel()); } for (int intRow = 0; intRow <= arrBricks.GetUpperBound(0); intRow++) { for (int intColumn = 0; intColumn <= arrBricks.GetUpperBound(1); intColumn++) { if (arrBricks[intRow, intColumn].GetBlnIsVisible()) { if (ball.CheckForIntersect(arrBricks[intRow, intColumn].GetRecPosition())) { arrBricks[intRow, intColumn].MakeInvisible(); if (ball.GetRecPosition().X + 10 < arrBricks[intRow, intColumn].GetRecPosition().X || ball.GetRecPosition().X > arrBricks[intRow, intColumn].GetRecPosition().X + arrBricks[intRow, intColumn].GetRecPosition().Width) { ball.SetFltXVel(-ball.GetfltXVel()); } else { ball.SetFltYVel(-ball.GetfltYVel()); } } } } } if (GetBrickCount() == 0) { tmrGame.Stop(); Cursor.Show(); if (MessageBox.Show("Would you like to play again?", "Play Again?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { InitialiseGameScreenForm(); } else { Application.Exit(); } } this.Refresh(); }
private void onTick(object sender, EventArgs e) { // if ball is glued, then it follows movement of paddle if (!ball.IsGlued()) { ball.ChangePosition((int)(ball.GetPosition().X + ball.GetXVelocity()), (int)(ball.GetPosition().Y + ball.GetYVelocity())); } // if ball falls off bottom, glue it back above paddle if (ball.GetPosition().Y - ball.GetPosition().Height > this.Height) { ball.Glue(); ball.ChangePosition(paddle.GetPosition().X + 72 / 2 - (ball.GetPosition().Width / 2), 432 - 16); } // if Y < 0, then ball's gone off top of screen so it comes back down if (ball.GetPosition().Y < 0) { ball.ChangePosition(ball.GetPosition().X, 0); ball.SetYVelocity(-ball.GetYVelocity()); } // if X < 0, then ball's gone off the left if (ball.GetPosition().X < 0) { ball.ChangePosition(0, ball.GetPosition().Y); ball.SetXVelocity(-ball.GetXVelocity()); } // if X > Width, then ball's gone off the right if (ball.GetPosition().X + ball.GetPosition().Width > this.Width) { ball.ChangePosition(this.Width - ball.GetPosition().Width, ball.GetPosition().Y); ball.SetXVelocity(-ball.GetXVelocity()); } // Check to see if its hit the paddle if (ball.CheckForIntersect(paddle)) { ball.ChangePosition(ball.GetPosition().X, ball.GetPosition().Y - ball.GetPosition().Height); ball.SetYVelocity(-ball.GetYVelocity()); } // Now deal with the bricks for (int row = 0; row <= bricks.GetUpperBound(0); row++) { for (int col = 0; col <= bricks.GetUpperBound(1); col++) { if (bricks[row, col].IsVisible()) { if (ball.CheckForIntersect(bricks[row, col])) { bricks[row, col].SetVisibility(false); if (ball.GetPosition().X + 10 < bricks[row, col].GetPosition().X || ball.GetPosition().X > bricks[row, col].GetPosition().X + bricks[row, col].GetPosition().Width) { ball.SetXVelocity(-ball.GetXVelocity()); } else { ball.SetYVelocity(-ball.GetYVelocity()); } } } } } if (GetBrickCount() == 0) { timer.Stop(); Cursor.Show(); if (MessageBox.Show("Would you like to play again?", "Play Again?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { ball.SetVisibility(false); // turn off the current ball paddle.SetVisibility(false); // turn off the paddle InitialiseGameScreenForm(); } else { Application.Exit(); } } }