2023-12-01 13:32:18 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <graph.h>
|
2023-12-07 14:32:42 +01:00
|
|
|
#include "serpent.h"
|
|
|
|
#include "terrain.h"
|
2023-12-01 13:32:18 +01:00
|
|
|
|
2023-12-04 17:27:48 +01:00
|
|
|
Serpent serpent;
|
2023-12-07 14:32:42 +01:00
|
|
|
int direction;
|
2023-12-04 17:27:48 +01:00
|
|
|
|
|
|
|
void initialiserSerpent() {
|
2023-12-07 14:32:42 +01:00
|
|
|
serpent.longueur = 1;
|
2023-12-04 17:27:48 +01:00
|
|
|
serpent.corps = (Position *)malloc(sizeof(Position) * serpent.longueur);
|
|
|
|
serpent.corps[0].x = LARGEUR_FENETRE / 2;
|
|
|
|
serpent.corps[0].y = HAUTEUR_FENETRE / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dessinerSerpent() {
|
2023-12-07 14:32:42 +01:00
|
|
|
int i;
|
2023-12-04 17:27:48 +01:00
|
|
|
for (i = 0; i < serpent.longueur; i++) {
|
|
|
|
if (i % 2 == 0) {
|
2023-12-07 14:32:42 +01:00
|
|
|
ChoisirCouleurDessin(CouleurParNom("yellow")); /*JAUNE*/
|
2023-12-04 17:27:48 +01:00
|
|
|
}
|
|
|
|
RemplirRectangle(serpent.corps[i].x, serpent.corps[i].y, TAILLE_CELLULE, TAILLE_CELLULE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void deplacerSerpent() {
|
2023-12-07 14:32:42 +01:00
|
|
|
int i;
|
2023-12-04 17:27:48 +01:00
|
|
|
/* Déplacer le corps du serpent*/
|
|
|
|
for (i = serpent.longueur - 1; i > 0; i--) {
|
|
|
|
serpent.corps[i] = serpent.corps[i - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Déplacer la tête du serpent en fonction de la direction*/
|
|
|
|
switch (direction) {
|
|
|
|
case 0:
|
|
|
|
serpent.corps[0].x += TAILLE_CELLULE;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
serpent.corps[0].y += TAILLE_CELLULE;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
serpent.corps[0].x -= TAILLE_CELLULE;
|
2023-12-01 13:32:18 +01:00
|
|
|
break;
|
2023-12-04 17:27:48 +01:00
|
|
|
case 3:
|
|
|
|
serpent.corps[0].y -= TAILLE_CELLULE;
|
2023-12-01 13:32:18 +01:00
|
|
|
break;
|
2023-12-04 17:27:48 +01:00
|
|
|
}
|
|
|
|
}
|
2023-12-07 14:32:42 +01:00
|
|
|
|
|
|
|
int collisionAvecSerpent() {
|
|
|
|
int i;
|
|
|
|
for (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-04 17:27:48 +01:00
|
|
|
}
|
2023-12-07 14:32:42 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2023-12-01 13:32:18 +01:00
|
|
|
|
2023-12-07 14:32:42 +01:00
|
|
|
int collisionAvecBordures() {
|
|
|
|
return (serpent.corps[0].x < 0 || serpent.corps[0].x >= LARGEUR_FENETRE ||
|
|
|
|
serpent.corps[0].y < 0 || serpent.corps[0].y >= HAUTEUR_FENETRE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void gestionCollision() {
|
2023-12-04 17:27:48 +01:00
|
|
|
if (collisionAvecSerpent() || collisionAvecBordures()) {
|
|
|
|
FermerGraphique();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|