Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b23fd11f2f | |||
|
48c0bd0f96
|
|||
|
7b0f8f28e0
|
|||
|
f16b505965
|
|||
|
fdb80a7cc2
|
|||
|
fc00084d03
|
|||
|
197de63bde
|
|||
|
cb05d9710b
|
2
Makefile
2
Makefile
@@ -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)
|
||||
|
||||
14
README-fr.md
14
README-fr.md
@@ -1,13 +1,14 @@
|
||||
# Jeu Snake 2025
|
||||
[](https://git.esiee.fr/frequela/snake2025/-/blob/tp2/README.md)
|
||||
[](https://git.esiee.fr/frequela/snake2025/-/blob/tp2/README-fr.md)
|
||||
[](https://git.esiee.fr/frequela/snake2025/-/blob/tp5/README.md)
|
||||
[](https://git.esiee.fr/frequela/snake2025/-/blob/tp5/README-fr.md)
|
||||
|
||||
Ce projet est une visualisation simple d'un jeu basé sur une grille utilisant la bibliothèque graphique MLV. La grille représente un plateau de jeu avec des murs, des espaces vides et des fruits. 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.
|
||||
|
||||
## Fonctionnalités
|
||||
|
||||
- Affiche une grille avec des murs, des espaces vides et des fruits.
|
||||
- Affiche une grille avec des murs, des espaces vides, des fruits et un serpent.
|
||||
- Utilise la bibliothèque graphique MLV pour le rendu graphique.
|
||||
- Assure que les blocs en dehors de la grille sont affichés en noir.
|
||||
- Permet à l'utilisateur de quitter le programme en appuyant sur la touche ESC.
|
||||
|
||||
## Représentation de la Grille
|
||||
@@ -16,6 +17,7 @@ Ce projet est une visualisation simple d'un jeu basé sur une grille utilisant l
|
||||
- `'w'` pour les murs.
|
||||
- `' '` (espace) pour les espaces vides.
|
||||
- `'f'` pour les fruits.
|
||||
- `'s'` pour le serpent.
|
||||
|
||||
## Dépendances
|
||||
|
||||
@@ -47,10 +49,10 @@ Cela compilera les fichiers sources et créera un exécutable nommé `game` dans
|
||||
|
||||
1. Après avoir compilé le programme, exécutez l'exécutable :
|
||||
```sh
|
||||
./build/game
|
||||
./game
|
||||
```
|
||||
|
||||
2. Une fenêtre s'ouvrira affichant la grille. Appuyez sur la touche ESC pour fermer la fenêtre.
|
||||
2. Une fenêtre s'ouvrira affichant la grille. Les blocs en dehors de la grille apparaîtront en noir. Appuyez sur la touche ESC pour fermer la fenêtre.
|
||||
|
||||
## Nettoyage
|
||||
|
||||
|
||||
12
README.md
12
README.md
@@ -1,13 +1,14 @@
|
||||
# Snake 2025 Game
|
||||
[](https://git.esiee.fr/frequela/snake2025/-/blob/tp2/README.md)
|
||||
[](https://git.esiee.fr/frequela/snake2025/-/blob/tp2/README-fr.md)
|
||||
[](https://git.esiee.fr/frequela/snake2025/-/blob/tp5/README.md)
|
||||
[](https://git.esiee.fr/frequela/snake2025/-/blob/tp5/README-fr.md)
|
||||
|
||||
This project is a simple grid-based game visualization using the MLV graphics library. The grid represents a game board with walls, empty spaces, and fruits. 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.
|
||||
|
||||
## Features
|
||||
|
||||
- Displays a grid with walls, empty spaces, and fruits.
|
||||
- Displays a grid with walls, empty spaces, fruits, and a snake.
|
||||
- Uses the MLV graphics library for graphical rendering.
|
||||
- Ensures that blocks outside the grid are displayed as black.
|
||||
- Allows the user to quit the program by pressing the ESC key.
|
||||
|
||||
## Grid Representation
|
||||
@@ -16,6 +17,7 @@ This project is a simple grid-based game visualization using the MLV graphics li
|
||||
- `'w'` for walls.
|
||||
- `' '` (space) for empty spaces.
|
||||
- `'f'` for fruits.
|
||||
- `'s'` for the snake.
|
||||
|
||||
## Dependencies
|
||||
|
||||
@@ -50,7 +52,7 @@ This will compile the source files and create an executable named `game` in the
|
||||
./game
|
||||
```
|
||||
|
||||
2. A window will open displaying the grid. Press the ESC key to close the window.
|
||||
2. A window will open displaying the grid. Blocks outside the grid will appear black. Press the ESC key to close the window.
|
||||
|
||||
## Cleaning Up
|
||||
|
||||
|
||||
371
game.c
371
game.c
@@ -1,194 +1,130 @@
|
||||
/**
|
||||
*
|
||||
* Ce programme montre comment il est possible de dessiner les figures
|
||||
* géométriques suivantes :
|
||||
*
|
||||
* cercle
|
||||
* disque ( cercle plein )
|
||||
* ellipse
|
||||
* ellipse pleine
|
||||
* rectangle
|
||||
* rectangle plein
|
||||
* ligne
|
||||
* point
|
||||
* Courbe de Bézier
|
||||
* polygone
|
||||
* polygone plein
|
||||
*
|
||||
* Pour cela le programme utilise les fonctions suivantes :
|
||||
*
|
||||
*------------------------------------------------------------------------------
|
||||
* MLV_draw_circle: Dessine un cercle à une position et un rayon spécifiés en
|
||||
* paramètres.
|
||||
*
|
||||
* void MLV_draw_circle(
|
||||
* int x, Coordonnée en X du centre du cercle
|
||||
* int y, Coordonnée en Y du centre du cercle
|
||||
* int radius, Rayon du cercle
|
||||
* MLV_Color color Couleur du tracé
|
||||
* );
|
||||
*
|
||||
* -----------------------------------------------------------------------------
|
||||
* MLV_draw_filled_circle : Dessine un disque plein dont le centre et le rayon
|
||||
* sont passés en parametres.
|
||||
*
|
||||
* void MLV_draw_filled_circle(
|
||||
* int x, Coordonnée en X du centre du cercle
|
||||
* int y, Coordonnée en Y du centre du cercle
|
||||
* int radius, Rayon du cercle
|
||||
* MLV_Color color Couleur du tracé
|
||||
* );
|
||||
*
|
||||
* -----------------------------------------------------------------------------
|
||||
* MLV_draw_ellipse : Dessine une ellipse dont la position, les rayons et la
|
||||
* couleur sont passés en paramètres.
|
||||
*
|
||||
* MLV_draw_ellipse(
|
||||
* int x, Coordonnée en X du centre de l'ellipse
|
||||
* int y, Coordonnée en Y du centre de l'ellipse
|
||||
* int radius_x, Rayon en X de l'ellipse
|
||||
* int radius_y, Rayon en Y de l'ellipse
|
||||
* MLV_Color color Couleur du tracé
|
||||
* );
|
||||
*
|
||||
* -----------------------------------------------------------------------------
|
||||
* MLV_draw_filled_ellipse : Dessine une ellipse pleine dont la position, les
|
||||
* rayons et la couleur sont passés en paramètres.
|
||||
*
|
||||
* void MLV_draw_filled_ellipse(
|
||||
* int x, Coordonnee en X du centre de l'ellipse
|
||||
* int y, Coordonnee en Y du centre de l'ellipse
|
||||
* int radius_x, Rayon en X de l'ellipse
|
||||
* int radius_y, Rayon en Y de l'ellipse
|
||||
* MLV_Color color Couleur du tracé
|
||||
* );
|
||||
*
|
||||
* -----------------------------------------------------------------------------
|
||||
* MLV_draw_rectangle : Dessine un rectangle dont la taille, la couleur et la
|
||||
* position du sommet Nord-Ouest sont données en
|
||||
* paramètres.
|
||||
*
|
||||
* void MLV_draw_rectangle(
|
||||
* int x, Coordonnée en X du sommet Nord-Ouest du rectangle
|
||||
* int y, Coordonnée en Y du sommet Nord-Ouest du rectangle
|
||||
* int width, Largeur du rectangle
|
||||
* int height, Hauteur du rectangle
|
||||
* MLV_Color color Couleur du tracé
|
||||
* );
|
||||
*
|
||||
* -----------------------------------------------------------------------------
|
||||
* MLV_draw_filled_rectangle : Dessine un rectangle dont la taille, la couleur
|
||||
* et la position du sommet Nord-Ouest sont données
|
||||
* en paramètres.
|
||||
* void MLV_draw_filled_rectangle(
|
||||
* int x, Coordonnée en X du sommet Nord-Ouest du rectangle
|
||||
* int y, Coordonnée en Y du sommet Nord-Ouest du rectangle
|
||||
* int width, Largeur du rectangle
|
||||
* int height, Hauteur du rectangle
|
||||
* MLV_Color color Couleur du tracé
|
||||
* );
|
||||
*
|
||||
* -----------------------------------------------------------------------------
|
||||
* MLV_draw_line : Dessine une ligne dont les coordonnées des deux extrémités
|
||||
* sont passées en paramètres.
|
||||
* void MLV_draw_line(
|
||||
* int x1, Coordonnée en X de la première extrémité de la ligne
|
||||
* int y1, Coordonnée en Y de la première extrémité de la ligne
|
||||
* int x2, Coordonnée en X de la deuxième extrémité de la ligne
|
||||
* int y2, Coordonnée en Y de la deuxième extrémité de la ligne
|
||||
* MLV_Color color Couleur du tracé
|
||||
* );
|
||||
*
|
||||
* -----------------------------------------------------------------------------
|
||||
* MLV_draw_point : Dessine un point dont les coordonées sont passées
|
||||
* en paramètres.
|
||||
*
|
||||
* void MLV_draw_point(
|
||||
* int x, Coordonnée en X du point
|
||||
* int y, Coordonnée en Y du point
|
||||
* MLV_Color color Couleur du tracé
|
||||
* );
|
||||
*
|
||||
* -----------------------------------------------------------------------------
|
||||
* MLV_draw_bezier_curve: Dessine une courbe de Bézier à partir d'une liste de
|
||||
* sommets.
|
||||
*
|
||||
* void MLV_draw_bezier_curve(
|
||||
* const int* vx, La liste des coordonnées en X des différents
|
||||
* sommets de la courbe.
|
||||
* const int* vy, La liste des coordonnées en Y des différents
|
||||
* sommets de la courbe.
|
||||
* int npoints, Le nombre de sommets de la courbe de Bézier.
|
||||
* MLV_Color color La couleur du tracé
|
||||
* );
|
||||
*
|
||||
* -----------------------------------------------------------------------------
|
||||
* MLV_draw_polygon: Dessine un polygone à partir d'une liste de sommets.
|
||||
*
|
||||
* void MLV_draw_polygon(
|
||||
* const int* vx, La liste des coordonnées en X des différents
|
||||
* sommets du polygone.
|
||||
* const int* vy, La liste des coordonnées en Y des différents
|
||||
* sommets du polygone.
|
||||
* int npoints, Le nombre de sommets du polygone.
|
||||
* MLV_Color color La couleur du tracé
|
||||
* );
|
||||
*
|
||||
* -----------------------------------------------------------------------------
|
||||
* MLV_draw_polygon: Dessine un polygone plein à partir d'une liste de sommets.
|
||||
*
|
||||
* void MLV_draw_filled_polygon(
|
||||
* const int* vx, La liste des coordonnées en X des différents
|
||||
* sommets du polygone.
|
||||
* const int* vy, La liste des coordonnées en Y des différents
|
||||
* sommets du polygone.
|
||||
* int npoints, Le nombre de sommets du polygone.
|
||||
* MLV_Color color La couleur du tracé
|
||||
* );
|
||||
*
|
||||
* -----------------------------------------------------------------------------
|
||||
*
|
||||
* Il existe d'autres fonctions de dessin. Vous les trouverez en consultant
|
||||
* le fichier MLV_shape.h ou en tapant dans le terminal la commande :
|
||||
*
|
||||
* man MLV_shape.h
|
||||
*/
|
||||
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <MLV/MLV_all.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <getopt.h>
|
||||
#include "grid.h"
|
||||
#include "snake.h"
|
||||
|
||||
int main()
|
||||
#define DIFFICULTY 6
|
||||
|
||||
void print_help()
|
||||
{
|
||||
char grid[NBL][NBC+1] = {
|
||||
"w w",
|
||||
" ",
|
||||
" f ",
|
||||
" ",
|
||||
" f f ",
|
||||
" ",
|
||||
" ",
|
||||
" f ",
|
||||
" ",
|
||||
" ",
|
||||
" wwwwwwwwww ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" f ",
|
||||
" ",
|
||||
" f f ",
|
||||
" ",
|
||||
" f ",
|
||||
"w w"
|
||||
};
|
||||
printf("Usage: snake2025 [OPTIONS]\n");
|
||||
printf("Options:\n");
|
||||
printf(" -h, --help Display this help message\n");
|
||||
printf(" -i, --input FILE Load initial grid configuration from FILE\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");
|
||||
}
|
||||
|
||||
MLV_Keyboard_button touche = MLV_KEYBOARD_NONE;
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE *stream;
|
||||
char *buf = NULL;
|
||||
size_t size_buf = 0;
|
||||
int nbl, nbc, i;
|
||||
int opt, option_index = 0;
|
||||
int loop_count = 0, nb_fruit = 0;
|
||||
char *input_file = NULL;
|
||||
int width = 640, height = 480;
|
||||
MLV_Keyboard_button touche = MLV_KEYBOARD_NONE;
|
||||
|
||||
Grid *g;
|
||||
|
||||
static struct option long_options[] = {
|
||||
{"help", no_argument, 0, 'h'},
|
||||
{"input", required_argument, 0, 'i'},
|
||||
{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, "hi:", long_options, &option_index)) != -1)
|
||||
{
|
||||
switch (opt)
|
||||
{
|
||||
case 'h':
|
||||
print_help();
|
||||
return 0;
|
||||
case 'i':
|
||||
input_file = optarg;
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "Unknown option. Use -h or --help for usage information.\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (input_file == NULL)
|
||||
{
|
||||
input_file = "levels/default";
|
||||
}
|
||||
stream = fopen(input_file, "r");
|
||||
if (!stream)
|
||||
{
|
||||
fprintf(stderr, "Error: unable to open file\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
nbl = count_nb_lines(stream);
|
||||
rewind(stream);
|
||||
nbc = getline(&buf, &size_buf, stream);
|
||||
if (nbc == -1)
|
||||
{
|
||||
fprintf(stderr, "Error: malformed file\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
nbc--;
|
||||
|
||||
g = allocate_grid(nbl, nbc);
|
||||
|
||||
copy(buf, g->grid[0]);
|
||||
for (i = 1; i < nbl; i++)
|
||||
{
|
||||
int size_tmp = getline(&buf, &size_buf, stream);
|
||||
if (size_tmp != nbc + 1)
|
||||
{
|
||||
fprintf(stderr, "Error: inconsistent line length\n");
|
||||
free(buf);
|
||||
fclose(stream);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
copy(buf, g->grid[i]);
|
||||
}
|
||||
|
||||
rewind(stream);
|
||||
nb_fruit = count_fruits(stream, g);
|
||||
if (nb_fruit == 0)
|
||||
{
|
||||
fprintf(stderr, "Error: no fruits in the grid\n");
|
||||
free(buf);
|
||||
fclose(stream);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (nb_fruit == -1)
|
||||
{
|
||||
fprintf(stderr, "Error: unable to count fruits\n");
|
||||
free(buf);
|
||||
fclose(stream);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
free(buf);
|
||||
fclose(stream);
|
||||
|
||||
place_snake(g, &snake);
|
||||
|
||||
/* Ouverture de la fenêtre graphique */
|
||||
MLV_create_window("SNAKE", "3R-IN1B", width, height);
|
||||
MLV_change_frame_rate(24);
|
||||
|
||||
@@ -197,20 +133,77 @@ int main()
|
||||
&touche, NULL, NULL,
|
||||
NULL, NULL,
|
||||
NULL, NULL, NULL,
|
||||
NULL
|
||||
) == MLV_NONE ||
|
||||
touche != MLV_KEYBOARD_ESCAPE
|
||||
) {
|
||||
MLV_clear_window(MLV_COLOR_BROWN);
|
||||
NULL) == MLV_NONE ||
|
||||
touche != MLV_KEYBOARD_ESCAPE)
|
||||
{
|
||||
Element result;
|
||||
MLV_clear_window(MLV_COLOR_BLACK);
|
||||
|
||||
draw_grid(grid);
|
||||
loop_count = (loop_count + 1) % DIFFICULTY;
|
||||
if (loop_count == 0)
|
||||
{
|
||||
result = move_snake(&snake, g);
|
||||
|
||||
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(g);
|
||||
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;
|
||||
MLV_delay_according_to_frame_rate();
|
||||
}
|
||||
|
||||
MLV_free_window();
|
||||
free_grid(g);
|
||||
return 0;
|
||||
}
|
||||
180
grid.c
180
grid.c
@@ -1,47 +1,177 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <MLV/MLV_all.h>
|
||||
#include "grid.h"
|
||||
#include "snake.h"
|
||||
|
||||
|
||||
void debug(char grid[NBL][NBC+1]) {
|
||||
Grid *allocate_grid(int n, int m)
|
||||
{
|
||||
Grid *g = malloc(sizeof(Grid));
|
||||
int i;
|
||||
for (i = 0; i < NBL; i++) {
|
||||
printf("%s\n", grid[i]);
|
||||
if (!g)
|
||||
{
|
||||
fprintf(stderr, "Error: could not allocate Grid.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
g->nbl = n;
|
||||
g->nbc = m;
|
||||
g->grid = malloc(n * sizeof(char *));
|
||||
if (!g->grid)
|
||||
{
|
||||
fprintf(stderr, "Error: could not allocate g->grid.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
g->grid[i] = calloc(m + 1, sizeof(char));
|
||||
if (!g->grid[i])
|
||||
{
|
||||
fprintf(stderr, "Error: could not allocate row.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
return g;
|
||||
}
|
||||
|
||||
void free_grid(Grid *g)
|
||||
{
|
||||
int i;
|
||||
if (!g)
|
||||
return;
|
||||
for (i = 0; i < g->nbl; i++)
|
||||
{
|
||||
free(g->grid[i]);
|
||||
}
|
||||
free(g->grid);
|
||||
free(g);
|
||||
}
|
||||
|
||||
void debug(Grid *g)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < g->nbl; i++)
|
||||
{
|
||||
printf("%s\n", g->grid[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int compute_size(int w, int h) {
|
||||
int size_width = w / NBC;
|
||||
int size_height = h / NBL;
|
||||
int compute_size(Grid *g, int w, int h)
|
||||
{
|
||||
int size_width = w / g->nbc;
|
||||
int size_height = h / g->nbl;
|
||||
return (size_width < size_height) ? size_width : size_height;
|
||||
}
|
||||
|
||||
|
||||
void draw_grid(char grid[NBL][NBC+1]) {
|
||||
void draw_grid(Grid *g)
|
||||
{
|
||||
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);
|
||||
int cell_size = compute_size(g, 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 < g->nbl; i++)
|
||||
{
|
||||
for (j = 0; j < g->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 (g->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(Grid *g, struct SnakeStruct *snake)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < SNAKE_SIZE; i++)
|
||||
{
|
||||
Coord part = snake->pos[i];
|
||||
g->grid[part.y][part.x] = 's';
|
||||
}
|
||||
}
|
||||
|
||||
Element move_snake(struct SnakeStruct *snake, Grid *g)
|
||||
{
|
||||
Coord tail = snake->pos[SNAKE_SIZE - 1];
|
||||
Coord head;
|
||||
Element element_at_head;
|
||||
|
||||
g->grid[tail.y][tail.x] = EMPTY;
|
||||
|
||||
crawl(snake, g);
|
||||
|
||||
head = snake->pos[0];
|
||||
element_at_head = g->grid[head.y][head.x];
|
||||
|
||||
g->grid[head.y][head.x] = SNAKE;
|
||||
|
||||
return element_at_head;
|
||||
}
|
||||
|
||||
int count_nb_lines(FILE *stream)
|
||||
{
|
||||
int count = 0;
|
||||
char buffer[256];
|
||||
while (fgets(buffer, sizeof(buffer), stream))
|
||||
{
|
||||
count++;
|
||||
}
|
||||
rewind(stream);
|
||||
return count;
|
||||
}
|
||||
|
||||
int count_fruits(FILE *stream, Grid *g)
|
||||
{
|
||||
int i, j, nb_fruit = 0;
|
||||
|
||||
for (i = 0; i < g->nbl; i++)
|
||||
{
|
||||
if (!fgets(g->grid[i], g->nbc + 2, stream))
|
||||
{
|
||||
fprintf(stderr, "Error: Stream does not contain enough lines\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
g->grid[i][strcspn(g->grid[i], "\n")] = '\0';
|
||||
|
||||
for (j = 0; j < g->nbc; j++)
|
||||
{
|
||||
if (g->grid[i][j] == FRUIT)
|
||||
{
|
||||
nb_fruit++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nb_fruit;
|
||||
}
|
||||
|
||||
void copy(const char *src, char *dst)
|
||||
{
|
||||
int i = 0;
|
||||
while (src[i] != '\0' && src[i] != '\n')
|
||||
{
|
||||
dst[i] = src[i];
|
||||
i++;
|
||||
}
|
||||
dst[i] = '\0';
|
||||
}
|
||||
33
grid.h
33
grid.h
@@ -1,19 +1,36 @@
|
||||
#ifndef GRID_H
|
||||
#define GRID_H
|
||||
|
||||
#include <MLV/MLV_all.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define NBL 22
|
||||
#define NBC 36
|
||||
struct SnakeStruct;
|
||||
|
||||
typedef enum {
|
||||
struct GridStruct
|
||||
{
|
||||
char **grid;
|
||||
int nbl;
|
||||
int nbc;
|
||||
};
|
||||
|
||||
typedef struct GridStruct Grid;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
WALL = 'w',
|
||||
EMPTY = ' ',
|
||||
FRUIT = 'f'
|
||||
FRUIT = 'f',
|
||||
SNAKE = 's'
|
||||
} Element;
|
||||
|
||||
void debug(char grid[NBL][NBC+1]);
|
||||
int compute_size(int w, int h);
|
||||
void draw_grid(char grid[NBL][NBC+1]);
|
||||
Grid* allocate_grid(int n, int m);
|
||||
void free_grid(Grid *g);
|
||||
void debug(Grid *g);
|
||||
int compute_size(Grid *g, int w, int h);
|
||||
void draw_grid(Grid *g);
|
||||
void place_snake(Grid *g, struct SnakeStruct *snake);
|
||||
Element move_snake(struct SnakeStruct *snake, Grid *g);
|
||||
int count_nb_lines(FILE *stream);
|
||||
int count_fruits(FILE *stream, Grid *g);
|
||||
void copy(const char *src, char *dst);
|
||||
|
||||
#endif /* GRID_H */
|
||||
22
levels/default
Normal file
22
levels/default
Normal file
@@ -0,0 +1,22 @@
|
||||
w w
|
||||
|
||||
f
|
||||
|
||||
f f
|
||||
|
||||
|
||||
f
|
||||
|
||||
|
||||
wwwwwwwwww
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
f
|
||||
|
||||
f f
|
||||
|
||||
f
|
||||
w w
|
||||
30
snake.c
Normal file
30
snake.c
Normal file
@@ -0,0 +1,30 @@
|
||||
#include "grid.h"
|
||||
#include "snake.h"
|
||||
|
||||
void crawl(Snake *snake, Grid *g)
|
||||
{
|
||||
int i;
|
||||
Coord new_head = snake->pos[0];
|
||||
switch (snake->dir)
|
||||
{
|
||||
case LEFT:
|
||||
new_head.x = (new_head.x - 1 + g->nbc) % g->nbc;
|
||||
break;
|
||||
case RIGHT:
|
||||
new_head.x = (new_head.x + 1) % g->nbc;
|
||||
break;
|
||||
case TOP:
|
||||
new_head.y = (new_head.y - 1 + g->nbl) % g->nbl;
|
||||
break;
|
||||
case BOTTOM:
|
||||
new_head.y = (new_head.y + 1) % g->nbl;
|
||||
break;
|
||||
}
|
||||
|
||||
for (i = SNAKE_SIZE - 1; i > 0; i--)
|
||||
{
|
||||
snake->pos[i] = snake->pos[i - 1];
|
||||
}
|
||||
|
||||
snake->pos[0] = new_head;
|
||||
}
|
||||
32
snake.h
Normal file
32
snake.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef SNAKE_H
|
||||
#define SNAKE_H
|
||||
|
||||
struct GridStruct;
|
||||
|
||||
#define SNAKE_SIZE 4
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
} Coord;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
TOP,
|
||||
BOTTOM,
|
||||
LEFT,
|
||||
RIGHT
|
||||
} Direction;
|
||||
|
||||
struct SnakeStruct
|
||||
{
|
||||
Coord pos[SNAKE_SIZE];
|
||||
Direction dir;
|
||||
};
|
||||
|
||||
typedef struct SnakeStruct Snake;
|
||||
|
||||
void crawl(Snake *snake, struct GridStruct *g);
|
||||
|
||||
#endif /* SNAKE_H */
|
||||
Reference in New Issue
Block a user