forked from snapshot112/minigame-menu
Fixed maze runner
This commit is contained in:
@@ -45,25 +45,26 @@ static grid *get_maze(void) {
|
||||
* bepaalde richting probeert te bewegen.
|
||||
* Input:
|
||||
* gp: een pointer naar het rooster
|
||||
* dx,dy: de richting waarin de speler probeert te bewegen. De vier mogelijk-
|
||||
* offset_vector: de richting waarin de speler probeert te bewegen. De vier mogelijk-
|
||||
* heden voor (dx,dy) zijn (-1,0), (1,0), (0,-1), (0,1) voor resp.
|
||||
* links, rechts, omhoog en omlaag.
|
||||
*
|
||||
* Side effect: het rooster wordt aangepast op basis van de handeling van
|
||||
* de speler.
|
||||
*/
|
||||
static void maze_runner_move(grid *gp, const int dx, const int dy) {
|
||||
static void maze_runner_move(grid *gp, const coordinate offset_vector) {
|
||||
const coordinate player_position = grid_find(gp, LIVING_PLAYER);
|
||||
|
||||
if (player_position.y == -1) {
|
||||
printf("Player not found!");
|
||||
// State begin for the hackerman ending screen.
|
||||
grid_put_state(gp, STATE_BEGIN);
|
||||
return;
|
||||
}
|
||||
|
||||
coordinate new_player_position = player_position;
|
||||
new_player_position.x += dx;
|
||||
new_player_position.y += dy;
|
||||
new_player_position.x += offset_vector.x;
|
||||
new_player_position.y += offset_vector.y;
|
||||
|
||||
if (grid_contains(gp, new_player_position) == 1) {
|
||||
const char new_location = grid_fetch(gp, new_player_position);
|
||||
@@ -79,7 +80,7 @@ static void maze_runner_move(grid *gp, const int dx, const int dy) {
|
||||
break;
|
||||
case EMPTY:
|
||||
update_grid(gp, EMPTY, player_position);
|
||||
update_grid(gp, LIVING_PLAYER, player_position);
|
||||
update_grid(gp, LIVING_PLAYER, new_player_position);
|
||||
break;
|
||||
case MAZE_EXIT:
|
||||
update_grid(gp, EMPTY, player_position);
|
||||
@@ -101,33 +102,35 @@ static void maze_runner_move(grid *gp, const int dx, const int dy) {
|
||||
* Het rooster wordt ge-updated afhankelijk van de user input.
|
||||
*/
|
||||
static void speel_maze(grid *gp) {
|
||||
coordinate offset_vector = {.x = 0, .y = 0};
|
||||
switch (getch()) {
|
||||
case KEY_UP: // fallthrough
|
||||
case 'w':
|
||||
case 'W':
|
||||
maze_runner_move(gp, 0, -1);
|
||||
offset_vector.y--;
|
||||
break;
|
||||
case KEY_DOWN: // fallthrough
|
||||
case 's':
|
||||
case 'S':
|
||||
maze_runner_move(gp, 0, 1);
|
||||
offset_vector.y++;
|
||||
break;
|
||||
case KEY_LEFT: // fallthrough
|
||||
case 'a':
|
||||
case 'A':
|
||||
maze_runner_move(gp, -1, 0);
|
||||
offset_vector.x--;
|
||||
break;
|
||||
case KEY_RIGHT: // fallthrough
|
||||
case 'd':
|
||||
case 'D':
|
||||
maze_runner_move(gp, 1, 0);
|
||||
offset_vector.x++;
|
||||
break;
|
||||
case 'p':
|
||||
case KEY_BACKSPACE:
|
||||
case KEY_ESCAPE:
|
||||
grid_put_state(gp, STATE_QUIT);
|
||||
break;
|
||||
return;
|
||||
}
|
||||
maze_runner_move(gp, offset_vector);
|
||||
}
|
||||
|
||||
void maze_runner(void) {
|
||||
|
||||
@@ -23,6 +23,7 @@ typedef enum {
|
||||
GAME_QUIT = 4,
|
||||
} game;
|
||||
|
||||
static grid *MENU;
|
||||
static game SELECTED_GAME = GAME_MANUAL;
|
||||
static coordinate OFFSET = {.x = 5, .y = 5};
|
||||
|
||||
@@ -34,6 +35,7 @@ static coordinate OFFSET = {.x = 5, .y = 5};
|
||||
* game: The game you want to launch.
|
||||
*/
|
||||
static void launch_game(const game game) {
|
||||
clear();
|
||||
switch (game) {
|
||||
case GAME_MANUAL:
|
||||
manual((coordinate){0,0});
|
||||
@@ -88,10 +90,10 @@ static void menu_highlight(const grid *menu) {
|
||||
* Side Effects:
|
||||
* Displays the menu
|
||||
*/
|
||||
static void show_menu(const grid *menu) {
|
||||
static void show_menu() {
|
||||
erase();
|
||||
show_grid_on_offset(menu, OFFSET);
|
||||
menu_highlight(menu);
|
||||
show_grid_on_offset(MENU, OFFSET);
|
||||
menu_highlight(MENU);
|
||||
refresh();
|
||||
}
|
||||
|
||||
@@ -121,7 +123,6 @@ static void menu_move(const int offset) {
|
||||
* 0: Continue running.
|
||||
* 1: Exit the menu.
|
||||
*
|
||||
*
|
||||
* Side Effect:
|
||||
* Changes the SELECTED_GAME as needed and launches selected games on select.
|
||||
*/
|
||||
@@ -156,28 +157,25 @@ static int navigate_menu(void) {
|
||||
|
||||
/*
|
||||
* Create the menu grid.
|
||||
*
|
||||
* Output:
|
||||
* A pointer to the menu grid.
|
||||
*/
|
||||
static grid *initialize_menu(void) {
|
||||
char menu[] = "How to play\n"
|
||||
static void initialize_menu(void) {
|
||||
const char menu[] = "How to play\n"
|
||||
"Maze Runner\n"
|
||||
" Snake \n"
|
||||
"Minesweeper\n"
|
||||
" Leave \n";
|
||||
grid *rp = grid_create_from_string(menu);
|
||||
return rp;
|
||||
MENU = grid_create_from_string(menu);
|
||||
}
|
||||
|
||||
void minigame_menu(void) {
|
||||
grid *menu = initialize_menu();
|
||||
initialize_menu();
|
||||
|
||||
while (true) {
|
||||
show_menu();
|
||||
if (navigate_menu() == 1) {
|
||||
show_menu(menu);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
grid_cleanup(menu);
|
||||
grid_cleanup(MENU);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user