diff options
author | Karol Kosek <krkk@krkk.ct8.pl> | 2021-07-11 12:36:47 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-07-11 14:11:26 +0200 |
commit | a90c484d4bc29976baaf0635d2cd6eb07e1a225f (patch) | |
tree | 6eb5ec4dd9f578d396f8164bd9623959418074ca /Userland/Games/Pong | |
parent | 9ab528ff5cfe14e3d9f159e2f09af4ebe1f2d2b7 (diff) | |
download | serenity-a90c484d4bc29976baaf0635d2cd6eb07e1a225f.zip |
Pong: Only paint areas that needs to be updated
Diffstat (limited to 'Userland/Games/Pong')
-rw-r--r-- | Userland/Games/Pong/Game.cpp | 42 | ||||
-rw-r--r-- | Userland/Games/Pong/Game.h | 8 |
2 files changed, 40 insertions, 10 deletions
diff --git a/Userland/Games/Pong/Game.cpp b/Userland/Games/Pong/Game.cpp index 83e7b0cb21..e11ff38778 100644 --- a/Userland/Games/Pong/Game.cpp +++ b/Userland/Games/Pong/Game.cpp @@ -21,15 +21,21 @@ Game::~Game() void Game::reset_paddles() { + if (m_cursor_paddle_target_y.has_value()) + update(cursor_paddle_target_rect()); m_cursor_paddle_target_y.clear(); + update(enclosing_int_rect(m_player1_paddle.rect)); m_player1_paddle.moving_up = m_up_key_held; m_player1_paddle.moving_down = m_down_key_held; m_player1_paddle.rect = { game_width - 12, game_height / 2 - 40, m_player1_paddle.width, m_player1_paddle.height }; + update(enclosing_int_rect(m_player1_paddle.rect)); + update(enclosing_int_rect(m_player2_paddle.rect)); m_player2_paddle.moving_up = false; m_player2_paddle.moving_down = false; m_player2_paddle.rect = { 4, game_height / 2 - 40, m_player2_paddle.width, m_player2_paddle.height }; + update(enclosing_int_rect(m_player2_paddle.rect)); } void Game::reset() @@ -56,12 +62,8 @@ void Game::paint_event(GUI::PaintEvent& event) painter.fill_rect(enclosing_int_rect(m_player1_paddle.rect), m_player1_paddle.color); painter.fill_rect(enclosing_int_rect(m_player2_paddle.rect), m_player2_paddle.color); - if (m_cursor_paddle_target_y.has_value()) { - int radius = 3; - int center_x = m_player1_paddle.rect.center().x(); - int center_y = *m_cursor_paddle_target_y + m_player1_paddle.rect.height() / 2; - painter.fill_ellipse(Gfx::IntRect { center_x - radius, center_y - radius, 2 * radius, 2 * radius }, Color::Blue); - } + if (m_cursor_paddle_target_y.has_value()) + painter.fill_ellipse(cursor_paddle_target_rect(), Color::Blue); painter.draw_text(player_1_score_rect(), String::formatted("{}", m_player_1_score), Gfx::TextAlignment::TopLeft, Color::White); painter.draw_text(player_2_score_rect(), String::formatted("{}", m_player_2_score), Gfx::TextAlignment::TopLeft, Color::White); @@ -113,6 +115,9 @@ void Game::mousemove_event(GUI::MouseEvent& event) return; } + if (m_cursor_paddle_target_y.has_value()) + update(cursor_paddle_target_rect()); + m_cursor_paddle_target_y = clamp(event.y() - m_player1_paddle.rect.height() / 2, 0.f, game_height - m_player1_paddle.rect.height()); if (m_player1_paddle.rect.y() > *m_cursor_paddle_target_y) { m_player1_paddle.moving_up = true; @@ -121,6 +126,7 @@ void Game::mousemove_event(GUI::MouseEvent& event) m_player1_paddle.moving_up = false; m_player1_paddle.moving_down = true; } + update(cursor_paddle_target_rect()); } void Game::reset_ball(int serve_to_player) @@ -149,11 +155,17 @@ void Game::game_over(int winner) void Game::round_over(int winner) { stop_timer(); - if (winner == 1) + if (winner == 1) { + update(player_1_score_rect()); m_player_1_score++; + update(player_1_score_rect()); + } - if (winner == 2) + if (winner == 2) { + update(player_2_score_rect()); m_player_2_score++; + update(player_2_score_rect()); + } if (m_player_1_score == m_score_to_win || m_player_2_score == m_score_to_win) { game_over(winner); @@ -207,6 +219,8 @@ void Game::tick() auto new_ball = m_ball; new_ball.position += new_ball.velocity; + update(enclosing_int_rect(m_ball.rect())); + if (new_ball.y() < new_ball.radius || new_ball.y() > game_height - new_ball.radius) { new_ball.position.set_y(m_ball.y()); new_ball.velocity.set_y(new_ball.velocity.y() * -1); @@ -222,6 +236,8 @@ void Game::tick() return; } + update(enclosing_int_rect(new_ball.rect())); + if (new_ball.rect().intersects(m_player1_paddle.rect)) { new_ball.position.set_x(m_ball.x()); new_ball.velocity.set_x(new_ball.velocity.x() * -1); @@ -241,32 +257,38 @@ void Game::tick() } if (m_player1_paddle.moving_up) { + update(enclosing_int_rect(m_player1_paddle.rect)); m_player1_paddle.rect.set_y(max(0.0f, m_player1_paddle.rect.y() - m_player1_paddle.speed)); if (m_cursor_paddle_target_y.has_value() && m_player1_paddle.rect.y() <= *m_cursor_paddle_target_y) { m_cursor_paddle_target_y.clear(); m_player1_paddle.moving_up = false; } + update(enclosing_int_rect(m_player1_paddle.rect)); } if (m_player1_paddle.moving_down) { + update(enclosing_int_rect(m_player1_paddle.rect)); m_player1_paddle.rect.set_y(min(game_height - m_player1_paddle.rect.height(), m_player1_paddle.rect.y() + m_player1_paddle.speed)); if (m_cursor_paddle_target_y.has_value() && m_player1_paddle.rect.y() >= *m_cursor_paddle_target_y) { m_cursor_paddle_target_y.clear(); m_player1_paddle.moving_down = false; } + update(enclosing_int_rect(m_player1_paddle.rect)); } calculate_move(); if (m_player2_paddle.moving_up) { + update(enclosing_int_rect(m_player2_paddle.rect)); m_player2_paddle.rect.set_y(max(0.0f, m_player2_paddle.rect.y() - m_player2_paddle.speed)); + update(enclosing_int_rect(m_player2_paddle.rect)); } if (m_player2_paddle.moving_down) { + update(enclosing_int_rect(m_player2_paddle.rect)); m_player2_paddle.rect.set_y(min(game_height - m_player2_paddle.rect.height(), m_player2_paddle.rect.y() + m_player2_paddle.speed)); + update(enclosing_int_rect(m_player2_paddle.rect)); } m_ball = new_ball; - - update(); } } diff --git a/Userland/Games/Pong/Game.h b/Userland/Games/Pong/Game.h index 64a2be5b34..92a8e7cc1c 100644 --- a/Userland/Games/Pong/Game.h +++ b/Userland/Games/Pong/Game.h @@ -90,6 +90,14 @@ private: return { (game_width / 2) - score_width - score_margin, score_margin, score_width, font().glyph_height() }; } + Gfx::IntRect cursor_paddle_target_rect() const + { + int radius = 3; + int center_x = m_player1_paddle.rect.center().x(); + int center_y = *m_cursor_paddle_target_y + m_player1_paddle.rect.height() / 2; + return { center_x - radius, center_y - radius, 2 * radius, 2 * radius }; + } + Net m_net; Ball m_ball; Paddle m_player1_paddle; |