Industrielle Fertigung
Industrielles Internet der Dinge | Industrielle Materialien | Gerätewartung und Reparatur | Industrielle Programmierung |
home  MfgRobots >> Industrielle Fertigung >  >> Manufacturing Technology >> Herstellungsprozess

Arduino Snake Game auf 8×8 LED-Matrix

Komponenten und Zubehör

Komponente Menge
Arduino UNO 1
Arduino Proto Shield (optional, für mehr 5 V/GND-Pins) 1
8×8 LED-Matrix mit MAX7219 (z. B. FC-16 Modul) 1
Analoger Joystick (VRX, VRY, SW) 1
Jumper-Kabel (Male/Female) 1 Set

Software

Über das Projekt

Ein klassisches Snake-Spiel auf einer 8×8 LED-Matrix, gesteuert per Joystick. Basis-Code von Sergey Royz, erweitert um flüssigere Bewegung und Geschwindigkeitssteigerung. Ein eigener Rahmen (z. B. aus Meccano/Erector) wird empfohlen, wird hier aber nicht detailliert.

Verdrahtung

LED-Matrix (MAX7219)

Joystick

Code (Snake.ino)

/**
 * Snake – Klassisches Snake-Spiel auf 8x8 Matrix
 * 23 Jun 2016 – Sergey Royz & hunter Buzzell
 */
#include <LedControl.h>

/* Joystick Pins */
#define VRX A0
#define VRY A1
#define SW  2

/* Display Pins */
#define CLK 8
#define CS  9
#define DIN 10
#define SIZE 8
#define ADVANCE_DELAY 20

int snake[SIZE*SIZE][2];
int length;
int food[2], v[2];
bool is_game_over = false;
long current_time, prev_advance;
int blink_count;
const short messageSpeed = 5;

LedControl lc = LedControl(DIN, CLK, CS, 1);

void init_game() {
  prev_advance = current_time = 0;
  blink_count = 3;
  int half = SIZE / 2;
  length = SIZE / 3;
  for (int i = 0; i < length; i++) {
    snake[i][0] = half - 1;
    snake[i][1] = half + i;
  }
  food[0] = half + 1;
  food[1] = half - 1;
  v[0] = 0; v[1] = -1;
}

void render() {
  for (int i = 0; i < length; i++)
    lc.setLed(0, snake[i][0], snake[i][1], 1);
  lc.setLed(0, food[0], food[1], 1);
}

void clearScreen() {
  for (int x = 0; x < SIZE; x++)
    for (int y = 0; y < SIZE; y++)
      lc.setLed(0, x, y, 0);
}

/* Bewegt Snake um einen Schritt; return true = Game Over */
bool advance() {
  int head[2] = {snake[0][0] + v[0], snake[0][1] + v[1]};

  // Wandkollision
  if (head[0] < 0 || head[0] >= SIZE || head[1] < 0 || head[1] >= SIZE) {
    delay(1000); showGameOverMessage(); return true;
  }
  // Selbstkollision
  for (int i = 0; i < length; i++)
    if (snake[i][0] == head[0] && snake[i][1] == head[1]) {
      delay(1000); showGameOverMessage(); return true;
    }

  bool grow = (head[0] == food[0] && head[1] == food[1]);
  if (grow) {
    length++;
    randomSeed(current_time);
    food[0] = random(SIZE);
    food[1] = random(SIZE);
  }
  for (int i = length - 1; i >= 0; i--) {
    snake[i + 1][0] = snake[i][0];
    snake[i + 1][1] = snake[i][1];
  }
  snake[0][0] += v[0];
  snake[0][1] += v[1];
  return false;
}

void setup() {
  pinMode(SW, INPUT_PULLUP);
  pinMode(VRX, INPUT);
  pinMode(VRY, INPUT);
  attachInterrupt(digitalPinToInterrupt(SW), restart, RISING);
  lc.shutdown(0, false);
  lc.setIntensity(0, 8);
  init_game();
  render();
}

void loop() {
  if (!is_game_over) {
    clearScreen();
    render();
    if (current_time - prev_advance > ADVANCE_DELAY) {
      is_game_over = advance();
      prev_advance = current_time;
    }
  } else {
    while (blink_count > 0) {
      clearScreen(); delay(300);
      render(); delay(300);
      blink_count--;
    }
  }
  readControls();
  current_time++;
}

void restart() { init_game(); is_game_over = false; }

void readControls() {
  int dx = map(analogRead(VRX), 0, 906, 2, -2);
  int dy = map(analogRead(VRY), 0, 906, -2, 2);
  if (dx != 0) dx = dx / abs(dx);
  if (dy != 0) dy = dy / abs(dy);
  if (dy != 0 && v[0] != 0) { v[0] = 0; v[1] = dy; }
  if (dx != 0 && v[1] != 0) { v[0] = dx; v[1] = 0; }
}

/* "GAME OVER" Scroll-Text im PROGMEM */
const PROGMEM bool gameOverMessage[8][90] = {
  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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0,0,0,0,0,0,0,0},
  {0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,1,1,1,1,0,0,0,1,1,0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,1,1,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,0,0,0,0,1,1,1,1,1,1,0,0,1,1,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0}
};

void showGameOverMessage() {
  for (int d = 0; d < sizeof(gameOverMessage[0]) - 7; d++) {
    for (int col = 0; col < 8; col++) {
      delay(messageSpeed);
      for (int row = 0; row < 8; row++) {
        lc.setLed(0, row, col, pgm_read_byte(&(gameOverMessage[row][col + d])));
      }
    }
  }
}

Hinweise

Herstellungsprozess

  1. DIY Arduino USB Gaming Controller – Bauen Sie Ihren eigenen High-Performance Gamepad
  2. 24×16 LED Pong-Konsole mit Arduino Nano & MAX7219 bauen
  3. Tetris-Spiel mit Arduino Nano auf selbstgebauter 16×8 LED-Matrix
  4. LED per Bluetooth mit Arduino steuern – Einfache DIY-Anleitung
  5. Interaktive LED-Matrix Tür-Anzeige mit PIR-Bewegungssensor – Arduino Feiertagsgruß
  6. Intelligenten Arduino-Vierbeiner-Roboter bauen: Schritt-für-Schritt-Anleitung
  7. Pixel Chaser: Interaktives One-Tap-LED-Spiel mit Arduino Nano
  8. Arduino Flip Clock mit 8×8 LED-Matrix – DIY Echtzeituhr-Projekt
  9. Arduino RGB-LED-Farbmischer – Einsteigerfreundliches DIY-Projekt
  10. 8×8 LED-Matrix mit Arduino Uno meistern: Schritt-für-Schritt-Anleitung