Ajout du timer en secondes réelles, mort serpent quand il se touche

This commit is contained in:
Vincent TEISSIER 2023-12-14 11:25:22 +01:00
parent f83e28683d
commit 04623f82ec
8 changed files with 71 additions and 19 deletions

@ -5,18 +5,27 @@
#include <time.h> #include <time.h>
void AfficherTimerEtScore(int m,int n,int score) /*Afficher le temps passé et le score*/ void AfficherTimerEtScore(long unsigned int *score, long unsigned int temps) /*Afficher le temps passé et le score*/
{ {
char buf[100]; char buf[100];
char buff[100]; char buff[100]; /* Stockage du score et du timer */
couleur j; couleur j;
time_t fin_temps;
long int timer;
int minutes,secondes;
fin_temps = time(NULL); /* Gestion du timer et des variables */
timer = difftime(fin_temps, temps);
secondes = timer % 60;
minutes = timer / 60;
j=CouleurParNom("yellow"); j=CouleurParNom("yellow");
ChoisirCouleurDessin(j); ChoisirCouleurDessin(j);
ChoisirEcran(1); ChoisirEcran(1);
CopierZone(2,1,0,0,930,710,0,0); CopierZone(2,1,0,0,930,710,0,0);
snprintf(buf,100,"temps : %02d:%02d",m,n); snprintf(buf,100,"TEMPS : %02d:%02d",minutes,secondes);
snprintf(buff,100,"SCORE : %07d",score); snprintf(buff,100,"SCORE : %07ld",*score);
EcrireTexte(60,650,buf,2); EcrireTexte(60,650,buf,2);
EcrireTexte(600,650,buff,2); EcrireTexte(600,650,buff,2);
CopierZone(1,0,0,0,930,710,0,0); CopierZone(1,0,0,0,930,710,0,0);
@ -32,30 +41,40 @@ void InitialiserPastilles(PIXELS *pastilles, PIXELS *serpent, int longueur_serpe
srand(time(NULL)); srand(time(NULL));
for (i = 0; i < PASTILLES; i++) { for (i = 0; i < PASTILLES; i++) {
pastilles[i] = gen_pastille(serpent,longueur_serpent); pastilles[i] = gen_pastille(serpent,pastilles,longueur_serpent);
RemplirRectangle(pastilles[i].x,pastilles[i].y,T_PIXEL,T_PIXEL); RemplirRectangle(pastilles[i].x,pastilles[i].y,T_PIXEL,T_PIXEL);
} }
} }
void MourrirSerpent(PIXELS *serpent, int longueur_serpent)
{
int i = 0;
for(i=1;i<longueur_serpent;i++)
{
if(serpent[0].x == serpent[i].x && serpent[0].y == serpent[i].y )
{
FermerGraphique();
}
}
}
int MangerPastille(PIXELS *serpent, PIXELS* pastilles,unsigned long *score,int longueur_serpent) int MangerPastille(PIXELS *serpent, PIXELS* pastilles,unsigned long *score,int longueur_serpent)
{ {
couleur r; couleur r;
int i = 0; int i = 0;
int j = 0;
r = CouleurParNom("red"); r = CouleurParNom("red");
ChoisirCouleurDessin(r); ChoisirCouleurDessin(r);
for(i=0;i<PASTILLES;i++) for(i=0;i<PASTILLES;i++)
{ {
if(serpent[0].x == pastilles[i].x && serpent[0].y == pastilles[i].y) if(serpent[0].x == pastilles[i].x && serpent[0].y == pastilles[i].y)
{ {
pastilles[i] = gen_pastille(serpent,longueur_serpent); pastilles[i] = gen_pastille(serpent,pastilles,longueur_serpent);
RemplirRectangle(pastilles[i].x,pastilles[i].y,T_PIXEL,T_PIXEL); RemplirRectangle(pastilles[i].x,pastilles[i].y,T_PIXEL,T_PIXEL);
*score+=5; *score+=5;
return 1; return 1;
} }
} }
return 0; return 0;
} }
@ -97,6 +116,7 @@ void DeplacementSerpent(int direction ,PIXELS *serpent, int longueur)
tempY = tempY2; tempY = tempY2;
} }
ChoisirCouleurDessin(g); ChoisirCouleurDessin(g);
RemplirRectangle(serpent[longueur-1].x,serpent[longueur-1].y,T_PIXEL,T_PIXEL); RemplirRectangle(serpent[longueur-1].x,serpent[longueur-1].y,T_PIXEL,T_PIXEL);

@ -9,9 +9,11 @@ void DeplacementSerpent(int direction ,PIXELS *serpent, int longueur);
void InitialiserPastilles(PIXELS *pastilles, PIXELS *serpent, int longueur_serpent); void InitialiserPastilles(PIXELS *pastilles, PIXELS *serpent, int longueur_serpent);
void AfficherTimerEtScore(int m,int n,unsigned long *score); void AfficherTimerEtScore(long unsigned int *score, long unsigned int temps);
int PastilleSurSerpent(PIXELS pastille, PIXELS *serpent, int longueur_serpent); int PastilleSurSerpent(PIXELS pastille, PIXELS *serpent, int longueur_serpent);
void MourrirSerpent(PIXELS *serpent, int longueur_serpent);
#endif #endif

Binary file not shown.

22
main.c

@ -18,7 +18,7 @@ int ArrondirPixel(int nombre) /* Calcule un arrondi du pixel pour pouvoir respec
return arrondi; return arrondi;
} }
PIXELS gen_pastille(PIXELS *serpent,int longueur_serpent) PIXELS gen_pastille(PIXELS *serpent,PIXELS *pastilles,int longueur_serpent)
/* nb_pastille = int nombre de pastille voulue , p_pastilles est un pointeur d'un tableau de pixels qui sont des pastilles*/ /* nb_pastille = int nombre de pastille voulue , p_pastilles est un pointeur d'un tableau de pixels qui sont des pastilles*/
/*Générer une pastille dans la grid*/ /*Générer une pastille dans la grid*/
@ -34,13 +34,12 @@ PIXELS gen_pastille(PIXELS *serpent,int longueur_serpent)
y_pastille = ArrondirPixel(rand()%H_GAME); y_pastille = ArrondirPixel(rand()%H_GAME);
for(i=0;i<longueur_serpent;i++) for(i=0;i<longueur_serpent;i++)
{ {
if(x_pastille == serpent[i].x && y_pastille == serpent[i].y) if(x_pastille == serpent[i].x && y_pastille == serpent[i].y && x_pastille == pastilles[i].x && y_pastille == pastilles[i].y )
{ {
ok = 1; ok = 1; /* Check si la pastille se genère sur une coordonné du serpent ou d'une pastille existante */
} }
} }
}while(ok); }while(ok);
if(x_pastille < DECALEMENT) if(x_pastille < DECALEMENT)
@ -101,13 +100,15 @@ void DessinerScene(PIXELS *pastilles, PIXELS *serpent, int longueur_serpent) /*
int main() int main()
{ {
unsigned char pause = 0; unsigned char pause = 0;
unsigned long *score =0; unsigned long score =0;
unsigned long suivant; unsigned long suivant;
int go_on=1; int go_on=1;
int n = 0; int n = 0;
int m = 0; int m = 0;
time_t temps;
size_t longueur_serpent = 200; size_t longueur_serpent = 12;
size_t longueur_pastilles = PASTILLES; size_t longueur_pastilles = PASTILLES;
PIXELS *serpent = (PIXELS *)malloc(longueur_serpent * sizeof(PIXELS)); PIXELS *serpent = (PIXELS *)malloc(longueur_serpent * sizeof(PIXELS));
PIXELS *pastilles = (PIXELS *)malloc(longueur_pastilles * sizeof(PIXELS)); PIXELS *pastilles = (PIXELS *)malloc(longueur_pastilles * sizeof(PIXELS));
@ -115,6 +116,8 @@ int main()
int direction = 0; int direction = 0;
int direction_davant = 0; int direction_davant = 0;
temps = time(NULL); /*Première mesure du temps*/
if(serpent == NULL) { if(serpent == NULL) {
fprintf(stderr, "Erreur d'allocation de mémoire.\n"); fprintf(stderr, "Erreur d'allocation de mémoire.\n");
return EXIT_FAILURE; return EXIT_FAILURE;
@ -189,11 +192,12 @@ int main()
n=0; n=0;
} }
AfficherTimerEtScore(m,n,score); AfficherTimerEtScore(&score,temps);
suivant=Microsecondes()+delta; suivant=Microsecondes()+delta;
direction_davant = direction; /* Check si le serpent à le droit de changer de direction */ direction_davant = direction; /* Check si le serpent à le droit de changer de direction */
MourrirSerpent(serpent,longueur_serpent);
DeplacementSerpent(direction,serpent,longueur_serpent); DeplacementSerpent(direction,serpent,longueur_serpent);
if(MangerPastille(serpent,pastilles,&score,longueur_serpent) == 1) if(MangerPastille(serpent,pastilles,&score,longueur_serpent) == 1)
{ {

2
main.h

@ -19,6 +19,6 @@ struct PIXELS {
typedef struct PIXELS PIXELS; typedef struct PIXELS PIXELS;
PIXELS gen_pastille(PIXELS *serpent,int longueur_serpent); PIXELS gen_pastille(PIXELS *serpent,PIXELS *pastilles,int longueur_serpent);
#endif #endif

BIN
main.o

Binary file not shown.

BIN
prog

Binary file not shown.

26
test.c Normal file

@ -0,0 +1,26 @@
#include <stdio.h>
#include <time.h>
void afficherTemps(double secondes) {
int minutes = (int)(secondes / 60);
int secondesRestantes = (int)(secondes) % 60;
printf("%02d:%02d\n", minutes, secondesRestantes);
}
int main(void)
{
time_t start, end;
double cpu_time_used;
start = time(NULL);
end = time(NULL);
cpu_time_used = difftime(end, start);
printf("Le programme a pris : ");
afficherTemps(cpu_time_used);
return 0;
}