From 711ab48c329e7e8d8c48f38d0f67fa75942aabc1 Mon Sep 17 00:00:00 2001 From: boutarci Date: Thu, 7 Dec 2023 14:32:42 +0100 Subject: [PATCH] =?UTF-8?q?S=C3=A9paration=20game.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SAE_semestre1/Makefile | 28 ++--- SAE_semestre1/fichier.h/controle.h | 6 - SAE_semestre1/fichier.h/pastille.h | 5 +- SAE_semestre1/fichier.h/serpent.h | 19 ++- SAE_semestre1/fichier.h/terrain.h | 12 ++ SAE_semestre1/fichier.h/time.h | 3 +- SAE_semestre1/img/fond.png | Bin 0 -> 600 bytes SAE_semestre1/img/pomme.png | Bin 0 -> 1235 bytes SAE_semestre1/img/serpent.png | Bin 492 -> 493 bytes SAE_semestre1/src/controle.c | 29 ----- SAE_semestre1/src/game.c | 181 ----------------------------- SAE_semestre1/src/main.c | 61 +--------- SAE_semestre1/src/pastille.c | 40 +++++++ SAE_semestre1/src/serpent.c | 40 ++++--- SAE_semestre1/src/terrain.c | 72 +++++++----- 15 files changed, 152 insertions(+), 344 deletions(-) delete mode 100644 SAE_semestre1/fichier.h/controle.h create mode 100644 SAE_semestre1/img/fond.png create mode 100644 SAE_semestre1/img/pomme.png delete mode 100644 SAE_semestre1/src/controle.c delete mode 100644 SAE_semestre1/src/game.c diff --git a/SAE_semestre1/Makefile b/SAE_semestre1/Makefile index fbfab30..61afba1 100644 --- a/SAE_semestre1/Makefile +++ b/SAE_semestre1/Makefile @@ -1,25 +1,11 @@ - - CC = gcc -CFLAGS = -ansi -pedantic -lgraph -g -SRCDIR = ./src -HDIR = ./fichier.h -ODIR = ./out -OFILES = $(subst src/,out/,$(subst .c,.o,$(shell find $(SRCDIR)/ -type f))) -EXE = snake +CFLAGS = -Wall -Wextra -std=c99 +LIBS = -lgraph -lm -but : $(EXE) +all: main -$(ODIR)/%.o : $(SRCDIR)/%.c - @mkdir -p $(@D) - $(CC) -c $< -o $@ +main: main.c jeu.c serpent.c pomme.c + $(CC) $(CFLAGS) -o main main.c jeu.c serpent.c pomme.c $(LIBS) -$(EXE) : $(OFILES) - $(CC) $(CFLAGS) -o $(EXE) $(OFILES) - -run : $(EXE) - ./$(EXE) - -clean : -rm -f $(OFILES) snake - -.PHONY : but clean +clean: + rm main \ No newline at end of file diff --git a/SAE_semestre1/fichier.h/controle.h b/SAE_semestre1/fichier.h/controle.h deleted file mode 100644 index 6818f0d..0000000 --- a/SAE_semestre1/fichier.h/controle.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef CONTROLE_H -#define CONTROLE_H - -void Controle(); - -#endif \ No newline at end of file diff --git a/SAE_semestre1/fichier.h/pastille.h b/SAE_semestre1/fichier.h/pastille.h index 9316f1e..fc18b9e 100644 --- a/SAE_semestre1/fichier.h/pastille.h +++ b/SAE_semestre1/fichier.h/pastille.h @@ -1,5 +1,8 @@ #ifndef PASTILLE_H #define PASTILLE_H +void genererPomme(); +void dessinerPomme(); +int collisionAvecPomme(); -#endif \ No newline at end of file +#endif diff --git a/SAE_semestre1/fichier.h/serpent.h b/SAE_semestre1/fichier.h/serpent.h index 709dc7a..014aaf3 100644 --- a/SAE_semestre1/fichier.h/serpent.h +++ b/SAE_semestre1/fichier.h/serpent.h @@ -1,10 +1,21 @@ #ifndef SERPENT_H #define SERPENT_H -typedef struct{ +#include "../fichier.h/terrain.h" + +typedef struct { Position *corps; int longueur; } Serpent; -Position pomme; -Serpent serpent; -#endif \ No newline at end of file + +extern Serpent serpent; +extern int direction; + +void initialiserSerpent(); +void dessinerSerpent(); +void deplacerSerpent(); +int collisionAvecSerpent(); +int collisionAvecBordures(); +void gestionCollision(); + +#endif diff --git a/SAE_semestre1/fichier.h/terrain.h b/SAE_semestre1/fichier.h/terrain.h index e69de29..dbc1dc6 100644 --- a/SAE_semestre1/fichier.h/terrain.h +++ b/SAE_semestre1/fichier.h/terrain.h @@ -0,0 +1,12 @@ +#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(); + +#endif diff --git a/SAE_semestre1/fichier.h/time.h b/SAE_semestre1/fichier.h/time.h index e5228cd..48ec409 100644 --- a/SAE_semestre1/fichier.h/time.h +++ b/SAE_semestre1/fichier.h/time.h @@ -1,7 +1,8 @@ #ifndef TIME_H #define TIME_H + #define CYCLE 10000L void Timer(unsigned long int suivant); -#endif TIME_H \ No newline at end of file +#endif \ No newline at end of file diff --git a/SAE_semestre1/img/fond.png b/SAE_semestre1/img/fond.png new file mode 100644 index 0000000000000000000000000000000000000000..7b85b77ce6f2676ff6ab74868e784c7f0c477c4e GIT binary patch literal 600 zcmV-e0;m0nP) zh|h_~4Z0xlBiCh@-#C{X7IusGS_L2Ac;jRAq^2S>ZqU!3o%+XQcPrMKjGmYcKm5_$>gelkz)ZhsE`~# z_#gc4)+|mDYDGy0}HFn9~}t+~Cm_i_3FWT~sv4RCM>jFl*R-Q(T8 z-M#&LrrqBUja_oFV6rKQ00003b3#c}2nYz<;ZNWI003WlR9JLUVRs;Ka&Km7Y-J#H zd2nSQK~PXJ000P?<4RA>OD)PwRwyXSPs_|nKTwmDQN(EtUQ8kkv{8JiiJ12Krt1pr946v;Z&LoxsW03At0K~y-6V~oC6#Xv4# mB3tLE=26X~nnyKLMKb_;69L=E*}C=s0000fdnxwEc-FLGwjZSuaq?{w|NXKJMYA!5iOBi&;l<6 z3D5?Fb<@p_$70ho9G`bIsr5FjvKpWJbF`;cToal!;W0Dkg^~#e2dbb5&0X zp9v2*azWt-r5hD~Rh%iaPi2Ojx%h-|NLb6%u~En7s$&Tc3-_ihTk?I03a1orDz>r( zvwBbU!r+QMKdW?^Jw%yeh8P+K1=h%tCoHQiic4JfBMtoBWq(XuhPc)+#WBe`7JAu_ z`h(xyy6K_8N>VTaEzh3!-Uopl_%3?h*OBLaAA|o1F729sy#%8lq8Dpg?;&(-!TD89 zGj`!(8@e8Ix}%pRzf4~`4R5FNO-bmyfwsj;Z`FNH?qN>4n!bkp1L)64_ELj)x7PRe z@0n_UKU4T}n$`j9NB{r;19L)2R0s$N+u={(00006VoOIv0RI600RN!9r;`8x0AG4k zSaechcOY9y8Ey_$*C@9KL%gjmTQUC*E6D|`= z6HB8+0|NsK0|+oQFfcX*l14zbIaDao00o#Dm|2<`n;Dt|F^JCv07$kJ$vV_SG5`Pq ziAh93R5*>5)K5s$VHgMS@AJOD-?rx5QfHfUIt$aB;wsaS{ty`m9_&yzDJb$5NOg%0 zLPWQ2L3Hidp)Nr|L|7OknMTbhEw%l1ZZ$XE_H(~KZ-+)%BI$E{UOs#u-sk0|f!S+V zSIhI?KR+C;KOEq8Q*v%vQIg;S1~Z!~9Zs*a#b+uvOY0c`xy-CO=9a?l^y*vLDTGA` z9Rfm_6Pej5&ih>*zCTi5)pEgjQ!sAN6tdgoDP78j#`X~=2_%Ed%+QegWNtpxTOFta zP?EWD`q-_Jqpy=TVgLjnofJ|UXJ1-VhMthMtVLHeBjUGD^_V~?$^1h0^rc5#ks^oz z&>=o*s(YRkD~duS4ZT@2AtzLyuQ-!x8+>s8cE5G6mkHh2fA>{qr1*Pm4r-H3JA=$M zPf4eq~+8N<4WgVhh%xixy(LKEzq$M07K(6dMX@6?U_yt}0(-K{wvq&)a4K-;@`1#twNViAHWG!EpB%-`9R6rsWU__u!c6#0n_fl5_sI-6j=Qk0C xC@d~3sT>jbs#Y1zs1nOHR9PFHO#bV6{{;4k6-{zeO#%P_002ovPDHLkV1g!IAF==d literal 0 HcmV?d00001 diff --git a/SAE_semestre1/img/serpent.png b/SAE_semestre1/img/serpent.png index f2284451a8e6925c472e6f42d6e224ad9d9bcd53..cdeccde6ed138d6ea09ffd7cc6cadefee82d41b9 100644 GIT binary patch literal 493 zcmVrd-(Wz7v)*r=l&c6O2K4+Pb7{r-LQx^h+~_U&Uv3W%*v8N zd`>)R&;^Mfxh}i>#<}FMz%#=}HZxBgCKgLwEO#+08!GV(aYRuy$`^7jE1b7DtJOMd z-;=*ERM1wIxlVHwNi1RsX^4<4==ICRYWF91EyHh2;3b z|KNAGW^sDTO$sG|z>95vj03@4pw+PL?_=9;odEu4;7aTGYfWJGlk`SMiyQ%i+rY(j zN0aw}%N=0&NtX=Ck^D4;Vi9;hqi@OsL$^Txn%i4@AEysMmbyya00)P_M2WK3J>K2h z-P^xs+Wq|it>|*TJS!=E00003b3#c}2nYz<;ZNWI0013HL_t(Ijbl`_zs5i=U?N-R jsOC}4qnbxGQ$;fXCZz#h@lj{Z00000NkvXXu0mjfa(L9m literal 492 zcmeAS@N?(olHy`uVBq!ia0vp^DIm j6{4-j35Avr_b2wYa*tHe-G6(4vB==*>gTe~DWM4fx=+Jy diff --git a/SAE_semestre1/src/controle.c b/SAE_semestre1/src/controle.c deleted file mode 100644 index fff9d12..0000000 --- a/SAE_semestre1/src/controle.c +++ /dev/null @@ -1,29 +0,0 @@ -#include -#include -#include - int t ; - int direction; - int go_on =0; -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; - break; - } - } -} \ No newline at end of file diff --git a/SAE_semestre1/src/game.c b/SAE_semestre1/src/game.c deleted file mode 100644 index e12370d..0000000 --- a/SAE_semestre1/src/game.c +++ /dev/null @@ -1,181 +0,0 @@ -#include -#include -#include -#include "time.c" - -#define LARGEUR_FENETRE 1250 -#define HAUTEUR_FENETRE 750 -#define TAILLE_CELLULE 25 -#define DELAI_MILLISECONDES 100 -#define ESPACE_NOIR 100 -#define CYCLE 1000L - - int j,i; - int pommex[5]; - int pommey[5]; -typedef struct { - int x, y; -} Position; - -typedef struct { - Position *corps; - int longueur; -} Serpent; -Position pomme; -Serpent serpent; -int direction = 0; /* 0: droite, 1: bas, 2: gauche, 3: haut*/ - - - -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() { - 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 deplacerSerpent() { - /* Déplacer le corps du serpent*/ - for (i = serpent.longueur - 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 i; -void genererPomme() { - 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 dessinerPomme() { - ChoisirCouleurDessin(CouleurParComposante(255, 0, 0)); - for(i=0; i<5; i++){ - RemplirRectangle(pommex[i], pommey[i], TAILLE_CELLULE, TAILLE_CELLULE); - } -} -int collisionAvecPomme() { - for(i=0;i<5;i++){ - return (serpent.corps[0].x == pommex[i] && serpent.corps[i].y == pommey[i]); -} -} - -int collisionAvecSerpent() { - 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; - } - } - 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 dessinerDamier() { - int cell_largeur = LARGEUR_FENETRE / TAILLE_CELLULE; - int cell_hauteur = HAUTEUR_FENETRE / TAILLE_CELLULE; - for (i = 0; i < cell_hauteur; i++) { - for (j = 0; j < cell_largeur; j++) { - if ((i + j) % 2 == 0) { - ChoisirCouleurDessin(CouleurParComposante(144, 238, 144)); - } else { - ChoisirCouleurDessin(CouleurParComposante(143, 218, 143)); - } - RemplirRectangle(j * TAILLE_CELLULE, i * TAILLE_CELLULE, TAILLE_CELLULE, TAILLE_CELLULE); - } - } -} -void gestionCollision() { - if (collisionAvecPomme()) { - serpent.longueur++; - serpent.corps = realloc(serpent.corps, sizeof(Position) * serpent.longueur); - genererPomme(); - } - - if (collisionAvecSerpent() || collisionAvecBordures()) { - FermerGraphique(); - exit(EXIT_SUCCESS); - } -} - -void attente(int milliseconds) { - clock_t start_time = clock(); - while ((clock() - start_time) * 1000 / CLOCKS_PER_SEC < milliseconds) { - /* Attente*/ - } -} -void jeu() { - 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*/ - Timer(); - 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(); - } - } - } -} - -int main() { - jeu(); - return 0; -} \ No newline at end of file diff --git a/SAE_semestre1/src/main.c b/SAE_semestre1/src/main.c index 13d5d24..650d0e4 100644 --- a/SAE_semestre1/src/main.c +++ b/SAE_semestre1/src/main.c @@ -1,59 +1,6 @@ -#include -#include -#include -#include -#include -#include "../fichier.h/time.h" -#include "../fichier.h/controle.h" -#define CYCLE 10000L - int go_on=0; -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(141,199,63)); - RemplirRectangle(20,20,1160,700); - ChoisirCouleurDessin(CouleurParComposante(255,255,255)); - EcrireTexte(10,760,timer,2); - serpent=ChargerSprite("serpent.png"); - fond = ChargerSprite("fond.png"); +#include "../fichier.h/terrain.h" - - 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("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]); - } -} - -int main(){ - - /* paramétrage de la fenêtre + chargement première scène */ - InitialiserGraphique(); - CreerFenetre(350,100,1200,800); - EffacerEcran(CouleurParComposante(0,0,0)); - suivant = Microsecondes()+CYCLE; - old_seconde=(suivant/1000000)%10; - DessinerScene(); - - /*Boucle Principale du Programme*/ - while(go_on){ - Timer(); - Controle(); - Serpent(); - Pomme(); - } - - /* fermeture de la fenêtre si ECHAP pressé*/ - FermerGraphique(); - return EXIT_SUCCESS; +int main() { + jeu(); + return 0; } \ No newline at end of file diff --git a/SAE_semestre1/src/pastille.c b/SAE_semestre1/src/pastille.c index e69de29..a9d1b4f 100644 --- a/SAE_semestre1/src/pastille.c +++ b/SAE_semestre1/src/pastille.c @@ -0,0 +1,40 @@ +#include +#include +#include "../fichier.h/pastille.h" +#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 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]); + } +} \ No newline at end of file diff --git a/SAE_semestre1/src/serpent.c b/SAE_semestre1/src/serpent.c index 1e9e8ce..2999793 100644 --- a/SAE_semestre1/src/serpent.c +++ b/SAE_semestre1/src/serpent.c @@ -1,34 +1,30 @@ #include -#include #include -#include "../pastille.h" +#include "serpent.h" +#include "terrain.h" - -typedef struct { - Position *corps; - int longueur; -} Serpent; -Position pomme; Serpent serpent; -int direction = 0; /* 0: droite, 1: bas, 2: gauche, 3: haut*/ +int direction; void initialiserSerpent() { - serpent.longueur = 10; + 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(CouleurParComposante(0,0,0)); /*JAUNE*/ + ChoisirCouleurDessin(CouleurParNom("yellow")); /*JAUNE*/ } RemplirRectangle(serpent.corps[i].x, serpent.corps[i].y, TAILLE_CELLULE, TAILLE_CELLULE); } } void deplacerSerpent() { + int i; /* Déplacer le corps du serpent*/ for (i = serpent.longueur - 1; i > 0; i--) { serpent.corps[i] = serpent.corps[i - 1]; @@ -50,13 +46,23 @@ void deplacerSerpent() { break; } } -void gestionCollision() { - if (collisionAvecPomme()) { - serpent.longueur++; - serpent.corps = realloc(serpent.corps, sizeof(Position) * serpent.longueur); - genererPomme(); - } +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; + } + } + 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); diff --git a/SAE_semestre1/src/terrain.c b/SAE_semestre1/src/terrain.c index 0a23148..2a0e331 100644 --- a/SAE_semestre1/src/terrain.c +++ b/SAE_semestre1/src/terrain.c @@ -1,38 +1,56 @@ #include -#include #include -/*#include "time.h"*/ +#include "../fichier.h/time.h" +#include "../fichier.h/terrain.h" +#include "../fichier.h/serpent.h" -#define HAUTEUR 20 -#define LARGEUR 30 -#define TAILLE_CASE 20 -#define ESPACE_NOIR 100 -int direction = 4; -int t; +#define LARGEUR_FENETRE 1250 +#define HAUTEUR_FENETRE 750 +#define TAILLE_CELLULE 25 +#define DELAI_MILLISECONDES 100 +#define ESPACE_NOIR 100 -void AfficherQuadrillage(){ - int j; - int i; +void jeu(void) { InitialiserGraphique(); - CreerFenetre(10, 10, 30 * TAILLE_CASE, 20 * TAILLE_CASE + ESPACE_NOIR); - - for (i = 0; i < LARGEUR; i++) { - for (j = 0; j < HAUTEUR; j++) { - if ((i + j) % 2 == 0) { - ChoisirCouleurDessin(CouleurParComposante(144, 238, 144)); - } else { - ChoisirCouleurDessin(CouleurParComposante(0, 128, 0)); + 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(); } - RemplirRectangle(i * TAILLE_CASE, j * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE); } } - ChoisirCouleurDessin(CouleurParComposante(0, 0, 0)); - RemplirRectangle(0, HAUTEUR * TAILLE_CASE, LARGEUR * TAILLE_CASE, ESPACE_NOIR); - ChoisirCouleurDessin(CouleurParComposante(0, 0, 0)); - RemplirRectangle(0, 0, LARGEUR * TAILLE_CASE, TAILLE_CASE); - RemplirRectangle(0, 0, TAILLE_CASE, HAUTEUR * TAILLE_CASE); - RemplirRectangle(0, (HAUTEUR - 1) * TAILLE_CASE, LARGEUR * TAILLE_CASE, TAILLE_CASE); - RemplirRectangle((LARGEUR - 1) * TAILLE_CASE, 0, TAILLE_CASE, HAUTEUR * TAILLE_CASE); }