diff --git a/evenement.c b/evenement.c index 1e0e6ab..867b40f 100644 --- a/evenement.c +++ b/evenement.c @@ -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; diff --git a/evenement.h b/evenement.h index 53aa91e..2d397c4 100644 --- a/evenement.h +++ b/evenement.h @@ -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); diff --git a/evenement.o b/evenement.o index 8e1a245..46b8640 100644 Binary files a/evenement.o and b/evenement.o differ diff --git a/main.c b/main.c index 871d46f..9a6b51c 100644 --- a/main.c +++ b/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)); diff --git a/main.h b/main.h index 031d981..67e5cc9 100644 --- a/main.h +++ b/main.h @@ -19,6 +19,6 @@ struct PIXELS { typedef struct PIXELS PIXELS; -PIXELS gen_pastille(); +PIXELS gen_pastille(PIXELS *serpent,int longueur_serpent); #endif diff --git a/main.o b/main.o index 3c504eb..4772387 100644 Binary files a/main.o and b/main.o differ diff --git a/prog b/prog index 746595a..5af8e29 100755 Binary files a/prog and b/prog differ