Advertisement
Willium_Bob_Cole

My RPG so far...

Oct 5th, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.00 KB | None | 0 0
  1. #include <iostream>
  2. #include "windows.h"
  3. #include "time.h"
  4.  
  5.  
  6. using namespace std;
  7.  
  8.  
  9. //Functions
  10. void initialiseMap();
  11. void printMap();
  12. void updateMap(int nextX, int nextY);
  13. void updateScore();
  14. void gameLoop();
  15. void getInput();
  16. void playerMove(char dir);
  17.  
  18. //Game State
  19. bool running = true;
  20.  
  21. const int mapWidth = 32;
  22. const int mapHeight = 8;
  23.  
  24. char map [mapWidth][mapHeight];
  25.  
  26. int coinsLeft = 0;
  27.  
  28. //Graphic Representations
  29. struct Graphics
  30. {
  31.     static const char space = ' ';
  32.     static const char player = 'T';
  33.     static const char block = '#';
  34.     static const char coin = '*';
  35.     static const char boulder = 'O';
  36.     static const char doorL = '[';
  37.     static const char doorR = ']';
  38. };
  39.  
  40.  
  41. //Player State
  42. struct Player
  43. {
  44.     int posX;
  45.     int posY;
  46.     int score;
  47. };
  48.  
  49. Graphics graphics;
  50. Player dave;
  51.  
  52. int main()
  53. {
  54.     dave.posX = 1;
  55.     dave.posY = 1;
  56.     dave.score = 0;
  57.  
  58.  
  59.     initialiseMap();
  60.  
  61.     gameLoop();
  62.  
  63.     system("CLS");
  64.  
  65.     if(dave.score < 50)
  66.     {
  67.         cout << "Game Ended. Final Score: " << dave.score << "! Press Enter to Quit.";
  68.     }
  69.     else
  70.     {
  71.         cout << "Congratulations! A Winner is You! Score: " << dave.score << "! Press Enter to Quit.";
  72.     }
  73.     cin.get();
  74.     return 0;
  75. }
  76.  
  77.  
  78. void gameLoop()
  79. {
  80.     while(running == true)
  81.     {
  82.         getInput();
  83.  
  84.         if(coinsLeft == 0 && dave.score < 50)
  85.         {
  86.             initialiseMap();
  87.         }
  88.  
  89.         if(dave.score >= 50)
  90.         {
  91.             running = false;
  92.         }
  93.  
  94.         Sleep(150);
  95.     }
  96. }
  97.  
  98.  
  99. void initialiseMap()
  100. {
  101.     srand (time(NULL));
  102.  
  103.     //Populate map
  104.     for(int y = 0; y < mapHeight; y++)
  105.     {
  106.         for(int x = 0; x < mapWidth; x++)
  107.         {
  108.             if(x == 0 || x == mapWidth - 1 || y == 0 || y == mapHeight - 1)
  109.             {
  110.                 map[x][y] = graphics.block;
  111.             }
  112.             else
  113.             {
  114.                 map[x][y] = graphics.space;
  115.             }
  116.         }
  117.     }
  118.  
  119.     //Spawn player
  120.     map[dave.posX][dave.posY] = graphics.player;
  121.  
  122.     //Spawn blocks
  123.     for(int count = 0; count < 10; count++)
  124.     {
  125.         int randX = 0;
  126.         int randY = 0;
  127.  
  128.         do
  129.         {
  130.             randX = rand() % (mapWidth - 1) + 1;
  131.             randY = rand() % (mapHeight - 1) + 1;
  132.         }
  133.         while(map[randX][randY] != graphics.space);
  134.  
  135.         map[randX][randY] = graphics.block;
  136.     }
  137.  
  138.     //Spawn coins
  139.     for(int count = 0; count < 10; count++)
  140.     {
  141.         int randX = rand() % (mapWidth - 1) + 1;
  142.         int randY = rand() % (mapHeight - 1) + 1;
  143.  
  144.         while(map[randX][randY] != graphics.space)
  145.         {
  146.             randX = rand() % (mapWidth - 1) + 1;
  147.             randY = rand() % (mapHeight - 1) + 1;
  148.         }
  149.  
  150.         map[randX][randY] = graphics.coin;
  151.     }
  152.  
  153.     //Spawn boulders
  154.     for(int count = 0; count < 2; count++)
  155.     {
  156.         int randX = rand() % (mapWidth - 1) + 1;
  157.         int randY = rand() % (mapHeight - 1) + 1;
  158.  
  159.         while(map[randX][randY] != graphics.space)
  160.         {
  161.             randX = rand() % (mapWidth - 1) + 1;
  162.             randY = rand() % (mapHeight - 1) + 1;
  163.         }
  164.  
  165.         map[randX][randY] = graphics.boulder;
  166.     }
  167.  
  168.     //Spawn portals
  169.     int randDoor = rand() % (mapHeight - 2) + 1;
  170.  
  171.     map[0][randDoor] = graphics.doorL;
  172.     map[mapWidth-1][randDoor] = graphics.doorR;
  173.  
  174.  
  175.     coinsLeft = 10;
  176.     printMap();
  177. }
  178.  
  179.  
  180. void printMap()
  181. {
  182.     system("CLS");
  183.  
  184.     for(int y = 0; y < mapHeight; y++)
  185.     {
  186.         for(int x = 0; x < mapWidth; x++)
  187.         {
  188.             cout << map[x][y];
  189.         }
  190.         cout << endl;
  191.     }
  192.  
  193.     updateScore();
  194. }
  195.  
  196.  
  197. void updateMap(int nextX, int nextY, int pushX, int pushY, bool pushing)
  198. {
  199.     COORD coord;
  200.  
  201.     coord.X = dave.posX;
  202.     coord.Y = dave.posY;
  203.     SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE ),coord);
  204.  
  205.     cout << map[coord.X][coord.Y];
  206.  
  207.     coord.X = nextX;
  208.     coord.Y = nextY;
  209.     SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE ),coord);
  210.  
  211.     cout << map[coord.X][coord.Y];
  212.  
  213.     if(pushing)
  214.     {
  215.         coord.X = pushX;
  216.         coord.Y = pushY;
  217.         SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE ),coord);
  218.  
  219.         cout << map[coord.X][coord.Y];
  220.     }
  221.  
  222.     coord.X = 0;
  223.     coord.Y = mapHeight;
  224.     SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE ),coord);
  225.  
  226.     updateScore();
  227. }
  228.  
  229.  
  230. void updateScore()
  231. {
  232.     cout << "Score: " << dave.score << ". Press Esc to End the Game.";
  233. }
  234.  
  235.  
  236. void getInput()
  237. {
  238.     if(GetAsyncKeyState(VK_UP))
  239.     {
  240.         playerMove('N');
  241.     }
  242.  
  243.     if(GetAsyncKeyState(VK_RIGHT))
  244.     {
  245.         playerMove('E');
  246.     }
  247.  
  248.     if(GetAsyncKeyState(VK_DOWN))
  249.     {
  250.         playerMove('S');
  251.     }
  252.  
  253.     if(GetAsyncKeyState(VK_LEFT))
  254.     {
  255.         playerMove('W');
  256.     }
  257.  
  258.     if(GetAsyncKeyState(VK_ESCAPE))
  259.     {
  260.         running = false;
  261.     }
  262. }
  263.  
  264.  
  265. void playerMove(char dir)
  266. {
  267.     int nextX = dave.posX;
  268.     int nextY = dave.posY;
  269.     int pushX = dave.posX;
  270.     int pushY = dave.posY;
  271.  
  272.     if(dir == 'N')
  273.     {
  274.         nextY -= 1;
  275.         pushY -= 2;
  276.     }
  277.  
  278.     if(dir == 'E')
  279.     {
  280.         nextX += 1;
  281.         pushX += 2;
  282.     }
  283.  
  284.     if(dir == 'S')
  285.     {
  286.         nextY += 1;
  287.         pushY += 2;
  288.     }
  289.  
  290.     if(dir == 'W')
  291.     {
  292.         nextX -= 1;
  293.         pushX -= 2;
  294.     }
  295.  
  296.     //move into space
  297.     if(map[nextX][nextY] == graphics.space)
  298.     {
  299.         map[dave.posX][dave.posY] = graphics.space;
  300.         map[nextX][nextY] = graphics.player;
  301.  
  302.         updateMap(nextX, nextY, 0, 0, false);
  303.  
  304.         dave.posX = nextX;
  305.         dave.posY = nextY;
  306.     }
  307.  
  308.     //Collect coin and move
  309.     if(map[nextX][nextY] == graphics.coin)
  310.     {
  311.         map[dave.posX][dave.posY] = graphics.space;
  312.         map[nextX][nextY] = graphics.player;
  313.  
  314.         dave.score += 1;
  315.         coinsLeft -= 1;
  316.  
  317.         updateMap(nextX, nextY, 0, 0, false);
  318.  
  319.         dave.posX = nextX;
  320.         dave.posY = nextY;
  321.     }
  322.  
  323.     //Push boulder and move
  324.     if(map[nextX][nextY] == graphics.boulder)
  325.     {
  326.         if(map[pushX][pushY] == graphics.doorL)
  327.         {
  328.             pushX = mapWidth - pushX - 2;
  329.         }
  330.         if(map[pushX][pushY] == graphics.doorR)
  331.         {
  332.             pushX = mapWidth - pushX;
  333.         }
  334.  
  335.         if(map[pushX][pushY] == graphics.space)
  336.         {
  337.             map[dave.posX][dave.posY] = graphics.space;
  338.             map[nextX][nextY] = graphics.player;
  339.             map[pushX][pushY] = graphics.boulder;
  340.  
  341.             updateMap(nextX, nextY, pushX, pushY, true);
  342.  
  343.             dave.posX = nextX;
  344.             dave.posY = nextY;
  345.         }
  346.     }
  347.  
  348.     //Move through portals
  349.     if(map[nextX][nextY] == graphics.doorL)
  350.     {
  351.         nextX = mapWidth - nextX - 2;
  352.  
  353.         if(map[nextX][nextY] == graphics.space)
  354.         {
  355.             map[dave.posX][dave.posY] = graphics.space;
  356.             map[nextX][nextY] = graphics.player;
  357.  
  358.             updateMap(nextX, nextY, 0, 0, false);
  359.  
  360.             dave.posX = nextX;
  361.             dave.posY = nextY;
  362.         }
  363.  
  364.         if(map[nextX][nextY] == graphics.boulder)
  365.         {
  366.             pushX = nextX - 1;
  367.  
  368.             if(map[pushX][pushY] == graphics.space)
  369.             {
  370.                 map[dave.posX][dave.posY] = graphics.space;
  371.                 map[nextX][nextY] = graphics.player;
  372.                 map[pushX][pushY] = graphics.boulder;
  373.  
  374.                 updateMap(nextX, nextY, pushX, pushY, true);
  375.  
  376.                 dave.posX = nextX;
  377.                 dave.posY = nextY;
  378.             }
  379.         }
  380.  
  381.  
  382.     }
  383.  
  384.     if(map[nextX][nextY] == graphics.doorR)
  385.     {
  386.         nextX = mapWidth - nextX;
  387.  
  388.         if(map[nextX][nextY] == graphics.space)
  389.         {
  390.             map[dave.posX][dave.posY] = graphics.space;
  391.             map[nextX][nextY] = graphics.player;
  392.  
  393.             updateMap(nextX, nextY, 0, 0, false);
  394.  
  395.             dave.posX = nextX;
  396.             dave.posY = nextY;
  397.         }
  398.  
  399.         if(map[nextX][nextY] == graphics.boulder)
  400.         {
  401.             pushX = nextX + 1;
  402.  
  403.             if(map[pushX][pushY] == graphics.space)
  404.             {
  405.                 map[dave.posX][dave.posY] = graphics.space;
  406.                 map[nextX][nextY] = graphics.player;
  407.                 map[pushX][pushY] = graphics.boulder;
  408.  
  409.                 updateMap(nextX, nextY, pushX, pushY, true);
  410.  
  411.                 dave.posX = nextX;
  412.                 dave.posY = nextY;
  413.             }
  414.         }
  415.     }
  416. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement