mb and mp

This commit is contained in:
2024-12-16 21:35:55 +01:00
parent e2d0e8e605
commit 9f51622e59
3 changed files with 38 additions and 12 deletions

View File

@@ -99,7 +99,8 @@ short int canIGoDirection (short int valueOfNCase,
return 0; 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 short int valueOfNCase
= tab[playerPos->x + direction.x][playerPos->y + direction.y]; = 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); = canIGoDirection (valueOfNCase, valueOfNPlusOneCase);
short int playerState = tab[playerPos->x][playerPos->y]; short int playerState = tab[playerPos->x][playerPos->y];
if (returnValue != 0)
score_user->move_player++;
switch (returnValue) switch (returnValue)
{ {
case 0: 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][playerPos->y + direction.y] = PLAYER;
tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2] tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2]
= BOX; = BOX;
score_user->move_box++;
break; break;
case 3: case 3:
// move player and the box is well-placed // 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][playerPos->y + direction.y] = PLAYER;
tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2] tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2]
= BOX_ON_TARGET; = BOX_ON_TARGET;
score_user->move_box++;
break; break;
case 4: case 4:
// move player on a target // move player on a target
@@ -157,6 +163,7 @@ void move (unsigned short int **tab, vect *playerPos, vect direction)
= BOX; = BOX;
tab[playerPos->x + direction.x][playerPos->y + direction.y] tab[playerPos->x + direction.x][playerPos->y + direction.y]
= PLAYER_ON_TARGET; = PLAYER_ON_TARGET;
score_user->move_box++;
break; break;
case 6: case 6:
// move player on a target and box on a target // 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; = BOX_ON_TARGET;
tab[playerPos->x + direction.x][playerPos->y + direction.y] tab[playerPos->x + direction.x][playerPos->y + direction.y]
= PLAYER_ON_TARGET; = PLAYER_ON_TARGET;
score_user->move_box++;
break; break;
default: default:
printf ("Commande inconnue !\n"); 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, 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 vect size_menu
= { display_user->size_menu, display_user->size_window / 3 }; = { display_user->size_menu -10, display_user->size_window / 3 -10};
vect coor_time 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 time_start = time (NULL);
time_t current_time = time (NULL); time_t current_time = time (NULL);
time_t delay = 0; time_t delay = 0;
@@ -231,7 +246,17 @@ void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos,
direction.x = 0; direction.x = 0;
direction.y = 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)) if (isWin (tab2d, targets, nbr_targets))
{ {
puts ("Win!"); puts ("Win!");
@@ -242,15 +267,14 @@ void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos,
puts ("lose!"); puts ("lose!");
finish = true; finish = true;
} }
screenDisplayGameSDL (tab2d, display_user);
} }
current_time = time (NULL); current_time = time (NULL);
if (current_time > delay) if (current_time > delay)
{ {
delay = current_time; delay = current_time;
displayTextSDL (display_user, char *char_time = timeToText (time(NULL) - time_start);
timeToText (time (NULL) - time_start), coor_time, displayTextSDL (display_user, char_time, coor_time,
size_time, 50); size_menu, 50);
} }
SDL_Delay (16); SDL_Delay (16);

View File

@@ -47,7 +47,7 @@ bool islose (unsigned short int **tab2d, const int N);
bool blockBox (unsigned short int **tab2d, vect box_coor); bool blockBox (unsigned short int **tab2d, vect box_coor);
vect plusVect (vect one, vect two); vect plusVect (vect one, vect two);
void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, 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); char *timeToText(time_t time);
#endif // FONCTION_H #endif // FONCTION_H

4
main.c
View File

@@ -23,13 +23,15 @@ int main ()
score playerScore; score playerScore;
unsigned short int **tab2d = creatArea2D (SIZE_PLAY); unsigned short int **tab2d = creatArea2D (SIZE_PLAY);
score score_user = {0, 0, 0,0};
targets targets
= fileToTab2D ("test.txt", tab2d, SIZE_PLAY, playerPos, &nbr_targets); = fileToTab2D ("test.txt", tab2d, SIZE_PLAY, playerPos, &nbr_targets);
screenDisplayGameSDL (tab2d, &display_user); screenDisplayGameSDL (tab2d, &display_user);
screenDisplay (tab2d, SIZE_PLAY); screenDisplay (tab2d, SIZE_PLAY);
inGameLoop (tab2d, SIZE_PLAY, playerPos, targets, nbr_targets, inGameLoop (tab2d, SIZE_PLAY, playerPos, targets, nbr_targets,
&display_user); &display_user, &score_user);
SDL_DestroyWindow (display_user.window); SDL_DestroyWindow (display_user.window);
SDL_DestroyRenderer (display_user.renderer); SDL_DestroyRenderer (display_user.renderer);