[C++] 내가 만든 TextRPG 게임

2022. 6. 3. 19:46코딩 1막 <C++개념편>/코딩 1막 <C++응용편>

728x90

*목표 : 몬스터와 플레이어들 간의 상호작용을 통하여 한쪽이 죽으면 끝나는 게임을 만들고자 했다.

*구현 순서

1. 몬스터 구조체와 플레이어 구조체를 만든다

2. 각각 속성과 기능에 무엇이 필요한지 고민 후 만든다

3. 몬스터와 플레이어가 싸우려면 외부함수를 두어 상호작용할수있게 만들어본다

#include <iostream>
#include <string>

using namespace std;

struct Monster
{
    string name;
    int hp;
    int atk;
    bool isDie;

    void Hit(float damage)
    {
        cout << "몬스터가 맞음" << endl;
        hp -= damage;
        if (hp <= 0)
        {
            Die();
        }
    }
    void Die()
    {
        isDie = true;
        cout << name << "은 죽었다" << endl;
    }
};

struct Player
{
    string name;
    int hp;
    int atk;
    bool isDie;

    void Hit(float damage)
    {
        cout << "플레이어가 맞음" << endl;
        hp -= damage;
        if (hp <= 0)
        {
            Die();
        }
    }
    void Die()
    {
        isDie = true;
        cout << name << "은 죽었다" << endl;
    }
};

bool Battle(Player* player, Monster* monster)
{
    bool isBattleEnd = false;

    cout << "----------------------------------------" << endl;
    cout << player->name << "과 " << monster->name << "의 전투" << endl;
    cout << player->name << "의 공격 : " << player->atk << "만큼 데미지!" << endl;
    monster->Hit(player->atk);

    if (monster->isDie == false)
    {
        cout << monster->name << "의 공격 : " << monster->atk << "만큼 데미지!" << endl;
        player->Hit(monster->atk);
    }

    cout << player->name << "의 체력 : " << player->hp << endl;
    cout << monster->name << "의 체력 : " << monster->hp << endl;
    cout << "----------------------------------------" << endl;

    isBattleEnd = monster->isDie || player->isDie;
    return isBattleEnd;
}


void main()
{

    Player player;
    player.name = "용사A";
    player.hp = 1000;
    player.atk = 100;
    player.isDie = false;
    Monster monster;
    monster.name = "슬라임";
    monster.hp = 600;
    monster.atk = 200;
    monster.isDie = false;

    while (true)
    {
        if (Battle(&player, &monster) == true)
        {
            break;
        }
    }

}

출력 결과

*고찰

1. 멤버의 기능이나 속성을 추가하면 좋겠다

- 예) 플레이어가 갖는 인벤토리기능, 상점의 추가 및  게임 화폐를 통한 물리기능 등..

2. 플레이어의 활동범위를 제한 하지 않고 다양한 공간을 추가했으면..

- 예) 상점, 사냥터 추가

3. 스토리를 추가하고 싶다. 그러려면.. 스토리보드 기획부터 다시짜야하는데..

나중에 천천히 해봐야겠다.

728x90