From 100b99bd9940e4a9a7224ac043b96946d90b4c05 Mon Sep 17 00:00:00 2001 From: brigitte Date: Thu, 7 Dec 2023 14:02:18 +0100 Subject: [PATCH] organisation du code + makefile --- JEUX_SERPENT/Makefile | 37 ++++---- JEUX_SERPENT/fonction.h | 36 +++++++- JEUX_SERPENT/main.c | 191 ++-------------------------------------- JEUX_SERPENT/pomme.c | 20 ----- JEUX_SERPENT/scene.c | 47 ++++++++++ JEUX_SERPENT/score.c | 16 ++++ JEUX_SERPENT/serpent | Bin 0 -> 17480 bytes JEUX_SERPENT/serpent.c | 10 +++ JEUX_SERPENT/terrain.c | 12 --- JEUX_SERPENT/timer.c | 3 +- 10 files changed, 134 insertions(+), 238 deletions(-) delete mode 100644 JEUX_SERPENT/pomme.c create mode 100644 JEUX_SERPENT/scene.c create mode 100644 JEUX_SERPENT/score.c create mode 100755 JEUX_SERPENT/serpent delete mode 100644 JEUX_SERPENT/terrain.c diff --git a/JEUX_SERPENT/Makefile b/JEUX_SERPENT/Makefile index d66f443..7e46f00 100644 --- a/JEUX_SERPENT/Makefile +++ b/JEUX_SERPENT/Makefile @@ -1,25 +1,24 @@ -but:Snake +CC=gcc +CFLAGS=-ansi -pedantic -lgraph -OFILES=main.o\ - serpent.o\ - terrain.o\ - pomme.o\ - timer.o\ - fonction.o +all:serpent -CC= gcc -CFLAGS= -ansi -pedantic -g -lgraph +serpent:main.o scene.o serpent.o timer.o score.o + $(CC) $(CFLAGS) -o serpent main.o scene.o serpent.o timer.o score.o -fonction.o:fonction.h -main.o:main.c -serpent.o:serpent.c -terrain.o:terrain.c -pomme.o:pomme.c -timer.o:timer.c +main.o:main.c fonction.h + $(CC) -c main.c $(CFLAGS) -Snake:$(OFILES) - $(CC) $(CFLAGS)Snake $(OFILES) +scene.o:scene.c fonction.h + $(CC) -c scene.c $(CFLAGS) -clean:-rm -f $(OFLIES)Snake +serpent.o: serpent.c fonction.h + $(CC) -c serpent.c $(CFLAGS) -.PHONY:but clean +timer.o:timer.c fonction.h + $(CC) -c timer.c $(CFLAGS) + +score.o:score.c fonction.h + $(CC) -c score.c $(CFLAGS) + +clean:-rm -f *.o serpent diff --git a/JEUX_SERPENT/fonction.h b/JEUX_SERPENT/fonction.h index fcd0b96..05ddd0b 100644 --- a/JEUX_SERPENT/fonction.h +++ b/JEUX_SERPENT/fonction.h @@ -1,5 +1,35 @@ -#ifndef MAIN.H -#define MAIN.H +#ifndef FONCTION_H +#define FONCTION_H +#include +#include +#include +#include +#include + + +extern int seconde, minute, seconde_actuel, old_seconde; +extern char timer[6]; +extern unsigned long int suivant; + +extern int serpent; +extern int x, y, direction, t, segment, i; +extern int pos_x[60], pos_y[60], old_x[60], old_y[60]; + +extern int p, pp, pomme, pommex[5], pommey[5]; +extern int fond, Nbr; +extern char score[4]; + +extern int go_on; + +void Pomme(); +void Controle(); +void Serpent(); +void Update_Serpent(); +void Terrain(); +void Timer(); +void Update_Timer(); +void DessinerScene(); +void Score(); +void Collision(); -void Pomme();void Controle();void Serpent();void Update_Serpent();void Terrain();void Timer();void Update_Timer(); #endif \ No newline at end of file diff --git a/JEUX_SERPENT/main.c b/JEUX_SERPENT/main.c index e950698..74f1ac3 100644 --- a/JEUX_SERPENT/main.c +++ b/JEUX_SERPENT/main.c @@ -3,199 +3,24 @@ #include #include #include +#include "fonction.h" #define CYCLE 10000L -/*timer*/ -int seconde=0; -int minute=0; -int seconde_actuel=0; -int old_seconde=0; + +int seconde, minute, seconde_actuel, old_seconde; char timer[6]; unsigned long int suivant; -/*serpent*/ + int serpent; -int x = 600; -int y = 400; -int direction = 4; /*1 : vers le haut , 2 : vers le bas; 3 : vers la gauche, 4 : vers la droite*/ -int t; -int segment=10; -int i=0; -int pos_x[60]; -int pos_y[60]; -int old_x[60]; -int old_y[60]; -/*variable pomme*/ -int p=0; -int pp=0; -int pomme, pommex[5], pommey[5]; +int x=500, y=500, direction=1, t, segment=10, i; +int pos_x[60], pos_y[60], old_x[60], old_y[60]; + +int p, pp, pomme, pommex[5], pommey[5]; int fond, Nbr; char score[4]; -/*fin de jeu*/ int go_on=1; -/*Fonction Pour créer la première scene du jeu*/ -void DessinerScene(){ - snprintf(timer,6,"%02d:%02d", minute, seconde); - ChoisirCouleurDessin(CouleurParComposante(5,130,4)); - RemplirRectangle(20,20,1160,700); - ChoisirCouleurDessin(CouleurParComposante(255,255,255)); - EcrireTexte(10,760,timer,2); - serpent=ChargerSprite("IMG/serpent.png"); - fond=ChargerSprite("IMG/fond.png"); - - for (i = 0; i < segment; i++){ - AfficherSprite(serpent, x-(i*20), y); - pos_x[i]=x-(i*20); - pos_y[i]=y; - old_y[i]=pos_y[i]; - old_x[i]=pos_x[i]; - } - srand(time(NULL)); - pomme=ChargerSprite("IMG/pomme.png"); - for (p = 0; p < 5; p++) { - pommex[p] = ((rand() % (58)+1)*20); - pommey[p] = ((rand() % (35)+1)*20); - AfficherSprite(pomme, pommex[p], pommey[p]); - } -} - -void Terrain(){ - if (pos_x[0] >1140 || pos_x[0]<=20) - go_on=0; - if (pos_y[0]<40 || pos_y[0] >=680) - go_on=0; -} - -/*Fonction pour calculer le temps*/ -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(); - } - } - } -} - -/*Fonction pour mettre à jour unuquement le timer*/ -void Update_Timer(void){ - snprintf(timer,6,"%02d:%02d", minute, seconde); - ChoisirCouleurDessin(CouleurParComposante(0,0,0)); - RemplirRectangle(0,700,1200,800); - ChoisirCouleurDessin(CouleurParComposante(255,255,255)); - EcrireTexte(10,750,timer,2); -} - -void Score(){ - Nbr=(segment-10)*10; - snprintf(score,4,"%04d0", Nbr); - ChoisirCouleurDessin(CouleurParComposante(0,0,0)); - RemplirRectangle(1100,730,1200,800); - ChoisirCouleurDessin(CouleurParComposante(255,255,255)); - EcrireTexte(1000,750,"Score :",2); - EcrireTexte(1100,750,score,2); -} - -/*Input Serpent*/ -void Controle(){ - while(ToucheEnAttente()){ - t = Touche(); - switch(t){ - case XK_Left : - direction=3; - break; - case XK_Right: - direction=4; - break; - case XK_Up: - direction=1; - break; - case XK_Down: - direction=2; - break; - case XK_Escape: - go_on=0; - break; - case XK_p: - direction=0; - break; - } - } -} - -/*Avancement automatique du serpent en fonction de la direction*/ -void Serpent(){ - if (direction == 1){ - pos_y[0]=old_y[0]-20; - } - if (direction == 2){ - pos_y[0]=old_y[0]+20; - } - if (direction == 3){ - pos_x[0]=old_x[0]-20; - } - if (direction == 4){ - pos_x[0]=old_x[0]+20; - } - - for(p=0; p<5; p++){ - if(pommex[p]==pos_x[0] && pommey[p]==pos_y[0]){ - segment+=2; - pommex[p] = ((rand() % (58)+1)*20); - pommey[p] = ((rand() % (35)+1)*20); - } - } - - Update_Serpent(); - Terrain(); - usleep(100000); -} - -void Collision(){ - if(seconde!=0 || minute!=0){ - for(i=1; i -#include -#include -#include -#include - -/*variable pomme*/ -int p=0; -int pp=0; -int pomme, pommex[5], pommey[5]; -int fond; - -/*Apparition aléatoire des pommes*/ -void Pomme(){ - - for (pp = 0; pp < 5; ++pp) { - AfficherSprite(pomme, pommex[pp], pommey[pp]); - } - -} \ No newline at end of file diff --git a/JEUX_SERPENT/scene.c b/JEUX_SERPENT/scene.c new file mode 100644 index 0000000..c81a7dc --- /dev/null +++ b/JEUX_SERPENT/scene.c @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include +#include "fonction.h" + +void Terrain(){ + if (pos_x[0] >1140 || pos_x[0]<=20) + go_on=0; + if (pos_y[0]<40 || pos_y[0] >=680) + go_on=0; +} + +/*Fonction Pour créer la première scene du jeu*/ +void DessinerScene(){ + snprintf(timer,6,"%02d:%02d", minute, seconde); + ChoisirCouleurDessin(CouleurParComposante(5,130,4)); + RemplirRectangle(20,20,1160,700); + ChoisirCouleurDessin(CouleurParComposante(255,255,255)); + EcrireTexte(10,760,timer,2); + serpent=ChargerSprite("IMG/serpent.png"); + fond=ChargerSprite("IMG/fond.png"); + + for (i = 0; i < segment; i++){ + AfficherSprite(serpent, x-(i*20), y); + pos_x[i]=x-(i*20); + pos_y[i]=y; + old_y[i]=pos_y[i]; + old_x[i]=pos_x[i]; + } + srand(time(NULL)); + pomme=ChargerSprite("IMG/pomme.png"); + for (p = 0; p < 5; p++) { + pommex[p] = ((rand() % (58)+1)*20); + pommey[p] = ((rand() % (35)+1)*20); + AfficherSprite(pomme, pommex[p], pommey[p]); + } +} +/*Apparition aléatoire des pommes*/ +void Pomme(){ + + for (pp = 0; pp < 5; ++pp) { + AfficherSprite(pomme, pommex[pp], pommey[pp]); + } + +} \ No newline at end of file diff --git a/JEUX_SERPENT/score.c b/JEUX_SERPENT/score.c new file mode 100644 index 0000000..0040b38 --- /dev/null +++ b/JEUX_SERPENT/score.c @@ -0,0 +1,16 @@ +#include +#include +#include +#include +#include +#include "fonction.h" + +void Score(){ + Nbr=(segment-10)*10; + snprintf(score,4,"%04d0", Nbr); + ChoisirCouleurDessin(CouleurParComposante(0,0,0)); + RemplirRectangle(1100,730,1200,800); + ChoisirCouleurDessin(CouleurParComposante(255,255,255)); + EcrireTexte(1000,750,"Score :",2); + EcrireTexte(1100,750,score,2); +} \ No newline at end of file diff --git a/JEUX_SERPENT/serpent b/JEUX_SERPENT/serpent new file mode 100755 index 0000000000000000000000000000000000000000..f1e9085060cfc2f1ff3b7f8bad8a7bb107a793f0 GIT binary patch literal 17480 zcmeHPeRP!7nZGm1t9;}O%V_*$R!u8x83INY#FPmkVWNNt!Ma=8ahSXzqe*6*nP9Le zXvE_TTW0P%`>q7?Z# z;%spe_+p7ExmOWLRr-xHmrl@l5h&SJQXvmLOTmIEM@W?H3Z=*-C1ENm6i;>r6*X_C zzbjLEOj&N#=T>rIqj&1|G0nE4zDdngcB$H2dRW`-JXzsvM=;q9YP&&g$8>}OLSoAO zNqs_p^K^gOsTVRzUYQGF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7<^^@LPa_rJjXSdH&h3PgS X*UP$7P_U^;(N7mBjZ literal 0 HcmV?d00001 diff --git a/JEUX_SERPENT/serpent.c b/JEUX_SERPENT/serpent.c index db9b266..7c45c61 100644 --- a/JEUX_SERPENT/serpent.c +++ b/JEUX_SERPENT/serpent.c @@ -3,6 +3,7 @@ #include #include #include +#include "fonction.h" /*Input Serpent*/ void Controle(){ @@ -76,3 +77,12 @@ void Update_Serpent(){ } } +void Collision(){ + if(seconde!=0 || minute!=0){ + for(i=1; i -#include -#include -#include -#include - -void Terrain(){ - if (pos_x[0] >1140 || pos_x[0]<=20) - go_on=0; - if (pos_y[0]<40 || pos_y[0] >=680) - go_on=0; -} \ No newline at end of file diff --git a/JEUX_SERPENT/timer.c b/JEUX_SERPENT/timer.c index e63ac0d..73e288a 100644 --- a/JEUX_SERPENT/timer.c +++ b/JEUX_SERPENT/timer.c @@ -3,10 +3,11 @@ #include #include #include +#include "fonction.h" #define CYCLE 10000L /*Fonction pour calculer le temps*/ -void Timer(unsigned long int suivant, char timer[6], int old_seconde=0, int seconde=0,int minute=0,int seconde_actuel=0){ +void Timer(){ if(Microsecondes()> suivant){ suivant = Microsecondes()+CYCLE; seconde_actuel = (suivant/1000000)%10;