2023-12-15 09:15:22 +01:00
|
|
|
#include "serpent.h"
|
|
|
|
#include "pommes.h"
|
2023-12-11 14:45:42 +01:00
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
void DeplacerSerpent(Serpent* serpent, int* direction) {
|
2023-12-12 13:46:39 +01:00
|
|
|
couleur couleurFond = CouleurParComposante(0, 0, 0);
|
|
|
|
couleur couleurSerpent = CouleurParComposante(255, 255, 0);
|
|
|
|
couleur couleurTerrain = CouleurParComposante(111, 255, 94);
|
|
|
|
|
2023-12-13 11:38:02 +01:00
|
|
|
// Clear the previous positions of the snake
|
2023-12-12 16:28:27 +01:00
|
|
|
for (int i = 0; i < serpent->longueur; i++) {
|
2023-12-12 13:46:39 +01:00
|
|
|
ChoisirCouleurDessin(couleurTerrain);
|
2023-12-12 16:28:27 +01:00
|
|
|
RemplirRectangle(serpent->corps[i].x, serpent->corps[i].y, TAILLE_CASE, TAILLE_CASE);
|
2023-12-12 13:46:39 +01:00
|
|
|
}
|
|
|
|
|
2023-12-13 11:38:02 +01:00
|
|
|
// Update the snake's body positions
|
2023-12-12 16:28:27 +01:00
|
|
|
for (int i = serpent->longueur - 1; i > 0; i--) {
|
|
|
|
serpent->corps[i] = serpent->corps[i - 1];
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|
|
|
|
|
2023-12-13 11:38:02 +01:00
|
|
|
// Move the snake's head based on the direction
|
2023-12-12 13:46:39 +01:00
|
|
|
if (*direction == 1) {
|
2023-12-12 16:28:27 +01:00
|
|
|
serpent->corps[0].x += TAILLE_CASE;
|
2023-12-12 13:46:39 +01:00
|
|
|
} else if (*direction == 2) {
|
2023-12-12 16:28:27 +01:00
|
|
|
serpent->corps[0].x -= TAILLE_CASE;
|
2023-12-12 13:46:39 +01:00
|
|
|
} else if (*direction == 3) {
|
2023-12-12 16:28:27 +01:00
|
|
|
serpent->corps[0].y -= TAILLE_CASE;
|
2023-12-12 13:46:39 +01:00
|
|
|
} else if (*direction == 4) {
|
2023-12-12 16:28:27 +01:00
|
|
|
serpent->corps[0].y += TAILLE_CASE;
|
2023-12-12 13:46:39 +01:00
|
|
|
}
|
|
|
|
|
2023-12-13 11:38:02 +01:00
|
|
|
// Draw the updated snake
|
2023-12-12 16:28:27 +01:00
|
|
|
for (int i = 0; i < serpent->longueur; i++) {
|
2023-12-12 13:46:39 +01:00
|
|
|
ChoisirCouleurDessin(couleurSerpent);
|
2023-12-12 16:28:27 +01:00
|
|
|
RemplirRectangle(serpent->corps[i].x, serpent->corps[i].y, TAILLE_CASE, TAILLE_CASE);
|
2023-12-12 13:46:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Attendre(100);
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
int GestionCollision(Serpent* serpent, Pommes* pommes, Obstacles* obstacles, int perdu, int* score) {
|
2023-12-12 13:46:39 +01:00
|
|
|
int loose = perdu;
|
2023-12-15 09:15:22 +01:00
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
if (CollisionAvecPomme(serpent, pommes, score)) {
|
|
|
|
serpent->longueur = serpent->longueur + 2;
|
|
|
|
serpent->corps = realloc(serpent->corps, sizeof(Position) * serpent->longueur);
|
2023-12-12 13:46:39 +01:00
|
|
|
loose = 1;
|
|
|
|
}
|
2023-12-15 09:15:22 +01:00
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
if (CollisionAvecObstacle(serpent, obstacles) || CollisionAvecSerpent(serpent) || CollisionAvecBordures(serpent)) {
|
2023-12-15 09:15:22 +01:00
|
|
|
AfficherEcranFin(*score);
|
|
|
|
Attendre(1000);
|
2023-12-12 13:46:39 +01:00
|
|
|
loose = -1;
|
|
|
|
}
|
2023-12-15 09:15:22 +01:00
|
|
|
|
2023-12-12 13:46:39 +01:00
|
|
|
return loose;
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|
|
|
|
|
2023-12-15 09:15:22 +01:00
|
|
|
int CollisionAvecSerpent(Serpent* serpent) {
|
|
|
|
for (int i = 1; i < serpent->longueur; i++) {
|
|
|
|
if (serpent->corps[0].x == serpent->corps[i].x && serpent->corps[0].y == serpent->corps[i].y) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2023-12-12 16:28:27 +01:00
|
|
|
|
2023-12-15 09:15:22 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2023-12-12 13:46:39 +01:00
|
|
|
|
2023-12-15 09:15:22 +01:00
|
|
|
int EstDirectionOpposee(int directionActuelle, int nouvelleDirection) {
|
|
|
|
if ((directionActuelle == 1 && nouvelleDirection == 2) ||
|
|
|
|
(directionActuelle == 2 && nouvelleDirection == 1) ||
|
|
|
|
(directionActuelle == 3 && nouvelleDirection == 4) ||
|
|
|
|
(directionActuelle == 4 && nouvelleDirection == 3)) {
|
|
|
|
return 1; // Les directions sont opposées
|
|
|
|
}
|
2023-12-12 13:46:39 +01:00
|
|
|
|
2023-12-15 09:15:22 +01:00
|
|
|
return 0; // Les directions ne sont pas opposées
|
|
|
|
}
|
2023-12-13 16:06:34 +01:00
|
|
|
|
2023-12-15 09:15:22 +01:00
|
|
|
int CollisionAvecBordures(Serpent* serpent) {
|
|
|
|
if (serpent->corps[0].x < 0 || serpent->corps[0].x >= LARGEUR * TAILLE_CASE ||
|
|
|
|
serpent->corps[0].y < 0 || serpent->corps[0].y >= HAUTEUR * TAILLE_CASE) {
|
|
|
|
return 1;
|
2023-12-13 16:06:34 +01:00
|
|
|
}
|
|
|
|
|
2023-12-15 09:15:22 +01:00
|
|
|
return 0;
|
2023-12-13 16:06:34 +01:00
|
|
|
}
|
2023-12-15 09:15:22 +01:00
|
|
|
int CollisionAvecPomme(Serpent* serpent, Pommes* pommes, int* score) {
|
|
|
|
PommeNode* current = pommes->head;
|
|
|
|
PommeNode* prev = NULL;
|
|
|
|
while (current != NULL) {
|
|
|
|
if (serpent->corps[0].x >= current->position.x * TAILLE_CASE &&
|
|
|
|
serpent->corps[0].x < (current->position.x + 1) * TAILLE_CASE &&
|
|
|
|
serpent->corps[0].y >= current->position.y * TAILLE_CASE &&
|
|
|
|
serpent->corps[0].y < (current->position.y + 1) * TAILLE_CASE) {
|
|
|
|
// La pomme a été mangée, mettre à jour les liens
|
|
|
|
if (prev == NULL) {
|
|
|
|
// La pomme était en tête de liste
|
|
|
|
pommes->head = current->next;
|
|
|
|
} else {
|
|
|
|
prev->next = current->next;
|
|
|
|
}
|
|
|
|
free(current);
|
|
|
|
*score = *score + 5;
|
|
|
|
AfficherScore(*score);
|
|
|
|
// Générer une nouvelle pomme
|
|
|
|
GenererNouvellePomme(pommes);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
prev = current;
|
|
|
|
current = current->next;
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|
2023-12-15 09:15:22 +01:00
|
|
|
return 0;
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|