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 {
|
2023-12-13 12:01:56 +01:00
|
|
|
int x;
|
|
|
|
int y;
|
2023-12-11 14:45:42 +01:00
|
|
|
} Position;
|
|
|
|
|
|
|
|
typedef struct {
|
2023-12-13 12:01:56 +01:00
|
|
|
Position* corps;
|
|
|
|
int longueur;
|
2023-12-11 14:45:42 +01:00
|
|
|
} Serpent;
|
|
|
|
|
2023-12-13 11:38:02 +01:00
|
|
|
typedef struct PommeNode {
|
2023-12-13 12:01:56 +01:00
|
|
|
Position position;
|
|
|
|
struct PommeNode* next;
|
2023-12-13 11:38:02 +01:00
|
|
|
} PommeNode;
|
|
|
|
|
2023-12-11 14:45:42 +01:00
|
|
|
typedef struct {
|
2023-12-13 12:01:56 +01:00
|
|
|
PommeNode* head;
|
|
|
|
int nombre;
|
2023-12-11 14:45:42 +01:00
|
|
|
} Pommes;
|
|
|
|
|
2023-12-12 13:46:39 +01:00
|
|
|
typedef struct {
|
2023-12-13 12:01:56 +01:00
|
|
|
Position* positions;
|
|
|
|
int nombre;
|
2023-12-12 13:46:39 +01:00
|
|
|
} Obstacles;
|
|
|
|
|
2023-12-11 14:45:42 +01:00
|
|
|
void Attendre(unsigned int millisecondes) {
|
2023-12-13 12:01:56 +01:00
|
|
|
usleep(millisecondes * 1000);
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|
|
|
|
|
2023-12-13 11:38:02 +01:00
|
|
|
void LibererPommes(Pommes* pommes) {
|
2023-12-13 12:01:56 +01:00
|
|
|
PommeNode* current = pommes->head;
|
|
|
|
while (current != NULL) {
|
|
|
|
PommeNode* next = current->next;
|
|
|
|
free(current);
|
|
|
|
current = next;
|
|
|
|
}
|
|
|
|
pommes->head = NULL;
|
2023-12-13 11:38:02 +01:00
|
|
|
}
|
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
void InitialiserJeu(Serpent* serpent, Pommes* pommes, Obstacles* obstacles) {
|
2023-12-13 12:01:56 +01:00
|
|
|
serpent->longueur = 10;
|
|
|
|
serpent->corps = malloc(sizeof(Position) * serpent->longueur);
|
|
|
|
pommes->nombre = NOMBRE_POMMES;
|
|
|
|
obstacles->nombre = NOMBRE_OBSTACLES;
|
|
|
|
|
|
|
|
srand(time(NULL));
|
|
|
|
|
|
|
|
serpent->corps[0].x = LARGEUR / 2 * TAILLE_CASE;
|
|
|
|
serpent->corps[0].y = HAUTEUR / 2 * TAILLE_CASE;
|
|
|
|
|
|
|
|
/* 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
|
|
|
void Graphique() {
|
2023-12-13 12:01:56 +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-13 12:01:56 +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-13 12:01:56 +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
|
|
|
}
|
|
|
|
|
2023-12-13 12:01:56 +01:00
|
|
|
void LibererMemoire(Serpent* serpent, Pommes* pommes, Obstacles* obstacles) {
|
|
|
|
free(serpent->corps);
|
|
|
|
LibererPommes(pommes);
|
|
|
|
free(obstacles->positions);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AfficheTemps(int minute, int seconde) {
|
|
|
|
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-14 12:21:34 +01:00
|
|
|
void AfficherEcranFin(int score) {
|
|
|
|
EffacerEcran(CouleurParComposante(0, 0, 0));
|
|
|
|
EcrireTexte(500, 300, "Game Over!", 2);
|
|
|
|
char scoreText[20];
|
|
|
|
snprintf(scoreText, 20, "Score: %d", score);
|
|
|
|
EcrireTexte(500, 400, scoreText, 2);
|
|
|
|
}
|
2023-12-11 14:45:42 +01:00
|
|
|
int PauseJeu() {
|
2023-12-13 12:01:56 +01:00
|
|
|
while (1) {
|
|
|
|
if (ToucheEnAttente()) {
|
|
|
|
int touche = Touche();
|
2023-12-12 13:46:39 +01:00
|
|
|
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 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-13 11:38:02 +01:00
|
|
|
pommes->head = NULL;
|
2023-12-12 13:46:39 +01:00
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
for (int i = 0; i < pommes->nombre; i++) {
|
2023-12-13 11:38:02 +01:00
|
|
|
PommeNode* nouvellePomme = (PommeNode*)malloc(sizeof(PommeNode));
|
|
|
|
if (nouvellePomme == NULL) {
|
|
|
|
perror("Allocation error");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
nouvellePomme->position.x = rand() % LARGEUR;
|
|
|
|
nouvellePomme->position.y = rand() % HAUTEUR;
|
|
|
|
nouvellePomme->next = pommes->head;
|
|
|
|
pommes->head = nouvellePomme;
|
|
|
|
|
2023-12-12 13:46:39 +01:00
|
|
|
ChoisirCouleurDessin(couleurPommes);
|
2023-12-13 12:01:56 +01:00
|
|
|
RemplirArc(nouvellePomme->position.x * TAILLE_CASE, nouvellePomme->position.y * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE,360,360);
|
2023-12-12 13:46:39 +01:00
|
|
|
}
|
|
|
|
}
|
2023-12-13 11:38:02 +01:00
|
|
|
void AjouterPomme(Pommes* pommes, int x, int y) {
|
|
|
|
PommeNode* nouvellePomme = (PommeNode*)malloc(sizeof(PommeNode));
|
|
|
|
if (nouvellePomme == NULL) {
|
|
|
|
perror("Allocation error");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
nouvellePomme->position.x = x;
|
|
|
|
nouvellePomme->position.y = y;
|
|
|
|
nouvellePomme->next = pommes->head;
|
|
|
|
pommes->head = nouvellePomme;
|
|
|
|
pommes->nombre++;
|
|
|
|
}
|
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-13 11:38:02 +01:00
|
|
|
int newX = rand() % LARGEUR;
|
|
|
|
int newY = rand() % HAUTEUR;
|
|
|
|
|
|
|
|
// Assuming you have a function named AjouterPomme to add a new apple to the linked list.
|
|
|
|
AjouterPomme(pommes, newX, newY);
|
|
|
|
|
2023-12-11 14:45:42 +01:00
|
|
|
ChoisirCouleurDessin(couleurPommes);
|
2023-12-13 12:01:56 +01:00
|
|
|
RemplirArc(newX * TAILLE_CASE, newY * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE,360,360);
|
2023-12-11 16:09:56 +01:00
|
|
|
}
|
2023-12-13 13:37:00 +01:00
|
|
|
int CollisionAvecPommeSerpent(Position position, PommeNode* pommes) {
|
|
|
|
PommeNode* current = pommes;
|
|
|
|
|
|
|
|
while (current != NULL) {
|
|
|
|
if (position.x == current->position.x && position.y == current->position.y) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2023-12-11 16:09:56 +01:00
|
|
|
|
2023-12-12 16:28:27 +01:00
|
|
|
int CollisionAvecPomme(Serpent* serpent, Pommes* pommes, int* score) {
|
2023-12-13 11:38:02 +01:00
|
|
|
PommeNode* current = pommes->head;
|
|
|
|
PommeNode* prev = NULL;
|
|
|
|
|
|
|
|
while (current != NULL) {
|
|
|
|
if (serpent->corps[0].x >= current->position.x * TAILLE_CASE &&
|
|
|
|
serpent->corps[0].x < (current->position.x + 1) * TAILLE_CASE &&
|
|
|
|
serpent->corps[0].y >= current->position.y * TAILLE_CASE &&
|
|
|
|
serpent->corps[0].y < (current->position.y + 1) * TAILLE_CASE) {
|
|
|
|
// La pomme a été mangée, mettre à jour les liens
|
|
|
|
if (prev == NULL) {
|
|
|
|
// La pomme était en tête de liste
|
|
|
|
pommes->head = current->next;
|
|
|
|
} else {
|
|
|
|
prev->next = current->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(current);
|
2023-12-12 16:28:27 +01:00
|
|
|
*score = *score + 5;
|
|
|
|
AfficherScore(*score);
|
2023-12-13 11:38:02 +01:00
|
|
|
|
|
|
|
// Générer une nouvelle pomme
|
|
|
|
GenererNouvellePomme(pommes);
|
|
|
|
|
2023-12-12 13:46:39 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2023-12-13 11:38:02 +01:00
|
|
|
|
|
|
|
prev = current;
|
|
|
|
current = current->next;
|
2023-12-12 13:46:39 +01:00
|
|
|
}
|
2023-12-13 11:38:02 +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 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-13 16:06:34 +01:00
|
|
|
int EstDirectionOpposee(int directionActuelle, int nouvelleDirection) {
|
|
|
|
if ((directionActuelle == 1 && nouvelleDirection == 2) ||
|
|
|
|
(directionActuelle == 2 && nouvelleDirection == 1) ||
|
|
|
|
(directionActuelle == 3 && nouvelleDirection == 4) ||
|
|
|
|
(directionActuelle == 4 && nouvelleDirection == 3)) {
|
|
|
|
return 1; // Les directions sont opposées
|
|
|
|
}
|
|
|
|
return 0; // Les directions ne sont pas opposées
|
|
|
|
}
|
|
|
|
|
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-13 13:37:00 +01:00
|
|
|
void GenererObstacles(Obstacles* obstacles, Pommes* pommes, Serpent* serpent) {
|
|
|
|
couleur couleurObstacle = CouleurParComposante(128, 128, 128);
|
|
|
|
|
|
|
|
for (int i = 0; i < obstacles->nombre; i++) {
|
|
|
|
do {
|
|
|
|
obstacles->positions[i].x = rand() % LARGEUR;
|
|
|
|
obstacles->positions[i].y = rand() % HAUTEUR;
|
|
|
|
} while (CollisionAvecPommeSerpent(obstacles->positions[i], pommes->head) || CollisionAvecSerpent(serpent));
|
|
|
|
|
|
|
|
ChoisirCouleurDessin(couleurObstacle);
|
|
|
|
RemplirRectangle(obstacles->positions[i].x * TAILLE_CASE, obstacles->positions[i].y * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE);
|
|
|
|
}
|
|
|
|
}
|
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-13 11:38:02 +01:00
|
|
|
// Clear the previous positions of the snake
|
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-13 11:38:02 +01:00
|
|
|
// Update the snake's body positions
|
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-13 11:38:02 +01:00
|
|
|
// Move the snake's head based on the direction
|
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-13 11:38:02 +01:00
|
|
|
// Draw the updated snake
|
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-14 12:21:34 +01:00
|
|
|
AfficherEcranFin(*score);
|
2023-12-13 13:37:00 +01:00
|
|
|
Attendre(1000);
|
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);
|
2023-12-13 13:37:00 +01:00
|
|
|
GenererObstacles(&obstacles, &pommes, &serpent);
|
2023-12-12 16:28:27 +01:00
|
|
|
AfficherSerpent(&serpent);
|
|
|
|
AfficherScore(score);
|
2023-12-12 13:46:39 +01:00
|
|
|
|
|
|
|
while (perdu != -1) {
|
|
|
|
if (!jeuEnPause) {
|
|
|
|
if (Microsecondes() > suivant) {
|
2023-12-13 12:01:56 +01:00
|
|
|
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]++;
|
|
|
|
}
|
2023-12-13 11:38:02 +01:00
|
|
|
AfficheTemps(temps[0], temps[1]);
|
2023-12-13 12:01:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ToucheEnAttente()) {
|
2023-12-13 16:06:34 +01:00
|
|
|
int touche = Touche();
|
|
|
|
int nouvelleDirection;
|
|
|
|
|
|
|
|
// Déterminer la nouvelle direction en fonction de la touche pressée
|
|
|
|
if (touche == XK_Right) {
|
|
|
|
nouvelleDirection = 1;
|
|
|
|
} else if (touche == XK_Left) {
|
|
|
|
nouvelleDirection = 2;
|
|
|
|
} else if (touche == XK_Up) {
|
|
|
|
nouvelleDirection = 3;
|
|
|
|
} else if (touche == XK_Down) {
|
|
|
|
nouvelleDirection = 4;
|
|
|
|
} else if (touche == XK_space) {
|
|
|
|
jeuEnPause = 1;
|
|
|
|
} else if (touche == XK_Escape) {
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vérifier si la nouvelle direction n'est pas opposée à la direction actuelle
|
|
|
|
if (!EstDirectionOpposee(direction, nouvelleDirection)) {
|
|
|
|
// Mettre à jour la direction du serpent
|
|
|
|
direction = nouvelleDirection;
|
|
|
|
}
|
|
|
|
}
|
2023-12-13 13:37:00 +01:00
|
|
|
DeplacerSerpent(&serpent, &direction);
|
|
|
|
perdu = GestionCollision(&serpent, &pommes, &obstacles, perdu, &score);
|
2023-12-13 12:01:56 +01:00
|
|
|
} else {
|
|
|
|
if (ToucheEnAttente()) {
|
2023-12-13 13:37:00 +01:00
|
|
|
int touche = Touche();
|
|
|
|
if (touche == XK_space) {
|
|
|
|
if (touche == XK_Escape) {
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
jeuEnPause = 0;
|
|
|
|
}
|
|
|
|
}
|
2023-12-13 12:01:56 +01:00
|
|
|
}
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|
2023-12-13 12:01:56 +01:00
|
|
|
LibererMemoire(&serpent, &pommes, &obstacles);
|
2023-12-13 13:37:00 +01:00
|
|
|
return EXIT_SUCCESS;
|
2023-12-11 14:45:42 +01:00
|
|
|
}
|