diff --git a/gui.c b/gui.c index 62a7958..b005bdd 100644 --- a/gui.c +++ b/gui.c @@ -2,35 +2,91 @@ #include <stdio.h> #include <graph.h> -void Menu(unsigned long *bestscore) +int lireScore() +{ + int score; + FILE* fichier = fopen("score.txt", "r"); + if (fichier == NULL) + { + fprintf(stderr, "Le fichier score.txt n'existe pas. Création du fichier avec la valeur 0.\n"); + + score = 0; + + /* Ouvrir le fichier en mode écriture pour créer le fichier s'il n'existe pas */ + fichier = fopen("score.txt", "w"); + if (fichier == NULL) + { + fprintf(stderr, "Erreur lors de la création du fichier score.txt\n"); + return -1; + } + + /* Écrire la valeur 0 par défaut */ + fprintf(fichier, "%d\n", score); + + fclose(fichier); + return score; + } + + fscanf(fichier, "%d", &score); + fclose(fichier); + return score; +} + +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"); + return; + } + + fprintf(fichier, "%ld\n", nouveauScore); + + fclose(fichier); +} + +void CheckScore(unsigned long nouveauScore) +{ + unsigned long scoreActuel = lireScore(); + + if (nouveauScore > scoreActuel || scoreActuel == 0) + { + sauvegarderScore(nouveauScore); + printf("Le score a été enregistré avec succès.\n"); + } + else + { + printf("Le score précédent est plus grand ou égal. Aucun changement effectué.\n"); + } +} + +void Menu() { char buf[100]; couleur text; + unsigned long bestscore = lireScore(); text=CouleurParComposante(78, 93, 47); ChoisirEcran(0); ChargerImage("./images/Menu.png",0,0,0,0,930,710); ChoisirCouleurDessin(text); - snprintf(buf,100,"BEST SCORE : %07ld",*bestscore); - EcrireTexte(450,470,buf,2); + snprintf(buf,100,"BEST SCORE : %07ld",bestscore); + EcrireTexte(420,350,buf,2); } -void Pause(unsigned long *bestscore) +void Pause() { - char buf[100]; - couleur text; - text=CouleurParComposante(78, 93, 47); ChoisirEcran(0); ChargerImage("./images/Pause.png",0,0,0,0,930,710); - ChoisirCouleurDessin(text); - snprintf(buf,100,"BEST SCORE : %07ld",*bestscore); - EcrireTexte(450,300,buf,2); + } -void PerduGUI(unsigned long *bestscore) +void PerduGUI() { char buf[100]; + unsigned long bestscore = lireScore(); ChoisirEcran(0); ChargerImage("./images/Perdu.png",0,0,0,0,930,710); - snprintf(buf,100,"BEST SCORE : %07ld",*bestscore); - EcrireTexte(450,250,buf,2); + snprintf(buf,100,"BEST SCORE : %07ld",bestscore); + EcrireTexte(420,350,buf,2); } \ No newline at end of file diff --git a/gui.h b/gui.h index 39a4d68..7999912 100644 --- a/gui.h +++ b/gui.h @@ -1,10 +1,13 @@ #ifndef GUI_H #define GUI_H +void CheckScore(unsigned long nouveauScore); +void sauvegarderScore(unsigned long nouveauScore); +int lireScore(); + void Menu(); -void PerduGUI(unsigned long *bestscore) -;void Pause(unsigned long *bestscore); -void Reinitialiser(); -int AfficherGUI(unsigned char menu); +void PerduGUI(); +void Pause(); +int AfficherGUI(); #endif \ No newline at end of file diff --git a/images/Menu.png b/images/Menu.png index 2d2beb1..a776e2b 100644 Binary files a/images/Menu.png and b/images/Menu.png differ diff --git a/images/Menu.png:Zone.Identifier b/images/Menu.png:Zone.Identifier new file mode 100644 index 0000000..1f57089 --- /dev/null +++ b/images/Menu.png:Zone.Identifier @@ -0,0 +1,4 @@ +[ZoneTransfer] +ZoneId=3 +ReferrerUrl=https://pixlr.com/fr/express/ +HostUrl=https://pixlr.com/fr/express/ diff --git a/images/Pause.png b/images/Pause.png index 99d037a..347d444 100644 Binary files a/images/Pause.png and b/images/Pause.png differ diff --git a/images/Pause.png:Zone.Identifier b/images/Pause.png:Zone.Identifier new file mode 100644 index 0000000..1f57089 --- /dev/null +++ b/images/Pause.png:Zone.Identifier @@ -0,0 +1,4 @@ +[ZoneTransfer] +ZoneId=3 +ReferrerUrl=https://pixlr.com/fr/express/ +HostUrl=https://pixlr.com/fr/express/ diff --git a/images/Perdu.png b/images/Perdu.png index e5cd2b4..965a3ab 100644 Binary files a/images/Perdu.png and b/images/Perdu.png differ diff --git a/images/Perdu.png:Zone.Identifier b/images/Perdu.png:Zone.Identifier new file mode 100644 index 0000000..1f57089 --- /dev/null +++ b/images/Perdu.png:Zone.Identifier @@ -0,0 +1,4 @@ +[ZoneTransfer] +ZoneId=3 +ReferrerUrl=https://pixlr.com/fr/express/ +HostUrl=https://pixlr.com/fr/express/ diff --git a/main.c b/main.c index 7361f89..f8fb1c0 100644 --- a/main.c +++ b/main.c @@ -14,7 +14,6 @@ int main() { unsigned char pause = 0; unsigned long score = 0; - unsigned long bestscore = 0; unsigned long suivant; unsigned long suivant2; int game_on=0; @@ -57,11 +56,8 @@ int main() while(window_on) { if(perdu == 1) - { if(score>bestscore) - { - bestscore=score; - } - PerduGUI(&bestscore); + { + PerduGUI(); if (ToucheEnAttente() == 1) { switch (Touche()) @@ -74,7 +70,7 @@ int main() } else { - Menu(&bestscore); + Menu(); if (ToucheEnAttente() == 1) { switch (Touche()) @@ -93,6 +89,7 @@ int main() case XK_Escape: free(serpent); free(pastilles); + free(obstacle); FermerGraphique(); return EXIT_SUCCESS; } @@ -148,6 +145,7 @@ int main() { free(serpent); free(pastilles); + free(obstacle); FermerGraphique(); return EXIT_SUCCESS; } @@ -157,7 +155,7 @@ int main() if(pause==1) { - Pause(&bestscore); + Pause(); } } @@ -189,6 +187,7 @@ int main() } else if (valeur_retourne == 2) { + CheckScore(score); perdu=1; game_on=0; } @@ -199,6 +198,7 @@ int main() } free(serpent); free(pastilles); + free(obstacle); FermerGraphique(); return EXIT_SUCCESS; } diff --git a/main.h b/main.h index 37bc429..7f6bd16 100644 --- a/main.h +++ b/main.h @@ -8,7 +8,7 @@ #define T_PIXEL 15 #define DECALEMENT 30 #define PASTILLES 5 -#define OBSTACLE 100 +#define OBSTACLE 30 diff --git a/score.txt b/score.txt new file mode 100644 index 0000000..7ed6ff8 --- /dev/null +++ b/score.txt @@ -0,0 +1 @@ +5