mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:07:35 +00:00
Snake: Move GUI into Snake namespace and rename SnakeGame to Game
The former is required for GML, and the latter is to avoid the verbosity and redundancy of Snake::SnakeGame (and matches most other games in the system).
This commit is contained in:
parent
36042fc1d6
commit
ae90f490bd
4 changed files with 35 additions and 27 deletions
|
@ -5,8 +5,8 @@ serenity_component(
|
||||||
)
|
)
|
||||||
|
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
|
Game.cpp
|
||||||
main.cpp
|
main.cpp
|
||||||
SnakeGame.cpp
|
|
||||||
)
|
)
|
||||||
|
|
||||||
serenity_app(Snake ICON app-snake)
|
serenity_app(Snake ICON app-snake)
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "SnakeGame.h"
|
#include "Game.h"
|
||||||
#include <AK/Random.h>
|
#include <AK/Random.h>
|
||||||
#include <LibConfig/Client.h>
|
#include <LibConfig/Client.h>
|
||||||
#include <LibGUI/MessageBox.h>
|
#include <LibGUI/MessageBox.h>
|
||||||
|
@ -15,7 +15,9 @@
|
||||||
#include <LibGfx/Font/Font.h>
|
#include <LibGfx/Font/Font.h>
|
||||||
#include <LibGfx/Font/FontDatabase.h>
|
#include <LibGfx/Font/FontDatabase.h>
|
||||||
|
|
||||||
ErrorOr<NonnullRefPtr<SnakeGame>> SnakeGame::create()
|
namespace Snake {
|
||||||
|
|
||||||
|
ErrorOr<NonnullRefPtr<Game>> Game::create()
|
||||||
{
|
{
|
||||||
NonnullRefPtrVector<Gfx::Bitmap> food_bitmaps;
|
NonnullRefPtrVector<Gfx::Bitmap> food_bitmaps;
|
||||||
food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F41F.png"sv)));
|
food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F41F.png"sv)));
|
||||||
|
@ -47,10 +49,10 @@ ErrorOr<NonnullRefPtr<SnakeGame>> SnakeGame::create()
|
||||||
food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F413.png"sv)));
|
food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F413.png"sv)));
|
||||||
food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1FAB0.png"sv)));
|
food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1FAB0.png"sv)));
|
||||||
food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1FAB1.png"sv)));
|
food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1FAB1.png"sv)));
|
||||||
return adopt_nonnull_ref_or_enomem(new (nothrow) SnakeGame(move(food_bitmaps)));
|
return adopt_nonnull_ref_or_enomem(new (nothrow) Game(move(food_bitmaps)));
|
||||||
}
|
}
|
||||||
|
|
||||||
SnakeGame::SnakeGame(NonnullRefPtrVector<Gfx::Bitmap> food_bitmaps)
|
Game::Game(NonnullRefPtrVector<Gfx::Bitmap> food_bitmaps)
|
||||||
: m_food_bitmaps(move(food_bitmaps))
|
: m_food_bitmaps(move(food_bitmaps))
|
||||||
{
|
{
|
||||||
set_font(Gfx::FontDatabase::default_fixed_width_font().bold_variant());
|
set_font(Gfx::FontDatabase::default_fixed_width_font().bold_variant());
|
||||||
|
@ -60,18 +62,18 @@ SnakeGame::SnakeGame(NonnullRefPtrVector<Gfx::Bitmap> food_bitmaps)
|
||||||
m_snake_base_color = Color::from_argb(Config::read_u32("Snake"sv, "Snake"sv, "BaseColor"sv, m_snake_base_color.value()));
|
m_snake_base_color = Color::from_argb(Config::read_u32("Snake"sv, "Snake"sv, "BaseColor"sv, m_snake_base_color.value()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void SnakeGame::pause()
|
void Game::pause()
|
||||||
{
|
{
|
||||||
stop_timer();
|
stop_timer();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SnakeGame::start()
|
void Game::start()
|
||||||
{
|
{
|
||||||
static constexpr int timer_ms = 100;
|
static constexpr int timer_ms = 100;
|
||||||
start_timer(timer_ms);
|
start_timer(timer_ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SnakeGame::reset()
|
void Game::reset()
|
||||||
{
|
{
|
||||||
m_head = { m_rows / 2, m_columns / 2 };
|
m_head = { m_rows / 2, m_columns / 2 };
|
||||||
m_tail.clear_with_capacity();
|
m_tail.clear_with_capacity();
|
||||||
|
@ -86,13 +88,13 @@ void SnakeGame::reset()
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SnakeGame::set_snake_base_color(Color color)
|
void Game::set_snake_base_color(Color color)
|
||||||
{
|
{
|
||||||
Config::write_u32("Snake"sv, "Snake"sv, "BaseColor"sv, color.value());
|
Config::write_u32("Snake"sv, "Snake"sv, "BaseColor"sv, color.value());
|
||||||
m_snake_base_color = color;
|
m_snake_base_color = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SnakeGame::is_available(Coordinate const& coord)
|
bool Game::is_available(Coordinate const& coord)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < m_tail.size(); ++i) {
|
for (size_t i = 0; i < m_tail.size(); ++i) {
|
||||||
if (m_tail[i] == coord)
|
if (m_tail[i] == coord)
|
||||||
|
@ -105,7 +107,7 @@ bool SnakeGame::is_available(Coordinate const& coord)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SnakeGame::spawn_fruit()
|
void Game::spawn_fruit()
|
||||||
{
|
{
|
||||||
Coordinate coord;
|
Coordinate coord;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
@ -118,19 +120,19 @@ void SnakeGame::spawn_fruit()
|
||||||
m_fruit_type = get_random_uniform(m_food_bitmaps.size());
|
m_fruit_type = get_random_uniform(m_food_bitmaps.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
Gfx::IntRect SnakeGame::score_rect() const
|
Gfx::IntRect Game::score_rect() const
|
||||||
{
|
{
|
||||||
int score_width = font().width(m_score_text);
|
int score_width = font().width(m_score_text);
|
||||||
return { frame_inner_rect().width() - score_width - 2, frame_inner_rect().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
|
Gfx::IntRect Game::high_score_rect() const
|
||||||
{
|
{
|
||||||
int high_score_width = font().width(m_high_score_text);
|
int high_score_width = font().width(m_high_score_text);
|
||||||
return { frame_thickness() + 2, frame_inner_rect().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&)
|
void Game::timer_event(Core::TimerEvent&)
|
||||||
{
|
{
|
||||||
Vector<Coordinate> dirty_cells;
|
Vector<Coordinate> dirty_cells;
|
||||||
|
|
||||||
|
@ -191,7 +193,7 @@ void SnakeGame::timer_event(Core::TimerEvent&)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SnakeGame::keydown_event(GUI::KeyEvent& event)
|
void Game::keydown_event(GUI::KeyEvent& event)
|
||||||
{
|
{
|
||||||
switch (event.key()) {
|
switch (event.key()) {
|
||||||
case KeyCode::Key_A:
|
case KeyCode::Key_A:
|
||||||
|
@ -224,7 +226,7 @@ void SnakeGame::keydown_event(GUI::KeyEvent& event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Gfx::IntRect SnakeGame::cell_rect(Coordinate const& coord) const
|
Gfx::IntRect Game::cell_rect(Coordinate const& coord) const
|
||||||
{
|
{
|
||||||
auto game_rect = frame_inner_rect();
|
auto game_rect = frame_inner_rect();
|
||||||
auto cell_size = Gfx::IntSize(game_rect.width() / m_columns, game_rect.height() / m_rows);
|
auto cell_size = Gfx::IntSize(game_rect.width() / m_columns, game_rect.height() / m_rows);
|
||||||
|
@ -236,7 +238,7 @@ Gfx::IntRect SnakeGame::cell_rect(Coordinate const& coord) const
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
void SnakeGame::paint_event(GUI::PaintEvent& event)
|
void Game::paint_event(GUI::PaintEvent& event)
|
||||||
{
|
{
|
||||||
GUI::Frame::paint_event(event);
|
GUI::Frame::paint_event(event);
|
||||||
GUI::Painter painter(*this);
|
GUI::Painter painter(*this);
|
||||||
|
@ -265,7 +267,7 @@ void SnakeGame::paint_event(GUI::PaintEvent& event)
|
||||||
painter.draw_text(score_rect(), m_score_text, Gfx::TextAlignment::TopLeft, Color::White);
|
painter.draw_text(score_rect(), m_score_text, Gfx::TextAlignment::TopLeft, Color::White);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SnakeGame::game_over()
|
void Game::game_over()
|
||||||
{
|
{
|
||||||
stop_timer();
|
stop_timer();
|
||||||
|
|
||||||
|
@ -282,17 +284,19 @@ void SnakeGame::game_over()
|
||||||
reset();
|
reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SnakeGame::queue_velocity(int v, int h)
|
void Game::queue_velocity(int v, int h)
|
||||||
{
|
{
|
||||||
if (last_velocity().vertical == v && last_velocity().horizontal == h)
|
if (last_velocity().vertical == v && last_velocity().horizontal == h)
|
||||||
return;
|
return;
|
||||||
m_velocity_queue.enqueue({ v, h });
|
m_velocity_queue.enqueue({ v, h });
|
||||||
}
|
}
|
||||||
|
|
||||||
SnakeGame::Velocity const& SnakeGame::last_velocity() const
|
Game::Velocity const& Game::last_velocity() const
|
||||||
{
|
{
|
||||||
if (!m_velocity_queue.is_empty())
|
if (!m_velocity_queue.is_empty())
|
||||||
return m_velocity_queue.last();
|
return m_velocity_queue.last();
|
||||||
|
|
||||||
return m_last_velocity;
|
return m_last_velocity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -11,12 +11,14 @@
|
||||||
#include <AK/NonnullRefPtrVector.h>
|
#include <AK/NonnullRefPtrVector.h>
|
||||||
#include <LibGUI/Frame.h>
|
#include <LibGUI/Frame.h>
|
||||||
|
|
||||||
class SnakeGame : public GUI::Frame {
|
namespace Snake {
|
||||||
C_OBJECT(SnakeGame);
|
|
||||||
|
class Game : public GUI::Frame {
|
||||||
|
C_OBJECT(Game);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static ErrorOr<NonnullRefPtr<SnakeGame>> create();
|
static ErrorOr<NonnullRefPtr<Game>> create();
|
||||||
virtual ~SnakeGame() override = default;
|
virtual ~Game() override = default;
|
||||||
|
|
||||||
void start();
|
void start();
|
||||||
void pause();
|
void pause();
|
||||||
|
@ -25,7 +27,7 @@ public:
|
||||||
void set_snake_base_color(Color color);
|
void set_snake_base_color(Color color);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit SnakeGame(NonnullRefPtrVector<Gfx::Bitmap> food_bitmaps);
|
explicit Game(NonnullRefPtrVector<Gfx::Bitmap> food_bitmaps);
|
||||||
|
|
||||||
virtual void paint_event(GUI::PaintEvent&) override;
|
virtual void paint_event(GUI::PaintEvent&) override;
|
||||||
virtual void keydown_event(GUI::KeyEvent&) override;
|
virtual void keydown_event(GUI::KeyEvent&) override;
|
||||||
|
@ -80,3 +82,5 @@ private:
|
||||||
|
|
||||||
Gfx::Color m_snake_base_color { Color::Yellow };
|
Gfx::Color m_snake_base_color { Color::Yellow };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -4,7 +4,7 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "SnakeGame.h"
|
#include "Game.h"
|
||||||
#include <AK/URL.h>
|
#include <AK/URL.h>
|
||||||
#include <LibConfig/Client.h>
|
#include <LibConfig/Client.h>
|
||||||
#include <LibCore/System.h>
|
#include <LibCore/System.h>
|
||||||
|
@ -46,7 +46,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
window->set_title("Snake");
|
window->set_title("Snake");
|
||||||
window->resize(324, 344);
|
window->resize(324, 344);
|
||||||
|
|
||||||
auto game = TRY(SnakeGame::create());
|
auto game = TRY(Snake::Game::create());
|
||||||
window->set_main_widget(game);
|
window->set_main_widget(game);
|
||||||
|
|
||||||
auto game_menu = TRY(window->try_add_menu("&Game"));
|
auto game_menu = TRY(window->try_add_menu("&Game"));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue