// Install from the Library Manager: // "LedControl" (Eberhard Fahle) #include // The MAX7219 module's DIN, CLK, CS pins const int DIN_PIN = 12; const int CLK_PIN = 11; const int CS_PIN = 10; // Joystick: two axes and its push button const int JOY_X = A0; const int JOY_Y = A1; const int JOY_BUTTON = 2; const int BUZZER_PIN = 8; // If the snake goes the wrong way, change // these: they flip the screen sideways or // upside down, and swap the axes const bool FLIP_X = false; const bool FLIP_Y = false; const bool SWAP_AXES = false; // One step's time (ms): a little shorter // after each food, MIN_DELAY at least const int START_DELAY = 350; const int MIN_DELAY = 120; // 3x5 digits for the score, row by row const byte DIGITS[10][5] = { {7, 5, 5, 5, 7}, {2, 6, 2, 2, 7}, {7, 1, 7, 4, 7}, {7, 1, 3, 1, 7}, {5, 5, 7, 1, 1}, {7, 4, 7, 1, 7}, {7, 4, 7, 5, 7}, {7, 1, 1, 2, 2}, {7, 5, 7, 5, 7}, {7, 5, 7, 1, 7} }; LedControl matrix(DIN_PIN, CLK_PIN, CS_PIN, 1); // The snake's cells, head to tail byte snakeX[64]; byte snakeY[64]; int length = 3; // Heading (-1, 0, 1) and the next heading int dx = 1, dy = 0; int nextDx = 1, nextDy = 0; byte foodX = 0, foodY = 0; int score = 0; int stepDelay = START_DELAY; bool playing = false; unsigned long lastStep = 0; // One LED on/off (x rightwards, y down) void plot(int x, int y, bool on) { int col = FLIP_X ? 7 - x : x; int row = FLIP_Y ? 7 - y : y; matrix.setLed(0, row, col, on); } bool isSnake(int x, int y, int count) { for (int i = 0; i < count; i++) { if (snakeX[i] == x && snakeY[i] == y) return true; } return false; } // Food goes to a random free spot void placeFood() { do { foodX = random(8); foodY = random(8); } while (isSnake(foodX, foodY, length)); } void draw() { matrix.clearDisplay(0); for (int i = 0; i < length; i++) { plot(snakeX[i], snakeY[i], true); } } void newGame() { length = 3; for (int i = 0; i < length; i++) { snakeX[i] = 3 - i; snakeY[i] = 4; } dx = nextDx = 1; dy = nextDy = 0; score = 0; stepDelay = START_DELAY; placeFood(); draw(); playing = true; lastStep = millis(); Serial.println("New game started."); } // Show the score in two-digit numbers void showScore() { matrix.clearDisplay(0); int digits[2] = {score / 10 % 10, score % 10}; for (int d = 0; d < 2; d++) { if (d == 0 && score < 10) continue; // One digit centred, two side by side int left = score < 10 ? 2 : 1 + d * 4; for (int r = 0; r < 5; r++) { for (int c = 0; c < 3; c++) { bool on = DIGITS[digits[d]][r] & (4 >> c); plot(left + c, r + 1, on); } } } } void gameOver() { playing = false; Serial.print("Game over! Score: "); Serial.println(score); int notes[3] = {440, 330, 220}; for (int i = 0; i < 3; i++) { tone(BUZZER_PIN, notes[i], 200); delay(250); } showScore(); } // Read the joystick; no turning back void readJoystick() { int x = analogRead(JOY_X); int y = analogRead(JOY_Y); if (SWAP_AXES) { int t = x; x = y; y = t; } int jx = 0, jy = 0; if (x < 300) jx = -1; if (x > 700) jx = 1; if (y < 300) jy = -1; if (y > 700) jy = 1; // Pushed diagonally: sideways wins if (jx != 0) jy = 0; if (jx == 0 && jy == 0) return; if (jx == -dx && jy == -dy) return; nextDx = jx; nextDy = jy; } void step() { dx = nextDx; dy = nextDy; // Off one edge, in at the other int x = (snakeX[0] + dx + 8) % 8; int y = (snakeY[0] + dy + 8) % 8; bool ate = (x == foodX && y == foodY); // On food the tail stays put if (isSnake(x, y, ate ? length : length - 1)) { gameOver(); return; } if (ate) length++; for (int i = length - 1; i > 0; i--) { snakeX[i] = snakeX[i - 1]; snakeY[i] = snakeY[i - 1]; } snakeX[0] = x; snakeY[0] = y; if (ate) { score++; tone(BUZZER_PIN, 1800, 60); stepDelay = max(MIN_DELAY, stepDelay - 15); Serial.print("Food eaten, score: "); Serial.println(score); if (length == 64) { gameOver(); return; } placeFood(); } draw(); } void setup() { Serial.begin(9600); pinMode(JOY_BUTTON, INPUT_PULLUP); // Noise on the unconnected A5 puts food // in different places every game randomSeed(analogRead(A5)); matrix.shutdown(0, false); matrix.setIntensity(0, 4); newGame(); } void loop() { if (!playing) { // Score shown; the button restarts if (digitalRead(JOY_BUTTON) == LOW) newGame(); return; } readJoystick(); if (millis() - lastStep >= (unsigned long)stepDelay) { lastStep = millis(); step(); } // Food blinks, so it stands apart if (playing) plot(foodX, foodY, millis() / 150 % 2); }