-
Notifications
You must be signed in to change notification settings - Fork 2
/
snake.h
84 lines (69 loc) · 1.72 KB
/
snake.h
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* @file snake.h
* @author techniccontroller (mail[at]techniccontroller.com)
* @brief Class declaration of snake game
* @version 0.1
* @date 2022-03-05
*
* @copyright Copyright (c) 2022
*
* main code from https://elektro.turanis.de/html/prj099/index.html
*
*/
#ifndef snake_h
#define snake_h
#include <Arduino.h>
#include "ledmatrix.h"
#include "udplogger.h"
#define DEBOUNCE_TIME 300 // in ms
#define X_MAX 11
#define Y_MAX 11
#define GAME_DELAY 400 // in ms
#define LED_TYPE_SNAKE 1
#define LED_TYPE_OFF 2
#define LED_TYPE_FOOD 3
#define LED_TYPE_BLOOD 4
#define DIRECTION_NONE 0
#define DIRECTION_UP 1
#define DIRECTION_DOWN 2
#define DIRECTION_LEFT 3
#define DIRECTION_RIGHT 4
#define GAME_STATE_RUNNING 1
#define GAME_STATE_END 2
#define GAME_STATE_INIT 3
#define MAX_TAIL_LENGTH X_MAX * Y_MAX
#define MIN_TAIL_LENGTH 3
class Snake{
struct Coords {
int x;
int y;
};
public:
Snake();
Snake(LEDMatrix *myledmatrix, UDPLogger *mylogger);
void loopCycle();
void initGame();
void ctrlUp();
void ctrlDown();
void ctrlLeft();
void ctrlRight();
private:
LEDMatrix *_ledmatrix;
UDPLogger *_logger;
uint8_t _userDirection;
uint8_t _gameState;
Coords _head;
Coords _tail[MAX_TAIL_LENGTH];
Coords _food;
unsigned long _lastDrawUpdate = 0;
unsigned long _lastButtonClick;
unsigned int _wormLength = 0;
void resetLEDs();
void updateGame();
void endGame();
void updateTail();
void updateFood();
bool isCollision();
void toggleLed(uint8_t x, uint8_t y, uint8_t type);
};
#endif