Implement a Keyboard-Controlled Console Maze Game in C
Required Header Files
#include <stdio.h>
#include <getch.h>
#include <stdlib.h>
#include <time.h>
stdio.h: Proivdes standard input and output functions for rendering the maze and printing status messages.getch.h: Offers unbuffered, non-echoing single character input to detect arrow key presses with out requiirng the user to press enter.stdlib.h: Supplies system call functions likesystem("clear")to refresh the terminal screen between game frames.time.h: Used to track elapsed play time from game start too completion.
Maze Grid Initialization
The game uses a 10x10 2D character array to represent the maze layout:
char labyrinth[10][10] = {
{'#','#','#','#','#','#','#','#','#','#'},
{'#','@','#','#','#','#','#','#',' ','#'},
{'#',' ','#',' ',' ',' ',' ',' ',' ','#'},
{'#',' ','#',' ','#',' ','#','#',' ','#'},
{'#',' ',' ',' ','#','#',' ','#',' ','#'},
{'#',' ','#','#','#',' ',' ',' ',' ','#'},
{'#',' ','#',' ','#',' ','#','#','#','#'},
{'#',' ','#',' ','#',' ',' ',' ',' ',' '},
{'#',' ',' ',' ','#','#','#','#','#','#'},
{'#','#','#','#','#','#','#','#','#','#'}
};
Grid character definitions:
#: Impassable wall(whitespace): Traversable path@: Player character avatar
Game State Variables
int player_x = 1, player_y = 1;
time_t game_start_ts = time(NULL);
player_xandplayer_y: Store the current coordinates of the player character, initialized to the maze entrance at (1,1).game_start_ts: Records the Unix timestamp when the game starts, used to calcluate total play time on completion.
Core Game Loop
The infinite loop runs until the player reaches the maze exit:
while(1) {
system("clear");
for(int row = 0; row < 10; row++) {
for(int col = 0; col < 10; col++) {
printf("%c ", labyrinth[row][col]);
}
putchar('\n');
}
if(player_x == 7 && player_y == 9) {
printf("Maze completed! Total play time: %u seconds\n", (unsigned int)(time(NULL) - game_start_ts));
break;
}
switch(getch()) {
case 183:
if(player_x > 0 && labyrinth[player_x - 1][player_y] == ' ') {
labyrinth[player_x][player_y] = ' ';
labyrinth[--player_x][player_y] = '@';
}
break;
case 184:
if(player_x < 9 && labyrinth[player_x + 1][player_y] == ' ') {
labyrinth[player_x][player_y] = ' ';
labyrinth[++player_x][player_y] = '@';
}
break;
case 185:
if(player_y < 9 && labyrinth[player_x][player_y + 1] == ' ') {
labyrinth[player_x][player_y] = ' ';
labyrinth[player_x][++player_y] = '@';
}
break;
case 186:
if(player_y > 0 && labyrinth[player_x][player_y - 1] == ' ') {
labyrinth[player_x][player_y] = ' ';
labyrinth[player_x][--player_y] = '@';
}
break;
}
}
The loop first clears and redraws the maze every frame, then checks for the win condition. It then reads user input to move the player, only allowing movement into empty path tiles and blocking movement through walls or out of the maze bounds.
Full Compilable Code
#include <stdio.h>
#include <getch.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, const char* argv[]) {
char labyrinth[10][10] = {
{'#','#','#','#','#','#','#','#','#','#'},
{'#','@','#','#','#','#','#','#',' ','#'},
{'#',' ','#',' ',' ',' ',' ',' ',' ','#'},
{'#',' ','#',' ','#',' ','#','#',' ','#'},
{'#',' ',' ',' ','#','#',' ','#',' ','#'},
{'#',' ','#','#','#',' ',' ',' ',' ','#'},
{'#',' ','#',' ','#',' ','#','#','#','#'},
{'#',' ','#',' ','#',' ',' ',' ',' ',' '},
{'#',' ',' ',' ','#','#','#','#','#','#'},
{'#','#','#','#','#','#','#','#','#','#'}
};
int player_x = 1, player_y = 1;
time_t game_start_ts = time(NULL);
while(1) {
system("clear");
for(int row = 0; row < 10; row++) {
for(int col = 0; col < 10; col++) {
printf("%c ", labyrinth[row][col]);
}
putchar('\n');
}
if(player_x == 7 && player_y == 9) {
printf("Maze completed! Total play time: %u seconds\n", (unsigned int)(time(NULL) - game_start_ts));
break;
}
switch(getch()) {
case 183:
if(player_x > 0 && labyrinth[player_x - 1][player_y] == ' ') {
labyrinth[player_x][player_y] = ' ';
labyrinth[--player_x][player_y] = '@';
}
break;
case 184:
if(player_x < 9 && labyrinth[player_x + 1][player_y] == ' ') {
labyrinth[player_x][player_y] = ' ';
labyrinth[++player_x][player_y] = '@';
}
break;
case 185:
if(player_y < 9 && labyrinth[player_x][player_y + 1] == ' ') {
labyrinth[player_x][player_y] = ' ';
labyrinth[player_x][++player_y] = '@';
}
break;
case 186:
if(player_y > 0 && labyrinth[player_x][player_y - 1] == ' ') {
labyrinth[player_x][player_y] = ' ';
labyrinth[player_x][--player_y] = '@';
}
break;
}
}
return 0;
}