Skip to content

Commit

Permalink
Snake: Spruce up the GUI a tiny bit
Browse files Browse the repository at this point in the history
Give the game window a GUI::Frame appearance, and make sure the
menus have Alt shortcuts. :^)
  • Loading branch information
awesomekling committed May 4, 2021
1 parent d136faf commit 04c3cdd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
12 changes: 7 additions & 5 deletions Userland/Games/Snake/SnakeGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ void SnakeGame::spawn_fruit()
Gfx::IntRect SnakeGame::score_rect() const
{
int score_width = font().width(m_score_text);
return { width() - score_width - 2, height() - font().glyph_height() - 2, score_width, font().glyph_height() };
return { frame_inner_rect().width() - score_width - 2, frame_inner_rect().height() - font().glyph_height() - 2, score_width, font().glyph_height() };
}

Gfx::IntRect SnakeGame::high_score_rect() const
{
int high_score_width = font().width(m_high_score_text);
return { 2, height() - font().glyph_height() - 2, high_score_width, font().glyph_height() };
return { frame_thickness() + 2, frame_inner_rect().height() - font().glyph_height() - 2, high_score_width, font().glyph_height() };
}

void SnakeGame::timer_event(Core::TimerEvent&)
Expand Down Expand Up @@ -179,19 +179,21 @@ void SnakeGame::keydown_event(GUI::KeyEvent& event)

Gfx::IntRect SnakeGame::cell_rect(const Coordinate& coord) const
{
auto game_rect = rect();
auto game_rect = frame_inner_rect();
auto cell_size = Gfx::IntSize(game_rect.width() / m_columns, game_rect.height() / m_rows);
return {
coord.column * cell_size.width(),
coord.row * cell_size.height(),
game_rect.x() + coord.column * cell_size.width(),
game_rect.y() + coord.row * cell_size.height(),
cell_size.width(),
cell_size.height()
};
}

void SnakeGame::paint_event(GUI::PaintEvent& event)
{
GUI::Frame::paint_event(event);
GUI::Painter painter(*this);
painter.add_clip_rect(frame_inner_rect());
painter.add_clip_rect(event.rect());
painter.fill_rect(event.rect(), Color::Black);

Expand Down
7 changes: 4 additions & 3 deletions Userland/Games/Snake/SnakeGame.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

#include <AK/CircularQueue.h>
#include <AK/NonnullRefPtrVector.h>
#include <LibGUI/Widget.h>
#include <LibGUI/Frame.h>

class SnakeGame : public GUI::Frame {
C_OBJECT(SnakeGame);

class SnakeGame : public GUI::Widget {
C_OBJECT(SnakeGame)
public:
virtual ~SnakeGame() override;

Expand Down
4 changes: 2 additions & 2 deletions Userland/Games/Snake/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ int main(int argc, char** argv)

window->set_double_buffering_enabled(false);
window->set_title("Snake");
window->resize(320, 320);
window->resize(324, 344);

auto& game = window->set_main_widget<SnakeGame>();

Expand All @@ -70,7 +70,7 @@ int main(int argc, char** argv)
GUI::Application::the()->quit();
}));

auto& help_menu = menubar->add_menu("Help");
auto& help_menu = menubar->add_menu("&Help");
help_menu.add_action(GUI::CommonActions::make_about_action("Snake", app_icon, window));

window->set_menubar(move(menubar));
Expand Down

0 comments on commit 04c3cdd

Please sign in to comment.