viernes, 15 de noviembre de 2013

Cómo hacer un juego RPG en C + +

Usted puede crear un simple juego RPG en C + +. 

Cuando se juega un juego de rol o RPG, asumes el papel de un personaje imaginario y la experiencia de sus aventuras. Estos juegos pueden ser muy complejos e implican gran cantidad de gráficos. Los desarrolladores suelen trabajar varios meses en la codificación y prueba el juego. Sin embargo, puede crear un simple juego de RPG en C + +. Por otra parte, sólo tiene que entender los conceptos básicos de programación en C + +. Usted puede crear un simple juego de rol con una matriz de dos dimensiones como el mapa y unas cuantas clases para controlar a tu personaje, los personajes enemigos y el juego.



Abre tu editor favorito de C + + y empezar un proyecto nuevo. Incluya el iostream y bibliotecas ctime utilizando este código:



# Include # include iostream ctime



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



clase CCreature {public: CCreature (): muiAttack (0), muiDefense (0), muiHitPoints (3) {for (unsigned int uiIndex = 0; uiIndex 3; + + uiIndex) {muiAttack = muiAttack + (rand ()% 34 ); muiDefense = muiDefense + (rand ()% 34); muiHitPoints = muiHitPoints + (rand ()% 10);}} void Attack (CCreature qrDefender) {/ / Generar un número entre 1 y 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; / / ¿Se golpeó al atacante (héroe)? if (uiAttack1 muiAttack uiDefense1 qrDefender.muiDefense) {- qrDefender muiHitPoints;. std :: cout Monster Hit! std :: endl;} else {std :: cout Monster perder! std :: endl;} / / Comprobar si el monstruo golpeó su personaje si (uiAttack2 qrDefender.muiAttack uiDefense1 muiDefense) {- muiHitPoints; std :: cout Hit héroe! std :: endl;} else {std :: cout héroe perder! std :: endl;} std :: cout sus Hitpoints: muiHitPoints std :: endl; std :: cout Puntos de ataque del monstruo: qrDefender.muiHitPoints std :: endl;} bool IsDead () {return (muiHitPoints == 0);} privada : muiAttack int sin firmar; muiDefense int sin firmar; muiHitPoints int sin firmar;};



Esta clase inicializa los tres atributos: ataque, defensa y ataque puntos. A continuación, utiliza un algoritmo de ataque para determinar si el ataque fue un éxito, el daño de los restantes puntos de golpe y ataque.



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



clase CDungeon {public: CDungeon () {/ / Crear un laberinto vacío caracteres caaMaze [10] [11] = {**********, ******, *****, * *, ******, *******, ****, ****, ****** **********}; for (int sin signo uiRow = 0; uiRow 10; + + uiRow) {for (unsigned int uiCol = 0; uiCol 10; + + uiCol) {mcaaMaze [uiRow] [uiCol] = caaMaze [uiRow] [uiCol];}}} caracteres GetMazeSquare ( uiRow unsigned int, unsigned int uiCol) {return mcaaMaze [uiRow] [uiCol];} privada: mcaaMaze char [10] [10];};



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



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



clase CRolePlayingGame {public: CRolePlayingGame () {/ / Initlialize el generador de números aleatorios time_t QTIME, a plazo (QTIME), srand ((unsigned int) QTIME);



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



/ / Crear un héroe bool bFoundSpot = false; (! BFoundSpot), mientras que {unsigned int uiRow = 1 + (rand ()% 8); unsigned int uiCol = 1 + (rand ()% 8); if (QueryLocation (uiRow, uiCol) == "") {bFoundSpot = true; mqpaaCreatures [uiRow] [uiCol] = mqHero;}} / / Crear 10 monstruos bFoundSpot = false; uiMonster unsigned int = 0;! while (bFoundSpot) {unsigned int uiRow = 1 + (rand ()% 8); unsigned int uiCol = 1 + (rand ()% 8); if (QueryLocation (uiRow, uiCol) == "") {mqpaaCreatures [uiRow] [uiCol] = mqaMonsters [uiMonster]; + + uiMonster; if (uiMonster == 10) {bFoundSpot = true;}}}} QueryLocation char (uiRow unsigned int, unsigned int uiCol) {for (unsigned int uIndex = 0; uIndex 10; + + uIndex) {if ( mqpaaCreatures [uiRow] [uiCol] == (mqaMonsters [uIndex])) {return (char) ('0 '+ uIndex);}} if (mqpaaCreatures [uiRow] [uiCol] == mqHero) {return' H '; } else {return mqDungeon.GetMazeSquare (uiRow, uiCol);}} bool MoveHero (kcDirection const char) {uiHeroRow unsigned int, unsigned int uiHeroCol; LocateCreature (uiHeroRow, uiHeroCol, mqHero); unsigned int uiNextRow = uiHeroRow; unsigned int uiNextCol = uiHeroCol; switch (kcDirection) {case 'w': case 'W': {- uiNextRow; break;} case 's': case 'S': {+ + uiNextCol; break;} case 'z': case ' Z ': {+ + uiNextRow; break;} case' a ': case' A ': {- uiNextCol; break;} default: {return false;}} caracteres cNextLoc = QueryLocation (uiNextRow, uiNextCol); if (cNextLoc == "") {mqpaaCreatures uiNextRow [] [] = uiNextCol mqHero; mqpaaCreatures [uiHeroRow] [uiHeroCol] = 0; return true;} else if (cNextLoc = '0 'cNextLoc = '9') {mqHero.Attack (mqaMonsters [(int) (cNextLoc - '0 ')]); return true;} else {return false;}} void printboard () {using namespace std; para (unsigned int uiRow = 0; uiRow 10; + + uiRow) { por (unsigned int uiCol = 0; uiCol 10; + + uiCol) {cout QueryLocation (uiRow, uiCol);} cout endl;}} bool HeroIsDead () {mqHero.IsDead retorno ();} RemoveDeadMonsters vacío () {for ( unsigned int uiIndex = 0; uiIndex 10; + + uiIndex) {if (mqaMonsters [uiIndex] IsDead (.)) {uiRow int sin firmar; uiCol int sin firmar; if (LocateCreature (uiRow, uiCol, (mqaMonsters [uiIndex]))) {mqpaaCreatures [uiRow] [uiCol] = 0; std :: cout monstruo muerto! std :: endl;}}}} bool AllMonstersDead () {bool bAllDead = true; para (unsigned int uiIndex = 0; uiIndex 10; + + uiIndex) {if (mqaMonsters [uiIndex] IsDead ()!.) {bAllDead = false;}} regresar bAllDead;}



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



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



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. Usa este código para crearlo:



int main () {using namespace std; / / Borrar la mazmorra CRolePlayingGame qGame; bool bGameOver = false; do {qGame.PrintBoard (); / / Obtener el siguiente movimiento caracteres cMove; cout Uso W, A, S o Z para movimiento: endl; std :: cin cMove; / / Comprobar si la medida es válida si (qGame.MoveHero (cMove)) {/ / Si el héroe está muerto if (qGame.HeroIsDead ()) {cout Usted ha muerto! endl; bGameOver = true;} else {/ / Eliminar los monstruos muertos de los qGame.RemoveDeadMonsters juego () / / Si todos los monstruos están muertos if (qGame.AllMonstersDead ()) {cout Dungeon liquidado! endl; bGameOver = true;}}}} while (bGameOver!); return 0;}



Compile su nuevo proyecto y ejecutarlo para probar si funciona como se espera. Cambiar varios ajustes para mejorar la jugabilidad hasta que esté satisfecho con el resultado.



 

No hay comentarios:

Publicar un comentario