From d34541178a102f4bc62643965f3defff637cf310 Mon Sep 17 00:00:00 2001
From: Vincent <xefal77@gmail.com>
Date: Sun, 24 Dec 2023 01:48:21 +0100
Subject: [PATCH] Amelioration code + commentaires. Rectifications erreurs etc.

---
 Makefile     | 10 ++++--
 evenements.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 evenements.h | 17 ++++++++++
 gui.c        | 35 +++++++++++++++----
 gui.h        |  1 +
 main.c       | 58 +++++++++++++++++++++++++++----
 main.h       |  9 ++---
 scene.c      | 50 ++++++++++++++-------------
 score.txt    |  2 +-
 9 files changed, 232 insertions(+), 46 deletions(-)
 create mode 100644 evenements.c
 create mode 100644 evenements.h

diff --git a/Makefile b/Makefile
index 1ea5a49..18d9fc7 100644
--- a/Makefile
+++ b/Makefile
@@ -1,15 +1,16 @@
 CC = gcc
 CFLAGS = -Wall -Wextra
 
+
 all: prog
 
-prog: main.o evenement.o gui.o scene.o
+prog: main.o evenements.o gui.o scene.o
 	$(CC) -o $@ $^ -lgraph
 
-main.o: main.c evenement.h
+main.o: main.c evenements.h
 	$(CC) $(CFLAGS) -c $< -o $@
 
-evenement.o: evenement.c evenement.h
+evenements.o: evenements.c evenements.h
 	$(CC) $(CFLAGS) -c $< -o $@
 
 gui.o: gui.c gui.h
@@ -21,3 +22,6 @@ scene.o: scene.c scene.h
 
 clean:
 	rm -f *.o prog
+
+run: 
+	./prog
diff --git a/evenements.c b/evenements.c
new file mode 100644
index 0000000..85d97a4
--- /dev/null
+++ b/evenements.c
@@ -0,0 +1,96 @@
+#include <graph.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include "main.h"
+#include "gui.h"   
+#include "scene.h"
+
+int MourrirSerpent(PIXELS *serpent,PIXELS *obstacle, unsigned long longueur_serpent,unsigned long longueur_obstacle)
+{
+    unsigned long i = 0;
+    for(i=1;i<longueur_serpent;i++)
+    {
+        if(serpent[0].x == serpent[i].x && serpent[0].y == serpent[i].y )
+        {
+            return 1;
+        }
+    }
+    for(i=1;i<longueur_obstacle;i++)
+    {
+        if(serpent[0].x == obstacle[i].x && serpent[0].y == obstacle[i].y )
+        {
+            return 1;
+        }
+    }
+    if (serpent[0].x<=0 || serpent[0].x>W_GAME || serpent[0].y<=0 || serpent[0].y>H_GAME)
+    {
+        return 1;
+    }
+    return 0;
+}
+
+int MangerPastille(PIXELS *serpent, PIXELS* pastilles,PIXELS *obstacle,unsigned long *score,unsigned long longueur_serpent,unsigned long longueur_obstacle,unsigned long int *vitesse)
+    {
+        int i = 0;
+        for(i=0;i<PASTILLES;i++)
+        {
+            if(serpent[0].x == pastilles[i].x && serpent[0].y == pastilles[i].y)
+            {
+                pastilles[i] = gen_pastille(serpent,pastilles,obstacle,longueur_serpent,longueur_obstacle);
+                ChargerImage("./images/PommePastille.png",pastilles[i].x,pastilles[i].y,0,0,T_PIXEL,T_PIXEL);
+                *score+=5;
+                *vitesse/=1.008;
+                return 1;
+            }
+        }
+    return 0;    
+    }
+
+void DeplacementSerpent(int direction, PIXELS *serpent, unsigned long *longueur) {
+    /* Sauvegarder la position actuelle de la queue du serpent */
+    int queueX = serpent[*longueur - 1].x;
+    int queueY = serpent[*longueur - 1].y;
+    unsigned long i = 0;
+
+    /* Mettre à jour les positions du corps du serpent */
+    for (i = *longueur - 1; i > 0; --i) {
+        serpent[i].x = serpent[i - 1].x;
+        serpent[i].y = serpent[i - 1].y;
+    }
+
+    /* Effacer la queue du serpent */
+    couleur bg = CouleurParComposante(171, 204, 104);
+    ChoisirEcran(2);
+    ChoisirCouleurDessin(bg);
+    RemplirRectangle(queueX, queueY, T_PIXEL, T_PIXEL);
+
+    /* Mettre à jour la position de la tête du serpent */
+    if (direction == 0) {
+        serpent[0].x -= T_PIXEL;  /* Vers la gauche */
+    } else if (direction == 1) {
+        serpent[0].y -= T_PIXEL;  /* Vers le haut */
+    } else if (direction == 2) {
+        serpent[0].x += T_PIXEL;  /* Vers la droite */
+    } else if (direction == 3) {
+        serpent[0].y += T_PIXEL;  /* Vers le bas */
+    }
+
+    /* Afficher la nouvelle tête du serpent */
+    ChargerImage("./images/SnakePart.png", serpent[0].x, serpent[0].y, 0, 0, T_PIXEL, T_PIXEL);
+}
+
+/* Mettre à jour la fonction Serpent */
+int Serpent(PIXELS *serpent, PIXELS *pastilles, PIXELS *obstacle, unsigned long *score,
+            unsigned long *longueur_serpent, unsigned long longueur_obstacle,
+            unsigned long int *vitesse, int direction) {
+    if (MourrirSerpent(serpent, obstacle, *longueur_serpent, longueur_obstacle) == 1) {
+        return 2;
+    }
+    DeplacementSerpent(direction, serpent, &(*longueur_serpent));
+    if (MangerPastille(serpent, pastilles, obstacle, score, *longueur_serpent, longueur_obstacle, vitesse) == 1) {
+        *longueur_serpent += 2;
+        return 1;
+    }
+    return 0;
+}
diff --git a/evenements.h b/evenements.h
new file mode 100644
index 0000000..fe8c020
--- /dev/null
+++ b/evenements.h
@@ -0,0 +1,17 @@
+#include "main.h"
+
+#ifndef EVENEMENTS_H
+#define EVENEMENTS_H
+
+int MangerPastille(PIXELS *serpent, PIXELS* pastilles,PIXELS *obstacle,unsigned long *score,unsigned long longueur_serpent,unsigned long longueur_obstacle,unsigned long int *vitesse);
+
+void DeplacementSerpent(int direction ,PIXELS *serpent, unsigned long *longueur);
+
+int PastilleSurSerpent(PIXELS pastille, PIXELS *serpent, unsigned long longueur_serpent);
+
+int MourrirSerpent(PIXELS *serpent,PIXELS *obstacle,unsigned long longueur_serpent,unsigned long longueur_obstacle);
+
+int Serpent(PIXELS *serpent,PIXELS *pastilles,PIXELS *obstacle,unsigned long *score,unsigned long *longueur_serpent,unsigned long longueur_obstacle,unsigned long int *vitesse,int direction);
+
+
+#endif
\ No newline at end of file
diff --git a/gui.c b/gui.c
index b005bdd..0d22b33 100644
--- a/gui.c
+++ b/gui.c
@@ -37,7 +37,7 @@ void sauvegarderScore(unsigned long nouveauScore)
     FILE* fichier = fopen("score.txt", "w");
     if (fichier == NULL)
     {
-        fprintf(stderr, "Erreur lors de l'ouverture du fichier score.txt pour écriture\n");
+        fprintf(stderr, "Erreur lors de l'ouverture du fichier score.txt \n");
         return;
     }
 
@@ -53,12 +53,13 @@ void CheckScore(unsigned long nouveauScore)
     if (nouveauScore > scoreActuel || scoreActuel == 0)
     {
         sauvegarderScore(nouveauScore);
-        printf("Le score a été enregistré avec succès.\n");
+        /* printf("Le score a été enregistré avec succès.\n");  DEBUG */
     }
-    else
+   /* else
     {
-        printf("Le score précédent est plus grand ou égal. Aucun changement effectué.\n");
-    }
+        printf("Le score précédent est plus grand ou égal.\n");
+    }                                                               DEBUG
+    */ 
 }
 
 void Menu()
@@ -70,7 +71,7 @@ void Menu()
     ChoisirEcran(0);
     ChargerImage("./images/Menu.png",0,0,0,0,930,710); 
     ChoisirCouleurDessin(text);
-    snprintf(buf,100,"BEST SCORE : %07ld",bestscore);
+    snprintf(buf,100,"MEILLEUR SCORE : %07ld",bestscore);
 	EcrireTexte(420,350,buf,2);
 }
 
@@ -87,6 +88,26 @@ void PerduGUI()
     unsigned long bestscore = lireScore();
     ChoisirEcran(0);
     ChargerImage("./images/Perdu.png",0,0,0,0,930,710);
-    snprintf(buf,100,"BEST SCORE : %07ld",bestscore);
+    snprintf(buf,100,"MEILLEUR SCORE : %07ld",bestscore);
 	EcrireTexte(420,350,buf,2);
+}
+
+void AfficherTimerEtScore(long unsigned int *score, int minutes,int secondes)   /*Afficher le temps passé et le score*/
+{   
+    char buf[100];
+    char buff[100]; /* Stockage du score et du timer */
+    couleur text;
+    text=CouleurParComposante(78, 93, 47);
+
+	ChoisirEcran(1);
+    CopierZone(2,1,0,0,930,710,0,0);
+
+    ChoisirCouleurDessin(text);
+    
+	snprintf(buf,100,"TEMPS : %02d:%02d",minutes,secondes);
+    snprintf(buff,100,"SCORE : %07ld",*score);
+	EcrireTexte(60,695,buf,2);
+    EcrireTexte(600,695,buff,2);
+
+	CopierZone(1,0,0,0,930,710,0,0);
 }
\ No newline at end of file
diff --git a/gui.h b/gui.h
index 7999912..17f8057 100644
--- a/gui.h
+++ b/gui.h
@@ -9,5 +9,6 @@ void Menu();
 void PerduGUI();
 void Pause();
 int AfficherGUI();
+void AfficherTimerEtScore(long unsigned int *score, int minutes,int secondes);
 
 #endif
\ No newline at end of file
diff --git a/main.c b/main.c
index 6370673..275564b 100644
--- a/main.c
+++ b/main.c
@@ -2,7 +2,7 @@
 #include <stdio.h>
 #include <graph.h>
 #include <time.h>
-#include "evenement.h"
+#include "evenements.h"
 #include "main.h"
 #include "gui.h"
 #include "scene.h"
@@ -114,12 +114,57 @@ int main()
 
         while(game_on) /* Lancement du cycle pour les Inputs et le Jeu*/
 	    {   
-            if(Touches(&direction,&direction_davant,&pause))
+            if (ToucheEnAttente() == 1)
             {
-                FinJeu(serpent,pastilles,obstacle);
-                return EXIT_SUCCESS;
-            }
+                switch (Touche())
+                {
+                    case XK_Up:
+                        direction = 1;
+                        if(direction_davant == 3 && direction == 1)
+                        {
+                            direction = direction_davant;
+                        }
+                        break;
+                    case XK_Down:
+                        direction = 3;
+                        if(direction_davant == 1 && direction == 3)
+                        {
+                            direction = direction_davant;               /* Changements de direction du serpent */
+                        }
+                        break;
+                    case XK_Left:
+                        direction = 0;
+                        if(direction_davant == 2 && direction == 0)
+                        {
+                            direction = direction_davant;
+                        }
+                        break;
+                    case XK_Right:
+                        direction = 2;
+                        if(direction_davant == 0 && direction == 2)
+                        {
+                            direction = direction_davant;
+                        }
+                        break;
+                    case XK_space:
+                        if(pause ==0)
+                        {
+                            pause=1;
+                            break;
+                        }
+                            pause=0;
+                            break;
                             
+                    case XK_Escape:
+                        if(pause == 1)
+                        {
+                            FinJeu(serpent,pastilles,obstacle);
+                            return EXIT_SUCCESS;
+                        }
+                        break;
+                }
+            }
+
             if(pause==1)
             {
                 Pause();
@@ -161,5 +206,4 @@ int main()
         }
     FinJeu(serpent,pastilles,obstacle);
     return EXIT_SUCCESS;
-}
-    
+}
\ No newline at end of file
diff --git a/main.h b/main.h
index 7f6bd16..b1abc12 100644
--- a/main.h
+++ b/main.h
@@ -10,8 +10,11 @@
 #define PASTILLES 5
 #define OBSTACLE 30
 
-
-
+// Définition des directions
+#define GAUCHE 0
+#define HAUT 1
+#define DROITE 2
+#define BAS 3
 
 struct PIXELS {
     int x;
@@ -20,6 +23,4 @@ struct PIXELS {
 
 typedef struct PIXELS PIXELS;
 
-PIXELS gen_pastille(PIXELS *serpent,PIXELS *pastilles,PIXELS *obstacle,unsigned long longueur_serpent,unsigned long longueur_obstacle);
-
 #endif
diff --git a/scene.c b/scene.c
index 68b40c7..13f7b95 100644
--- a/scene.c
+++ b/scene.c
@@ -29,7 +29,18 @@ PIXELS gen_pastille(PIXELS *serpent,PIXELS *pastilles,PIXELS *obstacle,unsigned
         ok = 0;
         x_pastille= ArrondirPixel(rand()%W_GAME);
         y_pastille = ArrondirPixel(rand()%H_GAME);
-        for(i=0;i<longueur_serpent;i++)
+
+        if(x_pastille < DECALEMENT)
+        {
+            x_pastille =+ DECALEMENT;
+        }
+
+        if(y_pastille < DECALEMENT)
+        {
+            y_pastille =+ DECALEMENT;
+        }
+        
+        for(i=0;i<=longueur_serpent;i++)
         {
             if(x_pastille == serpent[i].x && y_pastille == serpent[i].y)
             {
@@ -41,7 +52,7 @@ PIXELS gen_pastille(PIXELS *serpent,PIXELS *pastilles,PIXELS *obstacle,unsigned
             }
 
         }       
-        for(i=0;i<longueur_obstacle;i++)
+        for(i=0;i<=longueur_obstacle;i++)
         {
             if(x_pastille == obstacle[i].x && y_pastille == obstacle[i].y)
             {
@@ -55,16 +66,6 @@ PIXELS gen_pastille(PIXELS *serpent,PIXELS *pastilles,PIXELS *obstacle,unsigned
         }     
     }while(ok);
 
-    if(x_pastille < DECALEMENT)
-    {
-        x_pastille =+ DECALEMENT;
-    }
-
-    if(y_pastille < DECALEMENT)
-    {
-        y_pastille =+ DECALEMENT;
-    }
-
 
     pastille.x = x_pastille ;
     pastille.y = y_pastille ;
@@ -85,7 +86,18 @@ PIXELS gen_obstacle(PIXELS *serpent,PIXELS *pastilles,PIXELS *obstacle,unsigned
         ok = 0;
         x_obstacles= ArrondirPixel(rand()%W_GAME);
         y_obstacles = ArrondirPixel(rand()%H_GAME);
-        for(i=0;i<longueur_serpent;i++)
+
+        if(x_obstacles < DECALEMENT)
+        {
+            x_obstacles =+ DECALEMENT;
+        }
+
+        if(y_obstacles < DECALEMENT)
+        {
+            y_obstacles =+ DECALEMENT;
+        }
+
+        for(i=0;i<=longueur_serpent;i++)
         {
             if(x_obstacles == serpent[i].x && y_obstacles == serpent[i].y)
             {
@@ -97,7 +109,7 @@ PIXELS gen_obstacle(PIXELS *serpent,PIXELS *pastilles,PIXELS *obstacle,unsigned
             }
 
         }       
-        for(i=0;i<longueur_pastilles;i++)
+        for(i=0;i<=longueur_pastilles;i++)
         {
             if(x_obstacles == pastilles[i].x && y_obstacles == pastilles[i].y)
             {
@@ -111,16 +123,6 @@ PIXELS gen_obstacle(PIXELS *serpent,PIXELS *pastilles,PIXELS *obstacle,unsigned
         }       
     }while(ok);
 
-    if(x_obstacles < DECALEMENT)
-    {
-        x_obstacles =+ DECALEMENT;
-    }
-
-    if(y_obstacles < DECALEMENT)
-    {
-        y_obstacles =+ DECALEMENT;
-    }
-
 
     obstacles.x = x_obstacles ;
     obstacles.y = y_obstacles ;
diff --git a/score.txt b/score.txt
index 7ed6ff8..f8c9d43 100644
--- a/score.txt
+++ b/score.txt
@@ -1 +1 @@
-5
+235