129 lines
3.2 KiB
C
129 lines
3.2 KiB
C
|
#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)
|
||
|
|
||
|
enum direction{UP,DOWN,RIGHT,LEFT};
|
||
|
|
||
|
typedef struct {
|
||
|
int x, y;
|
||
|
} Position;
|
||
|
|
||
|
typedef struct {
|
||
|
Position position;
|
||
|
int taille;
|
||
|
Position queue[10]; // Augmentez la taille si nécessaire
|
||
|
} 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);
|
||
|
}
|
||
|
|
||
|
void deplacerSnake(Snake *snake) {
|
||
|
// 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
|
||
|
if (ToucheEnAttente()) {
|
||
|
int touche = Touche();
|
||
|
if (touche == UP) {
|
||
|
snake->queue[0].y--;
|
||
|
} else if (touche == DOWN) {
|
||
|
snake->queue[0].y++;
|
||
|
} else if (touche == LEFT) {
|
||
|
snake->queue[0].x--;
|
||
|
} else if (touche == RIGHT) {
|
||
|
snake->queue[0].x++;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
void jeuSnake() {
|
||
|
initialiserGraphique();
|
||
|
|
||
|
Snake snake;
|
||
|
snake.position.x = NOMBRE_CASES_X / 2;
|
||
|
snake.position.y = NOMBRE_CASES_Y / 2;
|
||
|
snake.taille = 1;
|
||
|
|
||
|
Fruit fruit;
|
||
|
srand(time(NULL));
|
||
|
fruit.position.x = rand() % NOMBRE_CASES_X;
|
||
|
fruit.position.y = rand() % NOMBRE_CASES_Y;
|
||
|
|
||
|
while (1) {
|
||
|
EffacerEcran(0);
|
||
|
|
||
|
deplacerSnake(&snake);
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int main(void) {
|
||
|
jeuSnake();
|
||
|
|
||
|
return EXIT_SUCCESS;
|
||
|
}
|