2023-12-11 14:45:42 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <graph.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#define HAUTEUR 40
|
|
|
|
#define LARGEUR 60
|
|
|
|
#define TAILLE_CASE 19
|
|
|
|
#define NOMBRE_POMMES 5
|
2023-12-12 13:46:39 +01:00
|
|
|
#define NOMBRE_OBSTACLES 20
|
2023-12-11 14:45:42 +01:00
|
|
|
#define CYCLE 100000L
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
} Position;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
Position* corps;
|
|
|
|
int longueur;
|
|
|
|
} Serpent;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
Position* positions;
|
|
|
|
int nombre;
|
|
|
|
} Pommes;
|
|
|
|
|
2023-12-12 13:46:39 +01:00
|
|
|
typedef struct {
|
|
|
|
Position* positions;
|
|
|
|
int nombre;
|
|
|
|
} Obstacles;
|
|
|
|
|
2023-12-11 14:45:42 +01:00
|
|
|
void Attendre(unsigned int millisecondes) {
|
2023-12-12 13:46:39 +01:00
|
|
|
usleep(millisecondes * 1000);
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
void InitialiserJeu(Serpent* serpent, Pommes* pommes, Obstacles* obstacles) {
|
|
|
|
serpent->longueur = 10;
|
|
|
|
serpent->corps = malloc(sizeof(Position) * serpent->longueur);
|
|
|
|
pommes->nombre = NOMBRE_POMMES;
|
|
|
|
obstacles->nombre = NOMBRE_OBSTACLES;
|
2023-12-12 13:46:39 +01:00
|
|
|
|
|
|
|
srand(time(NULL));
|
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
serpent->corps[0].x = LARGEUR / 2 * TAILLE_CASE;
|
|
|
|
serpent->corps[0].y = HAUTEUR / 2 * TAILLE_CASE;
|
2023-12-12 13:46:39 +01:00
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
/* Initialisation des obstacles*/
|
|
|
|
obstacles->positions = malloc(sizeof(Position) * obstacles->nombre);
|
|
|
|
for (int i = 0; i < obstacles->nombre; i++) {
|
|
|
|
obstacles->positions[i].x = rand() % LARGEUR;
|
|
|
|
obstacles->positions[i].y = rand() % HAUTEUR;
|
2023-12-12 13:46:39 +01:00
|
|
|
}
|
|
|
|
}
|
2023-12-11 14:45:42 +01:00
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
void GenererObstacles(Obstacles* obstacles) {
|
2023-12-12 13:46:39 +01:00
|
|
|
couleur couleurObstacle = CouleurParComposante(0, 0, 255);
|
2023-12-11 14:45:42 +01:00
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
for (int i = 0; i < obstacles->nombre; i++) {
|
2023-12-12 13:46:39 +01:00
|
|
|
ChoisirCouleurDessin(couleurObstacle);
|
2023-12-12 16:28:27 +01:00
|
|
|
RemplirRectangle(obstacles->positions[i].x * TAILLE_CASE, obstacles->positions[i].y * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE);
|
2023-12-12 13:46:39 +01:00
|
|
|
}
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Graphique() {
|
2023-12-12 13:46:39 +01:00
|
|
|
InitialiserGraphique();
|
|
|
|
CreerFenetre(1, 1, 1240, 940);
|
|
|
|
EcrireTexte(500, 400, "Le jeu va commencer !", 2);
|
|
|
|
Attendre(1000);
|
|
|
|
EffacerEcran(CouleurParComposante(0, 0, 0));
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AffichageBasique() {
|
2023-12-12 13:46:39 +01:00
|
|
|
ChoisirCouleurDessin(CouleurParComposante(111, 255, 94));
|
|
|
|
RemplirRectangle(0, 0, 1140, 760);
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
void AfficherScore(int score) {
|
2023-12-12 13:46:39 +01:00
|
|
|
char scoreText[20];
|
|
|
|
snprintf(scoreText, 20, "Score: %d", score);
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(0, 0, 0));
|
|
|
|
RemplirRectangle(20, 820, 150, 40);
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(255, 255, 255));
|
|
|
|
EcrireTexte(20, 850, scoreText, 2);
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int PauseJeu() {
|
2023-12-12 13:46:39 +01:00
|
|
|
while (1) {
|
|
|
|
if (ToucheEnAttente()) {
|
|
|
|
int touche = Touche();
|
|
|
|
if (touche == XK_space) {
|
|
|
|
return 1; // La barre d'espace a été pressée, reprendre le jeu
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Attendre(100);
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
void LibererMemoire(Serpent* serpent, Pommes* pommes, Obstacles* obstacles) {
|
|
|
|
free(serpent->corps);
|
|
|
|
free(pommes->positions);
|
|
|
|
free(obstacles->positions);
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AfficheTemps(int minute, int seconde) {
|
2023-12-12 13:46:39 +01:00
|
|
|
char temps[6];
|
|
|
|
snprintf(temps, 6, "%02d:%02d", minute, seconde);
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(0, 0, 0));
|
|
|
|
RemplirRectangle(20, 870, 70, 40);
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(255, 255, 255));
|
|
|
|
EcrireTexte(20, 900, temps, 2);
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
void AfficherSerpent(Serpent* serpent) {
|
2023-12-12 13:46:39 +01:00
|
|
|
couleur couleurSerpent = CouleurParComposante(255, 255, 0);
|
|
|
|
ChoisirCouleurDessin(couleurSerpent);
|
2023-12-11 14:45:42 +01:00
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
for (int i = 0; i < serpent->longueur; i++) {
|
|
|
|
RemplirRectangle(serpent->corps[i].x, serpent->corps[i].y, TAILLE_CASE, TAILLE_CASE);
|
2023-12-12 13:46:39 +01:00
|
|
|
}
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
void GenererPommes(Pommes* pommes) {
|
2023-12-12 13:46:39 +01:00
|
|
|
couleur couleurPommes = CouleurParComposante(255, 0, 0);
|
2023-12-12 16:28:27 +01:00
|
|
|
pommes->positions = malloc(sizeof(Position) * pommes->nombre);
|
2023-12-12 13:46:39 +01:00
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
for (int i = 0; i < pommes->nombre; i++) {
|
|
|
|
pommes->positions[i].x = rand() % LARGEUR;
|
|
|
|
pommes->positions[i].y = rand() % HAUTEUR;
|
2023-12-12 13:46:39 +01:00
|
|
|
ChoisirCouleurDessin(couleurPommes);
|
2023-12-12 16:28:27 +01:00
|
|
|
RemplirRectangle(pommes->positions[i].x * TAILLE_CASE, pommes->positions[i].y * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE);
|
2023-12-12 13:46:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
void GenererNouvellePomme(Pommes* pommes) {
|
2023-12-12 13:46:39 +01:00
|
|
|
couleur couleurPommes = CouleurParComposante(255, 0, 0);
|
2023-12-12 16:28:27 +01:00
|
|
|
int i = rand() % pommes->nombre;
|
|
|
|
pommes->positions[i].x = rand() % LARGEUR;
|
|
|
|
pommes->positions[i].y = rand() % HAUTEUR;
|
2023-12-11 14:45:42 +01:00
|
|
|
ChoisirCouleurDessin(couleurPommes);
|
2023-12-12 16:28:27 +01:00
|
|
|
RemplirRectangle(pommes->positions[i].x * TAILLE_CASE, pommes->positions[i].y * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE);
|
2023-12-11 16:09:56 +01:00
|
|
|
}
|
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
int CollisionAvecPomme(Serpent* serpent, Pommes* pommes, int* score) {
|
|
|
|
for (int i = 0; i < pommes->nombre; i++) {
|
|
|
|
if (serpent->corps[0].x >= pommes->positions[i].x * TAILLE_CASE &&
|
|
|
|
serpent->corps[0].x < (pommes->positions[i].x + 1) * TAILLE_CASE &&
|
|
|
|
serpent->corps[0].y >= pommes->positions[i].y * TAILLE_CASE &&
|
|
|
|
serpent->corps[0].y < (pommes->positions[i].y + 1) * TAILLE_CASE) {
|
|
|
|
GenererNouvellePomme(pommes);
|
|
|
|
*score = *score + 5;
|
|
|
|
AfficherScore(*score);
|
2023-12-12 13:46:39 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
int CollisionAvecObstacle(Serpent* serpent, Obstacles* obstacles) {
|
|
|
|
for (int i = 0; i < obstacles->nombre; i++) {
|
|
|
|
if (serpent->corps[0].x == obstacles->positions[i].x * TAILLE_CASE &&
|
|
|
|
serpent->corps[0].y == obstacles->positions[i].y * TAILLE_CASE) {
|
2023-12-12 13:46:39 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|
2023-12-12 13:46:39 +01:00
|
|
|
return 0;
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|
|
|
|
|
2023-12-12 16:28:27 +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) {
|
2023-12-12 13:46:39 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|
2023-12-12 13:46:39 +01:00
|
|
|
return 0;
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
int CollisionAvecBordures(Serpent* serpent) {
|
|
|
|
if (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-12-12 13:46:39 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
void DeplacerSerpent(Serpent* serpent, int* direction) {
|
2023-12-12 13:46:39 +01:00
|
|
|
couleur couleurFond = CouleurParComposante(0, 0, 0);
|
|
|
|
couleur couleurSerpent = CouleurParComposante(255, 255, 0);
|
|
|
|
couleur couleurTerrain = CouleurParComposante(111, 255, 94);
|
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
for (int i = 0; i < serpent->longueur; i++) {
|
2023-12-12 13:46:39 +01:00
|
|
|
ChoisirCouleurDessin(couleurTerrain);
|
2023-12-12 16:28:27 +01:00
|
|
|
RemplirRectangle(serpent->corps[i].x, serpent->corps[i].y, TAILLE_CASE, TAILLE_CASE);
|
2023-12-12 13:46:39 +01:00
|
|
|
}
|
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
for (int i = serpent->longueur - 1; i > 0; i--) {
|
|
|
|
serpent->corps[i] = serpent->corps[i - 1];
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|
|
|
|
|
2023-12-12 13:46:39 +01:00
|
|
|
if (*direction == 1) {
|
2023-12-12 16:28:27 +01:00
|
|
|
serpent->corps[0].x += TAILLE_CASE;
|
2023-12-12 13:46:39 +01:00
|
|
|
} else if (*direction == 2) {
|
2023-12-12 16:28:27 +01:00
|
|
|
serpent->corps[0].x -= TAILLE_CASE;
|
2023-12-12 13:46:39 +01:00
|
|
|
} else if (*direction == 3) {
|
2023-12-12 16:28:27 +01:00
|
|
|
serpent->corps[0].y -= TAILLE_CASE;
|
2023-12-12 13:46:39 +01:00
|
|
|
} else if (*direction == 4) {
|
2023-12-12 16:28:27 +01:00
|
|
|
serpent->corps[0].y += TAILLE_CASE;
|
2023-12-12 13:46:39 +01:00
|
|
|
}
|
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
for (int i = 0; i < serpent->longueur; i++) {
|
2023-12-12 13:46:39 +01:00
|
|
|
ChoisirCouleurDessin(couleurSerpent);
|
2023-12-12 16:28:27 +01:00
|
|
|
RemplirRectangle(serpent->corps[i].x, serpent->corps[i].y, TAILLE_CASE, TAILLE_CASE);
|
2023-12-12 13:46:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Attendre(100);
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
int GestionCollision(Serpent* serpent, Pommes* pommes, Obstacles* obstacles, int perdu, int* score) {
|
2023-12-12 13:46:39 +01:00
|
|
|
int loose = perdu;
|
2023-12-12 16:28:27 +01:00
|
|
|
if (CollisionAvecPomme(serpent, pommes, score)) {
|
|
|
|
serpent->longueur = serpent->longueur + 2;
|
|
|
|
serpent->corps = realloc(serpent->corps, sizeof(Position) * serpent->longueur);
|
2023-12-12 13:46:39 +01:00
|
|
|
loose = 1;
|
|
|
|
}
|
2023-12-12 16:28:27 +01:00
|
|
|
if (CollisionAvecObstacle(serpent, obstacles) || CollisionAvecSerpent(serpent) || CollisionAvecBordures(serpent)) {
|
2023-12-12 13:46:39 +01:00
|
|
|
loose = -1;
|
|
|
|
}
|
|
|
|
return loose;
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2023-12-12 13:46:39 +01:00
|
|
|
int direction = 1;
|
|
|
|
unsigned long suivant = Microsecondes() + CYCLE;
|
|
|
|
int temps[2] = {0, 0}, seconde_actuel, old_seconde;
|
2023-12-12 16:28:27 +01:00
|
|
|
int jeuEnPause = 0, perdu = 0, score = 0;
|
|
|
|
|
|
|
|
Serpent serpent;
|
|
|
|
Pommes pommes;
|
|
|
|
Obstacles obstacles;
|
2023-12-12 13:46:39 +01:00
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
InitialiserJeu(&serpent, &pommes, &obstacles);
|
2023-12-12 13:46:39 +01:00
|
|
|
Graphique();
|
|
|
|
EffacerEcran(CouleurParComposante(0, 0, 0));
|
|
|
|
AffichageBasique();
|
2023-12-12 16:28:27 +01:00
|
|
|
GenererPommes(&pommes);
|
|
|
|
GenererObstacles(&obstacles);
|
|
|
|
AfficherSerpent(&serpent);
|
|
|
|
AfficherScore(score);
|
2023-12-12 13:46:39 +01:00
|
|
|
|
|
|
|
while (perdu != -1) {
|
|
|
|
if (!jeuEnPause) {
|
|
|
|
if (Microsecondes() > suivant) {
|
|
|
|
suivant = Microsecondes() + CYCLE;
|
|
|
|
seconde_actuel = (suivant / 1000000) % 10;
|
|
|
|
if (seconde_actuel != old_seconde) {
|
|
|
|
old_seconde = seconde_actuel;
|
|
|
|
if ((temps[1] + 1) == 60) {
|
|
|
|
temps[0]++;
|
|
|
|
temps[1] = 0;
|
|
|
|
} else {
|
|
|
|
temps[1]++;
|
|
|
|
}
|
|
|
|
AfficheTemps(temps[0], temps[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ToucheEnAttente()) {
|
|
|
|
int touche = Touche();
|
|
|
|
switch (touche) {
|
|
|
|
case XK_Right:
|
|
|
|
direction = 1;
|
|
|
|
break;
|
|
|
|
case XK_Left:
|
|
|
|
direction = 2;
|
|
|
|
break;
|
|
|
|
case XK_Up:
|
|
|
|
direction = 3;
|
|
|
|
break;
|
|
|
|
case XK_Down:
|
|
|
|
direction = 4;
|
|
|
|
break;
|
|
|
|
case XK_space:
|
|
|
|
jeuEnPause = 1;
|
|
|
|
break;
|
|
|
|
case XK_Escape:
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2023-12-12 16:28:27 +01:00
|
|
|
DeplacerSerpent(&serpent, &direction);
|
|
|
|
perdu = GestionCollision(&serpent, &pommes, &obstacles, perdu, &score);
|
2023-12-12 13:46:39 +01:00
|
|
|
} else {
|
|
|
|
if (ToucheEnAttente()) {
|
|
|
|
int touche = Touche();
|
|
|
|
if (touche == XK_space) {
|
|
|
|
if (touche == XK_Escape) {
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
jeuEnPause = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|
|
|
|
|
2023-12-12 13:46:39 +01:00
|
|
|
FermerGraphique();
|
2023-12-12 16:28:27 +01:00
|
|
|
LibererMemoire(&serpent, &pommes, &obstacles);
|
2023-12-12 13:46:39 +01:00
|
|
|
return EXIT_SUCCESS;
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|