summaryrefslogtreecommitdiff
path: root/Userland/Games/Snake/SnakeGame.h
diff options
context:
space:
mode:
Diffstat (limited to 'Userland/Games/Snake/SnakeGame.h')
-rw-r--r--Userland/Games/Snake/SnakeGame.h91
1 files changed, 91 insertions, 0 deletions
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;
+};