Willium_Bob_Cole

Profitulation Game

Oct 24th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.51 KB | None | 0 0
  1. //Tile Struct
  2. struct Graphics
  3. {
  4.     //walkable
  5.     static const char sand = 's';
  6.     static const char grass = ',';
  7.     static const char forest = 'T';
  8.     static const char rock = 'R';
  9.  
  10.     //walkable but non buildable
  11.     static const char swamp = '#';
  12.  
  13.     //non walkable non buildable
  14.     static const char water = '~';
  15.     static const char mountain = 'M';
  16.  
  17.     static const char seed = 'o';
  18.     //static const char seedMat = 'o';
  19.    
  20.     static const char player = 1;
  21. };
  22.  
  23.  
  24. class generateNoise
  25. {
  26. public:
  27. // public area. visible to the 'user' of the new data type.
  28.  
  29.  
  30. private:
  31. // no one can see anything in this area except you.
  32.  
  33.  
  34. };
  35.  
  36.  
  37. class smoothNoise
  38. {
  39. public:
  40. // public area. visible to the 'user' of the new data type.
  41.  
  42.  
  43. private:
  44. // no one can see anything in this area except you.
  45.  
  46.  
  47. };
  48.  
  49.  
  50.  
  51. Graphics graphics;
  52.  
  53.  
  54. #include <iostream>
  55. #include "windows.h"
  56. #include "time.h"
  57.  
  58. //#import "\generateMap.*"
  59.  
  60. using namespace std;
  61.  
  62.  
  63. //Game Declarations
  64. bool running = true;
  65.  
  66. const int FPS = 6;
  67.  
  68.  
  69. //Map Declarations
  70. const int mapWidth = 64;
  71. const int mapHeight = 24;
  72.  
  73. char map[mapWidth][mapHeight];
  74.  
  75.  
  76. //Player Declarations
  77. const char startingTile = graphics.sand;
  78. int posX = 0;
  79. int posY = 0;
  80.  
  81. char currentTile;
  82.  
  83.  
  84. //Functions
  85. void generateMap();
  86. void printMap();
  87. void gameLoop();
  88. void getInput();
  89. void playerMove(char dir);
  90. void updateMap(int nextX, int nextY);
  91. void plantSeed();
  92.  
  93.  
  94. int main()
  95. {
  96.     generateMap();
  97.     printMap();
  98.  
  99.     gameLoop();
  100.  
  101.     system("CLS");
  102.     cout << "Game Over. Press Enter to Quit";
  103.  
  104.     cin.ignore();
  105.     cin.get();
  106.     return 0;
  107. }
  108.  
  109.  
  110. void generateMap()
  111. {
  112.     //referenced this http://breinygames.blogspot.co.uk/2012/06/generating-terrain-using-perlin-noise.html
  113.  
  114.     //map seed
  115.     srand(time(NULL));
  116.  
  117.  
  118.     //flood map
  119.     for(int y = 0; y < mapHeight; y++)
  120.     {
  121.         for(int x = 0; x < mapWidth; x++)
  122.         {
  123.             map[x][y] = graphics.sand;
  124.         }
  125.     }
  126.  
  127.     //grass island
  128.     for(int y = 1; y < mapHeight-1; y++)
  129.     {
  130.         for(int x = 1; x < mapWidth-1; x++)
  131.         {
  132.             map[x][y] = graphics.grass;
  133.         }
  134.     }
  135.  
  136.     do
  137.     {
  138.         posX = rand() % (mapWidth - 1) + 1;
  139.         posY = rand() % (mapHeight - 1) + 1;
  140.     }
  141.     while(map[posX][posY] != startingTile);
  142.  
  143.     currentTile = startingTile;
  144.  
  145.     map[posX][posY] = graphics.player;
  146. }
  147.  
  148.  
  149. void printMap()
  150. {
  151.     for(int y = 0; y < mapHeight; y++)
  152.     {
  153.         for(int x = 0; x < mapWidth; x++)
  154.         {
  155.             cout << map[x][y];
  156.         }
  157.  
  158.         cout << endl;
  159.     }
  160. }
  161.  
  162.  
  163. void gameLoop()
  164. {
  165.     while(running)
  166.     {
  167.         getInput();
  168.  
  169.         Sleep(1000/FPS);
  170.     }
  171. }
  172.  
  173.  
  174. void getInput()
  175. {
  176.     if(GetAsyncKeyState(VK_ESCAPE))
  177.     {
  178.         running = false;
  179.     }
  180.  
  181.     if(GetAsyncKeyState(VK_UP))
  182.     {
  183.         playerMove('N');
  184.     }
  185.  
  186.     if(GetAsyncKeyState(VK_RIGHT))
  187.     {
  188.         playerMove('E');
  189.     }
  190.  
  191.     if(GetAsyncKeyState(VK_DOWN))
  192.     {
  193.         playerMove('S');
  194.     }
  195.  
  196.     if(GetAsyncKeyState(VK_LEFT))
  197.     {
  198.         playerMove('W');
  199.     }
  200.  
  201.     if(GetAsyncKeyState(VK_SPACE))
  202.     {
  203.         plantSeed();
  204.     }
  205.  
  206. }
  207.  
  208.  
  209. void playerMove(char dir)
  210. {
  211.     int nextX = posX;
  212.     int nextY = posY;
  213.  
  214.  
  215.     if(dir == 'N')
  216.     {
  217.         nextY -= 1;
  218.     }
  219.  
  220.     if(dir == 'E')
  221.     {
  222.         nextX += 1;
  223.     }
  224.  
  225.     if(dir == 'S')
  226.     {
  227.         nextY += 1;
  228.     }
  229.  
  230.     if(dir == 'W')
  231.     {
  232.         nextX -= 1;
  233.     }
  234.  
  235.     if(nextX >= 0 && nextX < mapWidth &&
  236.         nextY >= 0 && nextY < mapHeight)
  237.     {
  238.         if(map[nextX][nextY] == graphics.sand
  239.         || map[nextX][nextY] == graphics.grass
  240.         || map[nextX][nextY] == graphics.forest
  241.         || map[nextX][nextY] == graphics.rock
  242.         || map[nextX][nextY] == graphics.swamp)
  243.         {
  244.             map[posX][posY] = currentTile;
  245.             currentTile = map[nextX][nextY];
  246.             map[nextX][nextY] = graphics.player;
  247.  
  248.             updateMap(nextX, nextY);
  249.  
  250.             posX = nextX;
  251.             posY = nextY;
  252.         }
  253.     }
  254. }
  255.  
  256.  
  257. void updateMap(int nextX, int nextY)
  258. {
  259.     COORD coord;
  260.  
  261.     coord.X = posX;
  262.     coord.Y = posY;
  263.     SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE ),coord);
  264.  
  265.     cout << map[coord.X][coord.Y];
  266.  
  267.     coord.X = nextX;
  268.     coord.Y = nextY;
  269.     SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE ),coord);
  270.  
  271.     cout << map[coord.X][coord.Y];
  272.  
  273.     //move cursor to end of map
  274.     coord.X = 0;
  275.     coord.Y = mapHeight;
  276.     SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE ),coord);
  277. }
  278.  
  279.  
  280. void plantSeed()
  281. {
  282.     if(currentTile == graphics.sand
  283.     || currentTile == graphics.grass
  284.     || currentTile == graphics.forest
  285.     || currentTile == graphics.rock)
  286.     {
  287.         bool plantable = true;
  288.  
  289.         for(int y = -2; y <= 2; y++)
  290.         {
  291.             for(int x = -2; x <= 2; x++)
  292.             {
  293.                 if(map[posX + x][posY + y] == graphics.seed /*|| map[x][y] == building*/)
  294.                 {
  295.                     plantable = false;
  296.                 }
  297.             }
  298.         }
  299.  
  300.  
  301.  
  302.         if(plantable == true)
  303.         {
  304.             currentTile = graphics.seed;
  305.         }
  306.     }
  307. }
Advertisement
Add Comment
Please, Sign In to add comment