Created
February 3, 2017 19:58
-
-
Save alexesDev/b852e90a99d933cdb6a85b35eda0bf56 to your computer and use it in GitHub Desktop.
arduino shake
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "LedControl.h" | |
| #define FIELD_SIZE 8 | |
| #define MAX_LENGTH 16 | |
| LedControl lc(12, 10, 11, 1); | |
| typedef struct { | |
| byte x, y; | |
| } Position; | |
| typedef struct { | |
| Position pos; | |
| byte direction; // 0 - up, 1 - right, 2 - bottom, 3 - left | |
| } Head; | |
| Position body[MAX_LENGTH]; | |
| byte bodyLength = 1; | |
| byte nextBodyIndex = 0; | |
| Head head = { 1, 1, 0 }; | |
| bool gameOver = false; | |
| bool appleVisible = false; | |
| bool appleDeadTicks = 0; | |
| Position applePos; | |
| inline void shift(byte *value, byte max, byte init, byte direction) { | |
| *value = *value == max ? init : *value + direction; | |
| } | |
| void renderGameOver() { | |
| int limit = FIELD_SIZE - 1; | |
| for (byte i = 1; i < limit; ++i) { | |
| lc.setLed(0, i, i, true); | |
| lc.setLed(0, i, limit - i, true); | |
| } | |
| delay(500); | |
| lc.clearDisplay(0); | |
| delay(500); | |
| } | |
| inline byte getBodyIndex(byte i) { | |
| return i <= nextBodyIndex ? nextBodyIndex - i : MAX_LENGTH + nextBodyIndex - i; | |
| } | |
| #define IS_EQUAL(a, b) (a.x == b.x && a.y == b.y) | |
| bool shakeCollided() { | |
| for (int i = 0; i < bodyLength; ++i) { | |
| auto v = getBodyIndex(i); | |
| if (IS_EQUAL(head.pos, body[v])) { | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| bool appleEaten() { | |
| return IS_EQUAL(applePos, head.pos); | |
| } | |
| void generateApple() { | |
| appleVisible = true; | |
| do { | |
| applePos.x = random(0, 7); | |
| applePos.y = random(0, 7); | |
| } while(IS_EQUAL(applePos, head.pos)); | |
| } | |
| void setup() { | |
| lc.shutdown(0, false); | |
| lc.setIntensity(0, 5); | |
| lc.clearDisplay(0); | |
| Serial.begin(9200); | |
| generateApple(); | |
| } | |
| void loop() { | |
| if (gameOver) { | |
| lc.clearDisplay(0); | |
| renderGameOver(); | |
| return; | |
| } | |
| auto xAxis = analogRead(A0); | |
| auto yAxis = analogRead(A1); | |
| if (xAxis < 400) { | |
| if (head.direction != 3) head.direction = 1; | |
| } else if (xAxis > 600) { | |
| if (head.direction != 1) head.direction = 3; | |
| } else if (yAxis < 400) { | |
| if (head.direction != 2) head.direction = 0; | |
| } else if (yAxis > 600) { | |
| if (head.direction != 0) head.direction = 2; | |
| } | |
| nextBodyIndex = (nextBodyIndex + 1) % MAX_LENGTH; | |
| body[nextBodyIndex] = head.pos; | |
| if (head.direction == 0) { | |
| shift(&head.pos.y, 7, 0, 1); | |
| } else if (head.direction == 2) { | |
| shift(&head.pos.y, 0, 7, -1); | |
| } else if (head.direction == 1) { | |
| shift(&head.pos.x, 7, 0, 1); | |
| } else if (head.direction == 3) { | |
| shift(&head.pos.x, 0, 7, -1); | |
| } | |
| if (shakeCollided()) { | |
| gameOver = true; | |
| return; | |
| } | |
| if (appleEaten()) { | |
| appleVisible = false; | |
| appleDeadTicks = 3; | |
| bodyLength += 1; | |
| } | |
| if (!appleVisible) { | |
| if (appleDeadTicks > 0) { | |
| appleDeadTicks -= 1; | |
| } else { | |
| generateApple(); | |
| } | |
| } | |
| lc.clearDisplay(0); | |
| lc.setLed(0, head.pos.x, head.pos.y, true); | |
| if (appleVisible) { | |
| lc.setLed(0, applePos.x, applePos.y, true); | |
| } | |
| for (int i = 0; i < bodyLength; ++i) { | |
| int v = getBodyIndex(i); | |
| lc.setLed(0, body[v].x, body[v].y, true); | |
| } | |
| delay(100); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment