Skip to content
Snippets Groups Projects
Commit f03cf17b authored by YANG TIANYI's avatar YANG TIANYI
Browse files

Mode console et mode graphique

parent 66e60ea4
Branches
No related merge requests found
/**
* @file grid.c
* @author ARMAGAN-YANG
*
*
*
*
* @fn void init_level
* @brief Initialise le niveau avec les informations contenues dans le fichier spécifié
* @param file_path Le chemin du fichier à lire
......@@ -18,8 +18,8 @@
* @brief Libèration de la mémoire allouée pour la grille de jeu
* @param g Pointeur vers la grille de jeu
* @return Rien
*
*
*
*
* @date 03/2023
* @copyright Copyright (c) 2023
*/
......@@ -44,7 +44,7 @@ void init_level(const char *file_path, struct Grid *g)
// on lit la première ligne du fichier
fgets(line, 100, file);
sscanf(line, "%d %d %d", &number_column, &number_row, &number_goals);
g->column_number = number_column; // remplir grid* g
g->column_number = number_column; // remplir grid* g
g->row_number = number_row;
g->game_grid = malloc(g->row_number * sizeof(enum CaseType *));
for (int i = 0; i < g->row_number; i++)
......@@ -52,7 +52,7 @@ void init_level(const char *file_path, struct Grid *g)
g->game_grid[i] = malloc(g->column_number * sizeof(enum CaseType *));
}
int current_row = 0;
int current_goal = 0;
// On lit le fichier ligne par ligne jusqu'à la fin du fichier
while (fgets(line, 100, file) != NULL)
......@@ -62,14 +62,14 @@ void init_level(const char *file_path, struct Grid *g)
while (*buffer && *buffer != '\n')
{
g->game_grid[current_row][current_column] = *buffer;
if (*buffer == '@') // set current position
if (*buffer == '@') // set current position
{
g->position.x = current_row; // x = ligne
g->position.x = current_row; // x = ligne
g->position.y = current_column; // y = colonne
}
if (*buffer == '.')
{
current_goal++;
current_goal++;
}
current_column += 1;
buffer += 1;
......@@ -94,6 +94,7 @@ void display(struct Grid *g)
}
}
void freetab(struct Grid *g)
{
for (int i = 0; i < g->row_number; i++)
......
/**
* @file main.c
* @author ARMAGAN-YANG
*
*
*
*
* @fn int main
* @brief Initialisation de la grille à partir du fichier "level1.txt"
* et on affiche la grille initiale et on deplace le joueur a l'aide des touches z,q,s,d
* Quand la touche 'a' est saisie, c'est la fin de la boucle et la mémoire allouée est libérée pour la grille
* @return 0 si pas d'erreur
*
*
*
*
* @date 03/2023
* @copyright Copyright (c) 2023
*/
#include "grid.h"
#include "player.h"
#include "sdl2.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main(void)
int main(int argc, char *argv[])
{
void (*handle_init)();
Event (*handle_event)(void);
void (*handle_display)(struct Grid * grid);
int ModeGraphique = 1, ModeConsole = 1;
if (argc > 1)
{
ModeConsole = strcmp(argv[1], "--console");
ModeGraphique = strcmp(argv[1], "--sdl2");
}
if (argc == 1 || ModeConsole == 0)
{
handle_init = console_init;
handle_event = event;
handle_display = display;
}
else if (ModeGraphique == 0)
{
handle_init = sdl_init;
handle_event = event_sdl2;
handle_display = display_sdl2;
}
else
{
printf("Les commandes correctes sont:\n --sdl2 (mode graphiques)\n --console (mode console)\n");
exit(EXIT_FAILURE);
}
struct Grid g;
init_level("level1.txt", &g);
display(&g);
handle_init();
bool run = true;
while (run) // z pour top, q pour gauch, s pour bottom, d pour droite, a pour quitter le jeu
while (run) // z pour top, q pour gauch, s pour bottom, d pour droite, a pour quitter le jeu console
{
char entry = fgetc(stdin);
switch (entry)
handle_display(&g);
Event action = handle_event();
switch (action)
{
case 'z':
case NON:
break;
case UP:
{
move_player(&g, entry);
display(&g);
move_player(&g, top);
handle_display(&g);
break;
}
case 'q':
case LEFT:
{
move_player(&g, entry);
display(&g);
move_player(&g, left);
handle_display(&g);
break;
}
case 's':
case DOWN:
{
move_player(&g, entry);
display(&g);
move_player(&g, bottom);
handle_display(&g);
break;
}
case 'd':
case RIGHT:
{
move_player(&g, entry);
display(&g);
move_player(&g, right);
handle_display(&g);
break;
}
case 'a':
case QUIT:
{
run = false;
break;
}
}
if(g.box_goal_number == g.goal_number)
if (g.box_goal_number == g.goal_number)
run = false;
}
freetab(&g);
return 0;
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment