Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- So this is what i did for loading a map into an array. The map file looks like this:
- ################################################################################
- #X# # # # # # # # #
- # # # # ### # # # ### # # # # # ##### # #
- # # # # # # # # ######### # # #
- ##### # # # ### # ### # # # # ##### #
- # # # # # # # # ##### # #
- # ##### ### # ### ##### # ##### ### #
- # # # # # # ### # #
- ### # # ##### ## # ## #### #### ###### #
- # ##### # # # # # # # # #
- # ### # # # ### ## # ############# ###### #
- # # # ### ### # # # # # # # #
- ## #### # # # # ###### # # # # # ## # #
- # # ### # ### # # # # # # # # # #
- # ### ##### # # # # # # # # ## #
- # # # # ### # ###### # ### ####### # #
- # # ####### # # # # # # # # #
- # # # # ########### # ## # ### ### # # #
- # # # # # # # # # # ## ### #
- # # ### ##### # # # ### #### # ### # # # #
- # # # # # # # # # # # #
- # ####### # # # # ### # # #### # # ##### # #
- # # # # # # # #
- ################################################################################
- And the code:
- #define GAME_WIDTH 80
- #define GAME_HEIGHT 24
- #define WALL 178
- #define PICKUP 254
- int yIndex, xIndex, tile;
- char gameField[GAME_HEIGHT][GAME_WIDTH];
- FILE *level;
- /* Starting at x and y 0 */
- yIndex = 0;
- xIndex = 0;
- /* Load up the game file */
- level = fopen("level.txt", "r+");
- /*
- * As long as our buffer is not pointing at the end of the
- * file, we'll keep reading characters from the file.
- */
- while (tile != EOF) {
- /*
- * Read the character where our internal file position
- * indicator is, then move it to the next chararacter.
- * fgetc() returns an int, but that's okey since we can
- * assign decimal values to a char variable!
- */
- tile = fgetc(level);
- /*
- * Check what value our tile has, and based on that
- * we might want to do some specific things, like
- * replacing it with an extended ASCII character!
- */
- switch (tile) {
- /* Wall tile, replace with a cool ascii char */
- case '#':
- tile = WALL;
- break;
- /* Pickup tile, replace with cool ascii char */
- case '!':
- tile = PICKUP;
- break;
- /* Player spawn position! */
- case 'X':
- playerX = xIndex;
- playerY = yIndex;
- break;
- /* New line, increment the yIndex and reset xIndex */
- case '\n':
- yIndex++;
- xIndex = 0;
- /* Skip to the next iteration */
- continue;
- break;
- }
- /*
- * This is where we add the character to our map array.
- * The map array is an array of chars, and the tile now
- * holds an decimal value. But once again, that's okey!
- */
- gameField[yIndex][xIndex] = tile;
- /*
- * Now that we've added one tile to the map array
- * we'll want to advance to the next x position.
- */
- xIndex++;
- }
- /* Close the level file */
- fclose(level);
- [Edited by - Zomgbie on December 2, 2010 1:31:20 AM]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement