mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-23 08:00:20 +00:00
Pong: Improve AI paddle movement
Begin and end triggers for AI paddle to start moving prevent it from continually jerking around the ball's position. It still moves in steps of paddle.speed though, but overall experience is smoother.
This commit is contained in:
parent
7c67c9b45f
commit
bb76bcde57
Notes:
sideshowbarker
2024-07-18 17:49:38 +09:00
Author: https://github.com/dmitrii-ubskii Commit: https://github.com/SerenityOS/serenity/commit/bb76bcde57f Pull-request: https://github.com/SerenityOS/serenity/pull/7196 Reviewed-by: https://github.com/linusg
1 changed files with 24 additions and 10 deletions
|
@ -141,18 +141,32 @@ void Game::round_over(int winner)
|
|||
|
||||
void Game::calculate_move()
|
||||
{
|
||||
if ((m_ball.y() + m_ball.radius) < (m_player2_paddle.rect.y() + (m_player2_paddle.rect.height() / 2))) {
|
||||
m_player2_paddle.moving_up = true;
|
||||
m_player2_paddle.moving_down = false;
|
||||
return;
|
||||
int player_2_paddle_top = m_player2_paddle.rect.top();
|
||||
int player_2_paddle_bottom = m_player2_paddle.rect.bottom();
|
||||
|
||||
int ball_position = m_ball.y() + m_ball.radius;
|
||||
|
||||
// AI paddle begins moving when the ball crosses the begin_trigger,
|
||||
// but stops only if it crosses the end_trigger. end_trigger forces
|
||||
// overcorrection, so that the paddle moves more smoothly.
|
||||
int begin_trigger = m_player2_paddle.rect.height() / 4;
|
||||
int end_trigger = m_player2_paddle.rect.height() / 2;
|
||||
|
||||
if (m_player2_paddle.moving_up) {
|
||||
if (player_2_paddle_top + end_trigger < ball_position)
|
||||
m_player2_paddle.moving_up = false;
|
||||
} else {
|
||||
if (player_2_paddle_top + begin_trigger > ball_position)
|
||||
m_player2_paddle.moving_up = true;
|
||||
}
|
||||
if ((m_ball.y() + m_ball.radius) > (m_player2_paddle.rect.y() + (m_player2_paddle.rect.height() / 2))) {
|
||||
m_player2_paddle.moving_up = false;
|
||||
m_player2_paddle.moving_down = true;
|
||||
return;
|
||||
|
||||
if (m_player2_paddle.moving_down) {
|
||||
if (player_2_paddle_bottom - end_trigger > ball_position)
|
||||
m_player2_paddle.moving_down = false;
|
||||
} else {
|
||||
if (player_2_paddle_bottom - begin_trigger < ball_position)
|
||||
m_player2_paddle.moving_down = true;
|
||||
}
|
||||
m_player2_paddle.moving_up = false;
|
||||
m_player2_paddle.moving_down = false;
|
||||
}
|
||||
|
||||
void Game::tick()
|
||||
|
|
Loading…
Reference in a new issue