summaryrefslogtreecommitdiff
path: root/Games/Snake/SnakeGame.h
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-04-20 03:44:01 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-04-20 03:44:56 +0200
commitb41e95b57812ec498842410068ef11676aaa9a4d (patch)
treec2260626c915464e00c66849f237af60e21d79fd /Games/Snake/SnakeGame.h
parente24e4867142d1ae2a495060fa07629f941e8e4ee (diff)
downloadserenity-b41e95b57812ec498842410068ef11676aaa9a4d.zip
Snake: Use a queue for the movement inputs.
This makes it a lot less finicky to make rapid moves like staircasing and sudden turns.
Diffstat (limited to 'Games/Snake/SnakeGame.h')
-rw-r--r--Games/Snake/SnakeGame.h15
1 files changed, 11 insertions, 4 deletions
diff --git a/Games/Snake/SnakeGame.h b/Games/Snake/SnakeGame.h
index 2e8d04c00d..20c926ff70 100644
--- a/Games/Snake/SnakeGame.h
+++ b/Games/Snake/SnakeGame.h
@@ -1,6 +1,7 @@
#pragma once
#include <LibGUI/GWidget.h>
+#include <AK/CircularQueue.h>
class SnakeGame : public GWidget {
public:
@@ -24,18 +25,24 @@ private:
}
};
+ 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;
int m_rows { 20 };
int m_columns { 20 };
- int m_horizontal_velocity { 1 };
- int m_vertical_velocity { 0 };
+ Velocity m_velocity { 0, 1 };
+ Velocity m_last_velocity { 0, 1 };
- int m_last_horizontal_velocity { 1 };
- int m_last_vertical_velocity { 0 };
+ CircularQueue<Velocity, 10> m_velocity_queue;
Coordinate m_head;
Vector<Coordinate> m_tail;