Effect snake is now a real snake game, with apples, dying and even a more or less smart AI.
This commit is contained in:
@ -3,23 +3,42 @@
|
||||
#include "Effect.h"
|
||||
#include "prototypes.h"
|
||||
|
||||
#define SNAKE_DIR_NORTH 0
|
||||
#define SNAKE_DIR_EAST 1
|
||||
#define SNAKE_DIR_SOUTH 2
|
||||
#define SNAKE_DIR_WEST 3
|
||||
|
||||
class SnakeEffect : public Effect {
|
||||
private:
|
||||
Coords coords;
|
||||
uint8_t direction = 1;
|
||||
uint8_t hue = 0;
|
||||
uint8_t run = 0;
|
||||
bool is_turn_needed();
|
||||
void turn_random();
|
||||
bool turn_right();
|
||||
bool turn_left();
|
||||
bool is_direction_okay(uint8_t direction);
|
||||
Coords _pos;
|
||||
Coords _apple;
|
||||
int8_t _dir = SNAKE_DIR_NORTH;
|
||||
uint8_t* _map;
|
||||
uint16_t _pixels;
|
||||
uint8_t _length;
|
||||
unsigned long _last_apple_at;
|
||||
unsigned long _last_move_at;
|
||||
// The following code is a handwritten "ai". Useful for testing and stuff.
|
||||
//int8_t _decisions[64] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,1,-1,1,-1,-1,-1,-1,0,0,0,0,-1,-1,0,0,0,1,0,0,-1,-1,0,0};
|
||||
int8_t _decisions[64] = {0, 1, 1, -1, 0, 1, 1, -1, 0, -1, 1, -1, -1, 0, 1, -1, -1, 0, 0, 1, -1, -1, 0, 0, -1, 0, 0, -1, 0, -1, 0, -1, -1, 0, 0, -1, -1, 0, 1, -1, -1, 1, 1, -1, -1, 1, -1, 0, 0, 1, 0, 1, -1, -1, 0, 0, 0, -1, 0, 1, 0, -1, 0, -1};
|
||||
//int8_t _decisions[64] = {1, 1, 0, 0, 1, 1, -1, -1, 0, -1, 0, -1, -1, 0, -1, 1, 1, -1, 0, 0, 1, -1, 0, 1, 0, -1, 0, -1, 0, 1, 1, 1, -1, 0, 0, 0, -1, -1, -1, 1, -1, -1, 1, 1, -1, 0, 1, 1, 1, 0, 0, -1, -1, 0, -1, 1, 0, 0, 0, -1, 0, -1, 1, 1};
|
||||
uint16_t _xy2i(uint8_t x, uint8_t y);
|
||||
uint16_t _xy2i(Coords c);
|
||||
Coords _i2xy(uint16_t i);
|
||||
Coords _new_pos(uint8_t dir);
|
||||
uint8_t _dying = 0;
|
||||
bool _is_free(uint8_t dir);
|
||||
bool _to_apple(uint8_t dir);
|
||||
void _place_apple();
|
||||
void _init();
|
||||
void _decide();
|
||||
int8_t _manual_decision();
|
||||
void _move();
|
||||
void _draw();
|
||||
public:
|
||||
SnakeEffect();
|
||||
~SnakeEffect();
|
||||
void loop(uint16_t ms);
|
||||
boolean valid_position(Coords c);
|
||||
Coords update_position(Coords c, uint8_t direction);
|
||||
boolean can_be_shown_with_clock();
|
||||
String get_name() override { return "snake"; }
|
||||
};
|
||||
|
Reference in New Issue
Block a user