ajout rectification makefile + struct pixel
This commit is contained in:
parent
b76b89f890
commit
f8d4e822f1
16
Makefile
16
Makefile
@ -0,0 +1,16 @@
|
|||||||
|
CC = gcc
|
||||||
|
CFLAGS = -Wall -Wextra
|
||||||
|
|
||||||
|
all: prog
|
||||||
|
|
||||||
|
prog: main.o evenement.o
|
||||||
|
$(CC) -o $@ $^ -lgraph
|
||||||
|
|
||||||
|
main.o: main.c evenement.h
|
||||||
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
evenement.o: evenement.c evenement.h
|
||||||
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f *.o prog
|
37
evenement.c
37
evenement.c
@ -2,6 +2,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
|
||||||
void AfficherTimerEtScore(int m,int n,int score) /*Afficher le temps passé et le score*/
|
void AfficherTimerEtScore(int m,int n,int score) /*Afficher le temps passé et le score*/
|
||||||
@ -21,18 +22,41 @@ void AfficherTimerEtScore(int m,int n,int score) /*Afficher le temps passé et
|
|||||||
CopierZone(1,0,0,0,930,710,0,0);
|
CopierZone(1,0,0,0,930,710,0,0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void InitialiserPastilles(PIXELS *pastilles) {
|
||||||
|
int i;
|
||||||
|
couleur r;
|
||||||
|
|
||||||
void MangerPastille(int *serpent, int* pastilles, int longueur_serpent, int longueur_pastilles)
|
r = CouleurParNom("red");
|
||||||
|
ChoisirCouleurDessin(r);
|
||||||
|
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
|
for (i = 0; i < PASTILLES; i++) {
|
||||||
|
pastilles[i] = gen_pastille();
|
||||||
|
RemplirRectangle(pastilles[i].x,pastilles[i].y,T_PIXEL,T_PIXEL);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void MangerPastille(int *serpent, PIXELS* pastilles, int longueur_serpent, int longueur_pastilles)
|
||||||
{
|
{
|
||||||
|
couleur r;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
for(i=0;i<longueur_pastilles;i+=2)
|
r = CouleurParNom("red");
|
||||||
|
ChoisirCouleurDessin(r);
|
||||||
|
|
||||||
|
for(i=0;i<PASTILLES;i++)
|
||||||
{
|
{
|
||||||
if(serpent[0] == pastilles[i] && serpent[1] == pastilles[i+1])
|
if(serpent[0] == pastilles[i].x && serpent[1] == pastilles[i].y)
|
||||||
{
|
{
|
||||||
|
printf("\nMANGER !\n");
|
||||||
|
pastilles[i] = gen_pastille();
|
||||||
|
RemplirRectangle(pastilles[i].x,pastilles[i].y,T_PIXEL,T_PIXEL);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,27 +88,22 @@ void DeplacementSerpent(int direction ,int *serpent, int longueur)
|
|||||||
ChoisirCouleurDessin(j);
|
ChoisirCouleurDessin(j);
|
||||||
if(direction == 0) /* Direction vers la gauche */
|
if(direction == 0) /* Direction vers la gauche */
|
||||||
{
|
{
|
||||||
printf("Gauche %d\n",direction);
|
|
||||||
serpent[0]-=T_PIXEL;
|
serpent[0]-=T_PIXEL;
|
||||||
|
|
||||||
RemplirRectangle(serpent[0],serpent[1],T_PIXEL,T_PIXEL);
|
RemplirRectangle(serpent[0],serpent[1],T_PIXEL,T_PIXEL);
|
||||||
printf("Longueur = %d | x %d | y %d",longueur,serpent[longueur-1],serpent[longueur-2]);
|
|
||||||
}
|
}
|
||||||
else if(direction == 1) /* Direction vers le haut */
|
else if(direction == 1) /* Direction vers le haut */
|
||||||
{
|
{
|
||||||
printf("Haut %d\n",direction);
|
|
||||||
serpent[1]-=T_PIXEL;
|
serpent[1]-=T_PIXEL;
|
||||||
RemplirRectangle(serpent[0],serpent[1],T_PIXEL,T_PIXEL);
|
RemplirRectangle(serpent[0],serpent[1],T_PIXEL,T_PIXEL);
|
||||||
}
|
}
|
||||||
else if(direction == 2) /* Direction vers la droite */
|
else if(direction == 2) /* Direction vers la droite */
|
||||||
{
|
{
|
||||||
printf("Droite %d\n",direction);
|
|
||||||
serpent[0]+=T_PIXEL;
|
serpent[0]+=T_PIXEL;
|
||||||
RemplirRectangle(serpent[0],serpent[1],T_PIXEL,T_PIXEL);
|
RemplirRectangle(serpent[0],serpent[1],T_PIXEL,T_PIXEL);
|
||||||
}
|
}
|
||||||
else if(direction == 3) /* Direction vers le bas */
|
else if(direction == 3) /* Direction vers le bas */
|
||||||
{
|
{
|
||||||
printf("Bas %d\n",direction);
|
|
||||||
serpent[1]+=T_PIXEL;
|
serpent[1]+=T_PIXEL;
|
||||||
RemplirRectangle(serpent[0],serpent[1],T_PIXEL,T_PIXEL);
|
RemplirRectangle(serpent[0],serpent[1],T_PIXEL,T_PIXEL);
|
||||||
}
|
}
|
||||||
|
14
evenement.h
14
evenement.h
@ -1,6 +1,14 @@
|
|||||||
|
#include "main.h"
|
||||||
|
|
||||||
|
#ifndef EVENEMENT_H
|
||||||
|
#define EVENEMENT_H
|
||||||
|
|
||||||
|
void MangerPastille(int *serpent, PIXELS* pastilles, int longueur_serpent, int longueur_pastilles);
|
||||||
|
|
||||||
|
void DeplacementSerpent(int direction ,int *serpent, int longueur);
|
||||||
|
|
||||||
|
void InitialiserPastilles(PIXELS *pastilles);
|
||||||
|
|
||||||
void AfficherTimerEtScore(int m,int n,int score);
|
void AfficherTimerEtScore(int m,int n,int score);
|
||||||
|
|
||||||
void MangerPastille(int *serpent, int* pastilles, int longueur_serpent, int longueur_pastilles);
|
#endif
|
||||||
|
|
||||||
void DeplacementSerpent(int direction ,int *serpent, int longueur);
|
|
111
main.c
111
main.c
@ -3,17 +3,7 @@
|
|||||||
#include <graph.h>
|
#include <graph.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include "evenement.h"
|
#include "evenement.h"
|
||||||
|
#include "main.h"
|
||||||
|
|
||||||
#define W_WINDOW 930 /* Largeur de la fenêtre*/
|
|
||||||
#define H_WINDOW 710 /* Hauteur de la fenêtre*/
|
|
||||||
|
|
||||||
#define W_GAME 900
|
|
||||||
#define H_GAME 600
|
|
||||||
|
|
||||||
#define T_PIXEL 15 /* Taille d'un pixel*/
|
|
||||||
|
|
||||||
#define DECALEMENT 30
|
|
||||||
|
|
||||||
#define delta 100000L
|
#define delta 100000L
|
||||||
|
|
||||||
@ -28,70 +18,46 @@ int ArrondirPixel(int nombre) /* Calcule un arrondi du pixel pour pouvoir respec
|
|||||||
return arrondi;
|
return arrondi;
|
||||||
}
|
}
|
||||||
|
|
||||||
void gen_pastille(int nb_pastille, int *p_pastilles)
|
PIXELS gen_pastille()
|
||||||
|
|
||||||
/* 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*/
|
||||||
/*Code n'est pas complet*/
|
/*Code n'est pas complet*/
|
||||||
/*-Elles se génèrent à des endroits qui peuvent être les mêmes ou gêner le snake*/
|
/*-Elles se génèrent à des endroits qui peuvent être les mêmes ou gêner le snake*/
|
||||||
|
|
||||||
{
|
{
|
||||||
couleur r;
|
int x_pastille,y_pastille;
|
||||||
int x_pastille,y_pastille,i;
|
PIXELS pastille;
|
||||||
r=CouleurParNom("red");
|
|
||||||
|
|
||||||
srand(time(NULL));
|
x_pastille= ArrondirPixel(rand()%W_GAME);
|
||||||
|
y_pastille = ArrondirPixel(rand()%H_GAME);
|
||||||
|
|
||||||
for(i=0;i<nb_pastille*2;i+=2)
|
if(x_pastille < DECALEMENT)
|
||||||
{
|
{
|
||||||
|
x_pastille =+ DECALEMENT;
|
||||||
x_pastille= ArrondirPixel(rand()%W_GAME);
|
}
|
||||||
y_pastille = ArrondirPixel(rand()%H_GAME);
|
else if(x_pastille >W_GAME-DECALEMENT)
|
||||||
|
{
|
||||||
if(x_pastille < DECALEMENT)
|
x_pastille - DECALEMENT;
|
||||||
{
|
}
|
||||||
x_pastille =+ DECALEMENT;
|
if(y_pastille < DECALEMENT)
|
||||||
}
|
{
|
||||||
else if(x_pastille >W_GAME-DECALEMENT)
|
y_pastille =+ DECALEMENT;
|
||||||
{
|
}
|
||||||
x_pastille - DECALEMENT;
|
else if(y_pastille > H_GAME-DECALEMENT)
|
||||||
}
|
{
|
||||||
if(y_pastille < DECALEMENT)
|
y_pastille - DECALEMENT;
|
||||||
{
|
|
||||||
y_pastille =+ DECALEMENT;
|
|
||||||
}
|
|
||||||
else if(y_pastille > H_GAME-DECALEMENT)
|
|
||||||
{
|
|
||||||
y_pastille - DECALEMENT;
|
|
||||||
}
|
|
||||||
|
|
||||||
p_pastilles[i] = x_pastille ;
|
|
||||||
p_pastilles[i+1] = y_pastille ;
|
|
||||||
|
|
||||||
printf("x : %d ; y : %d\n",p_pastilles[i],p_pastilles[i+1]);
|
|
||||||
ChoisirEcran(2);
|
|
||||||
ChoisirCouleurDessin(r);
|
|
||||||
RemplirRectangle(x_pastille,y_pastille,T_PIXEL,T_PIXEL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
pastille.x = x_pastille ;
|
||||||
|
pastille.y = y_pastille ;
|
||||||
|
|
||||||
void DessinerScene(int* pastilles) /* Dessine la scène */
|
return pastille;
|
||||||
{
|
|
||||||
couleur c;
|
|
||||||
ChoisirEcran(2);
|
|
||||||
c=CouleurParNom("lightgreen");
|
|
||||||
ChoisirCouleurDessin(c);
|
|
||||||
RemplirRectangle(T_PIXEL,T_PIXEL,W_GAME,H_GAME);
|
|
||||||
gen_pastille(5,pastilles);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void InitialisationDuSerpent(int *p_serpent) /* L'initialisation du serpent */
|
void InitialisationDuSerpent(int *p_serpent) /* L'initialisation du serpent */
|
||||||
{
|
{
|
||||||
int x_millieu = 0, y_millieu = 0 , compteur = 0;
|
int x_millieu = 0, y_millieu = 0 , compteur = 0;
|
||||||
couleur j;
|
couleur j;
|
||||||
int i = 0;
|
|
||||||
j=CouleurParNom("yellow");
|
j=CouleurParNom("yellow");
|
||||||
ChoisirCouleurDessin(j);
|
ChoisirCouleurDessin(j);
|
||||||
x_millieu = T_PIXEL*30; /* 30 = 60 colonnes divisé par 2*/
|
x_millieu = T_PIXEL*30; /* 30 = 60 colonnes divisé par 2*/
|
||||||
@ -103,7 +69,7 @@ void InitialisationDuSerpent(int *p_serpent) /* L'initialisation du serpent */
|
|||||||
|
|
||||||
p_serpent[0] = x_millieu;
|
p_serpent[0] = x_millieu;
|
||||||
p_serpent[1] = y_millieu;
|
p_serpent[1] = y_millieu;
|
||||||
for(compteur=0;compteur<=18;compteur+=2) /* Commence par 1 car p_serpent index 0 initialisées pour la tête et 2 + 2x9 = 10 couples de coordonnées 2D */
|
for(compteur=0;compteur<=16;compteur+=2) /* Commence par 1 car p_serpent index 0 initialisées pour la tête et 2 + 2x9 = 10 couples de coordonnées 2D */
|
||||||
{
|
{
|
||||||
p_serpent[compteur+2] = p_serpent[compteur]+T_PIXEL;
|
p_serpent[compteur+2] = p_serpent[compteur]+T_PIXEL;
|
||||||
p_serpent[compteur+3] = p_serpent[compteur+1];
|
p_serpent[compteur+3] = p_serpent[compteur+1];
|
||||||
@ -112,12 +78,18 @@ void InitialisationDuSerpent(int *p_serpent) /* L'initialisation du serpent */
|
|||||||
|
|
||||||
RemplirRectangle(p_serpent[compteur+2],p_serpent[compteur+3],T_PIXEL,T_PIXEL);
|
RemplirRectangle(p_serpent[compteur+2],p_serpent[compteur+3],T_PIXEL,T_PIXEL);
|
||||||
}
|
}
|
||||||
printf("Avant \n");
|
}
|
||||||
for (i = 0; i < 20; i += 2) {
|
|
||||||
printf("(%d, %d) ", p_serpent[i], p_serpent[i + 1]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
|
void DessinerScene(PIXELS *pastilles, int *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);
|
||||||
|
InitialisationDuSerpent(serpent);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
@ -126,13 +98,14 @@ int main()
|
|||||||
int score =0;
|
int score =0;
|
||||||
unsigned long suivant;
|
unsigned long suivant;
|
||||||
int go_on=1;
|
int go_on=1;
|
||||||
int x_pastille,y_pastille;
|
|
||||||
int n = 0;
|
int n = 0;
|
||||||
int m = 0;
|
int m = 0;
|
||||||
|
|
||||||
size_t longueur_serpent = 20;
|
size_t longueur_serpent = 20;
|
||||||
size_t longueur_pastilles = 10;
|
size_t longueur_pastilles = 10;
|
||||||
int *serpent = (int *)malloc(longueur_serpent * sizeof(int));
|
int *serpent = (int *)malloc(longueur_serpent * sizeof(int));
|
||||||
int *pastilles = (int *)malloc(longueur_pastilles * sizeof(int));
|
PIXELS *pastilles = (PIXELS *)malloc(longueur_pastilles * sizeof(PIXELS));
|
||||||
|
|
||||||
int direction = 0;
|
int direction = 0;
|
||||||
int direction_davant = 0;
|
int direction_davant = 0;
|
||||||
|
|
||||||
@ -149,8 +122,7 @@ int main()
|
|||||||
InitialiserGraphique();
|
InitialiserGraphique();
|
||||||
CreerFenetre(10,10,W_WINDOW,H_WINDOW); /* Peut être changer cette ligne avec la fonction Maxx et Maxy fournie dans graph.h ??*/
|
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");
|
ChoisirTitreFenetre("SNAKE SAE11 IN C");
|
||||||
DessinerScene(pastilles);
|
DessinerScene(pastilles,serpent);
|
||||||
InitialisationDuSerpent(serpent);
|
|
||||||
|
|
||||||
while(go_on) /* Lancement du cycle pour les Inputs et le Jeu*/
|
while(go_on) /* Lancement du cycle pour les Inputs et le Jeu*/
|
||||||
{
|
{
|
||||||
@ -220,10 +192,9 @@ int main()
|
|||||||
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 */
|
||||||
MangerPastille(serpent,pastilles,longueur_serpent,longueur_pastilles);
|
|
||||||
DeplacementSerpent(direction,serpent,longueur_serpent);
|
DeplacementSerpent(direction,serpent,longueur_serpent);
|
||||||
|
MangerPastille(serpent,pastilles,longueur_serpent,longueur_pastilles);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
15
main.h
15
main.h
@ -7,5 +7,18 @@
|
|||||||
#define H_GAME 600
|
#define H_GAME 600
|
||||||
#define T_PIXEL 15
|
#define T_PIXEL 15
|
||||||
#define DECALEMENT 30
|
#define DECALEMENT 30
|
||||||
|
#define PASTILLES 5
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
|
struct PIXELS {
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct PIXELS PIXELS;
|
||||||
|
|
||||||
|
PIXELS gen_pastille();
|
||||||
|
|
||||||
|
#endif
|
||||||
|
BIN
prog
Executable file
BIN
prog
Executable file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user