From 97cbf067486dad2ac087ece11422110921575b56 Mon Sep 17 00:00:00 2001 From: Aubin DORIVAL Date: Mon, 30 Dec 2024 13:20:40 +0100 Subject: [PATCH] affichage score --- include/function.h | 3 ++- script/function.c | 23 ++++++++++++++++++++++- script/main.c | 4 +++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/include/function.h b/include/function.h index a619a96..5e6b5fa 100644 --- a/include/function.h +++ b/include/function.h @@ -52,6 +52,7 @@ int inGameLoop (char **tab2d, vect *dim_tab, vect *playerPos, vect *targets, int nbr_targets, dis *display_user, score *score_user, bool menu); char *timeToText(time_t time); void nullScore(score *player_score); -void winOrLoseLoop(dis *display_user, bool win); +void winOrLoseLoop(dis *display_user,score *score_user, bool win); +unsigned int scoreCalculator(score *score_user, bool win); #endif // FONCTION_H diff --git a/script/function.c b/script/function.c index 45cbd36..77b5b56 100644 --- a/script/function.c +++ b/script/function.c @@ -636,7 +636,7 @@ void nullScore(score *player_score) * \param display_user Tout les information du display de l'utilisateur utile. * \param win Si on veut un affichage de victoire ou non. */ -void winOrLoseLoop(dis *display_user, bool win) +void winOrLoseLoop(dis *display_user,score *score_user, bool win) { SDL_Surface *img; SDL_Texture *texture; @@ -648,6 +648,12 @@ void winOrLoseLoop(dis *display_user, bool win) vect pos = {0, 0}; displayImage(display_user->renderer, texture, pos, display_user->size_window); + + char text[50] = ""; + snprintf(text, 50, "Score : %u",scoreCalculator(score_user, win)); + vect pos_score = {10, (display_user->size_window*3) / 4}; + vect size_score = {display_user->size_window / 4, display_user->size_window - 10}; + displayTextSDL(display_user, text , pos_score, size_score, 80); SDL_RenderPresent (display_user->renderer); bool finish = false; @@ -659,8 +665,23 @@ void winOrLoseLoop(dis *display_user, bool win) if(event.type == SDL_KEYDOWN && event.key.keysym.scancode == SDL_SCANCODE_RETURN) finish = true; SDL_Delay(16); } + + SDL_FreeSurface (img); SDL_DestroyTexture (texture); return; } + +unsigned int scoreCalculator(score *score_user, bool win) +{ + unsigned short int result = 65535; //plus grand nombre + int time = score_user->after - score_user->before; + printf("%d\n", time); + result -= (score_user->move_box + score_user->move_player) * time; + if (!win) + { + result /= 50; + } + return result; +} diff --git a/script/main.c b/script/main.c index dc8179d..6785f31 100644 --- a/script/main.c +++ b/script/main.c @@ -57,6 +57,7 @@ int main () } else { + score_user.before = time(NULL); char txt[30] = ""; if(output <= 3) { @@ -72,7 +73,8 @@ int main () targets = fileToTab2D(txt, tab2d, SIZE_PLAY, playerPos, &nbr_targets); output = inGameLoop(tab2d, &dim, playerPos, targets, nbr_targets, &display_user, &score_user, false); SDL_RenderClear(display_user.renderer); - if(output != -1000) winOrLoseLoop(&display_user, output==1); + score_user.after = time(NULL); + if(output != -1000) winOrLoseLoop(&display_user, &score_user, output==1); output = -1; } }