#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>
// ECRAN
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8
Adafruit_ILI9341 ecran(TFT_CS, TFT_DC, TFT_RST);
// JOYSTICK
#define JOY_X A0
#define JOY_Y A1
#define JOY_BOUTON 2
// LABYRINTHE
#define COLONNES 16
#define LIGNES 10
#define TAILLE 20
#define DEBUT_Y 35
char labyrinthe[LIGNES][17] = {
"################",
"#P..o.#...o....#",
"#.##..#.####...#",
"#......#.......#",
"######.#.####..#",
"#......#....#..#",
"#.###...##.#...#",
"#....#.........#",
"#.............G#",
"################"
};
// PAC-MAN
int pacX = 1;
int pacY = 1;
// FANTOMES
const int NB_FANTOMES = 4;
int fantomeX[NB_FANTOMES] = {14, 1, 13, 8};
int fantomeY[NB_FANTOMES] = {8, 7, 3, 5};
int maisonX[NB_FANTOMES] = {14, 1, 13, 8};
int maisonY[NB_FANTOMES] = {8, 7, 3, 5};
uint16_t couleursFantomes[NB_FANTOMES] = {
ILI9341_RED,
ILI9341_CYAN,
ILI9341_MAGENTA,
ILI9341_ORANGE
};
// SCORE ET VIES
int points = 0;
int vies = 3;
int billesRestantes = 0;
// SUPER POUVOIR
bool pouvoirActif = false;
unsigned long finPouvoir = 0;
// TEMPS
unsigned long dernierMouvement = 0;
unsigned long dernierMouvementFantome = 0;
// ------------------------------------------------
void dessinerCase(int x, int y) {
int px = x * TAILLE;
int py = y * TAILLE + DEBUT_Y;
if (labyrinthe[y][x] == '#') {
ecran.fillRect(px, py, TAILLE, TAILLE, ILI9341_BLUE);
} else {
ecran.fillRect(px, py, TAILLE, TAILLE, ILI9341_BLACK);
if (labyrinthe[y][x] == '.') {
ecran.fillCircle(px + 10, py + 10, 2, ILI9341_WHITE);
}
if (labyrinthe[y][x] == 'o') {
ecran.fillCircle(px + 10, py + 10, 7, ILI9341_YELLOW);
}
}
}
// ------------------------------------------------
void dessinerLabyrinthe() {
billesRestantes = 0;
for (int y = 0; y < LIGNES; y++) {
for (int x = 0; x < COLONNES; x++) {
dessinerCase(x, y);
if (labyrinthe[y][x] == '.' ||
labyrinthe[y][x] == 'o') {
billesRestantes++;
}
}
}
}
// ------------------------------------------------
void dessinerPacman() {
int px = pacX * TAILLE + 10;
int py = pacY * TAILLE + DEBUT_Y + 10;
ecran.fillCircle(px, py, 8, ILI9341_YELLOW);
}
// ------------------------------------------------
void dessinerFantome(int numero) {
int px = fantomeX[numero] * TAILLE + 10;
int py = fantomeY[numero] * TAILLE + DEBUT_Y + 10;
uint16_t couleur = couleursFantomes[numero];
if (pouvoirActif) {
couleur = ILI9341_BLUE;
}
ecran.fillCircle(px, py, 8, couleur);
ecran.fillCircle(px - 3, py - 2, 2, ILI9341_WHITE);
ecran.fillCircle(px + 3, py - 2, 2, ILI9341_WHITE);
}
// ------------------------------------------------
void dessinerFantomes() {
for (int i = 0; i < NB_FANTOMES; i++) {
dessinerFantome(i);
}
}
// ------------------------------------------------
void afficherCompteurs() {
ecran.fillRect(0, 0, 320, 30, ILI9341_BLACK);
ecran.setTextColor(ILI9341_WHITE);
ecran.setTextSize(2);
ecran.setCursor(5, 7);
ecran.print("Score:");
ecran.print(points);
ecran.setCursor(190, 7);
ecran.print("Vies:");
ecran.print(vies);
}
// ------------------------------------------------
bool mur(int x, int y) {
if (x < 0 || x >= COLONNES ||
y < 0 || y >= LIGNES) {
return true;
}
return labyrinthe[y][x] == '#';
}
// ------------------------------------------------
void reinitialiserPacman() {
pacX = 1;
pacY = 1;
for (int i = 0; i < NB_FANTOMES; i++) {
fantomeX[i] = maisonX[i];
fantomeY[i] = maisonY[i];
}
dessinerLabyrinthe();
dessinerPacman();
dessinerFantomes();
}
// ------------------------------------------------
void activerPouvoir() {
pouvoirActif = true;
finPouvoir = millis() + 6000;
dessinerFantomes();
}
// ------------------------------------------------
void desactiverPouvoir() {
pouvoirActif = false;
dessinerFantomes();
}
// ------------------------------------------------
void deplacerPacman() {
int x = analogRead(JOY_X);
int y = analogRead(JOY_Y);
int nouveauX = pacX;
int nouveauY = pacY;
if (x < 400) nouveauX--;
if (x > 600) nouveauX++;
if (y < 400) nouveauY--;
if (y > 600) nouveauY++;
if (nouveauX == pacX && nouveauY == pacY) {
return;
}
if (mur(nouveauX, nouveauY)) {
return;
}
dessinerCase(pacX, pacY);
pacX = nouveauX;
pacY = nouveauY;
if (labyrinthe[pacY][pacX] == '.') {
points += 10;
billesRestantes--;
labyrinthe[pacY][pacX] = ' ';
}
if (labyrinthe[pacY][pacX] == 'o') {
points += 50;
billesRestantes--;
labyrinthe[pacY][pacX] = ' ';
activerPouvoir();
}
dessinerPacman();
afficherCompteurs();
}
// ------------------------------------------------
void deplacerFantomes() {
for (int i = 0; i < NB_FANTOMES; i++) {
int ancienX = fantomeX[i];
int ancienY = fantomeY[i];
int direction = random(4);
int nx = ancienX;
int ny = ancienY;
if (direction == 0) nx++;
if (direction == 1) nx--;
if (direction == 2) ny++;
if (direction == 3) ny--;
if (mur(nx, ny)) {
continue;
}
bool occupe = false;
for (int j = 0; j < NB_FANTOMES; j++) {
if (j != i && fantomeX[j] == nx && fantomeY[j] == ny) {
occupe = true;
}
}
if (occupe) {
continue;
}
dessinerCase(ancienX, ancienY);
fantomeX[i] = nx;
fantomeY[i] = ny;
dessinerFantome(i);
}
}
// ------------------------------------------------
void verifierCollision() {
for (int i = 0; i < NB_FANTOMES; i++) {
if (pacX == fantomeX[i] && pacY == fantomeY[i]) {
if (pouvoirActif) {
points += 200;
dessinerCase(fantomeX[i], fantomeY[i]);
fantomeX[i] = maisonX[i];
fantomeY[i] = maisonY[i];
dessinerFantome(i);
afficherCompteurs();
} else {
vies--;
afficherCompteurs();
if (vies <= 0) {
ecran.fillScreen(ILI9341_BLACK);
ecran.setTextSize(3);
ecran.setTextColor(ILI9341_RED);
ecran.setCursor(65, 100);
ecran.print("GAME OVER");
while (true) {}
}
delay(500);
reinitialiserPacman();
return;
}
}
}
}
// ------------------------------------------------
void verifierVictoire() {
if (billesRestantes <= 0) {
ecran.fillScreen(ILI9341_BLACK);
ecran.setTextSize(3);
ecran.setTextColor(ILI9341_GREEN);
ecran.setCursor(65, 100);
ecran.print("GAGNE !");
while (true) {}
}
}
// ------------------------------------------------
void setup() {
pinMode(JOY_BOUTON, INPUT_PULLUP);
ecran.begin();
ecran.setRotation(1);
ecran.fillScreen(ILI9341_BLACK);
randomSeed(analogRead(A2));
dessinerLabyrinthe();
dessinerPacman();
dessinerFantomes();
afficherCompteurs();
}
// ------------------------------------------------
void loop() {
if (pouvoirActif && millis() >= finPouvoir) {
desactiverPouvoir();
}
if (millis() - dernierMouvement > 140) {
dernierMouvement = millis();
deplacerPacman();
verifierCollision();
verifierVictoire();
}
if (millis() - dernierMouvementFantome > 450) {
dernierMouvementFantome = millis();
deplacerFantomes();
verifierCollision();
}
}