#include #define MODE_0 0x0 #define MODE_1 0x1 #define MODE_2 0x2 #define MODE_3 0x3 #define BG2_ENABLE 0x400 #define RGB(r,g,b) (unsigned short)(r + (g << 5) + (b << 10)) #define WHITE 0xFFFF #define BLACK 0x0000 #define RED RGB(255, 0, 0) #define GREEN RGB(0, 255, 0) #define BLUE RGB(0, 0, 255) MULTIBOOT const unsigned short NUM_ROWS = 16, NUM_COLS = 24, CELL_SIZE = 10; const unsigned short BACKGROUND_PATH = 0, BACKGROUND_WALL = 1; const unsigned short BACKGROUND_PATH_COLOUR = WHITE, BACKGROUND_WALL_COLOUR = BLACK; unsigned short* videoBuffer = (unsigned short*)0x6000000; unsigned short maze[16][24] = { {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,0,0,0,0,1,0,1,1,1,1,0,0,0,0,1,1,0,0,0,0}, {1,0,1,1,1,1,1,0,0,0,0,1,1,0,0,1,1,0,0,0,0,1,0,1}, {1,0,1,0,0,0,1,0,1,1,0,1,0,0,1,1,1,1,1,1,1,1,0,1}, {1,0,1,0,1,0,1,1,1,1,0,1,0,1,1,0,0,0,0,0,0,1,0,1}, {1,0,1,1,1,0,1,0,0,1,0,0,0,0,1,0,1,0,1,1,1,1,0,1}, {1,0,0,0,0,0,1,0,1,1,1,1,1,1,1,0,1,0,0,0,0,1,1,1}, {1,1,1,1,0,1,1,0,0,0,0,1,0,0,0,0,1,0,1,1,0,0,0,1}, {1,0,0,0,0,0,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,0,1}, {1,0,1,1,1,0,0,0,0,1,0,1,0,1,0,0,0,0,0,0,1,1,0,1}, {1,0,0,0,0,0,1,1,0,0,0,0,0,1,0,1,1,1,1,0,0,0,0,1}, {1,1,1,1,1,0,1,1,1,1,1,1,0,1,0,0,0,0,1,1,1,1,0,1}, {1,0,0,0,0,0,0,1,0,0,1,0,0,1,0,1,0,1,1,1,0,0,0,1}, {1,1,0,1,1,1,0,1,1,0,1,0,1,1,0,1,0,0,0,1,1,1,0,1}, {0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}}; unsigned short gameStartRow = 14, gameStartCol = 0, gameEndRow = 1, gameEndCol = 23; unsigned short currentRow, currentCol; void clearScreen (); void displayEndGame (); void drawPixel (int x, int y, unsigned short colour); void drawPlayer (); void drawTile (int row, int col); void rectFill (int startX, int startY, int endX, int endY, unsigned short colour); void clearScreen () { rectFill(0, 0, 240, 160, WHITE); } void displayEndGame () { clearScreen(); } void drawPixel (int x, int y, unsigned short colour) { videoBuffer[y * 240 + x] = colour; } void drawPlayer () { rectFill(currentCol * CELL_SIZE, currentRow * CELL_SIZE, ((currentCol + 1) * CELL_SIZE), ((currentRow + 1) * CELL_SIZE), BLUE); } void drawTile (int row, int col) { unsigned short colour; if (maze[row][col] == BACKGROUND_PATH) { colour = BACKGROUND_PATH_COLOUR; } else { colour = BACKGROUND_WALL_COLOUR; } rectFill((col * CELL_SIZE), (row * CELL_SIZE), ((col + 1) * CELL_SIZE), ((row + 1) * CELL_SIZE), colour); } void rectFill (int startX, int startY, int endX, int endY, unsigned short colour) { int row, col; for (row = startX; row < endX; row++) { for (col = startY; col < endY; col++) { drawPixel(row, col, colour); } } } int main () { REG_DISPCNT = (MODE_3 | BG2_ENABLE); int row, col, lastRow, lastCol; int keyPressed = 0, gameWon = 0; for (row = 0; row < NUM_ROWS; row++) { for (col = 0; col < NUM_COLS; col++) { drawTile(row, col); } } currentRow = gameStartRow; currentCol = gameStartCol; drawPlayer(); while (1) { if (gameWon) { continue; } lastRow = currentRow; lastCol = currentCol; if (F_CTRLINPUT_UP_PRESSED) { if (!keyPressed && currentRow > 0 && maze[currentRow - 1][currentCol] == BACKGROUND_PATH) { currentRow--; } } else if (F_CTRLINPUT_DOWN_PRESSED) { if (!keyPressed && currentRow < (NUM_ROWS - 1) && maze[currentRow + 1][currentCol] == BACKGROUND_PATH) { currentRow++; } } else if (F_CTRLINPUT_LEFT_PRESSED) { if (!keyPressed && currentCol > 0 && maze[currentRow][currentCol - 1] == BACKGROUND_PATH) { currentCol--; } } else if (F_CTRLINPUT_RIGHT_PRESSED) { if (!keyPressed && currentCol < (NUM_COLS - 1) && maze[currentRow][currentCol + 1] == BACKGROUND_PATH) { currentCol++; } } else { keyPressed = 0; } if (!(lastRow == currentRow && lastCol == currentCol)) { drawTile(lastRow, lastCol); drawPlayer(); keyPressed = 1; if (currentRow == gameEndRow && currentCol == gameEndCol) { displayEndGame(); gameWon = 1; } } } return 0; } /* END OF FILE */