#include <LedControl.h>
const int dataPin = 11;
const int clockPin = 13;
const int csPin = 7;
LedControl lc = LedControl(dataPin, clockPin, csPin, 1);
const int joystickXPin = A1;
const int joystickYPin = A0;
int joueurVitesse = 100;
unsigned long joueurCompteurVitesse = 0;
int playerColumn = 3;
int playerLigne = 7;
int murGauche = 0;
int murDroit = 7;
const int murDelai = 500; // Délai en millisecondes entre les changements de mur
unsigned long dernierChangementMur = 0;
struct Obstacle {
int colonne;
int ligne;
};
Obstacle obstacles[8]; // Tableau pour stocker les obstacles
void setup() {
lc.shutdown(0, false);
lc.setIntensity(0, 8);
lc.clearDisplay(0);
Serial.begin(9600);
initialiserMur();
lc.setLed(0, playerColumn, playerLigne, true);
}
void loop() {
if (millis() > joueurVitesse + joueurCompteurVitesse) {
lectureValeursJoystick();
}
if (millis() > murDelai + dernierChangementMur) {
deplacerObstacles();
dernierChangementMur = millis();
}
}
void initialiserMur() {
for (int i = 0; i < 8; i++) {
lc.setLed(0, murGauche, i, true);
lc.setLed(0, murDroit, i, true);
}
// Initialiser les obstacles
for (int i = 0; i < 8; i++) {
obstacles[i].colonne = random(1, 3);
obstacles[i].ligne = 0;
for (int j=0;j<3; j++){
lc.setLed(0, obstacles[i].colonne, obstacles[i].ligne+j, true);
}
}
}
void lectureValeursJoystick() {
int joystickXValue = analogRead(joystickXPin);
int joystickYValue = analogRead(joystickYPin);
if (joystickXValue < 200) {
deplacementPlayer(1, 0);
} else if (joystickXValue > 800) {
deplacementPlayer(-1, 0);
} else if (joystickYValue < 200) {
deplacementPlayer(0, 1);
} else if (joystickYValue > 800) {
deplacementPlayer(0, -1);
}
joueurCompteurVitesse = millis();
}
void deplacementPlayer(int directionX, int directionY) {
int nouvelleColonne = playerColumn + directionX;
int nouvelleLigne = playerLigne + directionY;
if (nouvelleColonne >= murGauche + 1 && nouvelleColonne <= murDroit - 1) {
lc.setLed(0, playerColumn, playerLigne, false);
playerColumn = nouvelleColonne;
playerLigne = nouvelleLigne;
lc.setLed(0, playerColumn, playerLigne, true);
}
}
void deplacerObstacles() {
for (int i = 0; i < 8; i++) {
lc.setLed(0, obstacles[i].colonne, obstacles[i].ligne, false);
// Déplacer l'obstacle vers le bas
obstacles[i].ligne++;
// Si l'obstacle atteint le bas, le replacer en haut avec une nouvelle colonne
if (obstacles[i].ligne > 7) {randomSeed(A6);
obstacles[i].colonne = random(1, 4);
obstacles[i].ligne = 0;
}
for (int j=0;j<3; j++){
lc.setLed(0, obstacles[i].colonne, obstacles[i].ligne+j, true);
}
}
}