From 9f51622e59ddfb56682e619b43829e433bbb8c20 Mon Sep 17 00:00:00 2001 From: Aubin DORIVAL Date: Mon, 16 Dec 2024 21:35:55 +0100 Subject: [PATCH] mb and mp --- function.c | 44 ++++++++++++++++++++++++++++++++++---------- function.h | 2 +- main.c | 4 +++- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/function.c b/function.c index de5ff9a..73f53ee 100644 --- a/function.c +++ b/function.c @@ -99,7 +99,8 @@ short int canIGoDirection (short int valueOfNCase, return 0; } -void move (unsigned short int **tab, vect *playerPos, vect direction) +void move (unsigned short int **tab, vect *playerPos, vect direction, + score *score_user) { short int valueOfNCase = tab[playerPos->x + direction.x][playerPos->y + direction.y]; @@ -121,6 +122,9 @@ void move (unsigned short int **tab, vect *playerPos, vect direction) = canIGoDirection (valueOfNCase, valueOfNPlusOneCase); short int playerState = tab[playerPos->x][playerPos->y]; + if (returnValue != 0) + score_user->move_player++; + switch (returnValue) { case 0: @@ -136,6 +140,7 @@ void move (unsigned short int **tab, vect *playerPos, vect direction) tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER; tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2] = BOX; + score_user->move_box++; break; case 3: // move player and the box is well-placed @@ -143,6 +148,7 @@ void move (unsigned short int **tab, vect *playerPos, vect direction) tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER; tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2] = BOX_ON_TARGET; + score_user->move_box++; break; case 4: // move player on a target @@ -157,6 +163,7 @@ void move (unsigned short int **tab, vect *playerPos, vect direction) = BOX; tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER_ON_TARGET; + score_user->move_box++; break; case 6: // move player on a target and box on a target @@ -165,6 +172,7 @@ void move (unsigned short int **tab, vect *playerPos, vect direction) = BOX_ON_TARGET; tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER_ON_TARGET; + score_user->move_box++; break; default: printf ("Commande inconnue !\n"); @@ -186,12 +194,19 @@ void move (unsigned short int **tab, vect *playerPos, vect direction) } void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, - vect *targets, int nbr_targets, dis *display_user) + vect *targets, int nbr_targets, dis *display_user, + score *score_user) { - vect size_time - = { display_user->size_menu, display_user->size_window / 3 }; + vect size_menu + = { display_user->size_menu -10, display_user->size_window / 3 -10}; vect coor_time - = { 0, display_user->size_window - display_user->size_menu }; + = { 10, display_user->size_window - display_user->size_menu + 10}; + vect coor_move_player + = { display_user->size_window / 3 + 10, + display_user->size_window - display_user->size_menu + 10}; + vect coor_move_box + = { (display_user->size_window / 3) * 2 + 10, + display_user->size_window - display_user->size_menu + 10}; time_t time_start = time (NULL); time_t current_time = time (NULL); time_t delay = 0; @@ -231,7 +246,17 @@ void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, direction.x = 0; direction.y = 0; } - move (tab2d, playerPos, direction); + move (tab2d, playerPos, direction, score_user); + + screenDisplayGameSDL (tab2d, display_user); + char nbr_p[20]; + snprintf (nbr_p, 20, "MP :%d", score_user->move_player); + char nbr_b[20]; + snprintf (nbr_b, 20, "MB :%d", score_user->move_box); + + displayTextSDL (display_user, nbr_p, coor_move_player, size_menu, + 50); + displayTextSDL (display_user, nbr_b, coor_move_box, size_menu, 50); if (isWin (tab2d, targets, nbr_targets)) { puts ("Win!"); @@ -242,15 +267,14 @@ void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, puts ("lose!"); finish = true; } - screenDisplayGameSDL (tab2d, display_user); } current_time = time (NULL); if (current_time > delay) { delay = current_time; - displayTextSDL (display_user, - timeToText (time (NULL) - time_start), coor_time, - size_time, 50); + char *char_time = timeToText (time(NULL) - time_start); + displayTextSDL (display_user, char_time, coor_time, + size_menu, 50); } SDL_Delay (16); diff --git a/function.h b/function.h index 94cf6b7..6fe904f 100644 --- a/function.h +++ b/function.h @@ -47,7 +47,7 @@ bool islose (unsigned short int **tab2d, const int N); bool blockBox (unsigned short int **tab2d, vect box_coor); vect plusVect (vect one, vect two); void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, - vect *targets, int nbr_targets, dis *display_user); + vect *targets, int nbr_targets, dis *display_user, score *score_user); char *timeToText(time_t time); #endif // FONCTION_H diff --git a/main.c b/main.c index fa5ec89..993b053 100644 --- a/main.c +++ b/main.c @@ -23,13 +23,15 @@ int main () score playerScore; unsigned short int **tab2d = creatArea2D (SIZE_PLAY); + score score_user = {0, 0, 0,0}; + targets = fileToTab2D ("test.txt", tab2d, SIZE_PLAY, playerPos, &nbr_targets); screenDisplayGameSDL (tab2d, &display_user); screenDisplay (tab2d, SIZE_PLAY); inGameLoop (tab2d, SIZE_PLAY, playerPos, targets, nbr_targets, - &display_user); + &display_user, &score_user); SDL_DestroyWindow (display_user.window); SDL_DestroyRenderer (display_user.renderer);