Advertisement
CaptainLepidus

Dark Sun

Mar 7th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.38 KB | None | 0 0
  1. // Dark Sun by CaptainLepidus
  2. // As you can see, the level is currently flipped. I need to figure out how to fix this >.< I don't want to have to design levels 90 degrees rotated.
  3.  
  4. #include <iostream>
  5. #include "conio.h"
  6. #include <sstream>
  7. #include <list>
  8. #include <string>
  9. #define SSTR( x ) dynamic_cast< std::ostringstream & >( \
  10.         ( std::ostringstream() << std::dec << x ) ).str()
  11. #define WIDTH 10
  12. #define HEIGHT 12
  13. #define MAX_ITEMS 1
  14. #define WORLD_WIDTH 1
  15. #define WORLD_HEIGHT 1
  16. #define WORLD_DEPTH 1
  17. using namespace std;
  18. bool gameOver;
  19. int gameScreen;
  20. int current_item=0;
  21. struct item {
  22.        string name;
  23.        char symbol;
  24. } ;
  25. struct room {
  26.        string name;
  27.        char map[WIDTH][HEIGHT];
  28. } ;
  29. item items[MAX_ITEMS];
  30. room rooms[WORLD_WIDTH][WORLD_HEIGHT][WORLD_DEPTH];
  31. list<int> inventory;
  32. unsigned int p_x=2;
  33. unsigned int p_y=2;
  34. unsigned int p_rx=0;
  35. unsigned int p_ry=0;
  36. unsigned int p_rz=0;
  37. int p_level=1;
  38. int p_xp=0;
  39. int p_str=10;
  40. int p_dex=10;
  41. int p_con=10;
  42. int p_int=10;
  43. string msg="";
  44. void printLine(string text)
  45. {
  46.      cout << text;
  47.      cout << endl;
  48. }
  49. void registerItem(string name,char symbol)
  50. {
  51.      item temp;
  52.      temp.name=name;
  53.      temp.symbol=symbol;
  54.      items[current_item]=temp;
  55.      current_item++;
  56. }
  57. void registerRoom(string name,int x,int y,int z,char map[WIDTH][HEIGHT])
  58. {
  59.      room temp;
  60.      temp.name=name;
  61.      memcpy(temp.map,map,WIDTH*HEIGHT);
  62.      rooms[x][y][z]=temp;
  63. }
  64. void initGame()
  65. {
  66.      char temp[WIDTH][HEIGHT] = {
  67.             "***********",
  68.             "*[[[[[[[[[*",
  69.             "*         *",
  70.             "*         *",
  71.             "********   ",
  72.             "********   ",
  73.             "*         *",
  74.             "*         *",
  75.             "*[[[[[[[[[*",
  76.             "***********"};
  77.      registerRoom("Sleeping Quarters",0,0,0,temp);
  78.      registerItem("Pistol",'P');
  79.     inventory.push_front(0);
  80. }
  81. char getSquare(unsigned int col, unsigned int row)
  82. {
  83.      return rooms[p_rx][p_ry][p_rz].map[col][row];
  84. }
  85.            
  86. char getLoc(unsigned int col, unsigned int row)
  87. {
  88.      if ( p_x==col && p_y==row )
  89.      {
  90.         return '@';
  91.      }
  92.      return getSquare(col,row);
  93. }
  94.  
  95.     void printBoard() {
  96.         using namespace std;
  97.         cout << rooms[p_rx][p_ry][p_rz].name;
  98.         cout << endl;
  99.             for (unsigned int uiRow = 0; uiRow < HEIGHT; ++uiRow) {
  100.                 for (unsigned int uiCol = 0; uiCol < WIDTH; ++uiCol) {
  101.                 cout << getLoc(uiCol, uiRow);
  102.             }
  103.             cout << endl;
  104.         }
  105.         cout << msg;
  106.         cout << endl;
  107.         cout << "LVL " + SSTR(p_level) + " EXP " + SSTR(p_xp);
  108.         cout << endl;
  109.         cout << "STR " + SSTR(p_str) + " DEX " + SSTR(p_dex) + " CON " + SSTR(p_con) + " INT " + SSTR(p_int);
  110.         cout << endl;
  111.         cout << "You have: ";
  112.         cout << endl;
  113.         list<int>::iterator i;
  114.         for(i=inventory.begin(); i != inventory.end(); ++i)
  115.         {
  116.           cout << items[*i].name;
  117.           cout << endl;
  118.           }
  119.  
  120.     }
  121.     char movePlayer(char move)
  122.     {
  123.          msg = "";
  124.          int g_x = p_x;
  125.          int g_y = p_y;
  126.         switch(move)
  127.         {
  128.             case 'w':
  129.             case 'W':
  130.                  g_y--;
  131.             break;
  132.             case 's':
  133.             case 'S':
  134.                  g_y++;
  135.             break;
  136.             case 'a':
  137.             case 'A':
  138.                  g_x--;
  139.             break;
  140.             case 'd':
  141.             case 'D':
  142.                  g_x++;
  143.             break;
  144.             case 'g':
  145.             case 'G':
  146.                  msg="Get what?";
  147.             break;
  148.             case 'q':
  149.                  gameOver=1;
  150.             break;
  151.      }
  152.      if ( getLoc(g_x,g_y)==' ' )
  153.      {
  154.           p_x = g_x;
  155.           p_y = g_y;
  156.      }
  157.      else if (g_x!=p_x || g_y!=p_y)
  158.      {
  159.          msg="There is a wall there!";
  160.      }
  161. }
  162. void handleMenu(char move)
  163. {
  164.      switch(move)
  165.      {
  166.          case '1':
  167.               gameScreen=2;
  168.          break;
  169.          case '2':
  170.               gameScreen=4;
  171.          break;
  172.          case '3':
  173.               gameOver=1;
  174.          break;
  175.      }
  176. }
  177. void handleInput(char move)
  178. {
  179.      switch( gameScreen )
  180.      {
  181.          case 1:
  182.               handleMenu(move);
  183.          break;
  184.          case 2:
  185.               gameScreen=3;
  186.          break;
  187.          case 3:
  188.               movePlayer(move);
  189.          break;
  190.          case 4:
  191.               gameScreen=1;
  192.          break;
  193.      }
  194. }
  195. void renderGame()
  196. {
  197.      switch (gameScreen)
  198.      {
  199.          case 0:
  200.               cout << "Loading...";
  201.          break;
  202.          case 1:
  203.               printLine("##  ### ### # #    ### # # ###");
  204.               printLine("# # # # # # # #    #   # # # #");
  205.               printLine("# # ### ### ##     ### # # # #");
  206.               printLine("# # # # ##  # #      # # # # #");
  207.               printLine("##  # # # # # #    ### ### # #");
  208.               printLine("'The supreme irony of life is that hardly anyone gets out of it alive.' - Robert A. Heinlein");
  209.               printLine("1) Start Game");
  210.               printLine("2) About");
  211.               printLine("2) Exit");
  212.          break;
  213.          case 2:
  214.               printLine("The year is 2351. The ability to control and harness the power of fusion changed the fate of mankind forever. With near unlimited energy, humanity turned it's sights towards other goals, solar colonization foremost among them. Earth's moon, and then Mars, and then the asteroid belt, and then Europa. Slowly, humanity spread it's wings and soared to new places. Expansion on an unheard of scale. The population had passed 10 billion by 2200. The future seemed bright...");
  215.               printLine("All good things must come to an end eventually, however...");
  216.               printLine("You wake, alone and afraid. The darkness surrounds you. A single light in the ceiling flashes dimly, revealing sleeping quarters. Something has gone terribly wrong here; everything is thrown out of place, a far cry from the cleanly state of most settlements.");
  217.               printLine("Press any key.");
  218.          break;
  219.          case 3:
  220.               printBoard();
  221.          break;
  222.          case 4:
  223.               printLine("Dark Sun was developed in C++ by CaptainLepidus in 2013.");
  224.               printLine("Press any key.");
  225.          break;
  226.      }
  227. }
  228. int main ()
  229. {
  230.     int i;
  231.     char move;
  232.     gameOver=0;
  233.     gameScreen=0;
  234.     renderGame();
  235.     initGame();
  236.     gameScreen=1;
  237.     while(!gameOver)
  238.     {
  239.         system("CLS");
  240.         renderGame();
  241.         move = getch();
  242.         handleInput(move);
  243.     }
  244.     return 0;
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement