Fabrication industrielle
Internet des objets industriel | Matériaux industriels | Entretien et réparation d'équipement | Programmation industrielle |
home  MfgRobots >> Fabrication industrielle >  >> Manufacturing Technology >> Processus de fabrication

Créer un jeu Snake sur matrice LED 8x8 avec Arduino

Composants et fournitures

Composant Quantité
Shield proto Arduino 1
Arduino UNO 1
Joystick analogique (standard) 1
Fils de connexion Male/Female 1
Matrice LED 8x8 (monochrome) 1

Applications et services en ligne

Outil
Arduino IDE

À propos de ce projet

J'ai réalisé une console de jeu à partir d'un ensemble Meccano/Erector pour le châssis, en utilisant le code de Sergey Royz (allez voir son travail !) auquel j'ai ajouté des fonctionnalités et augmenté la vitesse pour un déplacement fluide. Je n'expliquerai pas la construction du châssis – créez le vôtre – mais un châssis est nécessaire. Amusez-vous bien ! Posez vos questions en commentaire, j'y répondrai au plus vite. Vos idées pour mon prochain projet sont aussi les bienvenues. Mon Fritzing ne fonctionnant pas, je décrirai le câblage textuellement :

Schéma de câblage

Enfichez le shield sur l'Arduino Uno pour disposer de plus de sorties GND et 5V sans breadboard.

Matrice LED

Joystick

Code

Snake (C/C++)

Copiez-collez simplement le code ci-dessous :

/**
 * Snake
 * Implémentation du jeu classique Snake
 * où le serpent se déplace constamment et le joueur
 * doit éviter les collisions avec lui-même et les bords.
 * Quand le serpent mange la nourriture, il grandit et accélère légèrement.
 *
 * 23 Juin 2016
 * par Sergey Royz et hunter Buzzell
 */
#include <LedControl.h>

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

/* Broches Affichage */
#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;
long 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);
    }
  }
}

/**
 * Fait avancer le serpent
 * retourne true si game over
 */
bool advance() {
  int head[2] = {snake[0][0] + v[0], snake[0][1] + v[1]};
  if (head[0] < 0 || head[0] >= SIZE) {
    delay(1000);
    showGameOverMessage();
    return true;
  }
  if (head[1] < 0 || head[1] >= SIZE) {
    delay(1000);
    showGameOverMessage();
    return true;
  }
  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,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0,0,1,1,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0},
  {0,0,0,0,0,0,0,0,1,1,0,1,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,1,1,0,0,1,1,0,0,1,1,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,0,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++) {
        // lit l'octet depuis PROGMEM et l'affiche
        lc.setLed(0, row, col, pgm_read_byte(&(gameOverMessage[row][col + d])));
      }
    }
  }
}

Processus de fabrication

  1. Manette de jeu USB Arduino DIY – Construisez votre propre gamepad haute performance
  2. Construire une console Pong 24×16 LED avec Arduino Nano et MAX7219
  3. Jeu Tetris sur Matrice LED 16×8 DIY avec Arduino Nano
  4. Contrôler une LED par Bluetooth avec Arduino et MIT App Inventor
  5. Affichage LED matrice interactif porte avec capteur PIR - Salutations fêtes Arduino
  6. Construire un robot quadrupède Arduino intelligent : guide étape par étape
  7. Pixel Chaser : Jeu LED interactif à une touche avec Arduino Nano
  8. Horloge à bascule Arduino avec matrice LED 8×8 – Projet DIY d'horloge temps réel
  9. Mélangeur de couleurs RVB Arduino – Projet DIY débutant