blob: 103255cd5944737cb98c9e7be347cd471e9e7842 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
#pragma once
#include <games/Game.hpp>
#include <common/math.hpp>
#include <random>
class Snake : public Game {
public:
enum Direction : int32_t {
up,
down,
left,
right,
};
public:
Snake();
bool Update(std::vector<SDL_Event>& events) override;
private:
void ProcessEventDuringPause(SDL_Event& event);
void ProcessEventDuringResume(SDL_Event& event);
void Start(int32_t map_width, int32_t map_height);
void MaybeMoveSnake(float dt_in_seconds);
void SpawnFood();
void Draw();
private:
static constexpr int32_t max_map_width = 16;
static constexpr int32_t max_map_height = 16;
static constexpr float tiles_per_second = 4.0f;
static std::mt19937 s_rng;
std::uniform_int_distribution<int32_t> m_dist;
Direction m_direction;
Direction m_last_advanced_direction;
int32_t m_map_width;
int32_t m_map_height;
int32_t m_tail {0};
int32_t m_head {1};
uint64_t m_body_bitmap[max_map_height];
V2I32 m_body_positions[max_map_width * max_map_height];
V2I32 m_food_position;
};
|