Advertisement
DrAungWinHtut

advanture2.cpp

Mar 16th, 2025
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <conio.h> // Use only on Windows, remove for Linux/macOS
  4. #include <thread>
  5. #include <chrono>
  6.  
  7. using namespace std;
  8. //function prototype
  9. void showUI();
  10.  
  11.  
  12. //global variables
  13. const int rows = 15;
  14. const int cols = 25;
  15. char map[rows][cols];
  16. // Main() function START HERE
  17. int main() {
  18.     //initialize variables
  19.     int life = 3;
  20.     int minx = 0;
  21.     int miny = 0;
  22.     int maxx = cols - 1;
  23.     int maxy = rows - 1;
  24.  
  25.     int treasurex = 0, treasurey = 2;
  26.     int playerx = 10, playery = 10;
  27.  
  28.     int trap1x = 0, trap1y = 5;
  29.     int trap2x = 15, trap2y = 5;
  30.     int trap3x = 5, trap3y = 10;
  31.     int trap4x = 15, trap4y = 10;
  32.  
  33.     int lifeaddx = 10, lifeaddy = 5;
  34.     char key = '\0';
  35.  
  36.     // Initialize the map
  37.     for (int i = 0; i < rows; i++) {
  38.         for (int j = 0; j < cols; j++) {
  39.             map[i][j] = '*';
  40.         }
  41.     }
  42.     map[playery][playerx] = 'P';  // Mark player position
  43.  
  44.     cout << "Welcome to the game" << endl;
  45.     cout << "You are at position (0,0)" << endl;
  46.     cout << "Enter 'w' to move up, 's' to move down, 'a' to move left, 'd' to move right" << endl;
  47.     cout << "Enter 'q' to quit the game" << endl;
  48.  
  49.     system("pause");  // Only works on Windows
  50.     system("cls");    // Use system("clear") for Unix/Linux
  51.     showUI();
  52.     while (true) {
  53.         key = _getch();  // Get user input
  54.         // Clear screen for next move  
  55.         system("cls");
  56.         // Erase the player's previous position
  57.         if (map[playery][playerx] == '*'|| map[playery][playerx] == 'P')
  58.         {
  59.             map[playery][playerx] = ' ';
  60.         }
  61.        
  62.  
  63.         // Handle movement
  64.         switch (key) {
  65.         case 'w': if (playery > miny) playery--; break;
  66.         case 's': if (playery < maxy) playery++; break;
  67.         case 'a': if (playerx > minx) playerx--; break;
  68.         case 'd': if (playerx < maxx) playerx++; break;
  69.         case 'q':
  70.             cout << "You quit the game" << endl;
  71.             exit(0);
  72.         }
  73.  
  74.         cout << "You are at position (" << playerx << "," << playery << ")" << endl;
  75.        
  76.         // Check for game events
  77.         if (playerx == treasurex && playery == treasurey) {
  78.             cout << "You found the treasure! You win!" << endl;
  79.             map[playery][playerx] = '$';
  80.             break;
  81.         }
  82.         else if ((playerx == trap1x && playery == trap1y) ||
  83.             (playerx == trap2x && playery == trap2y) ||
  84.             (playerx == trap3x && playery == trap3y) ||
  85.             (playerx == trap4x && playery == trap4y)) {
  86.             cout << "You stepped on a trap! Life -1" << endl;
  87.             life--;
  88.             map[playery][playerx] = '&';
  89.         }
  90.         else if (playerx == lifeaddx && playery == lifeaddy) {
  91.             cout << "You found a life! Life +1" << endl;
  92.             life++;
  93.             map[playery][playerx] = '+';
  94.         }
  95.         else {
  96.             map[playery][playerx] = 'P';  // Mark new player position
  97.         }
  98.  
  99.         cout << "Your life: " << life << endl;
  100.  
  101.         if (life == 0) {
  102.             cout << "You lost the game!" << endl;
  103.             break;
  104.         }
  105.        
  106.         // Display the map
  107.         showUI();
  108.         this_thread::sleep_for(chrono::milliseconds(100)); // Slow down the loop
  109.     }
  110.  
  111.     return 0;
  112. }
  113.  
  114.  
  115. void showUI() {
  116.     for (int i = 0; i < rows; i++) {
  117.         for (int j = 0; j < cols; j++) {
  118.             cout << map[i][j];
  119.         }
  120.         cout << endl;
  121.     }
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement