ajout fichier.h et makefile

This commit is contained in:
Jude Christ AISSI 2023-12-19 17:27:47 +01:00
parent 9450a335f5
commit 1676f05187
19 changed files with 69 additions and 316 deletions

View File

@ -1,25 +1,26 @@
### VARIABLES ###
CC = gcc CC = gcc
CFLAGS = -lgraph -ansi CFLAGS = -ansi -pedantic -lgraph
SRCDIR = ./src OBJS = main.o serpent.o time.o terrain.o pastille.o
HDIR = ./fichier.h
ODIR = ./out
OFILES = $(subst src/,out/,$(subst .c,.o,$(shell find $(SRCDIR)/ -type f))) serpent: $(OBJS)
EXE = game $(CC) -o serpent $(OBJS) $(CFLAGS)
### BUT PAR DEFAUT ###
but : $(EXE) main.o: serpent.h time.h pastille.h main.h
### REGLES ESSENTIELLES ### $(CC) -c main.c $(CFLAGS)
$(ODIR)/%.o : $(SRCDIR)/%.c
@mkdir -p $(@D) serpent.o: serpent.h
$(CC) -c $< -o $@ $(CC) -c serpent.c $(CFLAGS)
$(EXE) : $(OFILES)
$(CC) $(CFLAGS) -o $(EXE) $(OFILES) timer.o: timer.h serpent.h main.h
### REGLES OPTIONNELLES ### $(CC) -c timer.c $(CFLAGS)
run : $(EXE)
./$(EXE) pastille.o: pastille.h serpent.h
clean : $(CC) -c pastille.c $(CFLAGS)
-rm -rf $(ODIR)
mrproper : terrain.o: terrain.h serpent.h time.h main.h pastille.h
clean but $(CC) -c terrain.c $(CFLAGS)
### BUTS FACTICES ###
.PHONY : but run clean mrproper clean:
rm -f *.0 serpent

View File

@ -1,5 +0,0 @@
#ifndef FONCTIONS_H
#define FONCTIONS_H
#endif

View File

@ -1,6 +1,6 @@
#ifndef MAIN_H #ifndef MAIN_H
#define MAIN_H #define MAIN_H
void main(void); int main(void);
#endif #endif

View File

@ -1,6 +1,7 @@
#ifndef PASTILLE_H #ifndef PASTILLE_H
#define PASTILLE_H #define PASTILLE_H
void Pomme(); void InitialiserPommes(int pommex[], int pommey[], int segment);
void Pomme(int pos_x[], int pos_y[], int pommex[], int pommey[], int *segment);
#endif #endif

View File

@ -1,30 +1,8 @@
#ifndef SERPENT_H #ifndef SERPENT_H
#define SERPENT_H #define SERPENT_H
#define LARGEUR_FENETRE 1200
#define HAUTEUR_FENETRE 900
#define TAILLE_CASE 20
#define NB_COLONNES 60
#define NB_LIGNES 40
#define CYCLE 100000
typedef struct {
int x , y;
} Corps;
typedef struct {
int longueur;
int directionX;
int directionY;
Corps corps[NB_COLONNES * NB_LIGNES];
} Serpent;
void InitialiserSerpent(Serpent *serpent, int x, int y);
void DeplacerSerpent(Serpent *serpent);
int VerifierCollision(const Serpent *serpent);
void DessinerSerpent(const Serpent *serpent);
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);
void Collision_Serpent(int pos_x[], int pos_y[], int segment, int murx[], int mury[], int *go_on);
void Update_Serpent(int pos_x[], int pos_y[], int segment, int old_x[], int old_y[]);
void Controle(int *direction, int last_direction, int *go_on);
#endif #endif

View File

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

View File

@ -1,8 +1,7 @@
#ifndef TIME_H #ifndef TIME_H
#define TIME_H #define TIME_H
#define CYCLE 10000L
void Timer(); void Score(int segment);
#endif #endif

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 49 KiB

View File

@ -1,20 +0,0 @@
#include <stdlib.h>
#include <graph.h>
#include <time.h>
#include "fruit.h"
void pomme(){
int pos_x[60];
int pos_y[60];
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]){
pommex[p] = ((rand() % (60)+1)*20);
pommey[p] = ((rand() % (27)+1)*20);
}
}
for(pp = 0; pp < 5; ++pp){
AfficherSprite(pomme, pommex[pp], pommey[pp]);
}
}

View File

@ -1,21 +0,0 @@
#ifndef FRUIT_H
#define FRUIT_H
#include"serpent.h"
typedef struct {
int x;
int y;
int estMangee;
int sprite; /* Ajout d'un identifiant pour le sprite de la pomme */
} Pomme;
void InitialiserPommes(Pomme pommes[], int nombrePommes);
void GenererPommes(Pomme pommes[], int nombrePommes);
void AfficherPommes(Pomme pommes[], int nombrePommes);
void MangerPomme(Pomme pommes[], int nombrePommes, int x, int y);
void DessinerPomme(int x, int y, int sprite);
int ChargerSprite(char *file);
void AfficherSprite(int n, int x, int y);
void LibererSprite(int n);
#endif

View File

@ -1,87 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <graph.h>
#include "serpent.h"
#include "fruit.h"
#define LARGEUR_FENETRE 1600 /* Largeur de la fenêtre (60 colonnes de jeu + murs de 2 cases de chaque côté) */
#define HAUTEUR_FENETRE 1000 /* Hauteur de la fenêtre (40 lignes de jeu + murs de 2 cases en haut et en bas) */
#define TAILLE_CASE 20 /* Taille d'une case pour le jeu de Snake */
#define NB_COLONNES 60 /* Nombre de colonnes du jeu */
#define NB_LIGNES 40 /* Nombre de lignes du jeu */
#define CYCLE 100000
#define VITESSE_SERPENT 100000
#define NB_POMMES 5
int main() {
couleur couleurFond = CouleurParComposante(200, 200, 200);
int touchePressee, i;
Serpent serpent;
unsigned long tempsPrecedent = Microsecondes();
unsigned long tempsActuel;
couleur couleurMurs = CouleurParComposante(0, 0, 0);
Pomme pommes[NB_POMMES];
srand(time(NULL));
InitialiserGraphique();
CreerFenetre(10, 10, LARGEUR_FENETRE, HAUTEUR_FENETRE);
EffacerEcran(couleurFond);
AfficherFenetre();
pomme();
InitialiserSerpent(&serpent, LARGEUR_FENETRE / 2, HAUTEUR_FENETRE / 2);
while (1) {
if (ToucheEnAttente()) {
int touche = Touche();
if (touche == XK_Left && serpent.directionX != 1) {
serpent.directionX = -1;
serpent.directionY = 0;
} else if (touche == XK_Right && serpent.directionX != -1) {
serpent.directionX = 1;
serpent.directionY = 0;
} else if (touche == XK_Up && serpent.directionY != 1) {
serpent.directionX = 0;
serpent.directionY = -1;
} else if (touche == XK_Down && serpent.directionY != -1) {
serpent.directionX = 0;
serpent.directionY = 1;
} else if (touche == XK_Escape) {
break;
}
}
tempsActuel = Microsecondes();
if (tempsActuel - tempsPrecedent >= VITESSE_SERPENT) {
DeplacerSerpent(&serpent);
if (VerifierCollision(&serpent)) {
FermerGraphique();
return EXIT_SUCCESS;
}
EffacerEcran(couleurFond);
DessinerSerpent(&serpent);
AfficherFenetre();
tempsPrecedent = tempsActuel;
ChoisirCouleurDessin(couleurMurs);
RemplirRectangle(0, HAUTEUR_FENETRE - 80, LARGEUR_FENETRE, 80);
}
}
for (i = 0; i < NB_POMMES; i++) {
LibererSprite(pommes[i].sprite);
}
FermerGraphique();
return EXIT_SUCCESS;
}

View File

@ -1,30 +0,0 @@
#ifndef SERPENT_H
#define SERPENT_H
#define LARGEUR_FENETRE 1200
#define HAUTEUR_FENETRE 900
#define TAILLE_CASE 20
#define NB_COLONNES 60
#define NB_LIGNES 40
#define CYCLE 100000
typedef struct {
int x , y;
} Corps;
typedef struct {
int longueur;
int directionX;
int directionY;
Corps corps[NB_COLONNES * NB_LIGNES];
} Serpent;
void InitialiserSerpent(Serpent *serpent, int x, int y);
void DeplacerSerpent(Serpent *serpent);
int VerifierCollision(const Serpent *serpent);
void DessinerSerpent(const Serpent *serpent);
#endif

View File

@ -1,76 +0,0 @@
#include <stdlib.h>
#include <graph.h>
#include"serpent.h"
#include <unistd.h>
#define LARGEUR_FENETRE 1200
#define HAUTEUR_FENETRE 900
#define TAILLE_CASE 20
#define NB_COLONNES 60
#define NB_LIGNES 40
#define CYCLE 100000
void InitialiserSerpent(Serpent *serpent, int x, int y) {
serpent->longueur = 10;
serpent->directionX = 1;
serpent->directionY = 0;
serpent->corps[0].x = x;
serpent->corps[0].y = y;
}
void DeplacerSerpent(Serpent *serpent) {
int i;
for ( i = serpent->longueur - 1; i > 0; i--) {
serpent->corps[i] = serpent->corps[i - 1];
}
serpent->corps[0].x += serpent->directionX * TAILLE_CASE;
serpent->corps[0].y += serpent->directionY * TAILLE_CASE;
}
int VerifierCollision(const Serpent *serpent) {
int teteX = serpent->corps[0].x;
int teteY = serpent->corps[0].y;
int i;
if (teteX < 0 || teteX >= LARGEUR_FENETRE || teteY < 0 || teteY >= HAUTEUR_FENETRE-80) {
return 1;
}
for (i = 1; i < serpent->longueur; i++) {
if (teteX == serpent->corps[i].x && teteY == serpent->corps[i].y) {
return 1;
}
}
return 0;
}
void DessinerSerpent(const Serpent *serpent) {
int i;
couleur couleurSerpent = CouleurParComposante(34, 139, 34);
ChoisirEcran(1);
EffacerEcran(CouleurParNom("grey"));
ChoisirCouleurDessin(couleurSerpent);
/* Afficher la tête du serpent */
RemplirRectangle(serpent->corps[0].x, serpent->corps[0].y, TAILLE_CASE, TAILLE_CASE);
for (i = 0; i < serpent->longueur; i++) {
RemplirRectangle(serpent->corps[i].x, serpent->corps[i].y, TAILLE_CASE, TAILLE_CASE);
}
ChoisirEcran(0);
AfficherFenetre();
CopierZone(1, 0, 0, 0, LARGEUR_FENETRE, HAUTEUR_FENETRE, 0, 0);
}

View File

@ -1,9 +1,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <graph.h> #include <graph.h>
#include "serpent.h" #include "../fichier.h/serpent.h"
#include "pommes.h" #include "../fichier.h/pastille.h"
#include "time.h" #include "../fichier.h/time.h"
#include "../fichier.h/main.h"
#define CYCLE 10000L #define CYCLE 10000L
@ -25,17 +26,18 @@ int main(void) {
InitialiserGraphique(); InitialiserGraphique();
CreerFenetre(350,100,1200,900); CreerFenetre(350,100,1200,900);
EffacerEcran(CouleurParComposante(0,0,0)); EffacerEcran(CouleurParComposante(0,0,0));
suivant = Microsecondes()+CYCLE; /* suivant = Microsecondes()+CYCLE;
old_seconde=(suivant/1000000)%10; old_seconde=(suivant/1000000)%10;*/
DessinerScene(murx, mury); DessinerScene(murx, mury);
InitialiserPommes(pommex, pommey, segment); InitialiserPommes(pommex, pommey, segment);
while(go_on){ while(go_on){
Timer(); /*Timer();*/
Score(*pointeur_segment); Score(*pointeur_segment);
Controle(pointeur_direction, 0, pointeur_go_on); Controle(pointeur_direction, 0, pointeur_go_on);
Serpent(pos_x, pos_y, old_x, old_y, pointeur_segment, murx, mury, pointeur_go_on, pointeur_direction); Serpent(pos_x, pos_y, old_x, old_y, pointeur_segment, murx, mury, pointeur_go_on, pointeur_direction);
Pomme(pos_x, pos_y, pommex, pommey, pointeur_segment); Pomme(pos_x, pos_y, pommex, pommey, pointeur_segment);
} }
usleep(100000);
FermerGraphique(); FermerGraphique();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -1,6 +1,6 @@
#include <graph.h> #include <graph.h>
#include <stdlib.h> #include <stdlib.h>
#include "pommes.h" #include "../fichier.h/pastille.h"
void InitialiserPommes(int pommex[], int pommey[], int segment) { void InitialiserPommes(int pommex[], int pommey[], int segment) {
int p; int p;

View File

@ -1,5 +1,5 @@
#include <graph.h> #include <graph.h>
#include "serpent.h" #include "../fichier.h/serpent.h"
void Update_Serpent(int pos_x[], int pos_y[], int segment, int old_x[], int old_y[]){ void Update_Serpent(int pos_x[], int pos_y[], int segment, int old_x[], int old_y[]){
int i = 0; int i = 0;

View File

@ -10,7 +10,7 @@ void DessinerScene(int murx[30], int mury[30]){
int mur; int mur;
int i; int i;
int fond; int fond;
snprintf(timer,6,"%02d:%02d", minute ,seconde); /* snprintf(timer,6,"%02d:%02d", minute ,seconde);*/
ChoisirCouleurDessin(CouleurParComposante(91,222,122)); ChoisirCouleurDessin(CouleurParComposante(91,222,122));
RemplirRectangle(20,20,1160,700); RemplirRectangle(20,20,1160,700);
ChoisirCouleurDessin(CouleurParComposante(255,255,255)); ChoisirCouleurDessin(CouleurParComposante(255,255,255));

View File

@ -2,26 +2,37 @@
#include <unistd.h> #include <unistd.h>
#include <graph.h> #include <graph.h>
#include "../fichier.h/time.h" #include "../fichier.h/time.h"
#include "../fichier.h/main.h"
#include "../fichier.h/serpent.h"
#define CYCLE 10000L #define CYCLE 10000L
/*time*/ int seconde=0;
void Update_Timer(void){ int minute=0;
int seconde =0; int seconde_actuel=0;
int minute=1; int old_seconde=0;
char timer[6]; unsigned long int suivant;
char timer[6];
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 Update_Timer(){
snprintf(timer,6,"%02d:%02d", minute, seconde); snprintf(timer,6,"%02d:%02d", minute, seconde);
ChoisirCouleurDessin(CouleurParComposante(0,0,0)); ChoisirCouleurDessin(CouleurParComposante(0,0,0));
RemplirRectangle(10,700,12000,800); RemplirRectangle(10,700,12000,800);
ChoisirCouleurDessin(CouleurParComposante(255,255,255)); ChoisirCouleurDessin(CouleurParComposante(255,255,255));
EcrireTexte(50,760,"Temps: ",2); EcrireTexte(50,760,"time: ",2);
EcrireTexte(160,760,timer,2); EcrireTexte(120,760,timer,2);
} }
void Timer(void){ void Timer(){
int seconde = 0;
int minute = 0;
int seconde_actuel;
int old_seconde;
unsigned long int suivant;
if(Microsecondes()> suivant){ if(Microsecondes()> suivant){
suivant = Microsecondes()+CYCLE; suivant = Microsecondes()+CYCLE;
seconde_actuel = (suivant/1000000)%10; seconde_actuel = (suivant/1000000)%10;