summaryrefslogtreecommitdiff
path: root/Userland/Games/Snake
diff options
context:
space:
mode:
Diffstat (limited to 'Userland/Games/Snake')
-rw-r--r--Userland/Games/Snake/CMakeLists.txt7
-rw-r--r--Userland/Games/Snake/SnakeGame.cpp257
-rw-r--r--Userland/Games/Snake/SnakeGame.h91
-rw-r--r--Userland/Games/Snake/main.cpp102
4 files changed, 457 insertions, 0 deletions
diff --git a/Userland/Games/Snake/CMakeLists.txt b/Userland/Games/Snake/CMakeLists.txt
new file mode 100644
index 0000000000..d542cd4ead
--- /dev/null
+++ b/Userland/Games/Snake/CMakeLists.txt
@@ -0,0 +1,7 @@
+set(SOURCES
+ main.cpp
+ SnakeGame.cpp
+)
+
+serenity_app(Snake ICON app-snake)
+target_link_libraries(Snake LibGUI)
diff --git a/Userland/Games/Snake/SnakeGame.cpp b/Userland/Games/Snake/SnakeGame.cpp
new file mode 100644
index 0000000000..4c96a7aee9
--- /dev/null
+++ b/Userland/Games/Snake/SnakeGame.cpp
@@ -0,0 +1,257 @@
+/*
+ * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "SnakeGame.h"
+#include <LibCore/ConfigFile.h>
+#include <LibGUI/Painter.h>
+#include <LibGfx/Bitmap.h>
+#include <LibGfx/Font.h>
+#include <LibGfx/FontDatabase.h>
+#include <stdlib.h>
+#include <time.h>
+
+SnakeGame::SnakeGame()
+{
+ set_font(Gfx::FontDatabase::default_bold_fixed_width_font());
+ m_fruit_bitmaps.append(*Gfx::Bitmap::load_from_file("/res/icons/snake/paprika.png"));
+ m_fruit_bitmaps.append(*Gfx::Bitmap::load_from_file("/res/icons/snake/eggplant.png"));
+ m_fruit_bitmaps.append(*Gfx::Bitmap::load_from_file("/res/icons/snake/cauliflower.png"));
+ m_fruit_bitmaps.append(*Gfx::Bitmap::load_from_file("/res/icons/snake/tomato.png"));
+ srand(time(nullptr));
+ reset();
+
+ auto config = Core::ConfigFile::get_for_app("Snake");
+ m_high_score = config->read_num_entry("Snake", "HighScore", 0);
+ m_high_score_text = String::formatted("Best: {}", m_high_score);
+}
+
+SnakeGame::~SnakeGame()
+{
+}
+
+void SnakeGame::reset()
+{
+ m_head = { m_rows / 2, m_columns / 2 };
+ m_tail.clear_with_capacity();
+ m_length = 2;
+ m_score = 0;
+ m_score_text = "Score: 0";
+ m_velocity_queue.clear();
+ stop_timer();
+ start_timer(100);
+ spawn_fruit();
+ update();
+}
+
+bool SnakeGame::is_available(const Coordinate& coord)
+{
+ for (size_t i = 0; i < m_tail.size(); ++i) {
+ if (m_tail[i] == coord)
+ return false;
+ }
+ if (m_head == coord)
+ return false;
+ if (m_fruit == coord)
+ return false;
+ return true;
+}
+
+void SnakeGame::spawn_fruit()
+{
+ Coordinate coord;
+ for (;;) {
+ coord.row = rand() % m_rows;
+ coord.column = rand() % m_columns;
+ if (is_available(coord))
+ break;
+ }
+ m_fruit = coord;
+ m_fruit_type = rand() % m_fruit_bitmaps.size();
+}
+
+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() };
+}
+
+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() };
+}
+
+void SnakeGame::timer_event(Core::TimerEvent&)
+{
+ Vector<Coordinate> dirty_cells;
+
+ m_tail.prepend(m_head);
+
+ if (m_tail.size() > m_length) {
+ dirty_cells.append(m_tail.last());
+ m_tail.take_last();
+ }
+
+ if (!m_velocity_queue.is_empty())
+ m_velocity = m_velocity_queue.dequeue();
+
+ dirty_cells.append(m_head);
+
+ m_head.row += m_velocity.vertical;
+ m_head.column += m_velocity.horizontal;
+
+ m_last_velocity = m_velocity;
+
+ if (m_head.row >= m_rows)
+ m_head.row = 0;
+ if (m_head.row < 0)
+ m_head.row = m_rows - 1;
+ if (m_head.column >= m_columns)
+ m_head.column = 0;
+ if (m_head.column < 0)
+ m_head.column = m_columns - 1;
+
+ dirty_cells.append(m_head);
+
+ for (size_t i = 0; i < m_tail.size(); ++i) {
+ if (m_head == m_tail[i]) {
+ game_over();
+ return;
+ }
+ }
+
+ if (m_head == m_fruit) {
+ ++m_length;
+ ++m_score;
+ m_score_text = String::formatted("Score: {}", m_score);
+ if (m_score > m_high_score) {
+ m_high_score = m_score;
+ m_high_score_text = String::formatted("Best: {}", m_high_score);
+ update(high_score_rect());
+ auto config = Core::ConfigFile::get_for_app("Snake");
+ config->write_num_entry("Snake", "HighScore", m_high_score);
+ }
+ update(score_rect());
+ dirty_cells.append(m_fruit);
+ spawn_fruit();
+ dirty_cells.append(m_fruit);
+ }
+
+ for (auto& coord : dirty_cells) {
+ update(cell_rect(coord));
+ }
+}
+
+void SnakeGame::keydown_event(GUI::KeyEvent& event)
+{
+ switch (event.key()) {
+ case KeyCode::Key_A:
+ case KeyCode::Key_Left:
+ if (last_velocity().horizontal == 1)
+ break;
+ queue_velocity(0, -1);
+ break;
+ case KeyCode::Key_D:
+ case KeyCode::Key_Right:
+ if (last_velocity().horizontal == -1)
+ break;
+ queue_velocity(0, 1);
+ break;
+ case KeyCode::Key_W:
+ case KeyCode::Key_Up:
+ if (last_velocity().vertical == 1)
+ break;
+ queue_velocity(-1, 0);
+ break;
+ case KeyCode::Key_S:
+ case KeyCode::Key_Down:
+ if (last_velocity().vertical == -1)
+ break;
+ queue_velocity(1, 0);
+ break;
+ default:
+ break;
+ }
+}
+
+Gfx::IntRect SnakeGame::cell_rect(const Coordinate& coord) const
+{
+ auto game_rect = 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(),
+ cell_size.width(),
+ cell_size.height()
+ };
+}
+
+void SnakeGame::paint_event(GUI::PaintEvent& event)
+{
+ GUI::Painter painter(*this);
+ painter.add_clip_rect(event.rect());
+ painter.fill_rect(event.rect(), Color::Black);
+
+ painter.fill_rect(cell_rect(m_head), Color::Yellow);
+ for (auto& part : m_tail) {
+ auto rect = cell_rect(part);
+ painter.fill_rect(rect, Color::from_rgb(0xaaaa00));
+
+ Gfx::IntRect left_side(rect.x(), rect.y(), 2, rect.height());
+ Gfx::IntRect top_side(rect.x(), rect.y(), rect.width(), 2);
+ Gfx::IntRect right_side(rect.right() - 1, rect.y(), 2, rect.height());
+ Gfx::IntRect bottom_side(rect.x(), rect.bottom() - 1, rect.width(), 2);
+ painter.fill_rect(left_side, Color::from_rgb(0xcccc00));
+ painter.fill_rect(right_side, Color::from_rgb(0x888800));
+ painter.fill_rect(top_side, Color::from_rgb(0xcccc00));
+ painter.fill_rect(bottom_side, Color::from_rgb(0x888800));
+ }
+
+ painter.draw_scaled_bitmap(cell_rect(m_fruit), m_fruit_bitmaps[m_fruit_type], m_fruit_bitmaps[m_fruit_type].rect());
+
+ painter.draw_text(high_score_rect(), m_high_score_text, Gfx::TextAlignment::TopLeft, Color::from_rgb(0xfafae0));
+ painter.draw_text(score_rect(), m_score_text, Gfx::TextAlignment::TopLeft, Color::White);
+}
+
+void SnakeGame::game_over()
+{
+ reset();
+}
+
+void SnakeGame::queue_velocity(int v, int h)
+{
+ if (last_velocity().vertical == v && last_velocity().horizontal == h)
+ return;
+ m_velocity_queue.enqueue({ v, h });
+}
+
+const SnakeGame::Velocity& SnakeGame::last_velocity() const
+{
+ if (!m_velocity_queue.is_empty())
+ return m_velocity_queue.last();
+
+ return m_last_velocity;
+}
diff --git a/Userland/Games/Snake/SnakeGame.h b/Userland/Games/Snake/SnakeGame.h
new file mode 100644
index 0000000000..790fc75746
--- /dev/null
+++ b/Userland/Games/Snake/SnakeGame.h
@@ -0,0 +1,91 @@
+/*
+ * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <AK/CircularQueue.h>
+#include <AK/NonnullRefPtrVector.h>
+#include <LibGUI/Widget.h>
+
+class SnakeGame : public GUI::Widget {
+ C_OBJECT(SnakeGame)
+public:
+ virtual ~SnakeGame() override;
+
+ void reset();
+
+private:
+ SnakeGame();
+ virtual void paint_event(GUI::PaintEvent&) override;
+ virtual void keydown_event(GUI::KeyEvent&) override;
+ virtual void timer_event(Core::TimerEvent&) override;
+
+ struct Coordinate {
+ int row { 0 };
+ int column { 0 };
+
+ bool operator==(const Coordinate& other) const
+ {
+ return row == other.row && column == other.column;
+ }
+ };
+
+ struct Velocity {
+ int vertical { 0 };
+ int horizontal { 0 };
+ };
+
+ void game_over();
+ void spawn_fruit();
+ bool is_available(const Coordinate&);
+ void queue_velocity(int v, int h);
+ const Velocity& last_velocity() const;
+ Gfx::IntRect cell_rect(const Coordinate&) const;
+ Gfx::IntRect score_rect() const;
+ Gfx::IntRect high_score_rect() const;
+
+ int m_rows { 20 };
+ int m_columns { 20 };
+
+ Velocity m_velocity { 0, 1 };
+ Velocity m_last_velocity { 0, 1 };
+
+ CircularQueue<Velocity, 10> m_velocity_queue;
+
+ Coordinate m_head;
+ Vector<Coordinate> m_tail;
+
+ Coordinate m_fruit;
+ int m_fruit_type { 0 };
+
+ size_t m_length { 0 };
+ unsigned m_score { 0 };
+ String m_score_text;
+ unsigned m_high_score { 0 };
+ String m_high_score_text;
+
+ NonnullRefPtrVector<Gfx::Bitmap> m_fruit_bitmaps;
+};
diff --git a/Userland/Games/Snake/main.cpp b/Userland/Games/Snake/main.cpp
new file mode 100644
index 0000000000..220f44e87e
--- /dev/null
+++ b/Userland/Games/Snake/main.cpp
@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "SnakeGame.h"
+#include <LibCore/ConfigFile.h>
+#include <LibGUI/Action.h>
+#include <LibGUI/Application.h>
+#include <LibGUI/BoxLayout.h>
+#include <LibGUI/Button.h>
+#include <LibGUI/Icon.h>
+#include <LibGUI/Menu.h>
+#include <LibGUI/MenuBar.h>
+#include <LibGUI/Window.h>
+#include <stdio.h>
+
+int main(int argc, char** argv)
+{
+ if (pledge("stdio rpath wpath cpath shared_buffer accept cpath unix fattr", nullptr) < 0) {
+ perror("pledge");
+ return 1;
+ }
+
+ auto app = GUI::Application::construct(argc, argv);
+
+ if (pledge("stdio rpath wpath cpath shared_buffer accept", nullptr) < 0) {
+ perror("pledge");
+ return 1;
+ }
+
+ auto config = Core::ConfigFile::get_for_app("Snake");
+
+ if (unveil("/res", "r") < 0) {
+ perror("unveil");
+ return 1;
+ }
+
+ if (unveil(config->file_name().characters(), "crw") < 0) {
+ perror("unveil");
+ return 1;
+ }
+
+ if (unveil(nullptr, nullptr) < 0) {
+ perror("unveil");
+ return 1;
+ }
+
+ auto app_icon = GUI::Icon::default_icon("app-snake");
+
+ auto window = GUI::Window::construct();
+
+ window->set_double_buffering_enabled(false);
+ window->set_title("Snake");
+ window->resize(320, 320);
+
+ auto& game = window->set_main_widget<SnakeGame>();
+
+ auto menubar = GUI::MenuBar::construct();
+
+ auto& app_menu = menubar->add_menu("Snake");
+
+ app_menu.add_action(GUI::Action::create("New game", { Mod_None, Key_F2 }, [&](auto&) {
+ game.reset();
+ }));
+ app_menu.add_separator();
+ app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
+ GUI::Application::the()->quit();
+ }));
+
+ auto& help_menu = menubar->add_menu("Help");
+ help_menu.add_action(GUI::CommonActions::make_about_action("Snake", app_icon, window));
+
+ app->set_menubar(move(menubar));
+
+ window->show();
+
+ window->set_icon(app_icon.bitmap_for_size(16));
+
+ return app->exec();
+}