Advertisement
wild4fun88

read game borde

Nov 13th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.81 KB | None | 0 0
  1. So this is what i did for loading a map into an array. The map file looks like this:
  2.  
  3. ################################################################################
  4. #X#   #     #   #   #   #   #   #                                              #
  5. # # # # ### # # # ### # # #   #   # ##### #                                    #
  6. #   # # # #   # #     #   ######### #     #                                    #
  7. ##### # # # ###   # ### # #   #     # #####                                    #
  8. #         # #   # #     #   # # ##### #                                        #
  9. # ##### ### # ### ##### # #####       ###                                      #
  10. #   # # #     #         #       ### #                                          #
  11. ### #   # ##### ## # ## #### ####   ######                                     #
  12. #   ##### #   # #  #  #    #      #      #                                     #
  13. # ###   #   #   # ### ## # ############# ######                                #
  14. #     # # ### ###     #  #   #   #   # #  #                                    #
  15. ## #### # #       # # ###### # # # # # ## #                                    #
  16. #     #   ### # ### #  #   # # # # # #    #                                    #
  17. # ### #####   # #   #    # #   #   #   # ##                                    #
  18. # # #       # ### # ###### # ### ####### #                                     #
  19. # # ####### #     #     #  # #         # # #                                   #
  20. # #   #   # ########### # ## # ### ### #   #                                   #
  21. # # #   # #       #     #    # #   #   ## ###                                  #
  22. #   # ### ##### # # # ### #### # ### #  # #                                    #
  23. # # #     #   # #   #   #      #     #    #                                    #
  24. # ####### # # # # ### # # #### # #  ##### #                                    #
  25. #           #   #     #   #      #        #                                    #
  26. ################################################################################
  27.  
  28.  
  29.  
  30. And the code:
  31.  
  32. #define         GAME_WIDTH  80
  33. #define         GAME_HEIGHT 24
  34. #define         WALL        178
  35. #define         PICKUP      254
  36.  
  37. int         yIndex, xIndex, tile;
  38. char            gameField[GAME_HEIGHT][GAME_WIDTH];
  39. FILE            *level;
  40.  
  41. /* Starting at x and y 0 */
  42. yIndex = 0;
  43. xIndex = 0;
  44.  
  45. /* Load up the game file */
  46. level = fopen("level.txt", "r+");
  47.  
  48. /*
  49.  * As long as our buffer is not pointing at the end of the
  50.  * file, we'll keep reading characters from the file.
  51.  */
  52. while (tile != EOF) {
  53.     /*
  54.      * Read the character where our internal file position
  55.      * indicator is, then move it to the next chararacter.
  56.      * fgetc() returns an int, but that's okey since we can
  57.      * assign decimal values to a char variable!
  58.      */
  59.     tile = fgetc(level);
  60.  
  61.     /*
  62.      * Check what value our tile has, and based on that
  63.      * we might want to do some specific things, like
  64.      * replacing it with an extended ASCII character!
  65.      */
  66.     switch (tile) {
  67.         /* Wall tile, replace with a cool ascii char */
  68.         case '#':
  69.             tile = WALL;
  70.         break;
  71.  
  72.         /* Pickup tile, replace with cool ascii char */
  73.         case '!':
  74.             tile = PICKUP;
  75.         break;
  76.  
  77.         /* Player spawn position! */
  78.         case 'X':
  79.             playerX = xIndex;
  80.             playerY = yIndex;
  81.         break;
  82.  
  83.         /* New line, increment the yIndex and reset xIndex */
  84.         case '\n':
  85.             yIndex++;
  86.             xIndex = 0;
  87.  
  88.             /* Skip to the next iteration */
  89.             continue;
  90.         break;
  91.     }          
  92.    
  93.     /*
  94.      * This is where we add the character to our map array.
  95.      * The map array is an array of chars, and the tile now
  96.      * holds an decimal value. But once again, that's okey!
  97.      */
  98.     gameField[yIndex][xIndex] = tile;
  99.  
  100.     /*
  101.      * Now that we've added one tile to the map array
  102.      * we'll want to advance to the next x position.
  103.      */
  104.     xIndex++;
  105. }
  106.    
  107. /* Close the level file */
  108. fclose(level);
  109.  
  110.  
  111.  
  112. [Edited by - Zomgbie on December 2, 2010 1:31:20 AM]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement