2023-11-30 13:20:34 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <graph.h>
|
|
|
|
#include <time.h>
|
2023-12-09 15:34:02 +01:00
|
|
|
#include <stdio.h>
|
2023-11-30 13:20:34 +01:00
|
|
|
|
|
|
|
#define HAUTEUR 40
|
|
|
|
#define LARGEUR 60
|
|
|
|
#define TAILLE_CASE 20
|
2023-12-09 15:34:02 +01:00
|
|
|
#define NOMBRE_POMMES 5
|
|
|
|
#define CYCLE 100000L
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
} Segment;
|
2023-11-30 13:20:34 +01:00
|
|
|
|
2023-12-09 15:34:02 +01:00
|
|
|
typedef struct {
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
} Pomme;
|
2023-11-30 13:20:34 +01:00
|
|
|
|
|
|
|
typedef struct {
|
2023-12-09 15:34:02 +01:00
|
|
|
int longueur;
|
|
|
|
Segment* corps;
|
|
|
|
} Serpent;
|
|
|
|
|
|
|
|
void Attendre(unsigned int millisecondes) {
|
|
|
|
usleep(millisecondes * 1000);
|
|
|
|
}
|
2023-11-30 13:20:34 +01:00
|
|
|
|
|
|
|
/* Fonction pour concevoir le graphique */
|
2023-12-09 15:34:02 +01:00
|
|
|
void Graphique() {
|
2023-11-30 13:20:34 +01:00
|
|
|
InitialiserGraphique();
|
|
|
|
CreerFenetre(10, 10, 1240, 940);
|
|
|
|
EcrireTexte(500, 400, "Le jeu va commencer !", 2);
|
|
|
|
sleep(1);
|
|
|
|
EffacerEcran(CouleurParComposante(0, 0, 0));
|
|
|
|
}
|
|
|
|
|
2023-12-09 15:34:02 +01:00
|
|
|
/* Fonction permettant d'afficher le serpent */
|
|
|
|
void afficherSerpent(Serpent serpent) {
|
|
|
|
couleur couleurSerpent = CouleurParNom("yellow");
|
|
|
|
ChoisirCouleurDessin(couleurSerpent);
|
|
|
|
|
|
|
|
for (int i = 0; i < serpent.longueur; i++) {
|
|
|
|
RemplirRectangle(serpent.corps[i].x, serpent.corps[i].y, TAILLE_CASE, TAILLE_CASE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void genererPommes(Pomme pommes[]) {
|
|
|
|
for (int i = 0; i < NOMBRE_POMMES; i++) {
|
|
|
|
pommes[i].x = rand() % LARGEUR * TAILLE_CASE;
|
|
|
|
pommes[i].y = rand() % HAUTEUR * TAILLE_CASE;
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(255, 0, 0));
|
|
|
|
RemplirRectangle(pommes[i].x, pommes[i].y, TAILLE_CASE, TAILLE_CASE);
|
|
|
|
}
|
2023-11-30 13:20:34 +01:00
|
|
|
}
|
|
|
|
|
2023-12-09 15:34:02 +01:00
|
|
|
/* Fonction initialisant la fenetre de jeux en vert */
|
|
|
|
void EcranJeu() {
|
|
|
|
couleur v = CouleurParNom("light green");
|
|
|
|
for (int i = 2; i < 42; i++) {
|
|
|
|
for (int j = 5; j < 65; j++) {
|
|
|
|
ChoisirCouleurDessin(v);
|
|
|
|
RemplirRectangle(j * TAILLE_CASE, i * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-30 13:20:34 +01:00
|
|
|
|
2023-12-09 15:34:02 +01:00
|
|
|
int collisionAvecPomme(Serpent serpent, Pomme pomme) {
|
|
|
|
return (serpent.corps[0].x == pomme.x && serpent.corps[0].y == pomme.y);
|
|
|
|
}
|
2023-11-30 13:20:34 +01:00
|
|
|
|
2023-12-09 15:34:02 +01:00
|
|
|
int collisionAvecBordures(Serpent serpent) {
|
|
|
|
return (serpent.corps[0].x < 0 || serpent.corps[0].x >= LARGEUR * TAILLE_CASE ||
|
|
|
|
serpent.corps[0].y < 0 || serpent.corps[0].y >= HAUTEUR * TAILLE_CASE);
|
2023-11-30 13:20:34 +01:00
|
|
|
}
|
|
|
|
|
2023-12-09 15:34:02 +01:00
|
|
|
int collisionAvecSerpent(Serpent serpent) {
|
|
|
|
for (int 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;
|
|
|
|
}
|
2023-11-30 13:20:34 +01:00
|
|
|
}
|
2023-12-09 15:34:02 +01:00
|
|
|
return 0;
|
2023-11-30 13:20:34 +01:00
|
|
|
}
|
|
|
|
|
2023-12-09 15:34:02 +01:00
|
|
|
void gestionCollision(Serpent *serpent, Pomme pommes[]) {
|
|
|
|
if (collisionAvecPomme(*serpent, pommes[0])) {
|
|
|
|
serpent->longueur++;
|
|
|
|
serpent->corps = realloc(serpent->corps, sizeof(Segment) * serpent->longueur);
|
|
|
|
genererPommes(pommes);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (collisionAvecSerpent(*serpent) || collisionAvecBordures(*serpent)) {
|
|
|
|
FermerGraphique();
|
|
|
|
exit(EXIT_SUCCESS);
|
2023-11-30 13:20:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-09 15:34:02 +01:00
|
|
|
void deplacerSerpent(Serpent *serpent, int *direction) {
|
|
|
|
int i;
|
|
|
|
couleur c;
|
|
|
|
couleur v;
|
|
|
|
couleur s;
|
|
|
|
c = CouleurParNom("black");
|
|
|
|
s = CouleurParNom("yellow");
|
|
|
|
v = CouleurParNom("light green");
|
|
|
|
|
|
|
|
for (i = 0; i < serpent->longueur; i++) {
|
|
|
|
ChoisirCouleurDessin(v);
|
|
|
|
RemplirRectangle(serpent->corps[i].x, serpent->corps[i].y, TAILLE_CASE, TAILLE_CASE);
|
|
|
|
}
|
|
|
|
for (i = serpent->longueur - 1; i > 0; i--) {
|
|
|
|
serpent->corps[i] = serpent->corps[i - 1];
|
|
|
|
}
|
|
|
|
|
2023-11-30 13:20:34 +01:00
|
|
|
if (ToucheEnAttente()) {
|
|
|
|
int touche = Touche();
|
|
|
|
switch (touche) {
|
|
|
|
case XK_Right:
|
2023-12-09 15:34:02 +01:00
|
|
|
*direction = 1;
|
|
|
|
break;
|
|
|
|
case XK_Left:
|
|
|
|
*direction = 2;
|
2023-11-30 13:20:34 +01:00
|
|
|
break;
|
|
|
|
case XK_Up:
|
2023-12-09 15:34:02 +01:00
|
|
|
*direction = 3;
|
2023-11-30 13:20:34 +01:00
|
|
|
break;
|
|
|
|
case XK_Down:
|
2023-12-09 15:34:02 +01:00
|
|
|
*direction = 4;
|
2023-11-30 13:20:34 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-09 15:34:02 +01:00
|
|
|
/* Déplace la tête en fonction de la direction */
|
|
|
|
if (*direction == 1) {
|
|
|
|
serpent->corps[0].x += TAILLE_CASE;
|
|
|
|
} else if (*direction == 2) {
|
|
|
|
serpent->corps[0].x -= TAILLE_CASE;
|
|
|
|
} else if (*direction == 3) {
|
|
|
|
serpent->corps[0].y -= TAILLE_CASE;
|
|
|
|
} else if (*direction == 4) {
|
|
|
|
serpent->corps[0].y += TAILLE_CASE;
|
2023-11-30 13:20:34 +01:00
|
|
|
}
|
|
|
|
|
2023-12-09 15:34:02 +01:00
|
|
|
for (i = 0; i < serpent->longueur; i++) {
|
|
|
|
ChoisirCouleurDessin(s);
|
|
|
|
RemplirRectangle(serpent->corps[i].x, serpent->corps[i].y, TAILLE_CASE, TAILLE_CASE);
|
2023-11-30 13:20:34 +01:00
|
|
|
}
|
|
|
|
|
2023-12-09 15:34:02 +01:00
|
|
|
Attendre(100);
|
2023-11-30 13:20:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2023-12-09 15:34:02 +01:00
|
|
|
int touche, go_on = 1, direction = 1;
|
|
|
|
Serpent serpent;
|
|
|
|
Pomme pommes[NOMBRE_POMMES];
|
|
|
|
couleur c;
|
|
|
|
couleur v;
|
|
|
|
couleur s;
|
2023-11-30 13:20:34 +01:00
|
|
|
srand(time(NULL));
|
|
|
|
|
2023-12-09 15:34:02 +01:00
|
|
|
Graphique();
|
|
|
|
InitialiserGraphique();
|
|
|
|
CreerFenetre(10, 10, 1200, 820);
|
|
|
|
c = CouleurParNom("black");
|
|
|
|
s = CouleurParNom("yellow");
|
|
|
|
v = CouleurParNom("green");
|
|
|
|
EffacerEcran(c);
|
|
|
|
for (int i = 0; i < serpent.longueur; i++) {
|
|
|
|
serpent.corps[i].x = LARGEUR / 2;
|
|
|
|
serpent.corps[i].y = HAUTEUR / 2;
|
2023-11-30 13:20:34 +01:00
|
|
|
}
|
2023-12-09 15:34:02 +01:00
|
|
|
serpent.longueur = 10;
|
|
|
|
serpent.corps = malloc(sizeof(Segment) * serpent.longueur);
|
|
|
|
EcranJeu();
|
|
|
|
genererPommes(pommes);
|
|
|
|
afficherSerpent(serpent);
|
2023-11-30 13:20:34 +01:00
|
|
|
|
2023-12-09 15:34:02 +01:00
|
|
|
while (go_on) {
|
|
|
|
gestionCollision(&serpent, pommes);
|
|
|
|
deplacerSerpent(&serpent, &direction);
|
2023-11-30 13:20:34 +01:00
|
|
|
}
|
|
|
|
|
2023-12-09 15:34:02 +01:00
|
|
|
Touche();
|
2023-11-30 13:20:34 +01:00
|
|
|
FermerGraphique();
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|