2023-12-01 13:32:18 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <graph.h>
|
2023-12-04 17:27:48 +01:00
|
|
|
#include "../pastille.h"
|
2023-12-01 13:32:18 +01:00
|
|
|
|
2023-12-04 17:27:48 +01:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
Position *corps;
|
|
|
|
int longueur;
|
|
|
|
} Serpent;
|
|
|
|
Position pomme;
|
|
|
|
Serpent serpent;
|
|
|
|
int direction = 0; /* 0: droite, 1: bas, 2: gauche, 3: haut*/
|
|
|
|
|
|
|
|
void initialiserSerpent() {
|
|
|
|
serpent.longueur = 10;
|
|
|
|
serpent.corps = (Position *)malloc(sizeof(Position) * serpent.longueur);
|
|
|
|
serpent.corps[0].x = LARGEUR_FENETRE / 2;
|
|
|
|
serpent.corps[0].y = HAUTEUR_FENETRE / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dessinerSerpent() {
|
|
|
|
for (i = 0; i < serpent.longueur; i++) {
|
|
|
|
if (i % 2 == 0) {
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(0,0,0)); /*JAUNE*/
|
|
|
|
}
|
|
|
|
RemplirRectangle(serpent.corps[i].x, serpent.corps[i].y, TAILLE_CELLULE, TAILLE_CELLULE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void deplacerSerpent() {
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
}
|
|
|
|
void gestionCollision() {
|
|
|
|
if (collisionAvecPomme()) {
|
|
|
|
serpent.longueur++;
|
|
|
|
serpent.corps = realloc(serpent.corps, sizeof(Position) * serpent.longueur);
|
|
|
|
genererPomme();
|
|
|
|
}
|
2023-12-01 13:32:18 +01:00
|
|
|
|
2023-12-04 17:27:48 +01:00
|
|
|
if (collisionAvecSerpent() || collisionAvecBordures()) {
|
|
|
|
FermerGraphique();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|