ajout obstacle
This commit is contained in:
parent
a239e40e33
commit
ca367ad3fe
19
evenement.c
19
evenement.c
@ -26,7 +26,7 @@ void AfficherTimerEtScore(long unsigned int *score, int minutes,int secondes)
|
||||
CopierZone(1,0,0,0,930,710,0,0);
|
||||
}
|
||||
|
||||
int MourrirSerpent(PIXELS *serpent, int longueur_serpent)
|
||||
int MourrirSerpent(PIXELS *serpent,PIXELS *obstacle, int longueur_serpent,int longueur_obstacle)
|
||||
{
|
||||
int i = 0;
|
||||
for(i=1;i<longueur_serpent;i++)
|
||||
@ -36,6 +36,13 @@ int MourrirSerpent(PIXELS *serpent, int longueur_serpent)
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
for(i=1;i<longueur_obstacle;i++)
|
||||
{
|
||||
if(serpent[0].x == obstacle[i].x && serpent[0].y == obstacle[i].y )
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
if (serpent[0].x<=0 || serpent[0].x>W_GAME || serpent[0].y<=0 || serpent[0].y>H_GAME)
|
||||
{
|
||||
return 1;
|
||||
@ -43,14 +50,14 @@ int MourrirSerpent(PIXELS *serpent, int longueur_serpent)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int MangerPastille(PIXELS *serpent, PIXELS* pastilles,unsigned long *score,int longueur_serpent,unsigned long int *vitesse)
|
||||
int MangerPastille(PIXELS *serpent, PIXELS* pastilles,PIXELS *obstacle,unsigned long *score,int longueur_serpent,unsigned long longueur_obstacle,unsigned long int *vitesse)
|
||||
{
|
||||
int i = 0;
|
||||
for(i=0;i<PASTILLES;i++)
|
||||
{
|
||||
if(serpent[0].x == pastilles[i].x && serpent[0].y == pastilles[i].y)
|
||||
{
|
||||
pastilles[i] = gen_pastille(serpent,pastilles,longueur_serpent);
|
||||
pastilles[i] = gen_pastille(serpent,pastilles,obstacle,longueur_serpent,longueur_obstacle);
|
||||
ChargerImage("./images/PommePastille.png",pastilles[i].x,pastilles[i].y,0,0,T_PIXEL,T_PIXEL);
|
||||
*score+=5;
|
||||
*vitesse/=1.008;
|
||||
@ -106,14 +113,14 @@ void DeplacementSerpent(int direction ,PIXELS *serpent, int longueur)
|
||||
}
|
||||
}
|
||||
|
||||
int Serpent(PIXELS *serpent,PIXELS *pastilles,unsigned long *score,unsigned long *longueur_serpent,unsigned long int *vitesse,int direction)
|
||||
int Serpent(PIXELS *serpent,PIXELS *pastilles,PIXELS *obstacle,unsigned long *score,unsigned long *longueur_serpent,int longueur_obstacle,unsigned long int *vitesse,int direction)
|
||||
{
|
||||
if(MourrirSerpent(serpent,*longueur_serpent) == 1)
|
||||
if(MourrirSerpent(serpent,obstacle,*longueur_serpent,longueur_obstacle) == 1)
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
DeplacementSerpent(direction,serpent,*longueur_serpent);
|
||||
if(MangerPastille(serpent,pastilles,score,*longueur_serpent,vitesse) == 1)
|
||||
if(MangerPastille(serpent,pastilles,obstacle,score,*longueur_serpent,longueur_obstacle,vitesse) == 1)
|
||||
{
|
||||
*longueur_serpent+=2;
|
||||
return 1;
|
||||
|
@ -3,19 +3,17 @@
|
||||
#ifndef EVENEMENT_H
|
||||
#define EVENEMENT_H
|
||||
|
||||
int MangerPastille(PIXELS *serpent, PIXELS* pastilles,unsigned long int *score,int longueur_serpent,unsigned long int *vitesse);
|
||||
int MangerPastille(PIXELS *serpent, PIXELS* pastilles,PIXELS *obstacle,unsigned long *score,int longueur_serpent,unsigned long longueur_obstacle,unsigned long int *vitesse);
|
||||
|
||||
void DeplacementSerpent(int direction ,PIXELS *serpent, int longueur);
|
||||
|
||||
void InitialiserPastilles(PIXELS *pastilles, PIXELS *serpent, int longueur_serpent);
|
||||
|
||||
void AfficherTimerEtScore(long unsigned int *score,int minutes,int secondes);
|
||||
|
||||
int PastilleSurSerpent(PIXELS pastille, PIXELS *serpent, int longueur_serpent);
|
||||
|
||||
int MourrirSerpent(PIXELS *serpent, int longueur_serpent);
|
||||
int MourrirSerpent(PIXELS *serpent,PIXELS *obstacle, int longueur_serpent,int longueur_obstacle);
|
||||
|
||||
int Serpent(PIXELS *serpent,PIXELS *pastilles,unsigned long *score,unsigned long *longueur_serpent,unsigned long int *vitesse,int direction);
|
||||
int Serpent(PIXELS *serpent,PIXELS *pastilles,PIXELS *obstacle,unsigned long *score,unsigned long *longueur_serpent,int longueur_obstacle,unsigned long int *vitesse,int direction);
|
||||
|
||||
|
||||
#endif
|
2
gui.h
2
gui.h
@ -1,7 +1,7 @@
|
||||
#ifndef GUI_H
|
||||
#define GUI_H
|
||||
|
||||
void Menu(unsigned long *bestscore);
|
||||
void Menu();
|
||||
void PerduGUI(unsigned long *bestscore)
|
||||
;void Pause(unsigned long *bestscore);
|
||||
void Reinitialiser();
|
||||
|
BIN
images/Danger.png
Normal file
BIN
images/Danger.png
Normal file
Binary file not shown.
After ![]() (image error) Size: 132 B |
3
images/Danger.png:Zone.Identifier
Normal file
3
images/Danger.png:Zone.Identifier
Normal file
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
HostUrl=about:internet
|
14
main.c
14
main.c
@ -27,8 +27,11 @@ int main()
|
||||
|
||||
size_t longueur_serpent = 10;
|
||||
size_t longueur_pastilles = PASTILLES;
|
||||
size_t longueur_obstacle = OBSTACLE;
|
||||
PIXELS *serpent = (PIXELS *)malloc(longueur_serpent * sizeof(PIXELS));
|
||||
PIXELS *pastilles = (PIXELS *)malloc(longueur_pastilles * sizeof(PIXELS));
|
||||
PIXELS *obstacle = (PIXELS *)malloc(longueur_obstacle * sizeof(PIXELS));
|
||||
|
||||
|
||||
int direction = 0;
|
||||
int direction_davant = 0;
|
||||
@ -41,11 +44,16 @@ int main()
|
||||
fprintf(stderr, "Erreur d'allocation de mémoire.\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if(obstacle == NULL) {
|
||||
fprintf(stderr, "Erreur d'allocation de mémoire.\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
|
||||
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,longueur_serpent);
|
||||
DessinerScene(pastilles,serpent,obstacle,longueur_serpent,longueur_pastilles,longueur_obstacle);
|
||||
while(window_on)
|
||||
{
|
||||
if(perdu == 1)
|
||||
@ -80,7 +88,7 @@ int main()
|
||||
longueur_serpent = 10;
|
||||
direction = 0;
|
||||
direction_davant = 0;
|
||||
DessinerScene(pastilles,serpent,longueur_serpent);
|
||||
DessinerScene(pastilles,serpent,obstacle,longueur_serpent,longueur_pastilles,longueur_obstacle);
|
||||
break;
|
||||
case XK_Escape:
|
||||
free(serpent);
|
||||
@ -175,7 +183,7 @@ int main()
|
||||
AfficherTimerEtScore(&score,minutes,secondes);
|
||||
direction_davant = direction; /* Check si le serpent à le droit de changer de direction */
|
||||
|
||||
valeur_retourne = Serpent(serpent,pastilles,&score,&longueur_serpent,&vitesse,direction);
|
||||
valeur_retourne = Serpent(serpent,pastilles,obstacle,&score,&longueur_serpent,longueur_obstacle,&vitesse,direction);
|
||||
if(valeur_retourne == 1)
|
||||
{
|
||||
}
|
||||
|
3
main.h
3
main.h
@ -8,6 +8,7 @@
|
||||
#define T_PIXEL 15
|
||||
#define DECALEMENT 30
|
||||
#define PASTILLES 5
|
||||
#define OBSTACLE 100
|
||||
|
||||
|
||||
|
||||
@ -19,6 +20,6 @@ struct PIXELS {
|
||||
|
||||
typedef struct PIXELS PIXELS;
|
||||
|
||||
PIXELS gen_pastille(PIXELS *serpent,PIXELS *pastilles,int longueur_serpent);
|
||||
PIXELS gen_pastille(PIXELS *serpent,PIXELS *pastilles,PIXELS *obstacle,int longueur_serpent,int longueur_obstacle);
|
||||
|
||||
#endif
|
||||
|
89
scene.c
89
scene.c
@ -16,7 +16,7 @@ int ArrondirPixel(int nombre) /* Calcule un arrondi du pixel pour pouvoir respec
|
||||
return arrondi;
|
||||
}
|
||||
|
||||
PIXELS gen_pastille(PIXELS *serpent,PIXELS *pastilles,int longueur_serpent)
|
||||
PIXELS gen_pastille(PIXELS *serpent,PIXELS *pastilles,PIXELS *obstacle,int longueur_serpent,int longueur_obstacle)
|
||||
|
||||
/* nb_pastille = int nombre de pastille voulue , p_pastilles est un pointeur d'un tableau de pixels qui sont des pastilles*/
|
||||
/*-Elles se génèrent à des endroits qui peuvent être les mêmes ou gêner le snake*/
|
||||
@ -40,6 +40,18 @@ PIXELS gen_pastille(PIXELS *serpent,PIXELS *pastilles,int longueur_serpent)
|
||||
}
|
||||
|
||||
}
|
||||
for(i=0;i<longueur_obstacle;i++)
|
||||
{
|
||||
if(x_pastille == obstacle[i].x && y_pastille == obstacle[i].y)
|
||||
{
|
||||
ok = 1; /* Check si la pastille se genère sur une coordonné du serpent */
|
||||
}
|
||||
if(x_pastille == pastilles[i].x && y_pastille == pastilles[i].y)
|
||||
{
|
||||
ok = 1;
|
||||
}
|
||||
|
||||
}
|
||||
}while(ok);
|
||||
|
||||
if(x_pastille < DECALEMENT)
|
||||
@ -59,6 +71,61 @@ PIXELS gen_pastille(PIXELS *serpent,PIXELS *pastilles,int longueur_serpent)
|
||||
return pastille;
|
||||
}
|
||||
|
||||
PIXELS gen_obstacle(PIXELS *serpent,PIXELS *pastilles,PIXELS *obstacle,int longueur_serpent,int longueur_pastilles)
|
||||
|
||||
/* nb_pastille = int nombre de pastille voulue , p_pastilles est un pointeur d'un tableau de pixels qui sont des pastilles*/
|
||||
/*-Elles se génèrent à des endroits qui peuvent être les mêmes ou gêner le snake*/
|
||||
{
|
||||
int x_obstacles,y_obstacles,i;
|
||||
int ok = 0;
|
||||
PIXELS obstacles;
|
||||
do{
|
||||
ok = 0;
|
||||
x_obstacles= ArrondirPixel(rand()%W_GAME);
|
||||
y_obstacles = ArrondirPixel(rand()%H_GAME);
|
||||
for(i=0;i<longueur_serpent;i++)
|
||||
{
|
||||
if(x_obstacles == serpent[i].x && y_obstacles == serpent[i].y)
|
||||
{
|
||||
ok = 1; /* Check si la pastille se genère sur une coordonné du serpent */
|
||||
}
|
||||
if(x_obstacles == obstacle[i].x && y_obstacles == obstacle[i].y)
|
||||
{
|
||||
ok = 1;
|
||||
}
|
||||
|
||||
}
|
||||
for(i=0;i<longueur_pastilles;i++)
|
||||
{
|
||||
if(x_obstacles == pastilles[i].x && y_obstacles == pastilles[i].y)
|
||||
{
|
||||
ok = 1; /* Check si la pastille se genère sur une coordonné du serpent */
|
||||
}
|
||||
if(x_obstacles == obstacle[i].x && y_obstacles == obstacle[i].y)
|
||||
{
|
||||
ok = 1;
|
||||
}
|
||||
|
||||
}
|
||||
}while(ok);
|
||||
|
||||
if(x_obstacles < DECALEMENT)
|
||||
{
|
||||
x_obstacles =+ DECALEMENT;
|
||||
}
|
||||
|
||||
if(y_obstacles < DECALEMENT)
|
||||
{
|
||||
y_obstacles =+ DECALEMENT;
|
||||
}
|
||||
|
||||
|
||||
obstacles.x = x_obstacles ;
|
||||
obstacles.y = y_obstacles ;
|
||||
|
||||
return obstacles;
|
||||
}
|
||||
|
||||
void InitialisationDuSerpent(PIXELS *p_serpent) /* L'initialisation du serpent */
|
||||
{
|
||||
int x_millieu = 0, y_millieu = 0 , compteur = 0;
|
||||
@ -78,18 +145,29 @@ void InitialisationDuSerpent(PIXELS *p_serpent) /* L'initialisation du serpent *
|
||||
}
|
||||
}
|
||||
|
||||
void InitialiserPastilles(PIXELS *pastilles, PIXELS *serpent, int longueur_serpent) {
|
||||
void InitialiserPastilles(PIXELS *pastilles, PIXELS *serpent,PIXELS *obstacle, int longueur_serpent,int longueur_obstacle) {
|
||||
int i;
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
for (i = 0; i < PASTILLES; i++) {
|
||||
pastilles[i] = gen_pastille(serpent,pastilles,longueur_serpent);
|
||||
pastilles[i] = gen_pastille(serpent,pastilles,obstacle,longueur_serpent,longueur_obstacle);
|
||||
ChargerImage("./images/PommePastille.png",pastilles[i].x,pastilles[i].y,0,0,T_PIXEL,T_PIXEL);
|
||||
}
|
||||
}
|
||||
|
||||
void DessinerScene(PIXELS *pastilles, PIXELS *serpent, int longueur_serpent) /* Dessine la scène */
|
||||
void InitialiserObstacle(PIXELS *pastilles, PIXELS *serpent,PIXELS *obstacle, int longueur_serpent,int longueur_pastilles) {
|
||||
int i;
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
for (i = 0; i < OBSTACLE; i++) {
|
||||
obstacle[i] = gen_obstacle(serpent,pastilles,obstacle,longueur_serpent,longueur_pastilles);
|
||||
ChargerImage("./images/Danger.png",obstacle[i].x,obstacle[i].y,0,0,T_PIXEL,T_PIXEL);
|
||||
}
|
||||
}
|
||||
|
||||
void DessinerScene(PIXELS *pastilles, PIXELS *serpent,PIXELS *obstacle, int longueur_serpent,int longueur_pastilles,int longueur_obstacle) /* Dessine la scène */
|
||||
{
|
||||
couleur bg;
|
||||
couleur border;
|
||||
@ -108,6 +186,7 @@ void DessinerScene(PIXELS *pastilles, PIXELS *serpent, int longueur_serpent) /*
|
||||
|
||||
ChoisirCouleurDessin(bg);
|
||||
RemplirRectangle(T_PIXEL,T_PIXEL,W_GAME,H_GAME);
|
||||
InitialiserPastilles(pastilles,serpent,longueur_serpent);
|
||||
InitialisationDuSerpent(serpent);
|
||||
InitialiserPastilles(pastilles,serpent,obstacle,longueur_serpent,longueur_obstacle);
|
||||
InitialiserObstacle(pastilles,serpent,obstacle,longueur_serpent,longueur_pastilles);
|
||||
}
|
9
scene.h
9
scene.h
@ -3,8 +3,11 @@
|
||||
#ifndef SCENE_H
|
||||
#define SCENE_H
|
||||
|
||||
void InitialiserPastilles(PIXELS *pastilles, PIXELS *serpent, int longueur_serpent);
|
||||
PIXELS gen_pastille(PIXELS *serpent,PIXELS *pastilles,int longueur_serpent);
|
||||
void DessinerScene(PIXELS *pastilles, PIXELS *serpent, int longueur_serpent);
|
||||
PIXELS gen_pastille(PIXELS *serpent,PIXELS *pastilles,PIXELS *obstacle,int longueur_serpent,int longueur_obstacle);
|
||||
PIXELS gen_obstacle(PIXELS *serpent,PIXELS *pastilles,PIXELS *obstacle,int longueur_serpent,int longueur_pastilles);
|
||||
|
||||
void InitialiserPastilles(PIXELS *pastilles, PIXELS *serpent,PIXELS *obstacle, int longueur_serpent,int longueur_obstacle);
|
||||
void InitialiserPastilles(PIXELS *pastilles, PIXELS *serpent,PIXELS *obstacle, int longueur_serpent,int longueur_obstacle);
|
||||
void DessinerScene(PIXELS *pastilles, PIXELS *serpent,PIXELS *obstacle, int longueur_serpent,int longueur_pastilles,int longueur_obstacle);
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user