Skip to content

Commit

Permalink
updates to maze game
Browse files Browse the repository at this point in the history
See Issue #87
  • Loading branch information
MJoergen committed Oct 18, 2020
1 parent e863218 commit fb3ce8b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
41 changes: 26 additions & 15 deletions c/test_programs/maze2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,22 @@ static void draw_menu()
}
clrscr();
int row = 5;
cputsxy(1, row, "Welcome to this aMAZEing game!\0", color);
cputsxy(1, row+2, "Press g to generate a new maze.\0", color);
cputsxy(1, row+3, "Press 123 to change the level of the game.\0", color);
cputsxy(1, row+4, "Move around with the keys WASD / HJKL / arrows.\0", color);
cputsxy(1, row+5, "Press r to reset the current maze.\0", color);
cputsxy(1, row+6, "Press x to get a hint.\0", color);
cputsxy(1, row+7, "Press q to quit the game.\0", color);
cputsxy(1, row+8, "Press m to return to this menu.\0", color);
cputsxy(1, row++, "Welcome to this aMAZEing game!\0", color);
row++;
cputsxy(1, row++, "You are trapped in the maze.", color);
cputsxy(1, row++, "Find your way to the * to escape!", color);
cputsxy(1, row++, "* In Level 1, you know where you are.", color);
cputsxy(1, row++, "* In Level 2, you can still remember the path you walked.", color);
cputsxy(1, row++, "* But in level 3, only a dim torch is illuminating the direct surrounding.", color);
row++;
cputsxy(1, row++, "Don't be ashamed to ask for help and press 'x' in level 2 and level 3.", color);
row++;
cputsxy(1, row++, "Press g to generate a new maze.\0", color);
cputsxy(1, row++, "Press 123 to change the level of the game.\0", color);
cputsxy(1, row++, "Move around with the keys WASD / HJKL / arrows.\0", color);
cputsxy(1, row++, "Press r to reset the current maze.\0", color);
cputsxy(1, row++, "Press q to quit the game.\0", color);
cputsxy(1, row++, "Press m to return to this menu.\0", color);
} // end of draw_menu


Expand Down Expand Up @@ -105,23 +113,21 @@ static void draw_ending()

static int game_update()
{
MMIO(VGA_STATE) &= ~VGA_EN_HW_CURSOR; // Hide cursor

switch (gameState)
{
case MENU:
draw_menu();
break;
case PLAYING:
if (level == 3)
clrscr();
maze_draw(get_color(), get_mask());
break;
case GAME_OVER:
maze_draw(get_color(), get_mask());
draw_ending();
break;
}
MMIO(VGA_STATE) |= VGA_EN_HW_CURSOR; // Enable cursor

int ch;
do
{
Expand Down Expand Up @@ -155,10 +161,11 @@ static int game_update()

case 'w' : case 'k' : case KBD_CUR_UP : if (gameState == PLAYING) {if (maze_move(DIR_NORTH)) gameState = GAME_OVER;} break;
case 's' : case 'j' : case KBD_CUR_DOWN : if (gameState == PLAYING) {if (maze_move(DIR_SOUTH)) gameState = GAME_OVER;} break;
case 'd' : case 'l' : case KBD_CUR_RIGHT : if (gameState == PLAYING) {if (maze_move(DIR_EAST)) gameState = GAME_OVER;} break;
case 'a' : case 'h' : case KBD_CUR_LEFT : if (gameState == PLAYING) {if (maze_move(DIR_WEST)) gameState = GAME_OVER;} break;
case 'd' : case 'l' : case KBD_CUR_RIGHT : if (gameState == PLAYING) {if (maze_move(DIR_EAST)) gameState = GAME_OVER;} break;
case 'a' : case 'h' : case KBD_CUR_LEFT : if (gameState == PLAYING) {if (maze_move(DIR_WEST)) gameState = GAME_OVER;} break;

case 'q' : cputsxy(1, 38, "GOODBYE!\0", 0);
case 'q' : clrscr();
cputsxy(30, 18, "GOODBYE!\0", 0);
return 1; // End the game.
}

Expand All @@ -168,6 +175,8 @@ static int game_update()

int main()
{
MMIO(VGA_STATE) &= ~VGA_EN_HW_CURSOR; // Hide cursor

qmon_srand(time()); // Seed random number generator.
level = 1;

Expand All @@ -178,6 +187,8 @@ int main()
break;
}

MMIO(VGA_STATE) |= VGA_EN_HW_CURSOR; // Enable cursor

return 0;
} // end of main

14 changes: 10 additions & 4 deletions c/test_programs/maze_grid.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ static int GetRandomSquare()
return qmon_rand() % MAX_SQUARES;
}

// This swaps the foreground and background color.
static int invert(int color)
{
return (((color&0x0F00) << 4) + ((color&0xF000) >> 4)) ^ 0x8800;
}

// Draw the current square as 3x3 characters.
static void maze_drawPos(int sq, int color, int mask)
{
Expand All @@ -52,9 +58,9 @@ static void maze_drawPos(int sq, int color, int mask)
cputcxy(col, row+1, color + ((g&(1<<DIR_WEST)) ? ' ' : wall));
cputcxy(col+1, row+2, color + ((g&(1<<DIR_SOUTH)) ? ' ' : wall));
if (sq == endSq)
cputcxy(col+1, row+1, color + '*');
cputcxy(col+1, row+1, invert(color) + '*');
else if (sq == curSq)
cputcxy(col+1, row+1, color + '@');
cputcxy(col+1, row+1, invert(color) + '@');
else
cputcxy(col+1, row+1, color + ' ');
}
Expand Down Expand Up @@ -144,14 +150,14 @@ void maze_init()
do
{
curSq = GetRandomSquare();
} while (GetRow(curSq) < MAX_ROWS/2 || GetCol(curSq) < MAX_COLS/2);
} while (GetRow(curSq) < 3*MAX_ROWS/4 || GetCol(curSq) < 3*MAX_COLS/4);
grid[curSq] |= 1<<VISITED;

// Select an ending square near the top left corner
do
{
endSq = GetRandomSquare();
} while (GetRow(endSq) >= MAX_ROWS/2 || GetCol(endSq) >= MAX_COLS/2);
} while (GetRow(endSq) >= MAX_ROWS/4 || GetCol(endSq) >= MAX_COLS/4);
} // end of maze_init


Expand Down

0 comments on commit fb3ce8b

Please sign in to comment.