Tecnología

Inicio

Cómo hacer un juego de rol en C ++

Cómo hacer un juego de rol en C ++


Cuando se juega un juego de rol o RPG, que toma el papel de un personaje imaginario y la experiencia de sus aventuras. Estos juegos pueden ser bastante complejos e implican gráficos pesados. Los desarrolladores suelen trabajar varios meses en la codificación y probar el juego. Sin embargo, puede crear un juego de rol simple en C ++. Por otra parte, sólo tiene que entender los conceptos básicos de programación C ++. Se puede crear un simple juego de rol mediante el uso de una matriz bidimensional como el mapa y algunas clases para controlar su carácter, los personajes enemigos y el juego-juego.

Instrucciones

1 Abra la carpeta C ++ editor favorito y comenzar un nuevo proyecto. Incluir el "iostream" y bibliotecas "ctime" usando este código:

incluir <iostream> incluir <ctime>

2 Crear una nueva clase que representa a su héroe y los monstruos en el "calabozo" mediante el uso de este código:

CCreature clase
{
público:

CCreature() : muiAttack(0), muiDefense(0), muiHitPoints(3)
{
for (unsigned int uiIndex = 0; uiIndex &lt; 3; ++uiIndex) {
muiAttack = muiAttack + (rand() % 34);
muiDefense = muiDefense + (rand() % 34);
muiHitPoints = muiHitPoints + (rand() % 10);
}
}
void Attack(CCreature&amp; qrDefender) {
// Generate a numbers between 1 and 100
unsigned int uiAttack1 = (rand() % 100) + 1;
unsigned int uiDefense1 = (rand() % 100) + 1;
unsigned int uiAttack2 = (rand() % 100) + 1;
unsigned int uiDefense2 = (rand() % 100) + 1;
// Did the attacker (hero) hit?
if (uiAttack1 &lt; muiAttack &amp;&amp; uiDefense1 > qrDefender.muiDefense) {
--qrDefender.muiHitPoints;
std::cout &lt;&lt; &quot;Monster Hit!&quot; &lt;&lt; std::endl;
} else {
std::cout &lt;&lt; &quot;Monster Missed!&quot; &lt;&lt; std::endl;
}
// Check if the monster hit your character
if (uiAttack2 &lt; qrDefender.muiAttack &amp;&amp; uiDefense1 > muiDefense) {
--muiHitPoints;
std::cout &lt;&lt; &quot;Hero Hit!&quot; &lt;&lt; std::endl;
} else {
std::cout &lt;&lt; &quot;Hero Missed!&quot; &lt;&lt; std::endl;
}
std::cout &lt;&lt; &quot;Your Hitpoints:&quot; &lt;&lt; muiHitPoints &lt;&lt; std::endl;
std::cout &lt;&lt; &quot;Monster Hitpoints:&quot; &lt;&lt; qrDefender.muiHitPoints &lt;&lt; std::endl;
}
bool IsDead() {
return (muiHitPoints == 0);
}

privado:

unsigned int muiAttack;
unsigned int muiDefense;
unsigned int muiHitPoints;

};

Esta clase inicializa los tres atributos: ataque, de defensa y de los puntos de golpe. A continuación, utiliza un algoritmo de ataque para determinar si el ataque se ha realizado correctamente, el daño del ataque y los restantes puntos afectados.

3 Crear una nueva clase que representa el "calabozo" como una matriz bidimensional utilizando este código:

CDungeon clase {
público:

CDungeon() {
// Create an empty maze
char caaMaze[10][11] = {
&quot;**********&quot;,
&quot;* ** ** *&quot;,
&quot;* * ** *&quot;,
&quot;* *&quot;,
&quot;* * *** *&quot;,
&quot;* ** * ***&quot;,
&quot;* * * *&quot;,
&quot;* ** *&quot;,
&quot;* * *** *&quot;,
&quot;**********&quot;};
for (unsigned int uiRow = 0; uiRow &lt; 10; ++uiRow) {
for (unsigned int uiCol = 0; uiCol &lt; 10; ++uiCol) {
mcaaMaze[uiRow][uiCol] = caaMaze[uiRow][uiCol];
}
}
}
char GetMazeSquare(unsigned int uiRow, unsigned int uiCol) {
return mcaaMaze[uiRow][uiCol];
}

privado:

char mcaaMaze[10][10];

};

Puede cambiar la forma en que su "calabozo" se parece al cambiar la posición de los caracteres "*".

4 Crear la clase que controla el juego-juego utilizando este código:

CRolePlayingGame clase
{
público:

CRolePlayingGame() {
// Initlialize the random number generator
time_t qTime;
time(&amp;qTime);
srand((unsigned int)qTime);

// Inicializar el calabozo estar vacío
for (int sin signo uiRow = 0; uiRow & lt; 10; ++ uiRow) {
for (int sin signo uiCol = 0; uiCol & lt; 10; ++ uiCol) {
mqpaaCreatures [uiRow] [uiCol] = 0;
}
}

// Crear un héroe
bool bFoundSpot = false;
while (! bFoundSpot) {
unsigned int uiRow = 1 + (rand ()% 8);
unsigned int uiCol = 1 + (rand ()% 8);
si (QueryLocation (uiRow, uiCol) == '') {
bFoundSpot = true;
mqpaaCreatures [uiRow] [uiCol] = & amp; mqHero;
}
}
// Crear 10 monstruos
bFoundSpot = false;
unsigned int uiMonster = 0;
while (! bFoundSpot) {
unsigned int uiRow = 1 + (rand ()% 8);
unsigned int uiCol = 1 + (rand ()% 8);
si (QueryLocation (uiRow, uiCol) == '') {
mqpaaCreatures [uiRow] [uiCol] = & amp; mqaMonsters [uiMonster];
uiMonster ++;
si (== uiMonster 10) {
bFoundSpot = true;
}
}
}
}
Char QueryLocation (unsigned int uiRow, unsigned int uiCol) {
for (int sin signo uIndex = 0; uIndex & lt; 10; ++ uIndex) {
si (mqpaaCreatures [uiRow] [uiCol] == & amp; (mqaMonsters [uIndex])) {
retorno (char) ( '0' + uIndex);
}
}
si (mqpaaCreatures [uiRow] [uiCol] == & amp; mqHero) {
retorno 'H';
} Else {
volver mqDungeon.GetMazeSquare (uiRow, uiCol);
}
}
bool MoveHero (const kcDirection char) {
unsigned int uiHeroRow;
unsigned int uiHeroCol;
LocateCreature (uiHeroRow, uiHeroCol, & amp; mqHero);
unsigned int uiNextRow = uiHeroRow;
unsigned int uiNextCol = uiHeroCol;
interruptor (kcDirection) {
caso 'w':
caso 'W':
{
--uiNextRow;
descanso;
}
casos':
casos':
{
uiNextCol ++;
descanso;
}
caso 'z':
caso 'Z':
{
uiNextRow ++;
descanso;
}
caso "a":
caso "A":
{
--uiNextCol;
descanso;
}
defecto:
{
falso retorno;
}
}
Char cNextLoc = QueryLocation (uiNextRow, uiNextCol);
si (cNextLoc == '') {
mqpaaCreatures [uiNextRow] [uiNextCol] = & amp; mqHero;
mqpaaCreatures [uiHeroRow] [uiHeroCol] = 0;
return true;
} Else if (cNextLoc> = '0' & amp; & amp; cNextLoc y lt; = '9') {
mqHero.Attack (mqaMonsters [(int) (cNextLoc - '0')]);
return true;
} Else {
falso retorno;
}
}
printboard void () {
using namespace std;
for (int sin signo uiRow = 0; uiRow & lt; 10; ++ uiRow) {
for (int sin signo uiCol = 0; uiCol & lt; 10; ++ uiCol) {
cout & lt; & lt; QueryLocation (uiRow, uiCol);
}
cout & lt; & lt; endl;
}
}
bool HeroIsDead () {
mqHero.IsDead volver ();
}
RemoveDeadMonsters void () {
for (int sin signo uiIndex = 0; uiIndex & lt; 10; ++ uiIndex) {
si (mqaMonsters [uiIndex] .IsDead ()) {
unsigned int uiRow;
unsigned int uiCol;
si (LocateCreature (uiRow, uiCol, & amp; (mqaMonsters [uiIndex]))) {
mqpaaCreatures [uiRow] [uiCol] = 0;
std :: cout & lt; & lt; & Quot; & quot monstruo muerto!; & Lt; & lt; std :: endl;
}
}
}
}
bool AllMonstersDead () {
bool bAllDead = true;
for (int sin signo uiIndex = 0; uiIndex & lt; 10; ++ uiIndex) {
if (! mqaMonsters [uiIndex] .IsDead ()) {
bAllDead = false;
}
}
volver bAllDead;
}

privado:

bool LocateCreature(unsigned int&amp; uirRow, unsigned int&amp; uirCol, CCreature* qpCreature) {
for (unsigned int uiRow = 0; uiRow &lt; 10; ++uiRow) {
for (unsigned int uiCol = 0; uiCol &lt; 10; ++uiCol) {
if (mqpaaCreatures[uiRow][uiCol] == qpCreature) {
uirRow = uiRow;
uirCol = uiCol;
return true;
}
}
}
return false;
}
CDungeon mqDungeon;
CCreature mqHero;
CCreature mqaMonsters[10];
CCreature* mqpaaCreatures[10][10];

};

Esta clase controla el movimiento de tu héroe. Se utiliza "W", "A", "S" y "Z" para mover a tu personaje. También genera 10 monstruos y el héroe dentro de su "mazmorra" y controla las interacciones entre ellos, como las muertes y posiciones.

5 Crear el programa principal que llama a las distintas clases y funciones e interactúa con el usuario. Puede controlar varios aspectos de su juego de este programa. Utilizar el código para crearlo:

int main () {

using namespace std;
// Clear the dungeon
CRolePlayingGame qGame;
bool bGameOver = false;
do {
qGame.PrintBoard();
// Get the next move
char cMove;
cout &lt;&lt; &quot;Use W, A, S, or Z to move:&quot; &lt;&lt; endl;
std::cin >> cMove;
// Check if the move is valid
if (qGame.MoveHero(cMove)) {
// If the hero is dead
if (qGame.HeroIsDead()) {
cout &lt;&lt; &quot;You have Died!&quot; &lt;&lt; endl;
bGameOver = true;
} else {
// Remove the dead monsters from the game
qGame.RemoveDeadMonsters();
// If all of the monsters are dead
if (qGame.AllMonstersDead()) {
cout &lt;&lt; &quot;Dungeon Cleared!&quot; &lt;&lt; endl;
bGameOver = true;
}
}
}
} while (!bGameOver);
return 0;

}

6 Compilar su nuevo proyecto y ejecutarlo para probar si funciona como se espera. Cambiar varios ajustes para mejorar el juego-juego hasta que esté satisfecho con el resultado.