Files
wordclock/include/snake.h

94 lines
1.8 KiB
C++

/**
* @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 "led_matrix.h"
#include "udp_logger.h"
#ifdef DEBOUNCE_TIME
#undef DEBOUNCE_TIME
#endif
#define DEBOUNCE_TIME 300 // in ms
#define X_MAX 11
#define Y_MAX 11
#ifdef GAME_DELAY
#undef GAME_DELAY
#endif
#define GAME_DELAY 400 // in ms
#define LED_TYPE_SNAKE 1
#ifdef LED_TYPE_OFF
#undef LED_TYPE_OFF
#endif
#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 * matrix, UDPLogger * logger);
void loopCycle();
void initGame();
void ctrlUp();
void ctrlDown();
void ctrlLeft();
void ctrlRight();
private:
LEDMatrix * _ledmatrix;
UDPLogger * _logger;
uint8_t _userDirection = 0;
uint8_t _gameState = 0;
Coords _head = {0, 0};
Coords _tail[MAX_TAIL_LENGTH] = {0};
Coords _food = {0, 0};
unsigned long _lastDrawUpdate = 0;
unsigned long _lastButtonClick = 0;
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