kellex

Simple Game Kellex Linux version

Jul 20th, 2013
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4.  
  5. //Color Bash Prompt:
  6. //https://wiki.archlinux.org/index.php/Color_Bash_Prompt
  7.  
  8. unsigned int sleep(unsigned int seconds);
  9. using namespace std;
  10.  
  11.     // Initialized global variables
  12. int Gamespeed = 50;
  13. int HpStart = 1;
  14. int MaxHp = 100;
  15. char move;
  16.  
  17.     // Map prototypes
  18. const struct Map_S {int width, height; char data[64][64];} Maps[] = {
  19.     {
  20.         9, 10, {
  21.         "#########",
  22.         "#   #   #",
  23.         "# # # # #",
  24.         "# # # # #",
  25.         "#+# # #+#",
  26.         "# # # # #",
  27.         "# # # # #",
  28.         "# # # # #",
  29.         "#@# * # #",
  30.         "#######-#"
  31.     }},
  32.     {
  33.         39, 18, {
  34.         "#####################################-#",
  35.         "#@       #      # #  *       *  #   # #",
  36.         "# ###### # #++# # #  * *  +  *  # # # #",
  37.         "# ##   # # #### # #    *        # # # #",
  38.         "#    # #   #    # # ## #******* # #   #",
  39.         "###### ##### #### #    #        # #####",
  40.         "#      #          # ## #  ******#     #",
  41.         "# ########## #### #    ##       ####  #",
  42.         "#                       #****** #*    #",
  43.         "##### ############   ## #       #*    #",
  44.         "#     ##             ## # ******#**** #",
  45.         "#     ## +++  #######   #       #*    #",
  46.         "#      #      #######  ######## #* ***#",
  47.         "#      #      #######  *   *    *     #",
  48.         "#      ##     *   *    * * * *  *     #",
  49.         "#     ###     * * * *  * * * *  *     #",
  50.         "#++  +###       *   *    *   *        #",
  51.         "#######################################"
  52.     }},
  53.     {
  54.         39, 18, {
  55.         "#######################################",
  56.         "#@         # # #********##     #      #",
  57.         "########## #   #             # # # ####",
  58.         "#          # #+#        ###### # #    #",
  59.         "###### ##### ###        #    # # #### #",
  60.         "#      #                #        #    #",
  61.         "# ###### # #####********########## ####",
  62.         "#        # #               #          #",
  63.         "########## #               # *##  *## #",
  64.         "#++++#     #  ######::###### *##  *## #",
  65.         "#++++#   ###  #            #          #",
  66.         "#######  ######            ######**####",
  67.         "#             #            #         K#",
  68.         "#   ********  #            #**********#",
  69.         "#   ********  ######::######   +++    #",
  70.         "#   ********  * # K#  #        +++    #",
  71.         "#             #   ##  #               #",
  72.         "####################--#################"
  73.     }},
  74.     {
  75.         39, 18, {
  76.         "###########                 ###########",
  77.         "#@        #                 #*********#",
  78.         "#         ###################*********#",
  79.         "#                                *****#",
  80.         "#         ###################*** *****#",
  81.         "###########  #   #         ##### ######",
  82.         " #K# #       # # # ####### #   #      #",
  83.         " #*# # ##### # #   #     # # # ###### #",
  84.         " #*# #     #   ##### ### #   #        #",
  85.         " #*# #     #####     ### ##############",
  86.         " #*# ##### ##### #######              #",
  87.         " #*#     #       #    ##########   ####",
  88.         "##*###############          ##### #####",
  89.         "#* *******###################****     #",
  90.         "#*  *   *                     ******* #",
  91.         "#** * * * ########:::######## ******* #",
  92.         "#**   *   #      #   #      #         #",
  93.         "###########      #---#      ###########"
  94.     }},
  95.     {
  96.         39, 18, {
  97.         "#######################################",
  98.         "#@                                    #",
  99.         "#  ##############     ##############  #",
  100.         "#  ##############     ##############  #",
  101.         "#  ##############     ##############  #",
  102.         "#  #                               #  #",
  103.         "#  #          ##### #####          #  #",
  104.         "#  #          ###     ###          #  #",
  105.         "#  #####::#######  ?  #######::#####  #",
  106.         "#  #          ###     ###          #  #",
  107.         "#  #          ###########          #  #",
  108.         "#  #                               #  #",
  109.         "#  ##############     ##############  #",
  110.         "#  ##############     ##############  #",
  111.         "#  ##############     ##############  #",
  112.         "#  **********K###     ###K**********  #",
  113.         "#################-----#################"
  114.     }}
  115. };
  116.  
  117. //Map template.....
  118. /*
  119. {
  120.         39, 18, {
  121.         "#######################################",
  122.         "#@                                    #",
  123.         "#                                     #",
  124.         "#                                     #",
  125.         "#                                     #",
  126.         "#                                     #",
  127.         "#                                     #",
  128.         "#                                     #",
  129.         "#                                     #",
  130.         "#                                     #",
  131.         "#                                     #",
  132.         "#                                     #",
  133.         "#                                     #",
  134.         "#                                     #",
  135.         "#                                     #",
  136.         "#                                     #",
  137.         "#                                     #",
  138.         "#######################################"
  139.     }},
  140. */
  141.  
  142.     // Uninitialized variables
  143. int Hp;
  144. int Level;
  145. int px, py;
  146. int Keys;
  147. string w;
  148.     // Map data
  149. struct Map_S Map;
  150.  
  151. void what(){
  152.      wh:
  153.      cout << "\n\n:";
  154.      cin >> w;
  155.      if(w == "hp" | w == "HP" | w == "Hp"){
  156.           cout << "Enter a Value: ";
  157.           cin >> Hp;
  158.           w.clear();
  159.           }
  160.           else if(w == "Keys" | w == "keys" | w == "KEYS"){
  161.                cout << "Enter a Value: ";
  162.                cin >> Keys;
  163.                w.clear();
  164.                }
  165.           else if(w == "gamespeed"){
  166.                cout << "Enter a value: ";
  167.                cin >> Gamespeed;
  168.                w.clear();
  169.                }
  170.           else if(w == "px"){
  171.                cout << "Enter a value: ";
  172.                cin >> px;
  173.                w.clear();
  174.                }
  175.           else if(w == "py"){
  176.                cout << "Enter a value: ";
  177.                cin >> py;
  178.                w.clear();
  179.                }
  180.           else if(w == "Help" | w == "help" | w == "?"){
  181.                cout << "Possible commands:\n\n" << "hp\n" << "keys\n" << endl;
  182.                goto wh;
  183.                w.clear();
  184.                }
  185.           else {
  186.                cout << "Fuck Off!!!!\n" << endl;
  187.                system("read");
  188.                w.clear();
  189.                }
  190.      }
  191.  
  192. void SetMap(int Index) {
  193.     Level = Index;
  194.     Map = Maps[Level];
  195.         // Get @ coords
  196.     px = py = 0;
  197.     for(int y = 0; y < Map.height; y++)
  198.         for(int x = 0; x < Map.width; x++)
  199.             if (Map.data[y][x] == '@') {
  200.                 px = x;
  201.                 py = y;
  202.             }
  203. }
  204.  
  205. void DrawMap() {
  206.         // Clear screen
  207.     system("clear");
  208.  
  209.         // Draw map
  210.     for(int y = 0; y < Map.height; y++) {
  211.         for(int x = 0; x < Map.width; x++) {
  212.             if (Map.data[y][x] == '#')
  213.                 cout << (char)219;
  214.             else
  215.                 cout << Map.data[y][x];
  216.         }
  217.         cout << endl;
  218.     }
  219.  
  220.         // Show stats
  221.     cout << "KEY: " << Keys << "\n\n\nHP: ";
  222.     cout << Hp;
  223.     cout << " / ";
  224.     cout << MaxHp << endl;
  225.     cout << "Level: " << Level + 1 << "\n\n";
  226. }
  227.  
  228. int main() {
  229.         // Program loop
  230.     while (1) {
  231.             // Initialize vairables
  232.         Hp = HpStart;
  233.         Keys = 0;
  234.  
  235.             // Menu loop
  236.         while (1) {
  237.                 // Show menu screen
  238.             system("clear");
  239.             cout <<
  240.             "######################################\n"
  241.             "#                                    #\n"
  242.             "#            MENU                    #\n"
  243.             "#                                    #\n"
  244.             "#       1. Start Game                #\n"
  245.             "#       2. Level Select              #\n"
  246.             "#       3. Instructions              #\n"
  247.             "#       4. EXIT                      #\n"
  248.             "#                                    #\n"
  249.             "#                                    #\n"
  250.             "######################################\n\n";
  251.             cout << "Enter a choice: ";
  252.  
  253.                 // Get input
  254.             int input;
  255.             cin >> input;
  256.  
  257.                 // Start game at level 1
  258.             if (input == 1) {
  259.                 SetMap(0);
  260.                 break;
  261.             }
  262.                 // Go to select screen
  263.             else if (input == 2) {
  264.                     // Level select loop
  265.                 while (1) {
  266.                         // Show level select screen
  267.                     system("clear");
  268.                     cout <<
  269.                     "###########################################\n"
  270.                     "#                                         #\n"
  271.                     "#            LevelSelect                  #\n"
  272.                     "#                                         #\n"
  273.                     "#   1. level1   6. level6   11. level11   #\n"
  274.                     "#   2. level2   7. level7   12. level12   #\n"
  275.                     "#   3. level3   8. level8   13. level13   #\n"
  276.                     "#   4. level4   9. level9   14. level14   #\n"
  277.                     "#   5. level5  10. level10  15. level15   #\n"
  278.                     "#                                         #\n"
  279.                     "#                 9 - Return to menu      #\n"
  280.                     "###########################################\n\n";
  281.                     cout << "Enter a choice: ";
  282.  
  283.                         // Get input
  284.                     cin >> input;
  285.  
  286.                         // Go to selected map
  287.                     if (input >= 1 && input <= sizeof(Maps) / sizeof(*Maps)) {
  288.                         SetMap(input - 1);
  289.                         break;
  290.                     }
  291.                         // Return to menu
  292.                     else if (input == 9) {
  293.                         break;
  294.                     }
  295.                         // Invalid input
  296.                     else {
  297.                         cout << "That's not an option. Try again...\n";
  298.                         system("pause >nul");
  299.                     }
  300.                 }
  301.                     // Break menu loop
  302.                 if (input != 9)
  303.                     break;
  304.             }
  305.                 // Show instructions
  306.             else if (input == 3) {
  307.                 system("clear");
  308.                 cout <<
  309.                 "######################################\n"
  310.                 "#                                    #\n"
  311.                 "#            Instructions            #\n"
  312.                 "#                                    #\n"
  313.                 "#   WASD -- Movement                 #\n"
  314.                 "#                                    #\n"
  315.                 "#   @ --  Player                     #\n"
  316.                 "#   * --  Damage(-HP)                #\n"
  317.                 "#   + --  HP boost(+HP)              #\n"
  318.                 "#   K --  Keys                       #\n"
  319.                 "#   : --  Door(Open this with Keys   #\n"
  320.                 "#   # --  Walls                      #\n"
  321.                 "#   ? --  ? (WHAT?)                  #\n"
  322.                 "#                                    #\n"
  323.                 "#          Press any key to continue #\n"
  324.                 "######################################\n\n";
  325.                 system("read");
  326.             }
  327.                 // Quit
  328.             else if (input == 4)
  329.                 return 0;
  330.                 // Invalid input
  331.             else {
  332.                 cout << "That's not an option. Try again...\n";
  333.                 system("pause >nul");
  334.             }
  335.         }
  336.  
  337.             // Game logic loop
  338.         DrawMap();
  339.         while (1) {
  340.                 // Get new coords
  341.             cin >> move;
  342.             int px2 = px, py2 = py;
  343.             switch(move) {
  344.          
  345.                      case 'd':
  346.                         px2 = px + 1;
  347.                         break;
  348.                      case 'a':
  349.                         px2 = px - 1;
  350.                         break;
  351.                      case 's':
  352.                         py2 = py + 1;
  353.                         break;
  354.                      case 'w':
  355.                         py2 = py - 1;
  356.                         break;
  357.                         }
  358.  
  359.                 // Interact with objects
  360.                 // Spike
  361.             if (Map.data[py2][px2] == '*') {
  362.                 Hp -= 5;
  363.                     // Game over if no hp
  364.                 if (Hp <= 0)
  365.                     break;
  366.             }
  367.                 // Hp boost
  368.             else if (Map.data[py2][px2] == '+') {
  369.                 Hp += 5;
  370.                     // Limit hp
  371.                 if (Hp >= MaxHp)
  372.                     Hp = MaxHp;
  373.             }
  374.                 // Goal
  375.             else if (Map.data[py2][px2] == '-') {
  376.                     // Quit game loop if out of maps
  377.                 if (Level + 1 >= sizeof(Maps) / sizeof(*Maps))
  378.                     break;
  379.                     // Go to next map
  380.                 SetMap(Level + 1);
  381.                 DrawMap();
  382.                 continue;
  383.             }
  384.                 // Wall
  385.             else if (Map.data[py2][px2] == '#') {
  386.                 px2 = px;
  387.                 py2 = py;
  388.             }
  389.                 // Key
  390.             else if (Map.data[py2][px2] == 'K')
  391.                 Keys++;
  392.                 // Door
  393.             else if (Map.data[py2][px2] == ':') {
  394.                 if (!Keys) {
  395.                     px2 = px;
  396.                     py2 = py;
  397.                 } else
  398.                     Keys--;
  399.             }
  400.             else if(Map.data[py2][px2] == '?'){
  401.                  what();
  402.                  }
  403.  
  404.                 // Move character
  405.             Map.data[py][px] = ' ';
  406.             Map.data[py2][px2] = '@';
  407.  
  408.                 // Set new choords
  409.             px = px2;
  410.             py = py2;
  411.  
  412.                 // Draw map
  413.             DrawMap();
  414.  
  415.                 // Sleep
  416.             sleep(Gamespeed);
  417.         }
  418.  
  419.             // Show game over screen if no hp
  420.         if (Hp <= 0) {
  421.             system("clear");
  422.             cout << "\n\n\n"
  423.             "###########\n"
  424.             "#GAME OVER#\n"
  425.             "###########\n";
  426.             system("read");
  427.         }
  428.     }
  429. }
Advertisement
Add Comment
Please, Sign In to add comment