mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-23 08:00:20 +00:00
Pong: Restart timer if necessary
When the player runs into a game over condition, Game's timer is stopped. In order for reset() to work properly, the timer has to be started again. The condition is tracked via a new member variable, `m_game_over`. To prevent confusion, game_over() has been renamed to show_game_over_message().
This commit is contained in:
parent
0abdeb474f
commit
02d2a300e7
Notes:
sideshowbarker
2024-07-19 17:06:20 +09:00
Author: https://github.com/juni2k Commit: https://github.com/SerenityOS/serenity/commit/02d2a300e73 Pull-request: https://github.com/SerenityOS/serenity/pull/13446
2 changed files with 11 additions and 3 deletions
|
@ -36,6 +36,11 @@ void Game::reset_paddles()
|
|||
|
||||
void Game::reset()
|
||||
{
|
||||
if (m_game_over) {
|
||||
m_game_over = false;
|
||||
start_timer(16);
|
||||
}
|
||||
|
||||
// Make sure the current ball disappears.
|
||||
update(enclosing_int_rect(m_ball.rect()));
|
||||
|
||||
|
@ -162,7 +167,7 @@ void Game::reset_ball(int serve_to_player)
|
|||
m_ball.velocity = { velocity_x, velocity_y };
|
||||
}
|
||||
|
||||
void Game::game_over(int winner)
|
||||
void Game::show_game_over_message(int winner)
|
||||
{
|
||||
GUI::MessageBox::show(window(), String::formatted("Player {} wins!", winner), "Pong", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::OK);
|
||||
}
|
||||
|
@ -183,7 +188,8 @@ void Game::round_over(int winner)
|
|||
}
|
||||
|
||||
if (m_player_1_score == m_score_to_win || m_player_2_score == m_score_to_win) {
|
||||
game_over(winner);
|
||||
m_game_over = true;
|
||||
show_game_over_message(winner);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -44,7 +44,7 @@ private:
|
|||
void reset_paddles();
|
||||
void tick();
|
||||
void round_over(int player);
|
||||
void game_over(int player);
|
||||
void show_game_over_message(int player);
|
||||
void calculate_move();
|
||||
|
||||
struct Ball {
|
||||
|
@ -113,6 +113,8 @@ private:
|
|||
Optional<int> m_cursor_paddle_target_y;
|
||||
bool m_up_key_held = false;
|
||||
bool m_down_key_held = false;
|
||||
|
||||
bool m_game_over = false;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue