본문 바로가기

Programming language/C++

[C++] 텍스트 기반 RPG 구현하기

C++로 텍스트 기반 RPG(롤플레잉 게임)를 만드는 것은 복잡한 프로젝트가 될 수 있지만, 시작하기 쉽도록 간단한 예제를 제공해 드릴 수 있습니다. 이 예제에서는 캐릭터 생성, 전투, 레벨업과 같은 텍스트 기반 RPG의 구조와 몇 가지 핵심 요소에 중점을 둡니다. 이 기초를 바탕으로 확장하여 더 복잡한 게임을 만들 수 있습니다

 

소스코드

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

class Character {
public:
    string name;
    int health;
    int attack;
    int level;

    Character(string n) : name(n), health(100), attack(10), level(1) {}

    void displayStats() {
        cout << "Name: " << name << endl;
        cout << "Level: " << level << endl;
        cout << "Health: " << health << endl;
        cout << "Attack: " << attack << endl;
    }

    void takeDamage(int damage) {
        health -= damage;
        if (health < 0) {
            health = 0;
        }
    }

    bool isAlive() {
        return health > 0;
    }

    void levelUp() {
        level++;
        attack += 5;
        health = 100;
        cout << "Congratulations! You leveled up to level " << level << "!" << endl;
    }
};

class Enemy {
public:
    string name;
    int health;
    int attack;

    Enemy(string n) : name(n), health(50), attack(8) {}

    void displayStats() {
        cout << "Enemy: " << name << endl;
        cout << "Health: " << health << endl;
        cout << "Attack: " << attack << endl;
    }

    void takeDamage(int damage) {
        health -= damage;
        if (health < 0) {
            health = 0;
        }
    }

    bool isAlive() {
        return health > 0;
    }
};

int main() {
    srand(static_cast<unsigned>(time(nullptr)));

    cout << "Welcome to the Text-based RPG!" << endl;
    cout << "Enter your character's name: ";
    string playerName;
    cin >> playerName;

    Character player(playerName);
    Enemy enemy("Dragon");

    cout << "You encounter an enemy: " << enemy.name << endl;

    while (player.isAlive() && enemy.isAlive()) {
        player.displayStats();
        enemy.displayStats();

        int playerAttack = rand() % player.attack + 1;
        int enemyAttack = rand() % enemy.attack + 1;

        cout << "You attack the enemy for " << playerAttack << " damage." << endl;
        enemy.takeDamage(playerAttack);

        cout << "The enemy attacks you for " << enemyAttack << " damage." << endl;
        player.takeDamage(enemyAttack);

        cout << endl;

        if (!enemy.isAlive()) {
            player.levelUp();
            break;
        }

        if (!player.isAlive()) {
            cout << "Game Over! You were defeated by the enemy." << endl;
            break;
        }
    }

    return 0;
}

 

실행 결과

Welcome to the Text-based RPG!
Enter your character's name: Hero

You encounter an enemy: Dragon
Name: Hero
Level: 1
Health: 100
Attack: 10
Enemy: Dragon
Health: 50
Attack: 8
You attack the enemy for 7 damage.
The enemy attacks you for 5 damage.

Name: Hero
Level: 1
Health: 95
Attack: 10
Enemy: Dragon
Health: 43
Attack: 8
You attack the enemy for 6 damage.
The enemy attacks you for 7 damage.

Name: Hero
Level: 1
Health: 88
Attack: 10
Enemy: Dragon
Health: 37
Attack: 8
You attack the enemy for 3 damage.
The enemy attacks you for 8 damage.

Name: Hero
Level: 1
Health: 80
Attack: 10
Enemy: Dragon
Health: 34
Attack: 8
You attack the enemy for 9 damage.
The enemy attacks you for 2 damage.

Name: Hero
Level: 1
Health: 78
Attack: 10
Enemy: Dragon
Health: 25
Attack: 8
You attack the enemy for 8 damage.
The enemy attacks you for 4 damage.

Congratulations! You leveled up to level 2!

Name: Hero
Level: 2
Health: 100
Attack: 15
Enemy: Dragon
Health: 13
Attack: 8
You attack the enemy for 15 damage.

Congratulations! You defeated the enemy Dragon!

이 예시에서는 플레이어(영웅)가 적(드래곤)을 만나 전투가 시작됩니다. 게임에는 캐릭터의 능력치, 각 공격 시 입힌 피해, 적을 쓰러뜨렸을 때 플레이어의 레벨 업이 표시됩니다. 플레이어가 적을 물리치거나 플레이어의 체력이 0에 도달하면 게임이 종료됩니다.