#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 = 10, NUM_COLS = 15, CELL_SIZE = 16, NUM_LEVELS = 3; const unsigned short BACKGROUND_PATH = 0, BACKGROUND_HOLE = 1, BACKGROUND_WALL = 2, BACKGROUND_BORDER = 3; const unsigned short OBJECT_PLAYER = 0, OBJECT_BOX = 1, OBJECT_NOTHING = 2; const unsigned short BACKGROUNDS[4] = {RGB(168, 171, 169), BLACK, RED, RGB(16, 91, 41)}; const unsigned short OBJECTS[2] = {BLUE, RGB(124, 25, 208)}; unsigned short numBoxes, currentRow, currentCol, currentLevel, gameWon; unsigned short* videoBuffer = (unsigned short*)0x6000000; unsigned short mapBackground[10][15], mapObjects[10][15]; unsigned short levelBackground[30][15] = { {3,3,3,2,2,2,2,2,2,2,2,2,2,3,3}, {3,3,3,2,2,0,0,0,0,0,0,2,2,3,3}, {3,3,3,2,2,0,2,0,2,1,0,2,2,3,3}, {3,3,3,2,2,0,0,0,0,0,0,2,2,3,3}, {3,3,3,2,2,0,2,0,2,0,0,0,2,3,3}, {3,3,3,2,2,0,2,0,2,0,2,0,2,3,3}, {3,3,3,2,2,0,2,0,2,0,1,0,2,3,3}, {3,3,3,2,2,0,0,0,0,0,0,0,2,3,3}, {3,3,3,2,2,2,2,2,2,2,0,0,2,3,3}, {3,3,3,2,2,2,2,2,2,2,2,2,2,3,3}, {3,3,3,3,3,3,3,3,3,3,3,3,3,3,3}, //END OF LEVEL 1 {3,3,3,2,2,2,2,2,2,2,2,2,2,3,3}, {3,3,3,2,1,0,0,0,0,0,0,1,2,3,3}, {3,3,3,2,2,0,2,0,0,2,0,2,2,3,3}, {3,3,3,2,1,0,0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2,2,2}, {2,2,2,2,2,2,2,2,1,2,2,2,2,2,2}, {2,2,2,2,0,2,2,2,2,2,2,2,2,2,2}, {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}, {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}, {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}, {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},//END OF LEVEL 2 {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}, {2,2,2,2,2,2,2,1,2,2,2,2,2,2,2}, {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}, {2,2,2,2,2,2,2,2,2,2,2,2,1,2,2}, {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}, {2,2,2,2,1,2,2,2,2,2,2,2,2,2,2}, {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}, {2,2,2,2,0,2,2,1,2,2,2,2,2,2,2}, {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}};//END OF LEVEL 3 void clearScreen (); void drawPixel (int x, int y, unsigned short colour); void drawObject (int row, int col); void drawTile (int row, int col); void rectFill (int startX, int startY, int endX, int endY, unsigned short colour); void setupLevel (); void clearScreen () { rectFill(0, 0, 240, 160, WHITE); } void drawPixel (int x, int y, unsigned short colour) { videoBuffer[y * 240 + x] = colour; } void drawObject (int row, int col) { if (mapObjects[row][col] == OBJECT_PLAYER) { rectFill(col * CELL_SIZE, row * CELL_SIZE, ((col + 1) * CELL_SIZE), ((row + 1) * CELL_SIZE), OBJECTS[OBJECT_PLAYER]); } else if (mapObjects[row][col] == OBJECT_BOX) { rectFill(((col * CELL_SIZE) + 2), ((row * CELL_SIZE) + 2), (((col + 1) * CELL_SIZE) - 2), (((row + 1) * CELL_SIZE) - 2), OBJECTS[OBJECT_BOX]); } } void drawTile (int row, int col) { rectFill((col * CELL_SIZE), (row * CELL_SIZE), ((col + 1) * CELL_SIZE), ((row + 1) * CELL_SIZE), BACKGROUNDS[mapBackground[row][col]]); } 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); } } } void setupLevel () { if (currentLevel >= NUM_LEVELS) { clearScreen(); gameWon = 1; return; } int row, col, catchRow = (NUM_ROWS * currentLevel); clearScreen(); numBoxes = 0; for (row = 0; row < NUM_ROWS; row++) { for (col = 0; col < NUM_COLS; col++) { mapBackground[row][col] = levelBackground[catchRow + row][col]; mapObjects[row][col] = levelObjects[catchRow + row][col]; drawTile(row, col); if (mapObjects[row][col] == OBJECT_PLAYER) { currentRow = row; currentCol = col; } else if (mapObjects[row][col] == OBJECT_BOX) { numBoxes++; } else { continue; } drawObject(row, col); } } } int main () { REG_DISPCNT = (MODE_3 | BG2_ENABLE); int lastRow, lastCol; int keyPressed = 0; currentLevel = 0; gameWon = 0; setupLevel(); while (1) { if (gameWon) { continue; } lastRow = currentRow; lastCol = currentCol; if (F_CTRLINPUT_A_PRESSED) { currentLevel++; setupLevel(); continue; } else if (F_CTRLINPUT_B_PRESSED) { keyPressed = 1; setupLevel(); continue; } else if (F_CTRLINPUT_UP_PRESSED) { //Above cell can not be moved to if (keyPressed || !currentRow || mapBackground[currentRow - 1][currentCol] != BACKGROUND_PATH) { continue; } //Just moving to the above cell if (mapObjects[currentRow - 1][currentCol] == OBJECT_NOTHING) { currentRow--; } //Attempting to push a box else if (currentRow > 1 && mapObjects[currentRow - 2][currentCol] == OBJECT_NOTHING && (mapBackground[currentRow - 2][currentCol] == BACKGROUND_PATH || mapBackground[currentRow - 2][currentCol] == BACKGROUND_HOLE)) { //Pushed box into a hole if (mapBackground[currentRow - 2][currentCol] == BACKGROUND_HOLE) { numBoxes--; } mapObjects[currentRow - 2][currentCol] = OBJECT_BOX; drawObject(currentRow - 2, currentCol); currentRow--; } } else if (F_CTRLINPUT_DOWN_PRESSED) { if (keyPressed || currentRow >= (NUM_ROWS - 1) || mapBackground[currentRow + 1][currentCol] != BACKGROUND_PATH) { continue; } if (mapObjects[currentRow + 1][currentCol] == OBJECT_NOTHING) { currentRow++; } else if (currentRow < (NUM_ROWS - 2) && mapObjects[currentRow + 2][currentCol] == OBJECT_NOTHING && (mapBackground[currentRow + 2][currentCol] == BACKGROUND_PATH || mapBackground[currentRow + 2][currentCol] == BACKGROUND_HOLE)) { if (mapBackground[currentRow + 2][currentCol] == BACKGROUND_HOLE) { numBoxes--; } mapObjects[currentRow + 2][currentCol] = OBJECT_BOX; drawObject(currentRow + 2, currentCol); currentRow++; } } else if (F_CTRLINPUT_LEFT_PRESSED) { if (keyPressed || !currentCol || mapBackground[currentRow][currentCol - 1] != BACKGROUND_PATH) { continue; } if (mapObjects[currentRow][currentCol - 1] == OBJECT_NOTHING) { currentCol--; } else if (currentCol > 1 && mapObjects[currentRow][currentCol - 2] == OBJECT_NOTHING && (mapBackground[currentRow][currentCol - 2] == BACKGROUND_PATH || mapBackground[currentRow][currentCol - 2] == BACKGROUND_HOLE)) { if (mapBackground[currentRow][currentCol - 2] == BACKGROUND_HOLE) { numBoxes--; } mapObjects[currentRow][currentCol - 2] = OBJECT_BOX; drawObject(currentRow, currentCol - 2); currentCol--; } } else if (F_CTRLINPUT_RIGHT_PRESSED) { if (keyPressed || currentCol == (NUM_COLS - 1) || mapBackground[currentRow][currentCol + 1] != BACKGROUND_PATH) { continue; } if (mapObjects[currentRow][currentCol + 1] == OBJECT_NOTHING) { currentCol++; } else if (currentCol < (NUM_COLS - 2) && mapObjects[currentRow][currentCol + 2] == OBJECT_NOTHING && (mapBackground[currentRow][currentCol + 2] == BACKGROUND_PATH || mapBackground[currentRow][currentCol + 2] == BACKGROUND_HOLE)) { if (mapBackground[currentRow][currentCol + 2] == BACKGROUND_HOLE) { numBoxes--; } mapObjects[currentRow][currentCol + 2] = OBJECT_BOX; drawObject(currentRow, currentCol + 2); currentCol++; } } else { keyPressed = 0; } if (!(lastRow == currentRow && lastCol == currentCol)) { mapObjects[lastRow][lastCol] = OBJECT_NOTHING; mapObjects[currentRow][currentCol] = OBJECT_PLAYER; drawTile(lastRow, lastCol); drawTile(currentRow, currentCol); drawObject(currentRow, currentCol); keyPressed = 1; if (!numBoxes) { currentLevel++; setupLevel(); } } } return 0; } /* END OF FILE */