Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 112139bafd | |||
|
f16b505965
|
|||
|
fdb80a7cc2
|
@@ -1,6 +1,6 @@
|
|||||||
# Jeu Snake 2025
|
# Jeu Snake 2025
|
||||||
[](https://git.esiee.fr/frequela/snake2025/-/blob/tp2/README.md)
|
[](https://git.esiee.fr/frequela/snake2025/-/blob/tp4/README.md)
|
||||||
[](https://git.esiee.fr/frequela/snake2025/-/blob/tp2/README-fr.md)
|
[](https://git.esiee.fr/frequela/snake2025/-/blob/tp4/README-fr.md)
|
||||||
|
|
||||||
Ce projet est un jeu snake basé sur une grille utilisant la bibliothèque graphique MLV. La grille représente un plateau de jeu avec des murs, des espaces vides, des fruits et un serpent. Le programme affiche la grille dans une fenêtre graphique et attend une interaction de l'utilisateur.
|
Ce projet est un jeu snake basé sur une grille utilisant la bibliothèque graphique MLV. La grille représente un plateau de jeu avec des murs, des espaces vides, des fruits et un serpent. Le programme affiche la grille dans une fenêtre graphique et attend une interaction de l'utilisateur.
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# Snake 2025 Game
|
# Snake 2025 Game
|
||||||
[](https://git.esiee.fr/frequela/snake2025/-/blob/tp2/README.md)
|
[](https://git.esiee.fr/frequela/snake2025/-/blob/tp4/README.md)
|
||||||
[](https://git.esiee.fr/frequela/snake2025/-/blob/tp2/README-fr.md)
|
[](https://git.esiee.fr/frequela/snake2025/-/blob/tp4/README-fr.md)
|
||||||
|
|
||||||
This project is a simple grid-based snake game using the MLV graphics library. The grid represents a game board with walls, empty spaces, fruits, and a snake. The program displays the grid in a graphical window and waits for user interaction.
|
This project is a simple grid-based snake game using the MLV graphics library. The grid represents a game board with walls, empty spaces, fruits, and a snake. The program displays the grid in a graphical window and waits for user interaction.
|
||||||
|
|
||||||
|
|||||||
169
game.c
169
game.c
@@ -157,54 +157,84 @@
|
|||||||
#include <MLV/MLV_all.h>
|
#include <MLV/MLV_all.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include "grid.h"
|
#include "grid.h"
|
||||||
#include "snake.h"
|
#include "snake.h"
|
||||||
|
|
||||||
|
#define DIFFICULTY 6
|
||||||
|
|
||||||
void print_help()
|
void print_help()
|
||||||
{
|
{
|
||||||
printf("Usage: snake2025 [OPTIONS]\n");
|
printf("Usage: snake2025 [OPTIONS]\n");
|
||||||
printf("Options:\n");
|
printf("Options:\n");
|
||||||
printf(" -h, --help Display this help message\n");
|
printf(" -h, --help Display this help message\n");
|
||||||
|
printf(" -i, --input FILE Load initial grid configuration from FILE\n");
|
||||||
printf("\n");
|
printf("\n");
|
||||||
printf("This is a simple snake game. Use the arrow keys to control the snake.\n");
|
printf("This is a simple snake game. Use the arrow keys to control the snake.\n");
|
||||||
printf("Press ESC to exit the game.\n");
|
printf("Press ESC to exit the game.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int read_grid_from_file(const char *filename, char grid[NBL][NBC + 1])
|
||||||
|
{
|
||||||
|
int i, j, nb_fruit = 0;
|
||||||
|
FILE *file = fopen(filename, "r");
|
||||||
|
if (!file)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: Could not open file %s\n", filename);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < NBL; i++)
|
||||||
|
{
|
||||||
|
if (!fgets(grid[i], NBC + 2, file))
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: File %s does not contain enough lines\n", filename);
|
||||||
|
fclose(file);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
grid[i][strcspn(grid[i], "\n")] = '\0';
|
||||||
|
|
||||||
|
for (j = 0; j < NBC; j++)
|
||||||
|
{
|
||||||
|
if (grid[i][j] == FRUIT)
|
||||||
|
{
|
||||||
|
nb_fruit++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
return nb_fruit;
|
||||||
|
}
|
||||||
|
|
||||||
|
int count_fruits(char grid[NBL][NBC + 1])
|
||||||
|
{
|
||||||
|
int i, j, nb_fruit = 0;
|
||||||
|
for (i = 0; i < NBL; i++)
|
||||||
|
{
|
||||||
|
for (j = 0; j < NBC; j++)
|
||||||
|
{
|
||||||
|
if (grid[i][j] == FRUIT)
|
||||||
|
{
|
||||||
|
nb_fruit++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nb_fruit;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int opt;
|
int opt, option_index = 0, loop_count = 0, nb_fruit = 0;
|
||||||
int option_index = 0;
|
char *input_file = NULL;
|
||||||
|
|
||||||
MLV_Keyboard_button touche = MLV_KEYBOARD_NONE;
|
MLV_Keyboard_button touche = MLV_KEYBOARD_NONE;
|
||||||
int width = 640, height = 480;
|
int width = 640, height = 480;
|
||||||
|
char grid[NBL][NBC + 1] = {0};
|
||||||
char grid[NBL][NBC + 1] = {
|
|
||||||
"w w",
|
|
||||||
" ",
|
|
||||||
" f ",
|
|
||||||
" ",
|
|
||||||
" f f ",
|
|
||||||
" ",
|
|
||||||
" ",
|
|
||||||
" f ",
|
|
||||||
" ",
|
|
||||||
" ",
|
|
||||||
" wwwwwwwwww ",
|
|
||||||
" ",
|
|
||||||
" ",
|
|
||||||
" ",
|
|
||||||
" ",
|
|
||||||
" ",
|
|
||||||
" f ",
|
|
||||||
" ",
|
|
||||||
" f f ",
|
|
||||||
" ",
|
|
||||||
" f ",
|
|
||||||
"w w"};
|
|
||||||
|
|
||||||
static struct option long_options[] = {
|
static struct option long_options[] = {
|
||||||
{"help", no_argument, 0, 'h'},
|
{"help", no_argument, 0, 'h'},
|
||||||
|
{"input", required_argument, 0, 'i'},
|
||||||
{0, 0, 0, 0}};
|
{0, 0, 0, 0}};
|
||||||
|
|
||||||
Snake snake;
|
Snake snake;
|
||||||
@@ -218,19 +248,39 @@ int main(int argc, char *argv[])
|
|||||||
snake.pos[3].y = 0;
|
snake.pos[3].y = 0;
|
||||||
snake.dir = RIGHT;
|
snake.dir = RIGHT;
|
||||||
|
|
||||||
while ((opt = getopt_long(argc, argv, "h", long_options, &option_index)) != -1)
|
while ((opt = getopt_long(argc, argv, "hi:", long_options, &option_index)) != -1)
|
||||||
{
|
{
|
||||||
switch (opt)
|
switch (opt)
|
||||||
{
|
{
|
||||||
case 'h':
|
case 'h':
|
||||||
print_help();
|
print_help();
|
||||||
return 0;
|
return 0;
|
||||||
|
case 'i':
|
||||||
|
input_file = optarg;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "Unknown option. Use -h or --help for usage information.\n");
|
fprintf(stderr, "Unknown option. Use -h or --help for usage information.\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (input_file)
|
||||||
|
{
|
||||||
|
nb_fruit = read_grid_from_file(input_file, grid);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int i, j;
|
||||||
|
for (i = 0; i < NBL; i++)
|
||||||
|
{
|
||||||
|
for (j = 0; j < NBC; j++)
|
||||||
|
{
|
||||||
|
grid[i][j] = EMPTY;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nb_fruit = count_fruits(grid);
|
||||||
|
}
|
||||||
|
|
||||||
place_snake(grid, &snake);
|
place_snake(grid, &snake);
|
||||||
|
|
||||||
MLV_create_window("SNAKE", "3R-IN1B", width, height);
|
MLV_create_window("SNAKE", "3R-IN1B", width, height);
|
||||||
@@ -244,14 +294,69 @@ int main(int argc, char *argv[])
|
|||||||
NULL) == MLV_NONE ||
|
NULL) == MLV_NONE ||
|
||||||
touche != MLV_KEYBOARD_ESCAPE)
|
touche != MLV_KEYBOARD_ESCAPE)
|
||||||
{
|
{
|
||||||
MLV_clear_window(MLV_COLOR_BROWN);
|
Element result;
|
||||||
|
MLV_clear_window(MLV_COLOR_BLACK);
|
||||||
|
|
||||||
|
loop_count = (loop_count + 1) % DIFFICULTY;
|
||||||
|
if (loop_count == 0)
|
||||||
|
{
|
||||||
|
result = move_snake(&snake, grid);
|
||||||
|
|
||||||
|
if (result == WALL || result == SNAKE)
|
||||||
|
{
|
||||||
|
if (result == WALL)
|
||||||
|
{
|
||||||
|
MLV_draw_text(width / 2 - 75, height / 2, "Game Over! You hit a wall.", MLV_COLOR_RED);
|
||||||
|
}
|
||||||
|
else if (result == SNAKE)
|
||||||
|
{
|
||||||
|
MLV_draw_text(width / 2 - 75, height / 2, "Game Over! You hit yourself.", MLV_COLOR_RED);
|
||||||
|
}
|
||||||
|
MLV_actualise_window();
|
||||||
|
MLV_wait_seconds(3);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else if (result == FRUIT)
|
||||||
|
{
|
||||||
|
nb_fruit--;
|
||||||
|
if (nb_fruit == 0)
|
||||||
|
{
|
||||||
|
MLV_draw_text(
|
||||||
|
width / 2 - 75, height / 2,
|
||||||
|
"You Win! All fruits collected.",
|
||||||
|
MLV_COLOR_GREEN);
|
||||||
|
MLV_actualise_window();
|
||||||
|
MLV_wait_seconds(3);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
draw_grid(grid);
|
draw_grid(grid);
|
||||||
|
|
||||||
move_snake(&snake, grid);
|
|
||||||
|
|
||||||
MLV_actualise_window();
|
MLV_actualise_window();
|
||||||
|
|
||||||
|
switch (touche)
|
||||||
|
{
|
||||||
|
case MLV_KEYBOARD_DOWN:
|
||||||
|
if (snake.dir != TOP)
|
||||||
|
snake.dir = BOTTOM;
|
||||||
|
break;
|
||||||
|
case MLV_KEYBOARD_UP:
|
||||||
|
if (snake.dir != BOTTOM)
|
||||||
|
snake.dir = TOP;
|
||||||
|
break;
|
||||||
|
case MLV_KEYBOARD_LEFT:
|
||||||
|
if (snake.dir != RIGHT)
|
||||||
|
snake.dir = LEFT;
|
||||||
|
break;
|
||||||
|
case MLV_KEYBOARD_RIGHT:
|
||||||
|
if (snake.dir != LEFT)
|
||||||
|
snake.dir = RIGHT;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
touche = MLV_KEYBOARD_NONE;
|
touche = MLV_KEYBOARD_NONE;
|
||||||
MLV_delay_according_to_frame_rate();
|
MLV_delay_according_to_frame_rate();
|
||||||
}
|
}
|
||||||
|
|||||||
8
grid.c
8
grid.c
@@ -67,15 +67,19 @@ void place_snake(char grid[NBL][NBC + 1], Snake *snake)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void move_snake(Snake *snake, char grid[NBL][NBC + 1])
|
Element move_snake(Snake *snake, char grid[NBL][NBC + 1]) {
|
||||||
{
|
|
||||||
Coord tail = snake->pos[SNAKE_SIZE - 1];
|
Coord tail = snake->pos[SNAKE_SIZE - 1];
|
||||||
Coord head;
|
Coord head;
|
||||||
|
Element element_at_head;
|
||||||
|
|
||||||
grid[tail.y][tail.x] = EMPTY;
|
grid[tail.y][tail.x] = EMPTY;
|
||||||
|
|
||||||
crawl(snake);
|
crawl(snake);
|
||||||
|
|
||||||
head = snake->pos[0];
|
head = snake->pos[0];
|
||||||
|
element_at_head = grid[head.y][head.x];
|
||||||
|
|
||||||
grid[head.y][head.x] = SNAKE;
|
grid[head.y][head.x] = SNAKE;
|
||||||
|
|
||||||
|
return element_at_head;
|
||||||
}
|
}
|
||||||
2
grid.h
2
grid.h
@@ -18,6 +18,6 @@ void debug(char grid[NBL][NBC + 1]);
|
|||||||
int compute_size(int w, int h);
|
int compute_size(int w, int h);
|
||||||
void draw_grid(char grid[NBL][NBC + 1]);
|
void draw_grid(char grid[NBL][NBC + 1]);
|
||||||
void place_snake(char grid[NBL][NBC + 1], Snake *snake);
|
void place_snake(char grid[NBL][NBC + 1], Snake *snake);
|
||||||
void move_snake(Snake* snake, char grid[NBL][NBC+1]);
|
Element move_snake(Snake* snake, char grid[NBL][NBC+1]);
|
||||||
|
|
||||||
#endif /* GRID_H */
|
#endif /* GRID_H */
|
||||||
Reference in New Issue
Block a user