From e5def4ff969f4fc7ef6a0efda0ea7d6f4580b7e0 Mon Sep 17 00:00:00 2001 From: boutarci Date: Thu, 14 Dec 2023 15:00:02 +0100 Subject: [PATCH] =?UTF-8?q?Des=20beugs=20param=C3=A8tre=20dans=20les=20fon?= =?UTF-8?q?ctions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SAE_semestre1/Makefile | 34 ++++-- SAE_semestre1/fichier.h/main.h | 3 +- SAE_semestre1/fichier.h/menu.h | 2 +- SAE_semestre1/fichier.h/pastille.h | 4 +- SAE_semestre1/fichier.h/serpent.h | 19 +-- SAE_semestre1/fichier.h/terrain.h | 8 +- SAE_semestre1/fichier.h/time.h | 2 +- SAE_semestre1/out/test.c | 20 ++++ SAE_semestre1/src/game.c | 4 - SAE_semestre1/src/main.c | 31 ++++- SAE_semestre1/src/menu.c | 25 ++++ SAE_semestre1/src/pastille.c | 45 +++---- SAE_semestre1/src/serpent.c | 185 +++++++++++++++++++---------- SAE_semestre1/src/terrain.c | 97 +++++++-------- SAE_semestre1/src/time.c | 64 +++++----- 15 files changed, 330 insertions(+), 213 deletions(-) create mode 100644 SAE_semestre1/out/test.c diff --git a/SAE_semestre1/Makefile b/SAE_semestre1/Makefile index 61afba1..f0f3519 100644 --- a/SAE_semestre1/Makefile +++ b/SAE_semestre1/Makefile @@ -1,11 +1,25 @@ +### VARIABLES ### CC = gcc -CFLAGS = -Wall -Wextra -std=c99 -LIBS = -lgraph -lm - -all: main - -main: main.c jeu.c serpent.c pomme.c - $(CC) $(CFLAGS) -o main main.c jeu.c serpent.c pomme.c $(LIBS) - -clean: - rm main \ No newline at end of file +CFLAGS = -lgraph -ansi +SRCDIR = ./src +HDIR = ./fichier.h +ODIR = ./out +OFILES = $(subst src/,out/,$(subst .c,.o,$(shell find $(SRCDIR)/ -type f))) +EXE = game +### BUT PAR DEFAUT ### +but : $(EXE) +### REGLES ESSENTIELLES ### +$(ODIR)/%.o : $(SRCDIR)/%.c + @mkdir -p $(@D) + $(CC) -c $< -o $@ +$(EXE) : $(OFILES) + $(CC) $(CFLAGS) -o $(EXE) $(OFILES) +### REGLES OPTIONNELLES ### +run : $(EXE) + ./$(EXE) +clean : + -rm -rf $(ODIR) +mrproper : + clean but +### BUTS FACTICES ### +.PHONY : but run clean mrproper diff --git a/SAE_semestre1/fichier.h/main.h b/SAE_semestre1/fichier.h/main.h index 8b01770..e1cf47f 100644 --- a/SAE_semestre1/fichier.h/main.h +++ b/SAE_semestre1/fichier.h/main.h @@ -1,5 +1,6 @@ #ifndef MAIN_H #define MAIN_H - + void main(void); + #endif \ No newline at end of file diff --git a/SAE_semestre1/fichier.h/menu.h b/SAE_semestre1/fichier.h/menu.h index 2a7f8ac..132380e 100644 --- a/SAE_semestre1/fichier.h/menu.h +++ b/SAE_semestre1/fichier.h/menu.h @@ -1,6 +1,6 @@ #ifndef MENU_H #define MENU_H -void menu(void) +void menu(void); #endif \ No newline at end of file diff --git a/SAE_semestre1/fichier.h/pastille.h b/SAE_semestre1/fichier.h/pastille.h index fc18b9e..c135005 100644 --- a/SAE_semestre1/fichier.h/pastille.h +++ b/SAE_semestre1/fichier.h/pastille.h @@ -1,8 +1,6 @@ #ifndef PASTILLE_H #define PASTILLE_H -void genererPomme(); -void dessinerPomme(); -int collisionAvecPomme(); +void Pomme(); #endif diff --git a/SAE_semestre1/fichier.h/serpent.h b/SAE_semestre1/fichier.h/serpent.h index 014aaf3..043fc92 100644 --- a/SAE_semestre1/fichier.h/serpent.h +++ b/SAE_semestre1/fichier.h/serpent.h @@ -1,21 +1,12 @@ #ifndef SERPENT_H #define SERPENT_H - #include "../fichier.h/terrain.h" -typedef struct { - Position *corps; - int longueur; -} Serpent; -extern Serpent serpent; -extern int direction; - -void initialiserSerpent(); -void dessinerSerpent(); -void deplacerSerpent(); -int collisionAvecSerpent(); -int collisionAvecBordures(); -void gestionCollision(); +void Update_Serpent(int, int,int*, int*); +void Controle(int*, int*); +void Serpent(int*); +int Collision_Serpent(int*); +void Score(int*); #endif diff --git a/SAE_semestre1/fichier.h/terrain.h b/SAE_semestre1/fichier.h/terrain.h index dbc1dc6..75552ab 100644 --- a/SAE_semestre1/fichier.h/terrain.h +++ b/SAE_semestre1/fichier.h/terrain.h @@ -1,12 +1,6 @@ #ifndef TERRAIN_H #define TERRAIN_H -#define LARGEUR_FENETRE 1250 -#define HAUTEUR_FENETRE 750 -#define TAILLE_CELLULE 25 -#define DELAI_MILLISECONDES 100 -#define ESPACE_NOIR 100 - -void jeu(); +void DessinerScene(); #endif diff --git a/SAE_semestre1/fichier.h/time.h b/SAE_semestre1/fichier.h/time.h index 48ec409..2d5498d 100644 --- a/SAE_semestre1/fichier.h/time.h +++ b/SAE_semestre1/fichier.h/time.h @@ -3,6 +3,6 @@ #define CYCLE 10000L -void Timer(unsigned long int suivant); +void Timer(); #endif \ No newline at end of file diff --git a/SAE_semestre1/out/test.c b/SAE_semestre1/out/test.c new file mode 100644 index 0000000..a98b81d --- /dev/null +++ b/SAE_semestre1/out/test.c @@ -0,0 +1,20 @@ +#include +#include + +int test(int nombre) +{ + return nombre; +} + +int main(void) +{ + + int toto = 5; + + + + printf("%d",test(toto)); + + + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/SAE_semestre1/src/game.c b/SAE_semestre1/src/game.c index 149e715..1918a00 100644 --- a/SAE_semestre1/src/game.c +++ b/SAE_semestre1/src/game.c @@ -162,25 +162,21 @@ void Controle() { case XK_Left: if (last_direction != 4) { direction = 3; - last_direction = 3; } break; case XK_Right: if (last_direction != 3) { direction = 4; - last_direction = 4; } break; case XK_Up: if (last_direction != 2) { direction = 1; - last_direction = 1; } break; case XK_Down: if (last_direction != 1) { direction = 2; - last_direction = 2; } break; case XK_Escape: diff --git a/SAE_semestre1/src/main.c b/SAE_semestre1/src/main.c index 650d0e4..42a4a99 100644 --- a/SAE_semestre1/src/main.c +++ b/SAE_semestre1/src/main.c @@ -1,6 +1,31 @@ +#include +#include +#include "../fichier.h/time.h" #include "../fichier.h/terrain.h" +#include "../fichier.h/pastille.h" +#include "../fichier.h/serpent.h" +#include "../fichier.h/menu.h" -int main() { - jeu(); - return 0; +int main(void){ + int go_on = 1; + int segment = 10; + int* pointeur_segment = &segment; + int* pointeur_go_on= &go_on; + int seconde_actuel; + int old_seconde; + unsigned long int suivant; + InitialiserGraphique(); + CreerFenetre(350,100,1200,900); + EffacerEcran(CouleurParComposante(0,0,0)); + suivant = Microsecondes()+CYCLE; + old_seconde=(suivant/1000000)%10; + DessinerScene(); + usleep(1000); + while(go_on){ + Timer(); + Score(pointeur_segment); + Serpent(&go_on); + Pomme(); + } + FermerGraphique(); } \ No newline at end of file diff --git a/SAE_semestre1/src/menu.c b/SAE_semestre1/src/menu.c index e69de29..25d967b 100644 --- a/SAE_semestre1/src/menu.c +++ b/SAE_semestre1/src/menu.c @@ -0,0 +1,25 @@ +#include +#include +#include "../fichier.h/menu.h" + + +int MenuDebut(){ + InitialiserGraphique(); + EcrireTexte(1000,760,"Score", 2); +} +int menu(){ + while(menu){ + while(ToucheEnAttente()){ + t = Touche(); + switch(t3){ + case XK_q : + FermerGraphique(); + return EXIT_SUCCESS; + return; + case XK_Right : + menu = 0; + return + } + } + } +} \ No newline at end of file diff --git a/SAE_semestre1/src/pastille.c b/SAE_semestre1/src/pastille.c index a9d1b4f..475c537 100644 --- a/SAE_semestre1/src/pastille.c +++ b/SAE_semestre1/src/pastille.c @@ -4,37 +4,20 @@ #include "../fichier.h/serpent.h" #include "../fichier.h/pastille.h" -#define LARGEUR_FENETRE 1250 -#define HAUTEUR_FENETRE 750 -#define TAILLE_CELLULE 25 -#define DELAI_MILLISECONDES 100 -#define ESPACE_NOIR 100 - -void genererPomme() { - int pommex[5]; - int pommey[5]; - int i; - for (i = 0; i < 5; i++) { - pommex[i] = rand() % (LARGEUR_FENETRE / TAILLE_CELLULE) * TAILLE_CELLULE; - pommey[i] = rand() % (HAUTEUR_FENETRE / TAILLE_CELLULE) * TAILLE_CELLULE; +void Pomme(){ + int pos_x[60]; + int pos_y[60]; + int segment = 10; + int p, pp; + int pomme, pommex[5], pommey[5]; + for(p=0; p<6; p++){ + if(pommex[p]==pos_x[0] && pommey[p]==pos_y[0]){ + segment+=2; + pommex[p] = ((rand() % (60)+1)*20); + pommey[p] = ((rand() % (27)+1)*20); + } } -} - -void dessinerPomme() { - int i; - int pommex[5]; -int pommey[5]; - ChoisirCouleurDessin(CouleurParComposante(255, 0, 0)); - for (i = 0; i < 5; i++) { - RemplirRectangle(pommex[i], pommey[i], TAILLE_CELLULE, TAILLE_CELLULE); - } -} - -int collisionAvecPomme() { - int i; - int pommex[5]; -int pommey[5]; - for (i = 0; i < 5; i++) { - return (serpent.corps[0].x == pommex[i] && serpent.corps[i].y == pommey[i]); + for(pp = 0; pp < 5; ++pp){ + AfficherSprite(pomme, pommex[pp], pommey[pp]); } } \ No newline at end of file diff --git a/SAE_semestre1/src/serpent.c b/SAE_semestre1/src/serpent.c index 2999793..26d790a 100644 --- a/SAE_semestre1/src/serpent.c +++ b/SAE_semestre1/src/serpent.c @@ -1,70 +1,135 @@ #include +#include #include -#include "serpent.h" -#include "terrain.h" +#include "../fichier.h/serpent.h" +#include "../fichier.h/terrain.h" +#include "../fichier.h/pastille.h" -Serpent serpent; -int direction; - -void initialiserSerpent() { - serpent.longueur = 1; - serpent.corps = (Position *)malloc(sizeof(Position) * serpent.longueur); - serpent.corps[0].x = LARGEUR_FENETRE / 2; - serpent.corps[0].y = HAUTEUR_FENETRE / 2; -} - -void dessinerSerpent() { - int i; - for (i = 0; i < serpent.longueur; i++) { - if (i % 2 == 0) { - ChoisirCouleurDessin(CouleurParNom("yellow")); /*JAUNE*/ - } - RemplirRectangle(serpent.corps[i].x, serpent.corps[i].y, TAILLE_CELLULE, TAILLE_CELLULE); +void Update_Serpent(int segment, int direction, int pos_x[60], int pos_y[60]){ + int old_x[60]; + int old_y[60]; + int x = 600; + int y = 400; + int t, fond, i = 0, serpent; + fond = ChargerSprite("../img/fond.png"); + AfficherSprite(fond, pos_x[segment-1], pos_y[segment-1]); + AfficherSprite(serpent, pos_x[0], pos_y[0]); + if (direction == 1 && direction != 2 ){ + pos_y[0]=old_y[0]-20; + } + if (direction == 2 && direction != 1) { + pos_y[0]=old_y[0]+20; + } + if (direction == 3 & direction != 4) { + pos_x[0]=old_x[0]-20; + } + if (direction == 4 && direction != 3) { + pos_x[0]=old_x[0]+20; + } + for (i=1 ; i 0; i--) { - serpent.corps[i] = serpent.corps[i - 1]; - } - - /* Déplacer la tête du serpent en fonction de la direction*/ - switch (direction) { - case 0: - serpent.corps[0].x += TAILLE_CELLULE; - break; - case 1: - serpent.corps[0].y += TAILLE_CELLULE; - break; - case 2: - serpent.corps[0].x -= TAILLE_CELLULE; - break; - case 3: - serpent.corps[0].y -= TAILLE_CELLULE; - break; - } -} - -int collisionAvecSerpent() { - int i; - for (i = 1; i < serpent.longueur; i++) { - if (serpent.corps[0].x == serpent.corps[i].x && serpent.corps[0].y == serpent.corps[i].y) { - return 1; +void Controle(int* direction , int* go_on) { + int t; + int last_direction = 0; + while(ToucheEnAttente()) { + t = Touche(); + switch(t) { + case XK_Left: + if (last_direction != 4) { + *direction = 3; + printf("gauche\n"); + } + return; + case XK_Right: + if (last_direction != 3) { + *direction = 4; + } + return; + case XK_Up: + if (last_direction != 2) { + *direction = 1; + } + return; + case XK_Down: + if (last_direction != 1) { + *direction = 2; + } + return; + case XK_Escape: + *direction = 0; + *go_on = 0; + return; + case XK_p: + *direction = 0; } } - return 0; } - -int collisionAvecBordures() { - return (serpent.corps[0].x < 0 || serpent.corps[0].x >= LARGEUR_FENETRE || - serpent.corps[0].y < 0 || serpent.corps[0].y >= HAUTEUR_FENETRE); -} - -void gestionCollision() { - if (collisionAvecSerpent() || collisionAvecBordures()) { - FermerGraphique(); - exit(EXIT_SUCCESS); +int Collision_Serpent(int* go_on){ + int pos_x[60]; + int pos_y[60]; + int murx[50]; + int mury[50]; + int segment = 10; + int i; + /*Serpent contre coté*/ + if (pos_x[0] >1160 || pos_x[0]<=20){ + return *go_on= 0; + } else{ + return 1; } + /*Serpent contre coté*/ + if (pos_y[0]<40 || pos_y[0] >=700){ + return *go_on= 0; + } else{ + return 1; + } + /*Serpent contre Serpent*/ + for (i = 1; i < segment; i++) { + if (pos_x[0] == pos_x[i] && pos_y[0] == pos_y[i]){ + return *go_on = 0; + } else { + return 1; + } + } + /*Serpent contre mur*/ + for(i=0; i<30;i++){ + if(pos_x[0] == murx[i] && pos_y[0] == mury[i]){ + return *go_on= 0; + }else{ + return 1; + } + } +} +void Score(int segment){ + int nombre; + char score[4]; + nombre = (segment-10)*10; + snprintf(score,4,"%04d0", nombre); + ChoisirCouleurDessin(CouleurParNom("black")); + RemplirRectangle(1100,700,1200,800); + ChoisirCouleurDessin(CouleurParNom("white")); + EcrireTexte(1000,760,"Score: ",2); + EcrireTexte(1100,760,score,2); +} +void Serpent(int* go_on) { + int direction=0; + int pos_x[60]; + int pos_y[60]; + int* pointeur_pos_x= pos_x; + int* pointeur_pos_y= pos_y; + int* pointeur_direction=&direction; + + Controle(pointeur_direction, go_on); + Update_Serpent(segment, direction, pointeur_pos_x, pointeur_pos_y ); + Collision_Serpent(go_on); } \ No newline at end of file diff --git a/SAE_semestre1/src/terrain.c b/SAE_semestre1/src/terrain.c index 2a0e331..409bc8a 100644 --- a/SAE_semestre1/src/terrain.c +++ b/SAE_semestre1/src/terrain.c @@ -3,54 +3,57 @@ #include "../fichier.h/time.h" #include "../fichier.h/terrain.h" #include "../fichier.h/serpent.h" +#include "../fichier.h/main.h" +#include "../fichier.h/pastille.h" +void DessinerScene(void){ + /*Temps*/ + int minute = 0; + int seconde = 0; + char timer[6]; + /*Mur*/ + int murx[50]; + int mury[50]; + int mur; + /*Serpent*/ + int serpent; + int segment=10; + int x = 600; + int y = 400; + int i; + int pos_x[60]; + int pos_y[60]; + int old_x[60]; + int old_y[60]; + int fond; + int p=0; + int pp=0; + int pomme, pommex[5], pommey[5]; + snprintf(timer,6,"%02d:%02d", minute ,seconde); + ChoisirCouleurDessin(CouleurParComposante(91,222,122)); + RemplirRectangle(20,20,1160,700); + ChoisirCouleurDessin(CouleurParComposante(255,255,255)); + serpent=ChargerSprite("../img/serpent.png"); + fond = ChargerSprite("../img/fond.png"); + mur = ChargerSprite("../img/mur.png"); -#define LARGEUR_FENETRE 1250 -#define HAUTEUR_FENETRE 750 -#define TAILLE_CELLULE 25 -#define DELAI_MILLISECONDES 100 -#define ESPACE_NOIR 100 - -void jeu(void) { - InitialiserGraphique(); - CreerFenetre(10, 10, LARGEUR_FENETRE, HAUTEUR_FENETRE + ESPACE_NOIR); - ChoisirCouleurDessin(CouleurParComposante(0, 0, 0)); - EffacerEcran(CouleurParComposante(0, 0, 0)); - initialiserSerpent(); - genererPomme(); - - while (1) { - dessinerDamier(); /* Dessiner le damier en fond*/ - deplacerSerpent(); - gestionCollision(); - dessinerSerpent(); - dessinerPomme(); - attente(DELAI_MILLISECONDES); /* Attente pour ralentir le serpent*/ - - if (SourisCliquee()) { - FermerGraphique(); - exit(EXIT_SUCCESS); - } - - if (ToucheEnAttente()) { - int touche = Touche(); - switch (touche) { - case XK_Right: - direction = 0; - break; - case XK_Down: - direction = 1; - break; - case XK_Left: - direction = 2; - break; - case XK_Up: - direction = 3; - break; - case XK_Escape: - FermerGraphique(); - } - } + for (i = 0; i < segment; i++){ + AfficherSprite(serpent, x-(i*20), y); + pos_x[i]=x-(i*20); + pos_y[i]=y; + old_x[i]=pos_x[i]; + old_y[i]=pos_y[i]; + } + srand(time(NULL)); + pomme=ChargerSprite("../img/pomme.png"); + for (p = 0; p < 5; p++) { + pommex[p] = ((rand() % (55)+1)*20); + pommey[p] = ((rand() % (35)+1)*20); + AfficherSprite(pomme, pommex[p],pommey[p]); + } + for(i=0; i<30; i++){ + murx[i] = ((rand() % (55)+1)*20); + mury[i] = ((rand() % (35)+1)*20); + AfficherSprite(mur, murx[i], mury[i]); } } - diff --git a/SAE_semestre1/src/time.c b/SAE_semestre1/src/time.c index 48f2036..5e9604c 100644 --- a/SAE_semestre1/src/time.c +++ b/SAE_semestre1/src/time.c @@ -1,39 +1,41 @@ #include #include #include +#include "../fichier.h/time.h" #define CYCLE 10000L -int seconde=0; -int minute=0; -int seconde_actuel=0; -int old_seconde=0; -char timer[6]; +/*time*/ + void Update_Timer(void){ + int seconde =0; + int minute=1; + char timer[6]; + snprintf(timer,6,"%02d:%02d", minute, seconde); + ChoisirCouleurDessin(CouleurParComposante(0,0,0)); + RemplirRectangle(10,700,12000,800); + ChoisirCouleurDessin(CouleurParComposante(255,255,255)); + EcrireTexte(50,760,"Temps: ",2); + EcrireTexte(160,760,timer,2); +} +void Timer(void){ +int seconde = 0; +int minute = 0; +int seconde_actuel; +int old_seconde; unsigned long int suivant; -int nombre; - -void Update_Timer(){ - snprintf(timer,6,"%02d:%02d", minute, seconde); - ChoisirCouleurDessin(CouleurParComposante(0,0,0)); - RemplirRectangle(0,780,1200,800); - ChoisirCouleurDessin(CouleurParComposante(255,255,255)); - EcrireTexte(10,800,timer,2); -} - -void Timer(){ - if(Microsecondes()> suivant){ - suivant = Microsecondes()+CYCLE; - seconde_actuel = (suivant/1000000)%10; - if(seconde_actuel !=old_seconde){ - old_seconde = seconde_actuel; - if(seconde == 59){ - minute=minute+1; - seconde=0; - Update_Timer(); - }else{ - seconde = seconde+1; - Update_Timer(); - } - } - } + if(Microsecondes()> suivant){ + suivant = Microsecondes()+CYCLE; + seconde_actuel = (suivant/1000000)%10; + if(seconde_actuel !=old_seconde){ + old_seconde = seconde_actuel; + if(seconde == 59){ + minute=minute+1; + seconde=0; + Update_Timer(); + }else;{ + seconde = seconde+1; + Update_Timer(); + } + } + } }