#include "HaD_Badge.h" #include uint32_t tick = 150; uint32_t nextTick = 0; uint8_t started = 0; uint8_t gameOverFlag = 0; uint8_t snakeElementX[128]; uint8_t snakeElementY[128]; uint8_t snakeSize = 0; uint8_t direction; uint8_t newPositionX = 0; uint8_t newPositionY = 0; uint8_t eggX = 0; uint8_t eggY = 0; void newGame() { displayClear(); snakeSize = 3; snakeElementX[0] = 5; snakeElementX[1] = 5; snakeElementX[2] = 5; snakeElementY[0] = 11; snakeElementY[1] = 12; snakeElementY[2] = 13; for (uint8_t i = 0; i < snakeSize; i++) { displayPixel(snakeElementX[i], snakeElementY[i], ON); } eggX = 3; eggY = 7; displayPixel(eggX, eggY, ON); displayLatch(); nextTick = 0; started = 0; getControl(); } void gameOver() { gameOverFlag = 1; for (uint8_t i = 0; i < 15; i++) { for (uint8_t j = 0; j < snakeSize; j++) { displayPixel(snakeElementX[j], snakeElementY[j], i % 2); } displayLatch(); controlDelayMs(100); } gameOverFlag = 0; } void placeEgg() { //displayPixel(eggX, eggY, OFF); uint8_t tInt = 1; do { tInt = 0; srand ((int)getTime()); eggX = rand() % 7; eggY = rand() % 15; for (uint8_t i = 0; i < snakeSize; i++) { if (snakeElementX[i] == eggX && snakeElementY[i] == eggY) { tInt = 1; break; } } } while (tInt == 1); displayPixel(eggX, eggY, ON); displayLatch(); } void moveSnake() { if (gameOverFlag) { return; } if (newPositionX < 0 || newPositionX > 7 || newPositionY < 0 || newPositionY > 15) { gameOver(); newGame(); return; } for (uint8_t i = 3; i < snakeSize; i++) { if (snakeElementX[i] == newPositionX && snakeElementY[i] == newPositionY) { gameOver(); newGame(); return; } } if (newPositionX == eggX && newPositionY == eggY) { ++snakeSize; for (uint8_t i = snakeSize - 1; i > 0; i--) { snakeElementX[i] = snakeElementX[i - 1]; snakeElementY[i] = snakeElementY[i - 1]; } snakeElementX[0] = newPositionX; snakeElementY[0] = newPositionY; placeEgg(); } else { displayPixel(snakeElementX[snakeSize - 1], snakeElementY[snakeSize - 1], OFF); for (uint8_t i = snakeSize; i > 1; i--) { snakeElementX[i - 1] = snakeElementX[i - 2]; snakeElementY[i - 1] = snakeElementY[i - 2]; } snakeElementX[0] = newPositionX; snakeElementY[0] = newPositionY; displayPixel(snakeElementX[0], snakeElementY[0], ON); displayLatch(); } nextTick = getTime() + tick; } uint8_t checkDirection(uint8_t tDirection) { if (gameOverFlag) { return 0; } uint8_t tX = snakeElementX[0]; uint8_t tY = snakeElementY[0]; if (tDirection == UP) { --tY; } else if (tDirection == DOWN) { ++tY; } else if (tDirection == LEFT) { --tX; } else if (tDirection == RIGHT) { ++tX; } if (snakeElementX[1] != tX || snakeElementY[1] != tY) { newPositionX = tX; newPositionY = tY; direction = tDirection; return 1; } return 0; } void animateBadge(void) { newGame(); while(1) { switch (getControl()) { case (ESCAPE): displayClose(); return; case (UP): if (checkDirection(UP)) { started = 1; } break; case (DOWN): if (checkDirection(DOWN)) { started = 1; } break; case (LEFT): if (checkDirection(LEFT)) { started = 1; } break; case (RIGHT): if (checkDirection(RIGHT)) { started = 1; } break; } if (started && nextTick < getTime()) { nextTick = getTime() + tick; checkDirection(direction); moveSnake(); } } }