Réglage time, réglage usleep, réglage menu

This commit is contained in:
Jude Christ AISSI 2023-12-22 15:11:29 +01:00
parent 284f9bea71
commit 29ed7dc6df
18 changed files with 99 additions and 316 deletions

View File

@ -1,29 +1,31 @@
CC = gcc CC = gcc
CFLAGS = -ansi -pedantic -lgraph CFLAGS = -ansi -pedantic -lgraph
OBJS = main.o serpent.o time.o terrain.o Oeuf.o SRCDIR = ./src
HDIR = ./fichier.h
ODIR = ./out
OFILES = $(subst $(SRCDIR)/,$(ODIR)/,$(subst .c,.o,$(shell find $(SRCDIR)/ -type f)))
EXE = snake
.PHONY: all clean run
snake: $(OBJS) all: $(EXE)
$(CC) -o snake $(OBJS) $(CFLAGS)
main.o: serpent.h time.h Oeuf.h main.h menu.h $(ODIR)/%.o : $(SRCDIR)/%.c
$(CC) -c main.c $(CFLAGS) @mkdir -p $(@D)
$(CC) -c $< -o $@
serpent.o: serpent.h $(EXE) : $(OFILES)
$(CC) -c serpent.c $(CFLAGS) $(CC) $(CFLAGS) -o $(EXE) $(OFILES)
menu.o: main.h time.h run : $(EXE)
$(CC) -c menu.c $(CFLAGS) ./$(EXE)
time.o: time.h serpent.h main.h clean :
$(CC) -c time.c $(CFLAGS) -rm -rf $(ODIR)
Oeuf.o: Oeuf.h time.h # Nouvelle règle pour la dépendance implicite
$(CC) -c Oeuf.c $(CFLAGS) $(EXE): $(ODIR)/a.out
terrain.o: terrain.h serpent.h time.h main.h Oeuf.h # Nouvelle règle pour construire 'out/a.out'
$(CC) -c terrain.c $(CFLAGS) $(ODIR)/a.out: $(OFILES)
$(CC) $(CFLAGS) -o $@ $(OFILES)
clean:
rm -f *.o snake

View File

@ -2,6 +2,6 @@
#define OEUF_H #define OEUF_H
void InitialiserOeufs(int oeufx[], int oeufy[], int segment); void InitialiserOeufs(int oeufx[], int oeufy[], int segment);
void Oeuf(int pos_x[], int pos_y[], int oeufx[], int oeufy[], int *segment); void Oeuf(int old_x[], int old_y[], int oeufx[], int oeufy[], int *segment);
#endif #endif

View File

@ -1,6 +1,8 @@
#ifndef MAIN_H #ifndef MAIN_H
#define MAIN_H #define MAIN_H
void attente(int milliseconds);
int lancer_jeu(void); int lancer_jeu(void);
int main(void); int main(void);

View File

@ -0,0 +1,6 @@
#ifndef TERRAIN_H
#define TERRAIN_H
void Scene(int murx[], int mury[], int minute, int seconde, char timer []);
#endif

View File

@ -1,234 +0,0 @@
#include <stdlib.h>
#include <stdio.h>
#include <graph.h>
#include <time.h>
#define CYCLE 100000L
void DessinerScene(int murx[], int mury[], int minute, int seconde, char timer []){
int mur;
int i;
snprintf(timer,6,"%02d:%02d", minute ,seconde);
ChoisirCouleurDessin(CouleurParComposante(218,209,77));
RemplirRectangle(20,20,1160,700);
ChoisirCouleurDessin(CouleurParComposante(255,255,255));
srand(time(NULL));
for(i=0; i<30; i++){
murx[i] = ((rand() % (55)+1)*20);
mury[i] = ((rand() % (35)+1)*20);
ChargerImage("../img/cactus.png", murx[i], mury[i],0,0, 20,20);
}
}
void Score(int segment){
int nombre;
char score[4];
nombre= (segment-10)*25;
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 Update_Timer(int minute, int seconde, char timer[]){
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,"time: ",2);
EcrireTexte(120,760,timer,2);
}
void Update_Serpent(int pos_x[], int pos_y[], int segment, int old_x[], int old_y[], int* direction){
int i = 0;
for (i=1 ; i<segment ; i++){
pos_x[i]=old_x[i-1];
pos_y[i]=old_y[i-1];
}
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 (i=0 ; i<segment ; i++){
ChargerImage("../img/serpent2.png", pos_x[i], pos_y[i],0,0, 20,20);
}
for (i=0 ; i<segment ; i++){
ChoisirCouleurDessin(CouleurParComposante(218,209,77));
RemplirRectangle(pos_x[segment-1], pos_y[segment-1], 20,20);
old_x[i]=pos_x[i];
old_y[i]=pos_y[i];
}
}
void Collision_Serpent(int pos_x[], int pos_y[], int segment, int murx[], int mury[], int *go_on){
int i;
/*Serpent contre coté*/
if (pos_x[0] >1160 || pos_x[0]<=0){
*go_on=0;
}
/*Serpent contre coté*/
if (pos_y[0]<20 || pos_y[0] >=700){
*go_on=0;
}
/*Serpent contre Serpent*/
for (i = 1; i < segment; i++) {
if (pos_x[0] == pos_x[i] && pos_y[0] == pos_y[i]){
*go_on = 0;
}
}
/*Serpent contre mur*/
for(i=0; i<30;i++){
if(pos_x[0] == murx[i] && pos_y[0] == mury[i]){
*go_on=0;
}
}
}
void Timer(int *minute, int *seconde, unsigned long int *suivant, int *seconde_actuel, int *old_seconde, char 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(*minute, *seconde, timer);
}else{
*seconde = *seconde+1;
Update_Timer(*minute, *seconde, timer);
}
}
}
}
void Controle(int *direction, int last_direction, int *go_on) {
int t;
while(ToucheEnAttente()) {
t = Touche();
switch(t) {
case XK_Left:
if (last_direction != 4) {
*direction = 3;
}
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;
}
}
}
void Serpent(int pos_x[], int pos_y[], int old_x[], int old_y[], int *segment, int murx[], int mury[], int *go_on, int *direction) {
int serpent;
int i = 0;
pos_x[0] = 600;
pos_y[0] = 400;
for (i = 0; i < *segment; i++){
pos_x[i]= pos_x[0];
pos_y[i]= pos_y[0];
old_x[i]=pos_x[i];
old_y[i]=pos_y[i];
}
}
void InitialiserOeufs(int oeufx[], int oeufy[], int segment) {
int p;
int oeuf;
for (p = 0; p < 5; p++) {
oeufx[p] = ((rand() % (55) + 1) * 20);
oeuf[p] = ((rand() % (35) + 1) * 20);
ChargerImage("../img/oeuf.png", oeufx[p], oeufy[p],0,0, 20,20);
}
}
void Oeuf(int pos_x[], int pos_y[], int oeufx[], int oeufy[], int *segment){
int p;
int oeuf;
for (p = 0; p < 5; p++) {
ChargerImage("../img/oeuf.png", oeufx[p], oeufy[p],0,0, 20,20);
}
for(p=0; p<5; p++){
if(oeufx[p]==pos_x[0] && oeufy[p]==pos_y[0]){
(*segment) +=2;
oeufx[p] = ((rand() % (55)+1)*20);
oeufy[p] = ((rand() % (35)+1)*20);
}
}
}
/*void Menu_debut(){
affiche = ChargerSprite("Img/image.png");
AfficherSprite(affiche,0,0);
}
Menu_debut();*/
int main(void){
int segment = 10;
int go_on = 1;
int direction = 4;
int minute = 0;
int seconde = 0;
int seconde_actuel = 0;
int old_seconde = 0;
unsigned long int suivant;
int pos_x[2400];
int pos_y[2400];
int old_x[2400];
int old_y[2400];
int oeufx[5];
int oeufy[5];
int murx[30];
int mury[30];
char timer[6];
int *pointeur_segment = &segment;
int *pointeur_go_on = &go_on;
int *pointeur_direction = &direction;
int *pointeur_minute = &minute;
int *pointeur_seconde = &seconde;
unsigned long int *pointeur_suivant = &suivant;
int *pointeur_seconde_actuel = &seconde_actuel;
int *pointeur_old_seconde = &old_seconde;
InitialiserGraphique();
CreerFenetre(350,100,1200,900);
EffacerEcran(CouleurParComposante(0,0,0));
suivant = Microsecondes()+CYCLE;
old_seconde=(suivant/1000000)%10;
DessinerScene(murx, mury, minute, seconde ,timer);
InitialiserOeufs(oeufx, oeufy, segment);
Serpent(pos_x, pos_y, old_x, old_y, pointeur_segment, murx, mury, pointeur_go_on, pointeur_direction);
while(go_on){
Timer( pointeur_minute, pointeur_seconde, pointeur_suivant, pointeur_seconde_actuel, pointeur_old_seconde, timer);
Score(segment);
Controle(pointeur_direction, 0, pointeur_go_on);
Update_Serpent(pos_x, pos_y, segment, old_x, old_y, pointeur_direction);
Collision_Serpent(pos_x, pos_y, segment, murx, mury, pointeur_go_on);
usleep(100000);
Oeuf(pos_x, pos_y, oeufx, oeufy, pointeur_segment);
}
FermerGraphique();
return EXIT_SUCCESS;
}

BIN
SAE_semestre1/img/queue.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

BIN
SAE_semestre1/snake Executable file

Binary file not shown.

View File

@ -1,7 +1,7 @@
#include <graph.h> #include <graph.h>
#include <stdlib.h> #include <stdlib.h>
#include "Oeuf.h" #include "../fichier.h/Oeuf.h"
#include "time.h" #include "../fichier.h/time.h"
void InitialiserOeufs(int oeufx[], int oeufy[], int segment) { void InitialiserOeufs(int oeufx[], int oeufy[], int segment) {
int p; int p;
@ -9,17 +9,17 @@ void InitialiserOeufs(int oeufx[], int oeufy[], int segment) {
for (p = 0; p < 5; p++) { for (p = 0; p < 5; p++) {
oeufx[p] = ((rand() % (55) + 1) * 20); oeufx[p] = ((rand() % (55) + 1) * 20);
oeufy[p] = ((rand() % (35) + 1) * 20); oeufy[p] = ((rand() % (35) + 1) * 20);
ChargerImage("../img/oeuf.png", oeufx[p], oeufy[p],0,0, 20,20); ChargerImage("img/oeuf.png", oeufx[p], oeufy[p],0,0, 20,20);
} }
} }
void Oeuf(int pos_x[], int pos_y[], int oeufx[], int oeufy[], int *segment){ void Oeuf(int old_x[], int old_y[], int oeufx[], int oeufy[], int *segment){
int p; int p;
int oeuf; int oeuf;
for (p = 0; p < 5; p++) { for (p = 0; p < 5; p++) {
ChargerImage("../img/oeuf.png", oeufx[p], oeufy[p],0,0, 20,20); ChargerImage("img/oeuf.png", oeufx[p], oeufy[p],0,0, 20,20);
} }
for(p=0; p<5; p++){ for(p=0; p<5; p++){
if(oeufx[p]==pos_x[0] && oeufy[p]==pos_y[0]){ if(oeufx[p]==old_x[0] && oeufy[p]==old_y[0]){
(*segment) +=2; (*segment) +=2;
oeufx[p] = ((rand() % (55)+1)*20); oeufx[p] = ((rand() % (55)+1)*20);
oeufy[p] = ((rand() % (35)+1)*20); oeufy[p] = ((rand() % (35)+1)*20);

View File

@ -1,12 +1,21 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <graph.h> #include <graph.h>
#include "serpent.h" #include <time.h>
#include "Oeuf.h" #include "../fichier.h/serpent.h"
#include "time.h" #include "../fichier.h/Oeuf.h"
#include "main.h" #include "../fichier.h/time.h"
#include "menu.h" #include "../fichier.h/main.h"
#include "../fichier.h/menu.h"
#define CYCLE 100000L #define CYCLE 100000L
#define DELAI_MILLISECONDES 100
void attente(int milliseconds) {
clock_t start_time = clock();
while ((clock() - start_time) * 1000 / CLOCKS_PER_SEC < milliseconds) {
}
}
int lancer_jeu(){ int lancer_jeu(){
int go_on=1; int go_on=1;
@ -37,7 +46,7 @@ int lancer_jeu(){
int *pointeur_pause = &pause; int *pointeur_pause = &pause;
suivant = Microsecondes()+CYCLE; suivant = Microsecondes()+CYCLE;
old_seconde=(suivant/1000000)%10; old_seconde=(suivant/1000000)%10;
DessinerScene(murx, mury, minute, seconde ,timer); Scene(murx, mury, minute, seconde ,timer);
InitialiserOeufs(oeufx, oeufy, segment); InitialiserOeufs(oeufx, oeufy, segment);
Serpent(pos_x, pos_y, old_x, old_y, pointeur_segment, murx, mury, &go_on, pointeur_direction); Serpent(pos_x, pos_y, old_x, old_y, pointeur_segment, murx, mury, &go_on, pointeur_direction);
bordure(segment); bordure(segment);
@ -49,7 +58,7 @@ int lancer_jeu(){
Update_Serpent(pos_x, pos_y, segment, old_x, old_y, pointeur_direction); Update_Serpent(pos_x, pos_y, segment, old_x, old_y, pointeur_direction);
dessinerSerpent(pos_x, pos_y, segment, old_x, old_y); dessinerSerpent(pos_x, pos_y, segment, old_x, old_y);
Collision_Serpent(pos_x, pos_y, segment, murx, mury, &go_on); Collision_Serpent(pos_x, pos_y, segment, murx, mury, &go_on);
usleep(90000); attente(DELAI_MILLISECONDES);
Oeuf(pos_x, pos_y, oeufx, oeufy, pointeur_segment); Oeuf(pos_x, pos_y, oeufx, oeufy, pointeur_segment);
} }
} }
@ -63,9 +72,6 @@ int main(void){
if(Menu() == 1){ if(Menu() == 1){
lancer_jeu(); lancer_jeu();
} }
if(Menu() == 0){
Menu();
}
FermerGraphique(); FermerGraphique();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -1,12 +1,16 @@
#include <stdlib.h> #include <stdlib.h>
#include <graph.h> #include <graph.h>
#include <stdio.h> #include <stdio.h>
#include "main.h" #include "../fichier.h/main.h"
#include "time.h" #include "../fichier.h/time.h"
void Menu_debut(void) { void Menu_debut(void) {
int af = ChargerSprite("../img/image.jpg"); int af = ChargerSprite("img/image.jpg");
AfficherSprite(af, 0, 0); AfficherSprite(af, 0, 0);
} }
void Menu_fin(void){
int fin = ChargerSprite("img/Menu_Fin.jpg");
AfficherSprite(fin, 0,0);
}
int Menu(void) { int Menu(void) {
int choix = 1; int choix = 1;
InitialiserGraphique(); InitialiserGraphique();
@ -22,20 +26,11 @@ int Menu(void) {
case XK_q: /* Changement de XK_q à XK_a*/ case XK_q: /* Changement de XK_q à XK_a*/
/*Code pour traiter la touche "a" (fermer le jeu, par exemple)*/ /*Code pour traiter la touche "a" (fermer le jeu, par exemple)*/
FermerGraphique(); FermerGraphique();
exit(EXIT_SUCCESS); return choix;
return;
/* Ajoutez d'autres cas pour d'autres touches si nécessaire*/
} }
} }
} }
} }
int Menu_fin(void){
int fin = ChargerSprite("../img/Menu_Fin.png");
AfficherSprite(fin, 0,0);
Menu();
}
void bordure(int segment){ void bordure(int segment){
ChoisirCouleurDessin(CouleurParComposante(0,0,0)); ChoisirCouleurDessin(CouleurParComposante(0,0,0));
RemplirRectangle(20,720,1180,200); RemplirRectangle(20,720,1180,200);

View File

@ -1,5 +1,6 @@
#include <graph.h> #include <graph.h>
#include "serpent.h" #include "../fichier.h/serpent.h"
#include "../fichier.h/main.h"
void Update_Serpent(int pos_x[], int pos_y[], int segment, int old_x[], int old_y[], int* direction){ void Update_Serpent(int pos_x[], int pos_y[], int segment, int old_x[], int old_y[], int* direction){
int i = 0; int i = 0;
@ -7,37 +8,45 @@ void Update_Serpent(int pos_x[], int pos_y[], int segment, int old_x[], int old_
pos_x[i]=old_x[i-1]; pos_x[i]=old_x[i-1];
pos_y[i]=old_y[i-1]; pos_y[i]=old_y[i-1];
} }
if (*direction == 1){ /*Afficher tête serpent selon la direction*/
if (*direction == 1 && *direction !=2 ){
pos_y[0]=old_y[0]-20; pos_y[0]=old_y[0]-20;
ChargerImage("../img/tete_up.png", pos_x[0], pos_y[0], 0,0,20,20); ChargerImage("img/tete_up.png", pos_x[0], pos_y[0], 0,0,20,20);
} }
if (*direction == 2) { if (*direction == 2 && *direction !=1) {
pos_y[0]=old_y[0]+20; pos_y[0]=old_y[0]+20;
ChargerImage("../img/tete_down.png", pos_x[0], pos_y[0], 0,0,20,20); ChargerImage("img/tete_down.png", pos_x[0], pos_y[0], 0,0,20,20);
} }
if (*direction == 3) { if (*direction == 3 && *direction !=4) {
pos_x[0]=old_x[0]-20; pos_x[0]=old_x[0]-20;
ChargerImage("../img/tete_left.png", pos_x[0], pos_y[0], 0,0,20,20); ChargerImage("img/tete_left.png", pos_x[0], pos_y[0], 0,0,20,20);
} }
if (*direction == 4) { if (*direction == 4 && *direction !=3) {
pos_x[0]=old_x[0]+20; pos_x[0]=old_x[0]+20;
ChargerImage("../img/tete_right.png", pos_x[0], pos_y[0], 0,0,20,20); ChargerImage("img/tete_right.png", pos_x[0], pos_y[0], 0,0,20,20);
} }
return; return;
} }
void dessinerSerpent(int pos_x[], int pos_y[], int segment, int old_x[], int old_y[]){ void dessinerSerpent(int pos_x[], int pos_y[], int segment, int old_x[], int old_y[]){
int i; int i;
/*Afficher le serpent*/
for (i=0 ; i<segment ; i++){ for (i=0 ; i<segment ; i++){
ChargerImage("../img/serpent.png", old_x[i], old_y[i],0,0, 20,20); ChargerImage("img/serpent.png", old_x[i], old_y[i],0,0, 20,20);
} }
/*Incrémentation du fond par rapport au déplacement du serpent*/
for (i=0 ; i<segment ; i++){ for (i=0 ; i<segment ; i++){
ChoisirCouleurDessin(CouleurParComposante(218,209,77)); ChoisirCouleurDessin(CouleurParComposante(218,209,77));
RemplirRectangle(old_x[segment-1], old_y[segment-1], 20,20); RemplirRectangle(old_x[segment-1], old_y[segment-1], 20,20);
old_x[i]=pos_x[i]; old_x[i]=pos_x[i];
old_y[i]=pos_y[i]; old_y[i]=pos_y[i];
} }
/*Affichier la queue du serpent*/
for (i=0 ; i<segment; i++){
ChargerImage("img/queue.png", old_x[segment-1], old_y[segment-1],0,0, 20,20);
ChoisirCouleurDessin(CouleurParNom("black"));
RemplirRectangle(0,0,20,20);
}
} }
void Collision_Serpent(int pos_x[], int pos_y[], int segment, int murx[], int mury[], int *go_on){ void Collision_Serpent(int pos_x[], int pos_y[], int segment, int murx[], int mury[], int *go_on){
int i; int i;
@ -70,22 +79,22 @@ void Controle(int *direction, int last_direction, int *go_on, int *pause) {
t = Touche(); t = Touche();
switch(t) { switch(t) {
case XK_Left: case XK_Left:
if (last_direction != 4) { if (*direction != 4) {
*direction = 3; *direction = 3;
} }
return; return;
case XK_Right: case XK_Right:
if (last_direction != 3) { if (*direction != 3) {
*direction = 4; *direction = 4;
} }
return; return;
case XK_Up: case XK_Up:
if (last_direction != 2) { if (*direction != 2) {
*direction = 1; *direction = 1;
} }
return; return;
case XK_Down: case XK_Down:
if (last_direction != 1) { if (*direction != 1) {
*direction = 2; *direction = 2;
} }
return; return;
@ -98,7 +107,7 @@ void Controle(int *direction, int last_direction, int *go_on, int *pause) {
*pause == 1; *pause == 1;
}else{ }else{
*pause = 0; *pause = 0;
ChargerImage("../img/PAUSE.png", 400,200,0,0,350,300); ChargerImage("img/PAUSE.png", 400,200,0,0,350,300);
} }
} }
} }

View File

@ -1,12 +1,14 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <graph.h> #include <graph.h>
#include "time.h" #include <time.h>
#include "terrain.h" #include "../fichier.h/time.h"
#include "serpent.h" #include "../fichier.h/terrain.h"
#include "main.h" #include "../fichier.h/serpent.h"
#include "Oeuf.h" #include "../fichier.h/main.h"
#include "../fichier.h/Oeuf.h"
void DessinerScene(int murx[], int mury[], int minute, int seconde, char timer []){ void Scene(int murx[], int mury[], int minute, int seconde, char timer []){
int mur; int mur;
int i; int i;
snprintf(timer,6,"%02d:%02d", minute ,seconde); snprintf(timer,6,"%02d:%02d", minute ,seconde);
@ -17,6 +19,6 @@ void DessinerScene(int murx[], int mury[], int minute, int seconde, char timer [
for(i=0; i<30; i++){ for(i=0; i<30; i++){
murx[i] = ((rand() % (55)+1)*20); murx[i] = ((rand() % (55)+1)*20);
mury[i] = ((rand() % (35)+1)*20); mury[i] = ((rand() % (35)+1)*20);
ChargerImage("../img/cactus.png", murx[i], mury[i],0,0, 20,20); ChargerImage("img/cactus.png", murx[i], mury[i],0,0, 20,20);
} }
} }

View File

@ -1,10 +1,11 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <graph.h> #include <graph.h>
#include "time.h" #include "../fichier.h/time.h"
#include "main.h" #include "../fichier.h/main.h"
#include "serpent.h" #include "../fichier.h/serpent.h"
#define CYCLE 100000L #define CYCLE 1000000L
void Score(int segment){ void Score(int segment){
int nombre; int nombre;

View File

@ -1,6 +0,0 @@
#ifndef TERRAIN_H
#define TERRAIN_H
void DessinerScene(int murx[30], int mury[30], int minute, int seconde, char timer []);
#endif