Amelioration code + commentaires. Rectifications erreurs etc.
This commit is contained in:
parent
494177f3c1
commit
d34541178a
10
Makefile
10
Makefile
@ -1,15 +1,16 @@
|
|||||||
CC = gcc
|
CC = gcc
|
||||||
CFLAGS = -Wall -Wextra
|
CFLAGS = -Wall -Wextra
|
||||||
|
|
||||||
|
|
||||||
all: prog
|
all: prog
|
||||||
|
|
||||||
prog: main.o evenement.o gui.o scene.o
|
prog: main.o evenements.o gui.o scene.o
|
||||||
$(CC) -o $@ $^ -lgraph
|
$(CC) -o $@ $^ -lgraph
|
||||||
|
|
||||||
main.o: main.c evenement.h
|
main.o: main.c evenements.h
|
||||||
$(CC) $(CFLAGS) -c $< -o $@
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
evenement.o: evenement.c evenement.h
|
evenements.o: evenements.c evenements.h
|
||||||
$(CC) $(CFLAGS) -c $< -o $@
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
gui.o: gui.c gui.h
|
gui.o: gui.c gui.h
|
||||||
@ -21,3 +22,6 @@ scene.o: scene.c scene.h
|
|||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f *.o prog
|
rm -f *.o prog
|
||||||
|
|
||||||
|
run:
|
||||||
|
./prog
|
||||||
|
96
evenements.c
Normal file
96
evenements.c
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
#include <graph.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include "main.h"
|
||||||
|
#include "gui.h"
|
||||||
|
#include "scene.h"
|
||||||
|
|
||||||
|
int MourrirSerpent(PIXELS *serpent,PIXELS *obstacle, unsigned long longueur_serpent,unsigned long longueur_obstacle)
|
||||||
|
{
|
||||||
|
unsigned long i = 0;
|
||||||
|
for(i=1;i<longueur_serpent;i++)
|
||||||
|
{
|
||||||
|
if(serpent[0].x == serpent[i].x && serpent[0].y == serpent[i].y )
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(i=1;i<longueur_obstacle;i++)
|
||||||
|
{
|
||||||
|
if(serpent[0].x == obstacle[i].x && serpent[0].y == obstacle[i].y )
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (serpent[0].x<=0 || serpent[0].x>W_GAME || serpent[0].y<=0 || serpent[0].y>H_GAME)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int MangerPastille(PIXELS *serpent, PIXELS* pastilles,PIXELS *obstacle,unsigned long *score,unsigned long longueur_serpent,unsigned long longueur_obstacle,unsigned long int *vitesse)
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
for(i=0;i<PASTILLES;i++)
|
||||||
|
{
|
||||||
|
if(serpent[0].x == pastilles[i].x && serpent[0].y == pastilles[i].y)
|
||||||
|
{
|
||||||
|
pastilles[i] = gen_pastille(serpent,pastilles,obstacle,longueur_serpent,longueur_obstacle);
|
||||||
|
ChargerImage("./images/PommePastille.png",pastilles[i].x,pastilles[i].y,0,0,T_PIXEL,T_PIXEL);
|
||||||
|
*score+=5;
|
||||||
|
*vitesse/=1.008;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DeplacementSerpent(int direction, PIXELS *serpent, unsigned long *longueur) {
|
||||||
|
/* Sauvegarder la position actuelle de la queue du serpent */
|
||||||
|
int queueX = serpent[*longueur - 1].x;
|
||||||
|
int queueY = serpent[*longueur - 1].y;
|
||||||
|
unsigned long i = 0;
|
||||||
|
|
||||||
|
/* Mettre à jour les positions du corps du serpent */
|
||||||
|
for (i = *longueur - 1; i > 0; --i) {
|
||||||
|
serpent[i].x = serpent[i - 1].x;
|
||||||
|
serpent[i].y = serpent[i - 1].y;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Effacer la queue du serpent */
|
||||||
|
couleur bg = CouleurParComposante(171, 204, 104);
|
||||||
|
ChoisirEcran(2);
|
||||||
|
ChoisirCouleurDessin(bg);
|
||||||
|
RemplirRectangle(queueX, queueY, T_PIXEL, T_PIXEL);
|
||||||
|
|
||||||
|
/* Mettre à jour la position de la tête du serpent */
|
||||||
|
if (direction == 0) {
|
||||||
|
serpent[0].x -= T_PIXEL; /* Vers la gauche */
|
||||||
|
} else if (direction == 1) {
|
||||||
|
serpent[0].y -= T_PIXEL; /* Vers le haut */
|
||||||
|
} else if (direction == 2) {
|
||||||
|
serpent[0].x += T_PIXEL; /* Vers la droite */
|
||||||
|
} else if (direction == 3) {
|
||||||
|
serpent[0].y += T_PIXEL; /* Vers le bas */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Afficher la nouvelle tête du serpent */
|
||||||
|
ChargerImage("./images/SnakePart.png", serpent[0].x, serpent[0].y, 0, 0, T_PIXEL, T_PIXEL);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Mettre à jour la fonction Serpent */
|
||||||
|
int Serpent(PIXELS *serpent, PIXELS *pastilles, PIXELS *obstacle, unsigned long *score,
|
||||||
|
unsigned long *longueur_serpent, unsigned long longueur_obstacle,
|
||||||
|
unsigned long int *vitesse, int direction) {
|
||||||
|
if (MourrirSerpent(serpent, obstacle, *longueur_serpent, longueur_obstacle) == 1) {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
DeplacementSerpent(direction, serpent, &(*longueur_serpent));
|
||||||
|
if (MangerPastille(serpent, pastilles, obstacle, score, *longueur_serpent, longueur_obstacle, vitesse) == 1) {
|
||||||
|
*longueur_serpent += 2;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
17
evenements.h
Normal file
17
evenements.h
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#include "main.h"
|
||||||
|
|
||||||
|
#ifndef EVENEMENTS_H
|
||||||
|
#define EVENEMENTS_H
|
||||||
|
|
||||||
|
int MangerPastille(PIXELS *serpent, PIXELS* pastilles,PIXELS *obstacle,unsigned long *score,unsigned long longueur_serpent,unsigned long longueur_obstacle,unsigned long int *vitesse);
|
||||||
|
|
||||||
|
void DeplacementSerpent(int direction ,PIXELS *serpent, unsigned long *longueur);
|
||||||
|
|
||||||
|
int PastilleSurSerpent(PIXELS pastille, PIXELS *serpent, unsigned long longueur_serpent);
|
||||||
|
|
||||||
|
int MourrirSerpent(PIXELS *serpent,PIXELS *obstacle,unsigned long longueur_serpent,unsigned long longueur_obstacle);
|
||||||
|
|
||||||
|
int Serpent(PIXELS *serpent,PIXELS *pastilles,PIXELS *obstacle,unsigned long *score,unsigned long *longueur_serpent,unsigned long longueur_obstacle,unsigned long int *vitesse,int direction);
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
35
gui.c
35
gui.c
@ -37,7 +37,7 @@ void sauvegarderScore(unsigned long nouveauScore)
|
|||||||
FILE* fichier = fopen("score.txt", "w");
|
FILE* fichier = fopen("score.txt", "w");
|
||||||
if (fichier == NULL)
|
if (fichier == NULL)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Erreur lors de l'ouverture du fichier score.txt pour écriture\n");
|
fprintf(stderr, "Erreur lors de l'ouverture du fichier score.txt \n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,12 +53,13 @@ void CheckScore(unsigned long nouveauScore)
|
|||||||
if (nouveauScore > scoreActuel || scoreActuel == 0)
|
if (nouveauScore > scoreActuel || scoreActuel == 0)
|
||||||
{
|
{
|
||||||
sauvegarderScore(nouveauScore);
|
sauvegarderScore(nouveauScore);
|
||||||
printf("Le score a été enregistré avec succès.\n");
|
/* printf("Le score a été enregistré avec succès.\n"); DEBUG */
|
||||||
}
|
}
|
||||||
else
|
/* else
|
||||||
{
|
{
|
||||||
printf("Le score précédent est plus grand ou égal. Aucun changement effectué.\n");
|
printf("Le score précédent est plus grand ou égal.\n");
|
||||||
}
|
} DEBUG
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
void Menu()
|
void Menu()
|
||||||
@ -70,7 +71,7 @@ void Menu()
|
|||||||
ChoisirEcran(0);
|
ChoisirEcran(0);
|
||||||
ChargerImage("./images/Menu.png",0,0,0,0,930,710);
|
ChargerImage("./images/Menu.png",0,0,0,0,930,710);
|
||||||
ChoisirCouleurDessin(text);
|
ChoisirCouleurDessin(text);
|
||||||
snprintf(buf,100,"BEST SCORE : %07ld",bestscore);
|
snprintf(buf,100,"MEILLEUR SCORE : %07ld",bestscore);
|
||||||
EcrireTexte(420,350,buf,2);
|
EcrireTexte(420,350,buf,2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,6 +88,26 @@ void PerduGUI()
|
|||||||
unsigned long bestscore = lireScore();
|
unsigned long bestscore = lireScore();
|
||||||
ChoisirEcran(0);
|
ChoisirEcran(0);
|
||||||
ChargerImage("./images/Perdu.png",0,0,0,0,930,710);
|
ChargerImage("./images/Perdu.png",0,0,0,0,930,710);
|
||||||
snprintf(buf,100,"BEST SCORE : %07ld",bestscore);
|
snprintf(buf,100,"MEILLEUR SCORE : %07ld",bestscore);
|
||||||
EcrireTexte(420,350,buf,2);
|
EcrireTexte(420,350,buf,2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AfficherTimerEtScore(long unsigned int *score, int minutes,int secondes) /*Afficher le temps passé et le score*/
|
||||||
|
{
|
||||||
|
char buf[100];
|
||||||
|
char buff[100]; /* Stockage du score et du timer */
|
||||||
|
couleur text;
|
||||||
|
text=CouleurParComposante(78, 93, 47);
|
||||||
|
|
||||||
|
ChoisirEcran(1);
|
||||||
|
CopierZone(2,1,0,0,930,710,0,0);
|
||||||
|
|
||||||
|
ChoisirCouleurDessin(text);
|
||||||
|
|
||||||
|
snprintf(buf,100,"TEMPS : %02d:%02d",minutes,secondes);
|
||||||
|
snprintf(buff,100,"SCORE : %07ld",*score);
|
||||||
|
EcrireTexte(60,695,buf,2);
|
||||||
|
EcrireTexte(600,695,buff,2);
|
||||||
|
|
||||||
|
CopierZone(1,0,0,0,930,710,0,0);
|
||||||
}
|
}
|
1
gui.h
1
gui.h
@ -9,5 +9,6 @@ void Menu();
|
|||||||
void PerduGUI();
|
void PerduGUI();
|
||||||
void Pause();
|
void Pause();
|
||||||
int AfficherGUI();
|
int AfficherGUI();
|
||||||
|
void AfficherTimerEtScore(long unsigned int *score, int minutes,int secondes);
|
||||||
|
|
||||||
#endif
|
#endif
|
58
main.c
58
main.c
@ -2,7 +2,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <graph.h>
|
#include <graph.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include "evenement.h"
|
#include "evenements.h"
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "gui.h"
|
#include "gui.h"
|
||||||
#include "scene.h"
|
#include "scene.h"
|
||||||
@ -114,12 +114,57 @@ int main()
|
|||||||
|
|
||||||
while(game_on) /* Lancement du cycle pour les Inputs et le Jeu*/
|
while(game_on) /* Lancement du cycle pour les Inputs et le Jeu*/
|
||||||
{
|
{
|
||||||
if(Touches(&direction,&direction_davant,&pause))
|
if (ToucheEnAttente() == 1)
|
||||||
{
|
{
|
||||||
FinJeu(serpent,pastilles,obstacle);
|
switch (Touche())
|
||||||
return EXIT_SUCCESS;
|
{
|
||||||
}
|
case XK_Up:
|
||||||
|
direction = 1;
|
||||||
|
if(direction_davant == 3 && direction == 1)
|
||||||
|
{
|
||||||
|
direction = direction_davant;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case XK_Down:
|
||||||
|
direction = 3;
|
||||||
|
if(direction_davant == 1 && direction == 3)
|
||||||
|
{
|
||||||
|
direction = direction_davant; /* Changements de direction du serpent */
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case XK_Left:
|
||||||
|
direction = 0;
|
||||||
|
if(direction_davant == 2 && direction == 0)
|
||||||
|
{
|
||||||
|
direction = direction_davant;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case XK_Right:
|
||||||
|
direction = 2;
|
||||||
|
if(direction_davant == 0 && direction == 2)
|
||||||
|
{
|
||||||
|
direction = direction_davant;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case XK_space:
|
||||||
|
if(pause ==0)
|
||||||
|
{
|
||||||
|
pause=1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
pause=0;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case XK_Escape:
|
||||||
|
if(pause == 1)
|
||||||
|
{
|
||||||
|
FinJeu(serpent,pastilles,obstacle);
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(pause==1)
|
if(pause==1)
|
||||||
{
|
{
|
||||||
Pause();
|
Pause();
|
||||||
@ -161,5 +206,4 @@ int main()
|
|||||||
}
|
}
|
||||||
FinJeu(serpent,pastilles,obstacle);
|
FinJeu(serpent,pastilles,obstacle);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
9
main.h
9
main.h
@ -10,8 +10,11 @@
|
|||||||
#define PASTILLES 5
|
#define PASTILLES 5
|
||||||
#define OBSTACLE 30
|
#define OBSTACLE 30
|
||||||
|
|
||||||
|
// Définition des directions
|
||||||
|
#define GAUCHE 0
|
||||||
|
#define HAUT 1
|
||||||
|
#define DROITE 2
|
||||||
|
#define BAS 3
|
||||||
|
|
||||||
struct PIXELS {
|
struct PIXELS {
|
||||||
int x;
|
int x;
|
||||||
@ -20,6 +23,4 @@ struct PIXELS {
|
|||||||
|
|
||||||
typedef struct PIXELS PIXELS;
|
typedef struct PIXELS PIXELS;
|
||||||
|
|
||||||
PIXELS gen_pastille(PIXELS *serpent,PIXELS *pastilles,PIXELS *obstacle,unsigned long longueur_serpent,unsigned long longueur_obstacle);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
50
scene.c
50
scene.c
@ -29,7 +29,18 @@ PIXELS gen_pastille(PIXELS *serpent,PIXELS *pastilles,PIXELS *obstacle,unsigned
|
|||||||
ok = 0;
|
ok = 0;
|
||||||
x_pastille= ArrondirPixel(rand()%W_GAME);
|
x_pastille= ArrondirPixel(rand()%W_GAME);
|
||||||
y_pastille = ArrondirPixel(rand()%H_GAME);
|
y_pastille = ArrondirPixel(rand()%H_GAME);
|
||||||
for(i=0;i<longueur_serpent;i++)
|
|
||||||
|
if(x_pastille < DECALEMENT)
|
||||||
|
{
|
||||||
|
x_pastille =+ DECALEMENT;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(y_pastille < DECALEMENT)
|
||||||
|
{
|
||||||
|
y_pastille =+ DECALEMENT;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(i=0;i<=longueur_serpent;i++)
|
||||||
{
|
{
|
||||||
if(x_pastille == serpent[i].x && y_pastille == serpent[i].y)
|
if(x_pastille == serpent[i].x && y_pastille == serpent[i].y)
|
||||||
{
|
{
|
||||||
@ -41,7 +52,7 @@ PIXELS gen_pastille(PIXELS *serpent,PIXELS *pastilles,PIXELS *obstacle,unsigned
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
for(i=0;i<longueur_obstacle;i++)
|
for(i=0;i<=longueur_obstacle;i++)
|
||||||
{
|
{
|
||||||
if(x_pastille == obstacle[i].x && y_pastille == obstacle[i].y)
|
if(x_pastille == obstacle[i].x && y_pastille == obstacle[i].y)
|
||||||
{
|
{
|
||||||
@ -55,16 +66,6 @@ PIXELS gen_pastille(PIXELS *serpent,PIXELS *pastilles,PIXELS *obstacle,unsigned
|
|||||||
}
|
}
|
||||||
}while(ok);
|
}while(ok);
|
||||||
|
|
||||||
if(x_pastille < DECALEMENT)
|
|
||||||
{
|
|
||||||
x_pastille =+ DECALEMENT;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(y_pastille < DECALEMENT)
|
|
||||||
{
|
|
||||||
y_pastille =+ DECALEMENT;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pastille.x = x_pastille ;
|
pastille.x = x_pastille ;
|
||||||
pastille.y = y_pastille ;
|
pastille.y = y_pastille ;
|
||||||
@ -85,7 +86,18 @@ PIXELS gen_obstacle(PIXELS *serpent,PIXELS *pastilles,PIXELS *obstacle,unsigned
|
|||||||
ok = 0;
|
ok = 0;
|
||||||
x_obstacles= ArrondirPixel(rand()%W_GAME);
|
x_obstacles= ArrondirPixel(rand()%W_GAME);
|
||||||
y_obstacles = ArrondirPixel(rand()%H_GAME);
|
y_obstacles = ArrondirPixel(rand()%H_GAME);
|
||||||
for(i=0;i<longueur_serpent;i++)
|
|
||||||
|
if(x_obstacles < DECALEMENT)
|
||||||
|
{
|
||||||
|
x_obstacles =+ DECALEMENT;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(y_obstacles < DECALEMENT)
|
||||||
|
{
|
||||||
|
y_obstacles =+ DECALEMENT;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(i=0;i<=longueur_serpent;i++)
|
||||||
{
|
{
|
||||||
if(x_obstacles == serpent[i].x && y_obstacles == serpent[i].y)
|
if(x_obstacles == serpent[i].x && y_obstacles == serpent[i].y)
|
||||||
{
|
{
|
||||||
@ -97,7 +109,7 @@ PIXELS gen_obstacle(PIXELS *serpent,PIXELS *pastilles,PIXELS *obstacle,unsigned
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
for(i=0;i<longueur_pastilles;i++)
|
for(i=0;i<=longueur_pastilles;i++)
|
||||||
{
|
{
|
||||||
if(x_obstacles == pastilles[i].x && y_obstacles == pastilles[i].y)
|
if(x_obstacles == pastilles[i].x && y_obstacles == pastilles[i].y)
|
||||||
{
|
{
|
||||||
@ -111,16 +123,6 @@ PIXELS gen_obstacle(PIXELS *serpent,PIXELS *pastilles,PIXELS *obstacle,unsigned
|
|||||||
}
|
}
|
||||||
}while(ok);
|
}while(ok);
|
||||||
|
|
||||||
if(x_obstacles < DECALEMENT)
|
|
||||||
{
|
|
||||||
x_obstacles =+ DECALEMENT;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(y_obstacles < DECALEMENT)
|
|
||||||
{
|
|
||||||
y_obstacles =+ DECALEMENT;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
obstacles.x = x_obstacles ;
|
obstacles.x = x_obstacles ;
|
||||||
obstacles.y = y_obstacles ;
|
obstacles.y = y_obstacles ;
|
||||||
|
@ -1 +1 @@
|
|||||||
5
|
235
|
||||||
|
Loading…
x
Reference in New Issue
Block a user