1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
#include <Adafruit_NeoPixel.h>
#include "TM1637Display.h"
#define BUTTON_PIN 2
#define RESET_PIN 6
#define LEDA_PIN 3
#define RED_PIN 4
#define BLUE_PIN 5
#define NUM_LEDS 10
#define BRILLANCE 25
#define CLK 7
#define DIO 8
TM1637Display display(CLK, DIO);
Adafruit_NeoPixel ledA(8, LEDA_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel redLED(NUM_LEDS, RED_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel blueLED(NUM_LEDS, BLUE_PIN, NEO_GRB + NEO_KHZ800);
bool joueurBleuActif = false;
bool gameStarted = false;
int redScore = 0;
int blueScore = 0;
int totalTurns = 0;
long randNumber;
bool lastButtonState = HIGH;
bool lastResetState = HIGH;
void setup() {
Serial.begin(9600);
randomSeed(analogRead(0));
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(RESET_PIN, INPUT_PULLUP);
ledA.begin();
redLED.begin();
blueLED.begin();
ledA.show(); redLED.show(); blueLED.show();
ledA.setBrightness(BRILLANCE);
redLED.setBrightness(BRILLANCE);
blueLED.setBrightness(BRILLANCE);
display.setBrightness(2);
resetGame();
}
void loop() {
bool startState = digitalRead(BUTTON_PIN);
bool resetState = digitalRead(RESET_PIN);
if (lastResetState == HIGH && resetState == LOW) {
resetGame();
}
if (lastButtonState == HIGH && startState == LOW && !isGameOver()) {
if (!gameStarted) {
joueurBleuActif = tirageInitial();
gameStarted = true;
} else {
tourDeJeu();
}
}
lastButtonState = startState;
lastResetState = resetState;
}
void resetGame() {
joueurBleuActif = false;
redScore = 0;
blueScore = 0;
totalTurns = 0;
gameStarted = false;
ledA.clear(); ledA.show();
redLED.clear(); redLED.show();
blueLED.clear(); blueLED.show();
display.clear();
updateDisplay();
Serial.println("🔄 Jeu réinitialisé !");
}
bool tirageInitial() {
bool couleur=DeterminePlayer();
animationBlanche();
Serial.println(couleur);
clignoter(couleur);
updateDisplay();
Serial.print("🔰 Le joueur qui commence est : ");
Serial.println(couleur ? "BLEU" : "ROUGE");
return couleur;
}
void tourDeJeu() {
bool couleur=DeterminePlayer();
animationBlanche();
Serial.println(couleur);
clignoter(couleur);
Serial.print("🎲 Couleur tirée : ");
Serial.println(couleur ? "BLEU" : "ROUGE");
if (couleur == joueurBleuActif) {
if (joueurBleuActif && blueScore < NUM_LEDS) {
blueScore++;
} else if (!joueurBleuActif && redScore < NUM_LEDS) {
redScore++;
}
Serial.println("✅ Bon joueur → rejoue");
} else {
if (joueurBleuActif) {
if (blueScore > 0) blueScore--;
if (redScore < NUM_LEDS) redScore++;
joueurBleuActif = false;
} else {
if (redScore > 0) redScore--;
if (blueScore < NUM_LEDS) blueScore++;
joueurBleuActif = true;
}
Serial.println("❌ Mauvais joueur → adversaire rejoue");
}
totalTurns++;
updateScores();
updateDisplay();
if (redScore == NUM_LEDS) {
Serial.println("🏆 Joueur ROUGE GAGNE !");
flashWin(redLED, redLED.Color(255, 0, 0));
blueLED.clear(); blueLED.show();
} else if (blueScore == NUM_LEDS) {
Serial.println("🏆 Joueur BLEU GAGNE !");
flashWin(blueLED, blueLED.Color(0, 0, 255));
redLED.clear(); redLED.show();
}
Serial.print("🔴 ROUGE: "); Serial.println(redScore);
Serial.print("🔵 BLEU : "); Serial.println(blueScore);
Serial.print("⏳ Nb Tours : "); Serial.println(totalTurns);
}
bool isGameOver() {
return (redScore == NUM_LEDS || blueScore == NUM_LEDS );
}
void animationBlanche() {
for (int i = 0; i < 15; i++) {
ledA.clear();
ledA.setPixelColor(i, ledA.Color(255, 255, 255));
ledA.show();
delay(60);
}
}
void clignoter(bool isBlue) {
for (int i = 0; i < 6; i++) {
ledA.fill(isBlue ? ledA.Color(0, 0, 255) : ledA.Color(255, 0, 0));
ledA.show();
delay(200);
ledA.clear();
ledA.show();
delay(200);
}
}
void updateScores() {
redLED.clear();
blueLED.clear();
for (int i = 0; i < redScore; i++) {
redLED.setPixelColor(i, redLED.Color(255, 0, 0));
}
for (int i = 0; i < blueScore; i++) {
blueLED.setPixelColor(i, blueLED.Color(0, 0, 255));
}
redLED.show();
blueLED.show();
}
void updateDisplay() {
// Affichage des tours restants + joueur actif (r = 0x50, b = 0x7C)
uint8_t digits[4];
digits[0] = display.encodeDigit(totalTurns / 10);
digits[1] = display.encodeDigit(totalTurns % 10);
digits[2] = 0;
//digits[3] = joueurBleuActif ? 0x3E : 0x50; // 'b' or 'r'
if (totalTurns ==0 ){
digits[3] = 0x40; // '-'
}else{
digits[3] = joueurBleuActif ? 0x7C : 0x50; // 'b' or 'r'
}
display.setSegments(digits);
}
void flashWin(Adafruit_NeoPixel &strip, uint32_t color) {
for (int i = 0; i < 6; i++) {
strip.fill(color); strip.show();
delay(250);
strip.clear(); strip.show();
delay(250);
}
}
bool DeterminePlayer(void) {
return(random(0, 1000) > 500);
} |
Partager