Text RPG 만들기
본인의 재량에 따라 text로 진행되는 간단한 rpg 게임을 만들어 봅시다.
✨ 필수 요소
- 게임 시작
- 전투(반복)
- 사망확인
- 승리/패배
제가 만든 게임은 간단하게 몬스터를 잡는 게임입니다.
//플레이어 구조체
struct Player {
string name;
//int level;
int hp;
int mp;
int damage;
Player(string _name) {
name = _name;
//level = 1;
hp = 100;
mp = 50;
damage = 5;
}
};
//몬스터 구조체
struct Monster {
string name;
int hp;
int damage;
Monster(string _name, int _hp, int _damage) {
name = _name;
hp = _hp;
damage = _damage;
}
};
//몬스터가 플레이어를 때렸을 때
void TakeDamage(Player &ch, Monster &mon) {
ch.hp -= mon.damage;
}
//플레이어가 몬스터를 때렸을 때
void TakeDamage(Monster &mon, Player &ch, int skill) {
if (skill == 1) {
mon.hp -= ch.damage;
}
else if (skill == 2) {
mon.hp -= (ch.damage + 5);
ch.mp -= 10;
}
}
//플레이어나 몬스터가 죽었는지 체크
bool Result_(Player ch, Monster mon) {
if (ch.hp <= 0) {
cout << "플레이어가 사망했습니다..." << endl;
return false;
}
else if (mon.hp <= 0) {
cout << "몬스터를 처치했습니다!" << endl;
return false;
}
else {
return true;
}
}
//플레이어 스테이터스 확인
void PrintStatus(Player ch) {
cout << endl;
cout << "-----------------------------------------" << endl;
cout << "name : " << ch.name << endl;
cout << "HP : " << ch.hp << endl;
cout << "MP : " << ch.mp << endl;
cout << "Damage : " << ch.damage << endl;
cout << "-----------------------------------------" << endl;
}
//몬스터 스테이터스 확인
void PrintStatus(Monster mon) {
cout << "-----------------------------------------" << endl;
cout << "name : " << mon.name << endl;
cout << "HP : " << mon.hp << endl;
cout << "Damage : " << mon.damage << endl;
cout << "-----------------------------------------" << endl << endl;
}
//플레이어가 몬스터에게 공격을 하는 함수
void Battle(Player &ch, Monster &mon) {
cout << "1)베기 2)내려찍기(MP -10)" << endl;
int act;
while (true) {
cin >> act;
if (act == 1) {
cout << "베기!!" << endl;
TakeDamage(mon, ch, 1);
break;
}
else if (act == 2) {
if (ch.mp < 10) {
cout << "MP가 부족합니다. 1)베기 2)내려찍기(MP -10)" << endl;
}
else {
cout << "내려찍기!!!!" << endl;
TakeDamage(mon, ch, 2);
break;
}
}
else {
cout << "잘못된 번호입니다. 다시 입력해주세요. 1)베기 2)내려찍기(MP -10)" << endl;
}
}
}
//다음 몬스터로 갈 수 있는지 없는지 확인 - 플레이어가 죽었는지 확인한다.
bool NextStage(Player ch) {
if (ch.hp > 0) {
return 1;
}
else {
return 0;
}
}
cout << "[ Game Start ]" << endl;
cout << "Player의 이름을 알려주세요 : ";
string inputName; //이름 입력
bool collectName = false; // 이름 체크
string inputAns; //입력한 이름이 맞는지 확인
Player player("이름");
while (true) {
cin >> inputName;
cout << "입력하신 이름이 " << inputName << "님 맞습니까?(Y,N 대문자로 입력)" << endl;
//입력된 닉네임이 맞는지 아닌지 체크
while (true) {
cin >> inputAns;
if (inputAns == "Y") {
collectName = true;
break;
}
else if (inputAns == "N") {
collectName = false;
break;
}
else {
cout << "잘못된 입력입니다. 다시 입력해주세요 (Y,N 대문자로 입력)" << endl;
}
}
//설정된 닉네임이 맞을경우 시작 / 아니면 다시 위로 가서 닉네임 입력
if (collectName == true) {
player.name = inputName;
cout << inputName << "님 환영합니다!" << endl;
break;
}
else {
cout << "닉네임을 다시 입력해주세요 : " << endl;
}
}

이름을 입력하고 첫번째 몬스터와 조우
플레이어가 공격방식을 선택

적을 처치후 HP와 MP를 회복시켜준 후 다음 몬스터 등장

다음 몬스터를 처치 하지 못했을 경우 사망하고
Game Over... 을 출력

만약 2단계까지 클리어 후 3단계에서 MP가 부족하면
MP가 부족하다고 출력 후 다른 선택지를 출력

클리어 후 Victory! 글자를 출력하도록 설계하였음.
어려운 것은 없었으나 아직 부족한 부분이 많이 보임.
1. 랜덤한 대미지가 아니므로 클리어에 문제가 없음.
2. 몬스터의 수가 너무 적음.
3. 대미지 상승 요소나 MP를 사용하는 방식이 적음.
재미는 있었으나 더 열심히 해봐야겠다고 생각하였음.
전체 소스코드 입니다.
#include<iostream>
using namespace std;
//플레이어 구조체
struct Player {
string name;
//int level;
int hp;
int mp;
int damage;
Player(string _name) {
name = _name;
//level = 1;
hp = 100;
mp = 50;
damage = 5;
}
};
//몬스터 구조체
struct Monster {
string name;
int hp;
int damage;
Monster(string _name, int _hp, int _damage) {
name = _name;
hp = _hp;
damage = _damage;
}
};
//몬스터가 플레이어를 때렸을 때
void TakeDamage(Player &ch, Monster &mon) {
ch.hp -= mon.damage;
}
//플레이어가 몬스터를 때렸을 때
void TakeDamage(Monster &mon, Player &ch, int skill) {
if (skill == 1) {
mon.hp -= ch.damage;
}
else if (skill == 2) {
mon.hp -= (ch.damage + 5);
ch.mp -= 10;
}
}
//플레이어나 몬스터가 죽었는지 체크
bool Result_(Player ch, Monster mon) {
if (ch.hp <= 0) {
cout << "플레이어가 사망했습니다..." << endl;
return false;
}
else if (mon.hp <= 0) {
cout << "몬스터를 처치했습니다!" << endl;
return false;
}
else {
return true;
}
}
//플레이어 스테이터스 확인
void PrintStatus(Player ch) {
cout << endl;
cout << "-----------------------------------------" << endl;
cout << "name : " << ch.name << endl;
cout << "HP : " << ch.hp << endl;
cout << "MP : " << ch.mp << endl;
cout << "Damage : " << ch.damage << endl;
cout << "-----------------------------------------" << endl;
}
//몬스터 스테이터스 확인
void PrintStatus(Monster mon) {
cout << "-----------------------------------------" << endl;
cout << "name : " << mon.name << endl;
cout << "HP : " << mon.hp << endl;
cout << "Damage : " << mon.damage << endl;
cout << "-----------------------------------------" << endl << endl;
}
//플레이어가 몬스터에게 공격을 하는 함수
void Battle(Player &ch, Monster &mon) {
cout << "1)베기 2)내려찍기(MP -10)" << endl;
int act;
while (true) {
cin >> act;
if (act == 1) {
cout << "베기!!" << endl;
TakeDamage(mon, ch, 1);
break;
}
else if (act == 2) {
if (ch.mp < 10) {
cout << "MP가 부족합니다. 1)베기 2)내려찍기(MP -10)" << endl;
}
else {
cout << "내려찍기!!!!" << endl;
TakeDamage(mon, ch, 2);
break;
}
}
else {
cout << "잘못된 번호입니다. 다시 입력해주세요. 1)베기 2)내려찍기(MP -10)" << endl;
}
}
}
//다음 몬스터로 갈 수 있는지 없는지 확인 - 플레이어가 죽었는지 확인한다.
bool NextStage(Player ch) {
if (ch.hp > 0) {
return 1;
}
else {
return 0;
}
}
int main() {
cout << "[ Game Start ]" << endl;
cout << "Player의 이름을 알려주세요 : ";
string inputName; //이름 입력
bool collectName = false; // 이름 체크
string inputAns; //입력한 이름이 맞는지 확인
Player player("이름");
while (true) {
cin >> inputName;
cout << "입력하신 이름이 " << inputName << "님 맞습니까?(Y,N 대문자로 입력)" << endl;
//입력된 닉네임이 맞는지 아닌지 체크
while (true) {
cin >> inputAns;
if (inputAns == "Y") {
collectName = true;
break;
}
else if (inputAns == "N") {
collectName = false;
break;
}
else {
cout << "잘못된 입력입니다. 다시 입력해주세요 (Y,N 대문자로 입력)" << endl;
}
}
//설정된 닉네임이 맞을경우 시작 / 아니면 다시 위로 가서 닉네임 입력
if (collectName == true) {
player.name = inputName;
cout << inputName << "님 환영합니다!" << endl;
break;
}
else {
cout << "닉네임을 다시 입력해주세요 : " << endl;
}
}
//////////////////////////////////////////////////////////////////////
Monster rat("Rat", 10, 1); //쥐 체력 10, 대미지 1
Monster pig("Pig", 15, 50); //돼지 체력 10, 대미지 1
Monster cow("Cow", 190, 4); //소 체력 190, 대미지 4
Monster mon[3] = { rat, pig, cow };
bool vic = true;
for (int i = 0; i < 3; i++) {
cout << endl;
cout << i+1 <<"번 몬스터 > [" << mon[i].name << "]" << endl;
//count 3번당 마나 10회복
int count = 0;
while (true) {
if (count == 3) {
count = 0;
player.mp += 10;
}
PrintStatus(player);
PrintStatus(mon[i]);
Battle(player, mon[i]);
count++;
//내가 준 대미지로 몬스터가 죽었는지 확인
if (Result_(player, mon[i])) {
TakeDamage(player, mon[i]);
}
else {
break;
}
//몬스터가 준 대미지로 내가 죽었는지 확인
if (!Result_(player, mon[i])) {
break;
}
}
if (NextStage(player)) {
cout << "HP와 MP가 회복되었습니다." << endl;
player.hp = 100;
player.mp = 50;
}
else {
vic = false;
break;
}
}
if (vic == true) {
cout << "Victory!" << endl;
}
else {
cout << "Game Over..." << endl;
}
return 0;
}