41 lines
1.2 KiB
C++
41 lines
1.2 KiB
C++
|
#include "effect_snake.h"
|
||
|
#include "functions.h"
|
||
|
|
||
|
void SnakeEffect::loop() {
|
||
|
if (run++ % EFFECT_SNAKE_SLOWDOWN) return;
|
||
|
|
||
|
uint8_t r = random8(EFFECT_SNAKE_DIRECTION_CHANGE);
|
||
|
if (r==0 && valid_position(update_position(this->coords, direction+1))) {
|
||
|
direction++;
|
||
|
} else if (r==1 && valid_position(update_position(this->coords, direction-1))) {
|
||
|
direction--;
|
||
|
} else if (!valid_position(update_position(this->coords, direction))) {
|
||
|
if (valid_position(update_position(this->coords, direction+1))) {
|
||
|
direction++;
|
||
|
} else if (valid_position(update_position(this->coords, direction-1))) {
|
||
|
direction--;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
this->coords = update_position(this->coords, direction);
|
||
|
|
||
|
fadeToBlackBy(leds, LED_COUNT, 2);
|
||
|
setPixel(window, this->coords.x, this->coords.y, CHSV(hue, 200, 255));
|
||
|
hue++;
|
||
|
}
|
||
|
|
||
|
Coords SnakeEffect::update_position(Coords original, uint8_t direction) {
|
||
|
direction = direction % 4;
|
||
|
if (direction == 0) original.y--;
|
||
|
else if (direction == 1) original.x++;
|
||
|
else if (direction == 2) original.y++;
|
||
|
else if (direction == 3) original.x--;
|
||
|
return original;
|
||
|
}
|
||
|
|
||
|
boolean SnakeEffect::valid_position(Coords c) {
|
||
|
return c.x<window.w && c.y<window.h;
|
||
|
}
|
||
|
|
||
|
boolean SnakeEffect::can_be_shown_with_clock() { return true; }
|