Rendu tp3

This commit is contained in:
2025-05-14 14:15:45 +02:00
parent a9ed5e2e01
commit cb05d9710b
6 changed files with 179 additions and 37 deletions

View File

@@ -11,7 +11,7 @@ LDFLAGS = -lMLV
TARGET = game
# Source files
SRCS = game.c grid.c
SRCS = game.c grid.c snake.c
# Object files
OBJS = $(SRCS:.c=.o)

67
game.c
View File

@@ -156,11 +156,30 @@
#include <MLV/MLV_all.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include "grid.h"
#include "snake.h"
int main()
void print_help()
{
char grid[NBL][NBC+1] = {
printf("Usage: snake2025 [OPTIONS]\n");
printf("Options:\n");
printf(" -h, --help Display this help message\n");
printf("\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");
}
int main(int argc, char *argv[])
{
int opt;
int option_index = 0;
MLV_Keyboard_button touche = MLV_KEYBOARD_NONE;
int width = 640, height = 480;
char grid[NBL][NBC + 1] = {
"w w",
" ",
" f ",
@@ -182,13 +201,38 @@ int main()
" f f ",
" ",
" f ",
"w w"
};
"w w"};
MLV_Keyboard_button touche = MLV_KEYBOARD_NONE;
int width = 640, height = 480;
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}};
Snake snake;
snake.pos[0].x = 1;
snake.pos[0].y = 3;
snake.pos[1].x = 1;
snake.pos[1].y = 2;
snake.pos[2].x = 1;
snake.pos[2].y = 1;
snake.pos[3].x = 1;
snake.pos[3].y = 0;
snake.dir = RIGHT;
while ((opt = getopt_long(argc, argv, "h", long_options, &option_index)) != -1)
{
switch (opt)
{
case 'h':
print_help();
return 0;
default:
fprintf(stderr, "Unknown option. Use -h or --help for usage information.\n");
return 1;
}
}
place_snake(grid, &snake);
/* Ouverture de la fenêtre graphique */
MLV_create_window("SNAKE", "3R-IN1B", width, height);
MLV_change_frame_rate(24);
@@ -197,14 +241,15 @@ int main()
&touche, NULL, NULL,
NULL, NULL,
NULL, NULL, NULL,
NULL
) == MLV_NONE ||
touche != MLV_KEYBOARD_ESCAPE
) {
NULL) == MLV_NONE ||
touche != MLV_KEYBOARD_ESCAPE)
{
MLV_clear_window(MLV_COLOR_BROWN);
draw_grid(grid);
move_snake(&snake, grid);
MLV_actualise_window();
touche = MLV_KEYBOARD_NONE;

76
grid.c
View File

@@ -1,47 +1,81 @@
#include <stdio.h>
#include <MLV/MLV_all.h>
#include "grid.h"
#include "snake.h"
void debug(char grid[NBL][NBC+1]) {
void debug(char grid[NBL][NBC + 1])
{
int i;
for (i = 0; i < NBL; i++) {
for (i = 0; i < NBL; i++)
{
printf("%s\n", grid[i]);
}
}
int compute_size(int w, int h) {
int compute_size(int w, int h)
{
int size_width = w / NBC;
int size_height = h / NBL;
return (size_width < size_height) ? size_width : size_height;
}
void draw_grid(char grid[NBL][NBC+1]) {
void draw_grid(char grid[NBL][NBC + 1])
{
int i, j;
int window_width = MLV_get_window_width();
int window_height = MLV_get_window_height();
int cell_size = compute_size(window_width, window_height);
for (i = 0; i < NBL; i++) {
for (j = 0; j < NBC; j++) {
MLV_draw_filled_rectangle(0, 0, window_width, window_height, MLV_COLOR_BLACK);
for (i = 0; i < NBL; i++)
{
for (j = 0; j < NBC; j++)
{
int x = j * cell_size;
int y = i * cell_size;
switch (grid[i][j]) {
case WALL:
MLV_draw_filled_rectangle(x, y, cell_size, cell_size, MLV_COLOR_BROWN);
break;
case EMPTY:
MLV_draw_filled_rectangle(x, y, cell_size, cell_size, MLV_COLOR_WHITE);
break;
case FRUIT:
MLV_draw_filled_rectangle(x, y, cell_size, cell_size, MLV_COLOR_YELLOW);
break;
default:
break;
switch (grid[i][j])
{
case WALL:
MLV_draw_filled_rectangle(x, y, cell_size, cell_size, MLV_COLOR_BROWN);
break;
case EMPTY:
MLV_draw_filled_rectangle(x, y, cell_size, cell_size, MLV_COLOR_WHITE);
break;
case FRUIT:
MLV_draw_filled_rectangle(x, y, cell_size, cell_size, MLV_COLOR_RED);
break;
case SNAKE:
MLV_draw_filled_rectangle(x, y, cell_size, cell_size, MLV_COLOR_GREEN);
break;
default:
MLV_draw_filled_rectangle(x, y, cell_size, cell_size, MLV_COLOR_BLACK);
break;
}
}
}
}
void place_snake(char grid[NBL][NBC + 1], Snake *snake)
{
int i;
for (i = 0; i < SNAKE_SIZE; i++)
{
Coord part = snake->pos[i];
grid[part.y][part.x] = 's';
}
}
void move_snake(Snake *snake, char grid[NBL][NBC + 1])
{
Coord tail = snake->pos[SNAKE_SIZE - 1];
Coord head;
grid[tail.y][tail.x] = EMPTY;
crawl(snake);
head = snake->pos[0];
grid[head.y][head.x] = SNAKE;
}

13
grid.h
View File

@@ -2,18 +2,23 @@
#define GRID_H
#include <MLV/MLV_all.h>
#include "snake.h"
#define NBL 22
#define NBC 36
typedef enum {
typedef enum
{
WALL = 'w',
EMPTY = ' ',
FRUIT = 'f'
FRUIT = 'f',
SNAKE = 's'
} Element;
void debug(char grid[NBL][NBC+1]);
void debug(char grid[NBL][NBC + 1]);
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 move_snake(Snake* snake, char grid[NBL][NBC+1]);
#endif /* GRID_H */

30
snake.c Normal file
View File

@@ -0,0 +1,30 @@
#include "grid.h"
#include "snake.h"
void crawl(Snake *snake)
{
int i;
Coord new_head = snake->pos[0];
switch (snake->dir)
{
case LEFT:
new_head.x = (new_head.x - 1 + NBC) % NBC;
break;
case RIGHT:
new_head.x = (new_head.x + 1) % NBC;
break;
case TOP:
new_head.y = (new_head.y - 1 + NBL) % NBL;
break;
case BOTTOM:
new_head.y = (new_head.y + 1) % NBL;
break;
}
for (i = SNAKE_SIZE - 1; i > 0; i--)
{
snake->pos[i] = snake->pos[i - 1];
}
snake->pos[0] = new_head;
}

28
snake.h Normal file
View File

@@ -0,0 +1,28 @@
#ifndef SNAKE_H
#define SNAKE_H
#define SNAKE_SIZE 4
typedef struct
{
int x;
int y;
} Coord;
typedef enum
{
TOP,
BOTTOM,
LEFT,
RIGHT
} Direction;
typedef struct
{
Coord pos[SNAKE_SIZE];
Direction dir;
} Snake;
void crawl(Snake *snake);
#endif /* SNAKE_H */