Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <conio.h> // Use only on Windows, remove for Linux/macOS
- #include <thread>
- #include <chrono>
- using namespace std;
- //function prototype
- void showUI();
- //global variables
- const int rows = 15;
- const int cols = 25;
- char map[rows][cols];
- // Main() function START HERE
- int main() {
- //initialize variables
- int life = 3;
- int minx = 0;
- int miny = 0;
- int maxx = cols - 1;
- int maxy = rows - 1;
- int treasurex = 0, treasurey = 2;
- int playerx = 10, playery = 10;
- int trap1x = 0, trap1y = 5;
- int trap2x = 15, trap2y = 5;
- int trap3x = 5, trap3y = 10;
- int trap4x = 15, trap4y = 10;
- int lifeaddx = 10, lifeaddy = 5;
- char key = '\0';
- // Initialize the map
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- map[i][j] = '*';
- }
- }
- map[playery][playerx] = 'P'; // Mark player position
- cout << "Welcome to the game" << endl;
- cout << "You are at position (0,0)" << endl;
- cout << "Enter 'w' to move up, 's' to move down, 'a' to move left, 'd' to move right" << endl;
- cout << "Enter 'q' to quit the game" << endl;
- system("pause"); // Only works on Windows
- system("cls"); // Use system("clear") for Unix/Linux
- showUI();
- while (true) {
- key = _getch(); // Get user input
- // Clear screen for next move
- system("cls");
- // Erase the player's previous position
- if (map[playery][playerx] == '*'|| map[playery][playerx] == 'P')
- {
- map[playery][playerx] = ' ';
- }
- // Handle movement
- switch (key) {
- case 'w': if (playery > miny) playery--; break;
- case 's': if (playery < maxy) playery++; break;
- case 'a': if (playerx > minx) playerx--; break;
- case 'd': if (playerx < maxx) playerx++; break;
- case 'q':
- cout << "You quit the game" << endl;
- exit(0);
- }
- cout << "You are at position (" << playerx << "," << playery << ")" << endl;
- // Check for game events
- if (playerx == treasurex && playery == treasurey) {
- cout << "You found the treasure! You win!" << endl;
- map[playery][playerx] = '$';
- break;
- }
- else if ((playerx == trap1x && playery == trap1y) ||
- (playerx == trap2x && playery == trap2y) ||
- (playerx == trap3x && playery == trap3y) ||
- (playerx == trap4x && playery == trap4y)) {
- cout << "You stepped on a trap! Life -1" << endl;
- life--;
- map[playery][playerx] = '&';
- }
- else if (playerx == lifeaddx && playery == lifeaddy) {
- cout << "You found a life! Life +1" << endl;
- life++;
- map[playery][playerx] = '+';
- }
- else {
- map[playery][playerx] = 'P'; // Mark new player position
- }
- cout << "Your life: " << life << endl;
- if (life == 0) {
- cout << "You lost the game!" << endl;
- break;
- }
- // Display the map
- showUI();
- this_thread::sleep_for(chrono::milliseconds(100)); // Slow down the loop
- }
- return 0;
- }
- void showUI() {
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- cout << map[i][j];
- }
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement