pitrix/src/effect_snake.cpp

41 lines
1.3 KiB
C++
Raw Normal View History

2019-05-31 03:45:07 +00:00
#include "effect_snake.h"
#include "functions.h"
void SnakeEffect::loop() {
2019-05-31 21:58:58 +00:00
if (run++ % EFFECT_SNAKE_SLOWDOWN == 0) { // Change the coordinates only on every n-th run.
uint8_t r = random8(EFFECT_SNAKE_DIRECTION_CHANGE);
if (r==0 && valid_position(update_position(this->coords, direction+1))) {
2019-05-31 03:45:07 +00:00
direction++;
2019-05-31 21:58:58 +00:00
} else if (r==1 && valid_position(update_position(this->coords, direction-1))) {
2019-05-31 03:45:07 +00:00
direction--;
2019-05-31 21:58:58 +00:00
} 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--;
}
2019-05-31 03:45:07 +00:00
}
2019-05-31 21:58:58 +00:00
this->coords = update_position(this->coords, direction);
}
2019-05-31 03:45:07 +00:00
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; }