mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-06 02:55:49 +03:00
Pong: Only paint areas that needs to be updated
This commit is contained in:
parent
9ab528ff5c
commit
a90c484d4b
Notes:
sideshowbarker
2024-07-19 17:26:29 +09:00
Author: https://github.com/krkk Commit: https://github.com/SerenityOS/serenity/commit/a90c484d4bc Pull-request: https://github.com/SerenityOS/serenity/pull/8638
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user