kellex

Simple Game Kellex ver2

Jul 20th, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. using namespace std;
  4.  
  5.     // Initialized global variables
  6. int Gamespeed = 100;
  7. int HpStart = 1;
  8. int MaxHp = 100;
  9.  
  10.     // Map prototypes
  11. const struct Map_S {int width, height; char data[64][64];} Maps[] = {
  12.     {
  13.         9, 10, {
  14.         "#########",
  15.         "#   #   #",
  16.         "# # # # #",
  17.         "# # # # #",
  18.         "#+# # #+#",
  19.         "# # # # #",
  20.         "# # # # #",
  21.         "# # # # #",
  22.         "#@# * # #",
  23.         "#######-#"
  24.     }},
  25.     {
  26.         39, 18, {
  27.         "#####################################-#",
  28.         "#@       #      # #  *       *  #   # #",
  29.         "# ###### # #++# # #  * *  +  *  # # # #",
  30.         "# ##   # # #### # #    *        # # # #",
  31.         "#    # #   #    # # ## #******* # #   #",
  32.         "###### ##### #### #    #        # #####",
  33.         "#      #          # ## #  ******#     #",
  34.         "# ########## #### #    ##       ####  #",
  35.         "#                       #****** #*    #",
  36.         "##### ############   ## #       #*    #",
  37.         "#     ##             ## # ******#**** #",
  38.         "#     ## +++  #######   #       #*    #",
  39.         "#      #      #######  ######## #* ***#",
  40.         "#      #      #######  *   *    *     #",
  41.         "#      ##     *   *    * * * *  *     #",
  42.         "#     ###     * * * *  * * * *  *     #",
  43.         "#++  +###       *   *    *   *        #",
  44.         "#######################################"
  45.     }},
  46.     {
  47.         39, 18, {
  48.         "#######################################",
  49.         "#@         # # #********##     #      #",
  50.         "########## #   #             # # # ####",
  51.         "#          # #+#        ###### # #    #",
  52.         "###### ##### ###        #    # # #### #",
  53.         "#      #                #        #    #",
  54.         "# ###### # #####********########## ####",
  55.         "#        # #               #          #",
  56.         "########## #               # *##  *## #",
  57.         "#++++#     #  ######::###### *##  *## #",
  58.         "#++++#   ###  #            #          #",
  59.         "#######  ######            ######**####",
  60.         "#             #            #         K#",
  61.         "#   ********  #            #**********#",
  62.         "#   ********  ######::######   +++    #",
  63.         "#   ********  * # K#  #        +++    #",
  64.         "#             #   ##  #               #",
  65.         "####################--#################"
  66.     }},
  67.     {
  68.         39, 18, {
  69.         "###########                 ###########",
  70.         "#@        #                 #*********#",
  71.         "#         ###################*********#",
  72.         "#                                *****#",
  73.         "#         ###################*** *****#",
  74.         "###########  #   #         ##### ######",
  75.         " #K# #       # # # ####### #   #      #",
  76.         " #*# # ##### # #   #     # # # ###### #",
  77.         " #*# #     #   ##### ### #   #        #",
  78.         " #*# #     #####     ### ##############",
  79.         " #*# ##### ##### #######              #",
  80.         " #*#     #       #    ##########   ####",
  81.         "##*###############          ##### #####",
  82.         "#* *******###################****     #",
  83.         "#*  *   *                     ******* #",
  84.         "#** * * * ########:::######## ******* #",
  85.         "#**   *   #      #   #      #         #",
  86.         "###########      #---#      ###########"
  87.     }}
  88. };
  89.  
  90.     // Uninitialized variables
  91. int Hp;
  92. int Level;
  93. int px, py;
  94. int Keys;
  95.     // Map data
  96. struct Map_S Map;
  97.  
  98. void setcolor(unsigned short color) {
  99.     HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
  100.     SetConsoleTextAttribute(hcon, color);
  101. }
  102.  
  103. void SetMap(int Index) {
  104.     Level = Index;
  105.     Map = Maps[Level];
  106.         // Get @ coords
  107.     px = py = 0;
  108.     for(int y = 0; y < Map.height; y++)
  109.         for(int x = 0; x < Map.width; x++)
  110.             if (Map.data[y][x] == '@') {
  111.                 px = x;
  112.                 py = y;
  113.             }
  114. }
  115.  
  116. void DrawMap() {
  117.         // Clear screen
  118.     system("cls");
  119.  
  120.         // Draw map
  121.     for(int y = 0; y < Map.height; y++) {
  122.         for(int x = 0; x < Map.width; x++) {
  123.             if (Map.data[y][x] == '#')
  124.                 cout << (char)219;
  125.             else
  126.                 cout << Map.data[y][x];
  127.         }
  128.         cout << endl;
  129.     }
  130.  
  131.         // Show stats
  132.     cout << "KEY: " << Keys << "\n\n\nHP: ";
  133.     setcolor(10);
  134.     cout << Hp;
  135.     setcolor(7);
  136.     cout << " / ";
  137.     setcolor(12);
  138.     cout << MaxHp << endl;
  139.     setcolor(10);
  140.     cout << "Level: " << Level + 1;
  141.     setcolor(7);
  142. }
  143.  
  144. int main() {
  145.         // Program loop
  146.     while (1) {
  147.             // Initialize vairables
  148.         Hp = HpStart;
  149.         Keys = 0;
  150.  
  151.             // Menu loop
  152.         while (1) {
  153.                 // Show menu screen
  154.             system("cls");
  155.             setcolor(202);
  156.             cout <<
  157.             "######################################\n"
  158.             "#                                    #\n"
  159.             "#            MENU                    #\n"
  160.             "#                                    #\n"
  161.             "#       1. Start Game                #\n"
  162.             "#       2. Level Select              #\n"
  163.             "#       3. Instructions              #\n"
  164.             "#       4. EXIT                      #\n"
  165.             "#                                    #\n"
  166.             "#                                    #\n"
  167.             "######################################\n\n";
  168.             setcolor(7);
  169.             cout << "Enter a choice: ";
  170.  
  171.                 // Get input
  172.             int input;
  173.             setcolor(10);
  174.             cin >> input;
  175.             setcolor(7);
  176.  
  177.                 // Start game at level 1
  178.             if (input == 1) {
  179.                 SetMap(0);
  180.                 break;
  181.             }
  182.                 // Go to select screen
  183.             else if (input == 2) {
  184.                     // Level select loop
  185.                 while (1) {
  186.                         // Show level select screen
  187.                     system("cls");
  188.                     setcolor(202);
  189.                     cout <<
  190.                     "######################################\n"
  191.                     "#                                    #\n"
  192.                     "#            LevelSelect             #\n"
  193.                     "#                                    #\n"
  194.                     "#   1. level1                        #\n"
  195.                     "#   2. level2                        #\n"
  196.                     "#   3. level3                        #\n"
  197.                     "#   4. level4                        #\n"
  198.                     "#                                    #\n"
  199.                     "#                                    #\n"
  200.                     "#                 9 - Return to menu #\n"
  201.                     "######################################\n\n";
  202.                     setcolor(7);
  203.                     cout << "Enter a choice: ";
  204.  
  205.                         // Get input
  206.                     setcolor(10);
  207.                     cin >> input;
  208.                     setcolor(7);
  209.  
  210.                         // Go to selected map
  211.                     if (input >= 1 && input <= sizeof(Maps) / sizeof(*Maps)) {
  212.                         SetMap(input - 1);
  213.                         break;
  214.                     }
  215.                         // Return to menu
  216.                     else if (input == 9) {
  217.                         break;
  218.                     }
  219.                         // Invalid input
  220.                     else {
  221.                         setcolor(10);
  222.                         cout << "That's not an option. Try again...\n";
  223.                         setcolor(7);
  224.                         system("pause >nul");
  225.                     }
  226.                 }
  227.                     // Break menu loop
  228.                 if (input != 9)
  229.                     break;
  230.             }
  231.                 // Show instructions
  232.             else if (input == 3) {
  233.                 system("cls");
  234.                 setcolor(202);
  235.                 cout <<
  236.                 "######################################\n"
  237.                 "#                                    #\n"
  238.                 "#            Instructions            #\n"
  239.                 "#                                    #\n"
  240.                 "#   Blah blah.                       #\n"
  241.                 "#   Blahdeliblah blaha               #\n"
  242.                 "#   Remember to blah the blaha       #\n"
  243.                 "#   And ablaha the hablahala.        #\n"
  244.                 "#                                    #\n"
  245.                 "#                                    #\n"
  246.                 "#          Press any key to continue #\n"
  247.                 "######################################\n\n";
  248.                 setcolor(7);
  249.                 system("pause >nul");
  250.             }
  251.                 // Quit
  252.             else if (input == 4)
  253.                 return 0;
  254.                 // Invalid input
  255.             else {
  256.                 setcolor(10);
  257.                 cout << "That's not an option. Try again...\n";
  258.                 setcolor(7);
  259.                 system("pause >nul");
  260.             }
  261.         }
  262.  
  263.             // Game logic loop
  264.         DrawMap();
  265.         while (1) {
  266.                 // Get new coords
  267.             int px2 = px, py2 = py;
  268.             if (GetAsyncKeyState(VK_UP))
  269.                 py2 = py - 1;
  270.             else if (GetAsyncKeyState(VK_DOWN))
  271.                 py2 = py + 1;
  272.             else if (GetAsyncKeyState(VK_LEFT))
  273.                 px2 = px - 1;
  274.             else if (GetAsyncKeyState(VK_RIGHT))
  275.                 px2 = px + 1;
  276.                 // Quit when esc is pressed
  277.             else if (GetAsyncKeyState(VK_ESCAPE))
  278.                 break;
  279.                 // No logic is necessary unless a key is pressed
  280.             else
  281.                 continue;
  282.  
  283.                 // Interact with objects
  284.                 // Spike
  285.             if (Map.data[py2][px2] == '*') {
  286.                 Hp -= 5;
  287.                     // Game over if no hp
  288.                 if (Hp <= 0)
  289.                     break;
  290.             }
  291.                 // Hp boost
  292.             else if (Map.data[py2][px2] == '+') {
  293.                 Hp += 5;
  294.                     // Limit hp
  295.                 if (Hp >= MaxHp)
  296.                     Hp = MaxHp;
  297.             }
  298.                 // Goal
  299.             /*else if (Map.data[py2][px2] == '-') {
  300.                     // Quit game loop if out of maps
  301.                 if (Level + 1 >= sizeof(Maps) / sizeof(*Maps))
  302.                     break;
  303.                     // Go to next map
  304.                 SetMap(Level + 1);
  305.             } //Old Setup for goal
  306.             */
  307.             else if (Map.data[py2][px2] == '-') {
  308.                     // Quit game loop if out of maps
  309.                 if (Level + 1 >= sizeof(Maps) / sizeof(*Maps))
  310.                     break;
  311.                     // Go to next map
  312.                 SetMap(Level + 1);
  313.                 DrawMap();
  314.                 continue;
  315.             }
  316.                 // Wall
  317.             else if (Map.data[py2][px2] == '#') {
  318.                 px2 = px;
  319.                 py2 = py;
  320.             }
  321.                 // Key
  322.             else if (Map.data[py2][px2] == 'K')
  323.                 Keys++;
  324.                 // Door
  325.             else if (Map.data[py2][px2] == ':') {
  326.                 if (!Keys) {
  327.                     px2 = px;
  328.                     py2 = py;
  329.                 } else
  330.                     Keys--;
  331.             }
  332.  
  333.                 // Move character
  334.             Map.data[py][px] = ' ';
  335.             Map.data[py2][px2] = '@';
  336.  
  337.                 // Set new choords
  338.             px = px2;
  339.             py = py2;
  340.  
  341.                 // Draw map
  342.             DrawMap();
  343.  
  344.                 // Sleep
  345.             Sleep(Gamespeed);
  346.         }
  347.  
  348.             // Show game over screen if no hp
  349.         if (Hp <= 0) {
  350.             system("cls");
  351.             setcolor(12);
  352.             cout << "\n\n\n"
  353.             "###########\n"
  354.             "#GAME OVER#\n"
  355.             "###########\n";
  356.             setcolor(7);
  357.             system("pause >nul");
  358.         }
  359.     }
  360. }
Advertisement
Add Comment
Please, Sign In to add comment