#include #include #include int select = 2; int left = 3; int down = 4; int up = 5; int right = 7; int x = 0, y = 0; int minCoord = 0, maxCoord = 7; int bombMatrix[8][8] = {0}; int numberOfBombs = 7; static struct { int x; int y; } bombs[7]; int lost = 0, tryAgain = 0, goodbye = 0, newGame = 0, finished = 0, won = 0; int z; #define PIN 6 Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(8, 8, PIN, NEO_MATRIX_TOP + NEO_MATRIX_RIGHT + NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG, NEO_GRB + NEO_KHZ800); void initMatrix() { srand(time(NULL)); for (int i = 0 ; i <= numberOfBombs; ++i) { int ok = 1; int bombX = (rand() % (maxCoord - minCoord + 1)); int bombY = (rand() % (maxCoord - minCoord + 1)); bombMatrix[bombX][bombY] = 2; if (bombX - 1 >= minCoord) { if (bombMatrix[bombX - 1][bombY] != 2) bombMatrix[bombX - 1][bombY] = 1; if (bombY - 1 >= minCoord && bombMatrix[bombX - 1][bombY - 1] != 2) bombMatrix[bombX - 1][bombY - 1] = 1; if (bombY + 1 <= maxCoord && bombMatrix[bombX - 1][bombY + 1] != 2) bombMatrix[bombX - 1][bombY + 1] = 1; } if (bombX + 1 <= maxCoord) { if (bombMatrix[bombX + 1][bombY] != 2) bombMatrix[bombX + 1][bombY] = 1; if (bombY - 1 >= minCoord && bombMatrix[bombX + 1][bombY - 1] != 2) bombMatrix[bombX + 1][bombY - 1] = 1; if (bombY + 1 <= maxCoord && bombMatrix[bombX + 1][bombY + 1] != 2) bombMatrix[bombX + 1][bombY + 1] = 1; } if (bombY - 1 >= minCoord && bombMatrix[bombX][bombY - 1] != 2) bombMatrix[bombX][bombY - 1] = 1; if (bombY + 1 <= maxCoord && bombMatrix[bombX][bombY + 1] != 2) bombMatrix[bombX][bombY + 1] = 1; } } void setup() { x = 0; y = 0; bombMatrix[8][8] = {0}; numberOfBombs = 7; lost = 0; tryAgain = 0; goodbye = 0; newGame = 0; finished = 0; Serial.begin(9600); matrix.begin(); matrix.setBrightness(50); pinMode(select, INPUT_PULLUP); pinMode(up, INPUT_PULLUP); pinMode(down, INPUT_PULLUP); pinMode(left, INPUT_PULLUP); pinMode(right, INPUT_PULLUP); z = matrix.width(); matrix.setCursor(0, 0); matrix.setTextWrap(false); initMatrix(); Serial.println(); Serial.println(); for (int i = 0; i <= maxCoord; ++i) { for (int j = 0; j <= maxCoord; ++j) { Serial.print(bombMatrix[i][j]); Serial.print(" "); } Serial.println(); } } void printDiscoveredPixel() { if (bombMatrix[x][y] == 10) { matrix.drawPixel(x, y, matrix.Color(0, 255, 0)); } else if (bombMatrix[x][y] == 11) { matrix.drawPixel(x, y, matrix.Color(255, 255, 0)); } else if (bombMatrix[x][y] == 12) { matrix.drawPixel(x, y, matrix.Color(255, 0, 0)); } matrix.show(); delay(100); } void move(int selectState, int upState, int downState, int leftState, int rightState) { if (!selectState) { bombMatrix[x][y] += 10; printDiscoveredPixel(); delay(100); if (x == 2 && y == 4 && tryAgain == 1) { goodbye = 1; } if (x == 5 && y == 4 && tryAgain == 1) { newGame = 1; } } if (!upState) { --y; delay(100); } if (!downState) { ++y; delay(100); } if (!leftState) { --x; delay(100); } if (!rightState) { ++x; delay(100); } } void beginLosingAnimation() { for (int i = 0; i <= maxCoord; ++i) { for(int j = 0; j <= maxCoord; ++j) { matrix.drawPixel(i, j, matrix.Color(255, 0, 0)); matrix.show(); bombMatrix[i][j] = 22; delay(25); } delay(25); } lost = 1; } void drawPixels() { for (int i = 0 ; i <= maxCoord; ++i) { for (int j = 0; j <= maxCoord; ++j) { if (bombMatrix[i][j] == 10) { matrix.drawPixel(i, j, matrix.Color(0, 255, 0)); } else if (bombMatrix[i][j] == 11) { matrix.drawPixel(i, j, matrix.Color(255, 255, 0)); } else if (bombMatrix[i][j] == 12) { matrix.drawPixel(i, j, matrix.Color(255, 0, 0)); delay(100); matrix.drawPixel(i, j, matrix.Color(0, 0, 0)); delay(100); matrix.drawPixel(i, j, matrix.Color(255, 0, 0)); delay(100); matrix.drawPixel(i, j, matrix.Color(0, 0, 0)); delay(100); beginLosingAnimation(); } } } } void tryAgainAnimation() { matrix.fillScreen(0); matrix.setCursor(0, 0); matrix.drawPixel(2, 4, matrix.Color(255, 0, 0)); matrix.drawPixel(5, 4, matrix.Color(0, 255, 0)); matrix.setCursor(0, 0); x = 0; y = 0; for (int i = 0 ; i <= maxCoord; ++i) { for (int j = 0 ; j <= maxCoord; ++j) { bombMatrix[i][j] = 0; } } tryAgain = 1; } void beginWinningAnimation() { for (int i = 0; i <= maxCoord; ++i) { for(int j = 0; j <= maxCoord; ++j) { matrix.drawPixel(i, j, matrix.Color(0, 255, 0)); matrix.show(); bombMatrix[i][j] = 21; delay(25); } delay(25); } } void checkForWin() { int overturned = 0; for (int i = 0; i <= maxCoord; ++i) { for (int j = 0; j <= maxCoord; ++j) { if (bombMatrix[i][j] >= 10) { ++overturned; } } } if (overturned == 64 - 7) { won = 1; beginWinningAnimation(); } } void loop() { matrix.fillScreen(0); if (newGame == 1) { matrix.setCursor(z, 0); matrix.setTextColor(matrix.Color(0, 0, 255)); matrix.print(F("New game!")); if (--z < -50) { setup(); } matrix.show(); delay(100); } else if (goodbye == 1) { matrix.setCursor(z, 0); matrix.setTextColor(matrix.Color(0, 0, 255)); matrix.print(F("Goodbye!")); if (--z < -50) { matrix.fillScreen(0); } matrix.show(); delay(100); } else if (tryAgain == 1) { matrix.drawPixel(x, y, matrix.Color(255, 255, 255)); matrix.drawPixel(2, 4, matrix.Color(255, 0, 0)); matrix.drawPixel(5, 4, matrix.Color(0, 255, 0)); matrix.show(); int selectState = digitalRead(select); int upState = digitalRead(up); int downState = digitalRead(down); int leftState = digitalRead(left); int rightState = digitalRead(right); move(selectState, upState, downState, leftState, rightState); delay(100); } else if (won == 1) { matrix.setCursor(z, 0); matrix.setTextColor(matrix.Color(0, 255, 0)); matrix.print(F("You won! Try again?")); if (--z < -120) { z = matrix.width(); tryAgainAnimation(); } matrix.show(); delay(100); } else if (lost == 1) { matrix.setCursor(z, 0); matrix.setTextColor(matrix.Color(255, 0, 0)); matrix.print(F("You lost! Try again?")); if (--z < -120) { z = matrix.width(); tryAgainAnimation(); } matrix.show(); delay(100); } else { drawPixels(); matrix.drawPixel(x, y, matrix.Color(255, 255, 255)); matrix.show(); int selectState = digitalRead(select); int upState = digitalRead(up); int downState = digitalRead(down); int leftState = digitalRead(left); int rightState = digitalRead(right); move(selectState, upState, downState, leftState, rightState); checkForWin(); delay(100); } }