Fix generation pastilles sur le serpent

This commit is contained in:
David AKAGUNDUZ 2023-12-12 17:29:02 +01:00
parent 15a99a4583
commit f83e28683d
7 changed files with 31 additions and 20 deletions

@ -22,7 +22,7 @@ void AfficherTimerEtScore(int m,int n,int score) /*Afficher le temps passé et
CopierZone(1,0,0,0,930,710,0,0);
}
void InitialiserPastilles(PIXELS *pastilles) {
void InitialiserPastilles(PIXELS *pastilles, PIXELS *serpent, int longueur_serpent) {
int i;
couleur r;
@ -32,25 +32,24 @@ void InitialiserPastilles(PIXELS *pastilles) {
srand(time(NULL));
for (i = 0; i < PASTILLES; i++) {
pastilles[i] = gen_pastille();
pastilles[i] = gen_pastille(serpent,longueur_serpent);
RemplirRectangle(pastilles[i].x,pastilles[i].y,T_PIXEL,T_PIXEL);
}
}
int MangerPastille(PIXELS *serpent, PIXELS* pastilles,unsigned long *score)
int MangerPastille(PIXELS *serpent, PIXELS* pastilles,unsigned long *score,int longueur_serpent)
{
couleur r;
int i = 0;
int j = 0;
r = CouleurParNom("red");
ChoisirCouleurDessin(r);
for(i=0;i<PASTILLES;i++)
{
if(serpent[0].x == pastilles[i].x && serpent[0].y == pastilles[i].y)
{
pastilles[i] = gen_pastille();
pastilles[i] = gen_pastille(serpent,longueur_serpent);
RemplirRectangle(pastilles[i].x,pastilles[i].y,T_PIXEL,T_PIXEL);
*score+=5;
return 1;

@ -3,11 +3,11 @@
#ifndef EVENEMENT_H
#define EVENEMENT_H
int MangerPastille(PIXELS *serpent, PIXELS* pastilles,unsigned long *score);
int MangerPastille(PIXELS *serpent, PIXELS* pastilles,unsigned long *score,int longueur_serpent);
void DeplacementSerpent(int direction ,PIXELS *serpent, int longueur);
void InitialiserPastilles(PIXELS *pastilles);
void InitialiserPastilles(PIXELS *pastilles, PIXELS *serpent, int longueur_serpent);
void AfficherTimerEtScore(int m,int n,unsigned long *score);

Binary file not shown.

34
main.c

@ -18,18 +18,30 @@ int ArrondirPixel(int nombre) /* Calcule un arrondi du pixel pour pouvoir respec
return arrondi;
}
PIXELS gen_pastille()
PIXELS gen_pastille(PIXELS *serpent,int longueur_serpent)
/* 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*/
/*Code n'est pas complet*/
/*-Elles se génèrent à des endroits qui peuvent être les mêmes ou gêner le snake*/
{
int x_pastille,y_pastille;
int x_pastille,y_pastille,i;
int ok = 0;
PIXELS pastille;
x_pastille= ArrondirPixel(rand()%W_GAME);
y_pastille = ArrondirPixel(rand()%H_GAME);
do{
ok = 0;
x_pastille= ArrondirPixel(rand()%W_GAME);
y_pastille = ArrondirPixel(rand()%H_GAME);
for(i=0;i<longueur_serpent;i++)
{
if(x_pastille == serpent[i].x && y_pastille == serpent[i].y)
{
ok = 1;
}
}
}while(ok);
if(x_pastille < DECALEMENT)
{
@ -74,14 +86,14 @@ void InitialisationDuSerpent(PIXELS *p_serpent) /* L'initialisation du serpent *
}
}
void DessinerScene(PIXELS *pastilles, PIXELS *serpent) /* Dessine la scène */
void DessinerScene(PIXELS *pastilles, PIXELS *serpent, int longueur_serpent) /* Dessine la scène */
{
couleur c;
ChoisirEcran(2);
c=CouleurParNom("lightgreen");
ChoisirCouleurDessin(c);
RemplirRectangle(T_PIXEL,T_PIXEL,W_GAME,H_GAME);
InitialiserPastilles(pastilles);
InitialiserPastilles(pastilles,serpent,longueur_serpent);
InitialisationDuSerpent(serpent);
}
@ -95,8 +107,8 @@ int main()
int n = 0;
int m = 0;
size_t longueur_serpent = 10;
size_t longueur_pastilles = 10;
size_t longueur_serpent = 200;
size_t longueur_pastilles = PASTILLES;
PIXELS *serpent = (PIXELS *)malloc(longueur_serpent * sizeof(PIXELS));
PIXELS *pastilles = (PIXELS *)malloc(longueur_pastilles * sizeof(PIXELS));
@ -116,7 +128,7 @@ int main()
InitialiserGraphique();
CreerFenetre(10,10,W_WINDOW,H_WINDOW); /* Peut être changer cette ligne avec la fonction Maxx et Maxy fournie dans graph.h ??*/
ChoisirTitreFenetre("SNAKE SAE11 IN C");
DessinerScene(pastilles,serpent);
DessinerScene(pastilles,serpent,longueur_serpent);
while(go_on) /* Lancement du cycle pour les Inputs et le Jeu*/
{
@ -183,7 +195,7 @@ int main()
direction_davant = direction; /* Check si le serpent à le droit de changer de direction */
DeplacementSerpent(direction,serpent,longueur_serpent);
if(MangerPastille(serpent,pastilles,&score) == 1)
if(MangerPastille(serpent,pastilles,&score,longueur_serpent) == 1)
{
longueur_serpent+=2;
serpent = (PIXELS*) realloc(serpent,longueur_serpent * sizeof(PIXELS));

2
main.h

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

BIN
main.o

Binary file not shown.

BIN
prog

Binary file not shown.