t
This commit is contained in:
parent
311db2f769
commit
c6b09bb5ca
115
serpent.c
115
serpent.c
@ -21,8 +21,13 @@ typedef struct {
|
||||
int longueur;
|
||||
} Serpent;
|
||||
|
||||
typedef struct PommeNode {
|
||||
Position position;
|
||||
struct PommeNode* next;
|
||||
} PommeNode;
|
||||
|
||||
typedef struct {
|
||||
Position* positions;
|
||||
PommeNode* head;
|
||||
int nombre;
|
||||
} Pommes;
|
||||
|
||||
@ -35,6 +40,16 @@ void Attendre(unsigned int millisecondes) {
|
||||
usleep(millisecondes * 1000);
|
||||
}
|
||||
|
||||
void LibererPommes(Pommes* pommes) {
|
||||
PommeNode* current = pommes->head;
|
||||
while (current != NULL) {
|
||||
PommeNode* next = current->next;
|
||||
free(current);
|
||||
current = next;
|
||||
}
|
||||
pommes->head = NULL;
|
||||
}
|
||||
|
||||
void InitialiserJeu(Serpent* serpent, Pommes* pommes, Obstacles* obstacles) {
|
||||
serpent->longueur = 10;
|
||||
serpent->corps = malloc(sizeof(Position) * serpent->longueur);
|
||||
@ -56,7 +71,6 @@ void InitialiserJeu(Serpent* serpent, Pommes* pommes, Obstacles* obstacles) {
|
||||
|
||||
void GenererObstacles(Obstacles* obstacles) {
|
||||
couleur couleurObstacle = CouleurParComposante(0, 0, 255);
|
||||
|
||||
for (int i = 0; i < obstacles->nombre; i++) {
|
||||
ChoisirCouleurDessin(couleurObstacle);
|
||||
RemplirRectangle(obstacles->positions[i].x * TAILLE_CASE, obstacles->positions[i].y * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE);
|
||||
@ -99,7 +113,7 @@ int PauseJeu() {
|
||||
|
||||
void LibererMemoire(Serpent* serpent, Pommes* pommes, Obstacles* obstacles) {
|
||||
free(serpent->corps);
|
||||
free(pommes->positions);
|
||||
LibererPommes(pommes);
|
||||
free(obstacles->positions);
|
||||
}
|
||||
|
||||
@ -123,37 +137,79 @@ void AfficherSerpent(Serpent* serpent) {
|
||||
|
||||
void GenererPommes(Pommes* pommes) {
|
||||
couleur couleurPommes = CouleurParComposante(255, 0, 0);
|
||||
pommes->positions = malloc(sizeof(Position) * pommes->nombre);
|
||||
pommes->head = NULL;
|
||||
|
||||
for (int i = 0; i < pommes->nombre; i++) {
|
||||
pommes->positions[i].x = rand() % LARGEUR;
|
||||
pommes->positions[i].y = rand() % HAUTEUR;
|
||||
PommeNode* nouvellePomme = (PommeNode*)malloc(sizeof(PommeNode));
|
||||
if (nouvellePomme == NULL) {
|
||||
perror("Allocation error");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
nouvellePomme->position.x = rand() % LARGEUR;
|
||||
nouvellePomme->position.y = rand() % HAUTEUR;
|
||||
nouvellePomme->next = pommes->head;
|
||||
pommes->head = nouvellePomme;
|
||||
|
||||
ChoisirCouleurDessin(couleurPommes);
|
||||
RemplirRectangle(pommes->positions[i].x * TAILLE_CASE, pommes->positions[i].y * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE);
|
||||
RemplirRectangle(nouvellePomme->position.x * TAILLE_CASE, nouvellePomme->position.y * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE);
|
||||
}
|
||||
}
|
||||
|
||||
void AjouterPomme(Pommes* pommes, int x, int y) {
|
||||
PommeNode* nouvellePomme = (PommeNode*)malloc(sizeof(PommeNode));
|
||||
if (nouvellePomme == NULL) {
|
||||
perror("Allocation error");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
nouvellePomme->position.x = x;
|
||||
nouvellePomme->position.y = y;
|
||||
nouvellePomme->next = pommes->head;
|
||||
pommes->head = nouvellePomme;
|
||||
pommes->nombre++;
|
||||
}
|
||||
void GenererNouvellePomme(Pommes* pommes) {
|
||||
couleur couleurPommes = CouleurParComposante(255, 0, 0);
|
||||
int i = rand() % pommes->nombre;
|
||||
pommes->positions[i].x = rand() % LARGEUR;
|
||||
pommes->positions[i].y = rand() % HAUTEUR;
|
||||
int newX = rand() % LARGEUR;
|
||||
int newY = rand() % HAUTEUR;
|
||||
|
||||
// Assuming you have a function named AjouterPomme to add a new apple to the linked list.
|
||||
AjouterPomme(pommes, newX, newY);
|
||||
|
||||
ChoisirCouleurDessin(couleurPommes);
|
||||
RemplirRectangle(pommes->positions[i].x * TAILLE_CASE, pommes->positions[i].y * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE);
|
||||
RemplirRectangle(newX * TAILLE_CASE, newY * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE);
|
||||
}
|
||||
|
||||
int CollisionAvecPomme(Serpent* serpent, Pommes* pommes, int* score) {
|
||||
for (int i = 0; i < pommes->nombre; i++) {
|
||||
if (serpent->corps[0].x >= pommes->positions[i].x * TAILLE_CASE &&
|
||||
serpent->corps[0].x < (pommes->positions[i].x + 1) * TAILLE_CASE &&
|
||||
serpent->corps[0].y >= pommes->positions[i].y * TAILLE_CASE &&
|
||||
serpent->corps[0].y < (pommes->positions[i].y + 1) * TAILLE_CASE) {
|
||||
GenererNouvellePomme(pommes);
|
||||
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;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -189,15 +245,18 @@ void DeplacerSerpent(Serpent* serpent, int* direction) {
|
||||
couleur couleurSerpent = CouleurParComposante(255, 255, 0);
|
||||
couleur couleurTerrain = CouleurParComposante(111, 255, 94);
|
||||
|
||||
// Clear the previous positions of the snake
|
||||
for (int i = 0; i < serpent->longueur; i++) {
|
||||
ChoisirCouleurDessin(couleurTerrain);
|
||||
RemplirRectangle(serpent->corps[i].x, serpent->corps[i].y, TAILLE_CASE, TAILLE_CASE);
|
||||
}
|
||||
|
||||
// Update the snake's body positions
|
||||
for (int i = serpent->longueur - 1; i > 0; i--) {
|
||||
serpent->corps[i] = serpent->corps[i - 1];
|
||||
}
|
||||
|
||||
// Move the snake's head based on the direction
|
||||
if (*direction == 1) {
|
||||
serpent->corps[0].x += TAILLE_CASE;
|
||||
} else if (*direction == 2) {
|
||||
@ -208,6 +267,7 @@ void DeplacerSerpent(Serpent* serpent, int* direction) {
|
||||
serpent->corps[0].y += TAILLE_CASE;
|
||||
}
|
||||
|
||||
// Draw the updated snake
|
||||
for (int i = 0; i < serpent->longueur; i++) {
|
||||
ChoisirCouleurDessin(couleurSerpent);
|
||||
RemplirRectangle(serpent->corps[i].x, serpent->corps[i].y, TAILLE_CASE, TAILLE_CASE);
|
||||
@ -289,19 +349,10 @@ int main() {
|
||||
DeplacerSerpent(&serpent, &direction);
|
||||
perdu = GestionCollision(&serpent, &pommes, &obstacles, perdu, &score);
|
||||
} else {
|
||||
if (ToucheEnAttente()) {
|
||||
int touche = Touche();
|
||||
if (touche == XK_space) {
|
||||
if (touche == XK_Escape) {
|
||||
return EXIT_FAILURE;
|
||||
jeuEnPause = PauseJeu();
|
||||
}
|
||||
jeuEnPause = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FermerGraphique();
|
||||
LibererMemoire(&serpent, &pommes, &obstacles);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
LibererMemoire(&serpent, &pommes, &obstacles);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user