Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstdlib>
- #include <time.h>
- using namespace std;
- /**Klasa dowolnej postaci*/
- class Character // Entity
- {
- private:
- string name;
- int health;
- int maxHealth;
- int damage;
- public:
- Character(string characterName)
- {
- name = characterName;
- maxHealth = 10;
- health = maxHealth;
- damage = 2;
- }
- string get_name() const
- {
- return name;
- }
- int get_health() const
- {
- return health;
- }
- int get_max_health() const
- {
- return maxHealth;
- }
- int get_damage() const
- {
- return damage;
- }
- /**
- * Dodaje zdrowia postaci
- * @param value ilość zdrowia do dodania
- * @return nic nie zwraca
- */
- void add_health(int value)
- {
- health += value;
- if (health > maxHealth)
- health = maxHealth;
- }
- void lower_health(int value)
- {
- health -= value;
- if (health < 0)
- health = 0;
- }
- };
- class Player : public Character
- {
- private:
- int metal;
- int oxygen;
- public:
- Player(string playerName) : Character(playerName)
- {
- metal = 0;
- oxygen = 100;
- }
- Player() : Player("John Doe")
- {
- }
- int get_metal() const
- {
- return metal;
- }
- int get_oxygen() const
- {
- return oxygen;
- }
- bool has_oxygen()
- {
- return get_oxygen() > 0;
- }
- void lower_oxygen(int value)
- {
- oxygen -= value;
- if (oxygen < 0)
- oxygen = 0;
- }
- void add_oxygen(int value)
- {
- oxygen += value;
- if (oxygen > 100)
- oxygen = 100;
- }
- void add_metal(int value)
- {
- metal += value;
- }
- void rest()
- {
- cout << this->get_name() << " odpoczywa. Zużywa 2% tlenu." << endl
- << endl;
- this->lower_oxygen(2);
- this->add_health(5);
- display_player_info();
- }
- void display_player_info()
- {
- cout << "Stan gracza" << endl;
- cout << "-----------------------------------------------------------------" << endl;
- cout << "Życie: " << this->get_health() << "/" << this->get_max_health() << endl;
- cout << "Atak: " << this->get_damage() << endl;
- cout << "Tlen: " << this->get_oxygen() << endl;
- cout << "Metal: " << this->get_metal() << endl;
- }
- };
- class Game
- {
- private:
- Player player;
- void attack(Character *enemy, bool isCombatRunning)
- {
- int playerDamage = rand() % player.get_damage() + 1;
- enemy->lower_health(playerDamage);
- cout << player.get_name() << " atakuje! Zadaje " << enemy->get_name() << " " <<
- playerDamage << " punkty obrażeń!" << endl;
- if (enemy->get_health() > 0)
- {
- int enemyDamage = rand() % enemy->get_damage() + 1;
- player.lower_health(enemyDamage);
- cout << enemy->get_name() << " atakuje! Zadaje " << player.get_name() << " " <<
- enemyDamage << " punkty obrażeń!" << endl;
- if (player.get_health() <= 0)
- {
- cout << player.get_name() << " przegrywa! Koniec walki" << endl;
- isCombatRunning = false;
- }
- }
- else
- {
- cout << enemy->get_name() << " przegrywa! Koniec walki" << endl;
- player.add_metal(10);
- cout << player.get_name() << " znajduje 10 metalu!" << endl;
- isCombatRunning = false;
- }
- }
- public:
- Game(string playerName)
- {
- player = Player(playerName);
- cout << player.get_name() << endl;
- }
- void search_resources()
- {
- cout << endl
- << player.get_name() << " wybiera się na zewnątrz by szukać zasobów. "
- << "Zużyto 5% tlenu\n";
- player.lower_oxygen(5);
- cout << "----------------------------------------------------------\n";
- int chance = rand() % 3; // 0 , 1 albo 2
- switch (chance)
- {
- case 0:
- {
- int oxygen = rand() % 10 + 1;
- cout << player.get_name() << " ma szczęście. Natrafił na mały zbiornik tlenu!" << endl;
- cout << "Zasób tlenu zwiększony o" << oxygen << " % !" << endl
- << endl;
- player.add_oxygen(oxygen);
- player.display_player_info();
- break;
- }
- case 1:
- cout << player.get_name() << " natrafił na małe złoże metalu. " << endl;
- cout << "Zasoby metalu zwiększone o 5 ! \n\n";
- player.add_metal(5);
- player.display_player_info();
- break;
- case 2:
- cout << player.get_name() << " napotkał przeciwnika!" << endl;
- start_combat();
- break;
- }
- }
- void start_combat()
- {
- Character enemy = Character("Kosmita");
- bool isCombatRunning = true;
- int playerDamage, enemyDamage, choice;
- while (isCombatRunning)
- {
- cout << endl
- << "Co chcesz zrobić?" << endl;
- cout << "------------------------------------------------\n";
- cout << "Twoje życie: " << player.get_health() << "\t|\t"
- << "Życie przeciwnika: " << enemy.get_health() << endl;
- cout << "------------------------------------------------\n";
- cout << "[1] Atakuj!\n";
- cout << "[2] Uciekaj!\n";
- cout << "Wpisz numer: ";
- cin >> choice;
- switch (choice)
- {
- case 1:
- // Atak
- // TO DO
- attack(&enemy, &isCombatRunning);
- break;
- case 2:
- // Uciekaj
- isCombatRunning = false;
- player.lower_oxygen(5);
- cout << player.get_name() << " ucieka tracąc 5% tlenu! Koniec walki." << endl;
- break;
- }
- }
- }
- void print_my_menu()
- {
- cout << endl
- << "Co chcesz zrobić?" << endl;
- cout << "--------------------------------------------- \n";
- cout << "[1] Sprawdź swój stan." << endl;
- cout << "[2] Odpocznij." << endl;
- cout << "[3] Szukaj zasobów." << endl;
- if (player.get_metal() >= 50)
- {
- cout << "[4] Zbuduj statek. " << endl;
- }
- cout << "[0] Skończ grę." << endl;
- }
- void start_game()
- {
- cout << "------------------------------------------------" << endl;
- cout << "Rozbiłeś się na księżycu." << endl;
- cout << "Twoim zadaniem jest przetrwanie i zdobycie wystarczającej ilości surowców by"
- << " zbudować statek do domu." << endl;
- cout << "Uważaj na zapasy swojego tlenu, bez niego nie możesz kontynuować gry." << endl;
- cout << "Powodzenia " << player.get_name() << "!\n\n";
- player.display_player_info();
- // runtime
- game_runtime();
- }
- void game_runtime()
- {
- int playerChoice{};
- bool isGameRunning = true;
- while (isGameRunning)
- {
- print_my_menu();
- cout << "Wpisz numer: ";
- cin >> playerChoice;
- switch (playerChoice)
- {
- case 1:
- player.display_player_info();
- break;
- case 2:
- player.rest();
- // TO DO
- if (!player.has_oxygen())
- {
- cout << "Gracz przegrywa!" << endl;
- cout << "Koniec Gry! Spróbuj ponownie!" << endl;
- isGameRunning = false;
- }
- break;
- case 3:
- search_resources();
- // TO DO
- if (!player.has_oxygen())
- {
- cout << "Gracz przegrywa!" << endl;
- cout << "Koniec Gry! Spróbuj ponownie!" << endl;
- isGameRunning = false;
- }
- break;
- case 4:
- if (player.get_metal() >= 50)
- {
- cout << "Gracz wygrywa!" << endl;
- cout << "Pomyślnie zbudowałeś statek i wydostałeś się z księzyca" << endl;
- cout << "Dzięki za grę !" << endl;
- }
- else
- {
- cout << "Opcja niedostępna" << endl;
- }
- break;
- case 0:
- cout << "Koniec gry!" << endl;
- isGameRunning = false;
- break;
- }
- }
- }
- };
- using Entity = Character;
- int main(int argc, char const *argv[])
- {
- cout << "Podaj swoje imię:";
- string name;
- getline(cin, name);
- srand(time(0));
- Game game(name);
- game.start_game();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement