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 Asteroids classique avec Arduino Mega et matrice LED MAX72XX

Composants et fournitures

Composant Quantité
Arduino Mega 2560 1
Shield pilote afficheur MAX7219 (MAXREFDES99#) 1
Boutons poussoirs 12 mm (SparkFun) 2
Buzzer actif 1
Plaque d'essai (breadboard) 1

À propos de ce projet

Jeu simple utilisant une matrice LED MAX72XX (8×8) comme écran, deux micro-interrupteurs pour les commandes et un buzzer actif pour les effets sonores.

Règle : Vous pilotez un vaisseau spatial (un pixel en bas) et devez éviter les astéroïdes qui tombent. Le jeu accélère progressivement. À la fin, le score (temps de survie) s'affiche.

Code Arduino complet

Bibliothèque requise : LedControl.h (Eberhard Fahle – GitHub)

/*********************************************************
 * Jeu Asteroids sur matrice LED MAX72XX
 * (c) 2018 Joshua Weßner
 *********************************************************/
#include "LedControl.h"

// Brochage : DIN=12, CLK=11, LOAD=10, 1 seul MAX72XX
LedControl lc = LedControl(12, 11, 10, 1);

// Variables globales
unsigned long delaytime = 1000;
String scoreString; int score; int hundreds;
String scoreStr; String scoreArr[3] = {"", "", ""};
int pinLeft = 2, pinRight = 3;
volatile unsigned long buttonPressed = 0;
int buttonDelay = 150; // anti-rebond
volatile bool gameOver = false;
int tick = 300, tickCounter = 1;
unsigned long now = 0;
int ship = 3; // Position initiale vaisseau (colonne 3)
int columns[8] = {0}; // 8 colonnes, valeur = ligne astéroïde (0=aucun)
int randomInt;

void setup() {
  gameOver = false; hundreds = 0; score = 0; tick = 300; tickCounter = 1; ship = 3; now = millis(); buttonPressed = millis();
  randomSeed(analogRead(15));
  for (int i = 0; i < 8; i++) columns[i] = 0;

  lc.shutdown(0, false); // Sortie veille
  lc.setIntensity(0, 1); // Luminosité moyenne
  lc.clearDisplay(0);

  pinMode(pinLeft, INPUT_PULLUP);
  pinMode(pinRight, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(pinLeft), left, FALLING);
  attachInterrupt(digitalPinToInterrupt(pinRight), right, FALLING);
}

// Interruption bouton gauche
void left() {
  if (millis() - buttonPressed > buttonDelay) {
    if (ship != 0) ship--; else ship = 7;
    lc.clearDisplay(0);
    buttonPressed = millis();
  }
  if (gameOver) { gameOver = false; setup(); }
}

// Interruption bouton droit
void right() {
  if (millis() - buttonPressed > buttonDelay) {
    if (ship != 7) ship++; else ship = 0;
    lc.clearDisplay(0);
    buttonPressed = millis();
  }
  if (gameOver) { gameOver = false; setup(); }
}

void loop() {
  if (millis() - now > tick) {
    now = millis();
    score++; // Score = nombre de ticks survécus

    if (tickCounter == 1) { // Tous les 4 ticks
      tick = tick / 1.02; // Accélération progressive
      randomInt = random(0, 8);
      if (columns[randomInt] == 0) columns[randomInt] = 1; // Nouvel astéroïde en haut
    }
    tickCounter = (tickCounter == 4) ? 1 : tickCounter + 1;

    // Faire tomber les astéroïdes
    for (int i = 0; i < 8; i++) {
      if (columns[i] == 10) columns[i] = 0; // Sorti de l'écran
      if (columns[i] != 0) columns[i]++;
    }
    lc.clearDisplay(0);
  }

  // Affichage vaisseau (ligne 7 = bas)
  lc.setLed(0, 7, ship, true);

  // Affichage astéroïdes (2 LEDs de haut pour meilleure visibilité)
  for (int i = 0; i < 8; i++) {
    if (columns[i] > 0) {
      lc.setLed(0, columns[i] - 2, i, true);
      lc.setLed(0, columns[i] - 3, i, true);
    }
  }

  // Détection collision
  if (columns[ship] == 9 || columns[ship] == 10) {
    lc.clearDisplay(0);
    // Animation explosion
    for (int i = 0; i < 4; i++) {
      lc.setLed(0, 7, ship + i, true);
      lc.setLed(0, 7, ship - i, true);
      lc.setLed(0, 7 - i, ship + i, true);
      lc.setLed(0, 7 - i, ship - i, true);
      lc.setLed(0, 7 - 1.5 * i, ship, true);
      // Son explosion
      unsigned long time = millis();
      int randomSound = 1000;
      while (millis() - time <= 250) {
        randomSound--;
        tone(9, random(randomSound, 1000));
      }
      lc.clearDisplay(0);
      noTone(9);
    }
    delay(500);

    // Affichage score sur matrice (max 2 chiffres = 99)
    scoreStr = String(score);
    scoreArr[0] = scoreStr.charAt(0);
    scoreArr[1] = scoreStr.charAt(1);
    scoreArr[2] = scoreStr.charAt(2);

    if (score < 100) {
      for (int i = 0; i < 2; i++) {
        drawDigit(scoreArr[i], 1 + i * 4);
      }
    } else {
      // Afficher 2 derniers chiffres + centaines en points (ligne 0-1)
      for (int i = 1; i < 3; i++) {
        drawDigit(scoreArr[i], 1 + (i - 1) * 4);
      }
      for (int i = 1; i <= 9; i++) {
        if (scoreArr[0] == String(i)) hundreds = i;
      }
      for (int i = 1; i <= hundreds; i++) {
        lc.setLed(0, 0, i - 1, true);
        lc.setLed(0, 1, i - 1, true);
        delay(200);
      }
    }
    gameOver = true;
    while (gameOver) { /* Attente appui bouton pour redémarrer */ }
  }
}

// Dessin chiffres 0-9 sur 3 colonnes (position = colonne de départ)
void drawDigit(String d, int pos) {
  if (d == "0") draw0(pos);
  else if (d == "1") draw1(pos);
  else if (d == "2") draw2(pos);
  else if (d == "3") draw3(pos);
  else if (d == "4") draw4(pos);
  else if (d == "5") draw5(pos);
  else if (d == "6") draw6(pos);
  else if (d == "7") draw7(pos);
  else if (d == "8") draw8(pos);
  else if (d == "9") draw9(pos);
}

void draw0(int p) { lc.setColumn(0, 0+p, B00011111); lc.setColumn(0, 1+p, B00010001); lc.setColumn(0, 2+p, B00011111); }
void draw1(int p) { lc.setColumn(0, 0+p, B00001000); lc.setColumn(0, 1+p, B00011111); }
void draw2(int p) { lc.setColumn(0, 0+p, B00010111); lc.setColumn(0, 1+p, B00010101); lc.setColumn(0, 2+p, B00011101); }
void draw3(int p) { lc.setColumn(0, 0+p, B00010001); lc.setColumn(0, 1+p, B00010101); lc.setColumn(0, 2+p, B00011111); }
void draw4(int p) { lc.setColumn(0, 0+p, B00011100); lc.setColumn(0, 1+p, B00000100); lc.setColumn(0, 2+p, B00011111); }
void draw5(int p) { lc.setColumn(0, 0+p, B00011101); lc.setColumn(0, 1+p, B00010101); lc.setColumn(0, 2+p, B00010111); }
void draw6(int p) { lc.setColumn(0, 0+p, B00011111); lc.setColumn(0, 1+p, B00010101); lc.setColumn(0, 2+p, B00010111); }
void draw7(int p) { lc.setColumn(0, 0+p, B00010000); lc.setColumn(0, 1+p, B00010011); lc.setColumn(0, 2+p, B00011100); }
void draw8(int p) { lc.setColumn(0, 0+p, B00011111); lc.setColumn(0, 1+p, B00010101); lc.setColumn(0, 2+p, B00011111); }
void draw9(int p) { lc.setColumn(0, 0+p, B00011101); lc.setColumn(0, 1+p, B00010101); lc.setColumn(0, 2+p, B00011111); }

Schématique

Câblage : Matrice LED (DIN→12, CLK→11, LOAD→10), Bouton gauche→D2, Bouton droit→D3 (pull-up interne), Buzzer→D9. Alimentation via USB Arduino.

Processus de fabrication

  1. Les Échecs : Des Origines Anciennes à la Fabrication Moderne — Vue d'Expert
  2. Flipper : histoire, conception et fabrication d'un jeu d'arcade classique
  3. L'Évolution et l'Artisanat du Football Américain : Des Jeux Anciens aux Standards Modernes de la NFL
  4. Créer un jeu Pong classique sur Arduino UNO avec écran OLED – Tutoriel étape par étape
  5. Créer votre propre jeu de roulette à 37 LED avec Arduino Nano
  6. Construire une console Pong 24×16 LED avec Arduino Nano et MAX7219
  7. Jeu Tetris sur Matrice LED 16×8 DIY avec Arduino Nano
  8. Jeu interactif joystick + LEDs avec Arduino
  9. Affichage LED matrice interactif porte avec capteur PIR - Salutations fêtes Arduino