2023-11-28 13:23:50 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <graph.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#define LARGEUR_FENETRE 1080
|
|
|
|
#define HAUTEUR_FENETRE 1080
|
|
|
|
#define TAILLE_CASE 5
|
|
|
|
#define NOMBRE_CASES_X (LARGEUR_FENETRE / TAILLE_CASE)
|
|
|
|
#define NOMBRE_CASES_Y (HAUTEUR_FENETRE / TAILLE_CASE)
|
|
|
|
|
2023-12-04 20:37:50 +01:00
|
|
|
enum direction {UP, DOWN, RIGHT, LEFT};
|
2023-11-28 13:23:50 +01:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int x, y;
|
|
|
|
} Position;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
Position position;
|
|
|
|
int taille;
|
2023-12-04 20:37:50 +01:00
|
|
|
Position queue[100]; // Augmentez la taille si nécessaire
|
2023-11-28 13:23:50 +01:00
|
|
|
} Snake;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
Position position;
|
|
|
|
} Fruit;
|
|
|
|
|
|
|
|
void initialiserGraphique() {
|
|
|
|
InitialiserGraphique();
|
|
|
|
CreerFenetre(LARGEUR_FENETRE, HAUTEUR_FENETRE, LARGEUR_FENETRE, HAUTEUR_FENETRE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dessinerCase(Position position, couleur c) {
|
|
|
|
ChoisirCouleurDessin(c);
|
|
|
|
RemplirRectangle(position.x * TAILLE_CASE, position.y * TAILLE_CASE, (position.x + 1) * TAILLE_CASE, (position.y + 1) * TAILLE_CASE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void afficherSnake(Snake *snake) {
|
|
|
|
couleur couleurSnake = CouleurParComposante(0, 255, 0); // Vert
|
|
|
|
for (int i = 0; i < snake->taille; i++) {
|
|
|
|
dessinerCase(snake->queue[i], couleurSnake);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void afficherFruit(Fruit *fruit) {
|
|
|
|
couleur couleurFruit = CouleurParComposante(255, 0, 0); // Rouge
|
|
|
|
dessinerCase(fruit->position, couleurFruit);
|
|
|
|
}
|
|
|
|
|
2023-12-04 20:37:50 +01:00
|
|
|
void deplacerSnake(Snake *snake, enum direction dir) {
|
2023-11-28 13:23:50 +01:00
|
|
|
// Déplacer la queue
|
|
|
|
for (int i = snake->taille - 1; i > 0; i--) {
|
|
|
|
snake->queue[i] = snake->queue[i - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Déplacer la tête
|
2023-12-04 20:37:50 +01:00
|
|
|
switch (dir) {
|
|
|
|
case UP:
|
2023-11-28 13:23:50 +01:00
|
|
|
snake->queue[0].y--;
|
2023-12-04 20:37:50 +01:00
|
|
|
break;
|
|
|
|
case DOWN:
|
2023-11-28 13:23:50 +01:00
|
|
|
snake->queue[0].y++;
|
2023-12-04 20:37:50 +01:00
|
|
|
break;
|
|
|
|
case LEFT:
|
2023-11-28 13:23:50 +01:00
|
|
|
snake->queue[0].x--;
|
2023-12-04 20:37:50 +01:00
|
|
|
break;
|
|
|
|
case RIGHT:
|
2023-11-28 13:23:50 +01:00
|
|
|
snake->queue[0].x++;
|
2023-12-04 20:37:50 +01:00
|
|
|
break;
|
2023-11-28 13:23:50 +01:00
|
|
|
}
|
2023-12-04 20:37:50 +01:00
|
|
|
}
|
2023-11-28 13:23:50 +01:00
|
|
|
|
|
|
|
int collisionAvecFruit(Snake *snake, Fruit *fruit) {
|
|
|
|
return snake->queue[0].x == fruit->position.x && snake->queue[0].y == fruit->position.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
int collisionAvecMur(Snake *snake) {
|
|
|
|
return snake->queue[0].x < 0 || snake->queue[0].x >= NOMBRE_CASES_X ||
|
|
|
|
snake->queue[0].y < 0 || snake->queue[0].y >= NOMBRE_CASES_Y;
|
|
|
|
}
|
|
|
|
|
|
|
|
int collisionAvecQueue(Snake *snake) {
|
|
|
|
for (int i = 1; i < snake->taille; i++) {
|
|
|
|
if (snake->queue[0].x == snake->queue[i].x && snake->queue[0].y == snake->queue[i].y) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2023-12-04 20:37:50 +01:00
|
|
|
|
2023-11-28 13:23:50 +01:00
|
|
|
void jeuSnake() {
|
|
|
|
initialiserGraphique();
|
|
|
|
|
|
|
|
Snake snake;
|
2023-12-04 20:37:50 +01:00
|
|
|
snake.queue[0].x = NOMBRE_CASES_X / 2;
|
|
|
|
snake.queue[0].y = NOMBRE_CASES_Y / 2;
|
2023-11-28 13:23:50 +01:00
|
|
|
snake.taille = 1;
|
|
|
|
|
|
|
|
Fruit fruit;
|
|
|
|
srand(time(NULL));
|
|
|
|
fruit.position.x = rand() % NOMBRE_CASES_X;
|
|
|
|
fruit.position.y = rand() % NOMBRE_CASES_Y;
|
|
|
|
|
2023-12-04 20:37:50 +01:00
|
|
|
enum direction direction = RIGHT; // Direction initiale
|
|
|
|
|
2023-11-28 13:23:50 +01:00
|
|
|
while (1) {
|
2023-12-04 20:37:50 +01:00
|
|
|
EffacerEcran(0);
|
|
|
|
|
|
|
|
deplacerSnake(&snake, direction);
|
2023-11-28 13:23:50 +01:00
|
|
|
afficherSnake(&snake);
|
|
|
|
afficherFruit(&fruit);
|
|
|
|
|
|
|
|
if (collisionAvecFruit(&snake, &fruit)) {
|
|
|
|
// Manger le fruit
|
|
|
|
snake.taille++;
|
|
|
|
fruit.position.x = rand() % NOMBRE_CASES_X;
|
|
|
|
fruit.position.y = rand() % NOMBRE_CASES_Y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (collisionAvecMur(&snake) || collisionAvecQueue(&snake)) {
|
|
|
|
FermerGraphique();
|
|
|
|
printf("Game Over!\n");
|
|
|
|
break;
|
|
|
|
}
|
2023-12-04 20:37:50 +01:00
|
|
|
|
|
|
|
if (ToucheEnAttente()) {
|
|
|
|
int touche = Touche();
|
|
|
|
if (touche == 65 && direction != DOWN) { // 65 correspond à la touche '↑'
|
|
|
|
direction = UP;
|
|
|
|
} else if (touche == 66 && direction != UP) { // 66 correspond à la touche '↓'
|
|
|
|
direction = DOWN;
|
|
|
|
} else if (touche == 68 && direction != RIGHT) { // 68 correspond à la touche '←'
|
|
|
|
direction = LEFT;
|
|
|
|
} else if (touche == 67 && direction != LEFT) { // 67 correspond à la touche '→'
|
|
|
|
direction = RIGHT;
|
|
|
|
}
|
|
|
|
}
|
2023-11-28 13:23:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
2023-12-04 20:37:50 +01:00
|
|
|
Snake snake;
|
|
|
|
Fruit fruit;
|
|
|
|
enum direction{RIGHT,UP,LEFT,DOWN};
|
|
|
|
Position positionCase;
|
|
|
|
positionCase.x = 5;
|
|
|
|
positionCase.y = 5;
|
|
|
|
couleur couleurCase = CouleurParComposante(255, 255, 255);
|
2023-11-28 15:43:48 +01:00
|
|
|
initialiserGraphique();
|
2023-12-04 20:37:50 +01:00
|
|
|
dessinerCase(positionCase, couleurCase);
|
|
|
|
afficherSnake(&Snake);
|
|
|
|
afficherFruit(&Fruit);
|
|
|
|
deplacerSnake(&Snake, direction);
|
|
|
|
jeuSnake();
|
|
|
|
|
|
|
|
|
2023-11-28 13:23:50 +01:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|