March 18
Durante la riproduzione di un gioco di ruolo, o RPG, si prende il ruolo di un personaggio immaginario e sperimentare le sue avventure. Questi giochi possono essere molto complesse e coinvolgere grafica pesante. Gli sviluppatori di solito lavorano diversi mesi sulla codifica e test del gioco. Tuttavia, è possibile creare un semplice gioco di ruolo in C ++. Inoltre, è solo bisogno di capire le basi della programmazione C ++. È possibile creare un semplice gioco di ruolo utilizzando una matrice bidimensionale come la mappa e alcune classi per il controllo del personaggio, i personaggi nemici e il game-play.
1 Aprite il vostro editor preferito ++ C e avviare un nuovo progetto. Includere il "iostream" e "ctime" biblioteche utilizzando questo codice:
includere <iostream> includere <ctime>
2 Creare una nuova classe che rappresenta il tuo eroe e mostri nel "Dungeon" utilizzando questo codice:
classe CCreature
{
pubblico:
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) {
// 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 < muiAttack && uiDefense1 > qrDefender.muiDefense) {
--qrDefender.muiHitPoints;
std::cout << "Monster Hit!" << std::endl;
} else {
std::cout << "Monster Missed!" << std::endl;
}
// Check if the monster hit your character
if (uiAttack2 < qrDefender.muiAttack && uiDefense1 > muiDefense) {
--muiHitPoints;
std::cout << "Hero Hit!" << std::endl;
} else {
std::cout << "Hero Missed!" << std::endl;
}
std::cout << "Your Hitpoints:" << muiHitPoints << std::endl;
std::cout << "Monster Hitpoints:" << qrDefender.muiHitPoints << std::endl;
}
bool IsDead() {
return (muiHitPoints == 0);
}
privato:
unsigned int muiAttack;
unsigned int muiDefense;
unsigned int muiHitPoints;
};
Questa classe inizializza i tre attributi: attacco, difesa e-punti ferita. Viene quindi utilizzato un algoritmo attacco per determinare se l'attacco ha avuto successo, il danno dell'attacco e le restanti punti colpiti.
3 Creare una nuova classe che rappresenta il "Dungeon" come una matrice bidimensionale utilizzando questo codice:
class CDungeon {
pubblico:
CDungeon() {
// Create an empty maze
char caaMaze[10][11] = {
"**********",
"* ** ** *",
"* * ** *",
"* *",
"* * *** *",
"* ** * ***",
"* * * *",
"* ** *",
"* * *** *",
"**********"};
for (unsigned int uiRow = 0; uiRow < 10; ++uiRow) {
for (unsigned int uiCol = 0; uiCol < 10; ++uiCol) {
mcaaMaze[uiRow][uiCol] = caaMaze[uiRow][uiCol];
}
}
}
char GetMazeSquare(unsigned int uiRow, unsigned int uiCol) {
return mcaaMaze[uiRow][uiCol];
}
privato:
char mcaaMaze[10][10];
};
È possibile modificare il modo in cui il "Dungeon" si presenta come cambiando la posizione dei caratteri "*".
4 Creare la classe che controlla il game-play utilizzando questo codice:
classe CRolePlayingGame
{
pubblico:
CRolePlayingGame() {
// Initlialize the random number generator
time_t qTime;
time(&qTime);
srand((unsigned int)qTime);
// Inizializza il dungeon di essere vuoto
for (int unsigned uiRow = 0; uiRow & lt; 10; ++ uiRow) {
for (int unsigned uiCol = 0; uiCol & lt; 10; ++ uiCol) {
mqpaaCreatures [uiRow] [uiCol] = 0;
}
}
// Creazione di un eroe
bool bFoundSpot = false;
while (! bFoundSpot) {
unsigned int uiRow = 1 + (rand ()% 8);
unsigned int uiCol = 1 + (rand ()% 8);
if (QueryLocation (uiRow, uiCol) == '') {
bFoundSpot = true;
mqpaaCreatures [uiRow] [uiCol] = & amp; mqHero;
}
}
// Creazione di 10 mostri
bFoundSpot = false;
unsigned int uiMonster = 0;
while (! bFoundSpot) {
unsigned int uiRow = 1 + (rand ()% 8);
unsigned int uiCol = 1 + (rand ()% 8);
if (QueryLocation (uiRow, uiCol) == '') {
mqpaaCreatures [uiRow] [uiCol] = & amp; mqaMonsters [uiMonster];
++ UiMonster;
if (uiMonster == 10) {
bFoundSpot = true;
}
}
}
}
char QueryLocation (unsigned int uiRow, unsigned int uiCol) {
for (int unsigned uIndex = 0; uIndex & lt; 10; ++ uIndex) {
if (mqpaaCreatures [uiRow] [uiCol] == & amp; (mqaMonsters [uIndex])) {
ritorno (char) ( '0' + uIndex);
}
}
if (mqpaaCreatures [uiRow] [uiCol] == & amp; mqHero) {
ritorno 'H';
} altro {
tornare mqDungeon.GetMazeSquare (uiRow, uiCol);
}
}
bool MoveHero (const char kcDirection) {
unsigned int uiHeroRow;
unsigned int uiHeroCol;
LocateCreature (uiHeroRow, uiHeroCol, & amp; mqHero);
unsigned int uiNextRow = uiHeroRow;
unsigned int uiNextCol = uiHeroCol;
interruttore (kcDirection) {
caso 'w':
caso 'W':
{
--uiNextRow;
rompere;
}
caso 's':
caso 'S':
{
++ UiNextCol;
rompere;
}
caso 'z':
caso 'Z':
{
++ UiNextRow;
rompere;
}
caso 'a':
case 'A':
{
--uiNextCol;
rompere;
}
predefinito:
{
return false;
}
}
char cNextLoc = QueryLocation (uiNextRow, uiNextCol);
if (cNextLoc == '') {
mqpaaCreatures [uiNextRow] [uiNextCol] = & amp; mqHero;
mqpaaCreatures [uiHeroRow] [uiHeroCol] = 0;
return true;
} Else if (cNextLoc> = '0' & amp; & amp; cNextLoc & lt; = '9') {
mqHero.Attack (mqaMonsters [(int) (cNextLoc - '0')]);
return true;
} altro {
return false;
}
}
vuoto PrintBoard () {
using namespace std;
for (int unsigned uiRow = 0; uiRow & lt; 10; ++ uiRow) {
for (int unsigned uiCol = 0; uiCol & lt; 10; ++ uiCol) {
cout & lt; & lt; QueryLocation (uiRow, uiCol);
}
cout & lt; & lt; endl;
}
}
bool HeroIsDead () {
tornare mqHero.IsDead ();
}
RemoveDeadMonsters vuoto () {
for (int unsigned uiIndex = 0; uiIndex & lt; 10; ++ uiIndex) {
if (mqaMonsters [uiIndex] .IsDead ()) {
unsigned int uiRow;
unsigned int uiCol;
if (LocateCreature (uiRow, uiCol, & amp; (mqaMonsters [uiIndex]))) {
mqpaaCreatures [uiRow] [uiCol] = 0;
std :: cout & lt; & lt; & Quot; mostro ucciso & quot!; & Lt; & lt; std :: endl;
}
}
}
}
bool AllMonstersDead () {
bool bAllDead = true;
for (int unsigned uiIndex = 0; uiIndex & lt; 10; ++ uiIndex) {
if (! mqaMonsters [uiIndex] .IsDead ()) {
bAllDead = false;
}
}
tornare bAllDead;
}
privato:
bool LocateCreature(unsigned int& uirRow, 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;
CCreature mqaMonsters[10];
CCreature* mqpaaCreatures[10][10];
};
Questa classe controlla il movimento del tuo eroe. Si utilizza "W", "A", "S" e "Z" per muovere il personaggio. Esso genera anche 10 mostri e il tuo eroe dentro la "prigione" e controlla le interazioni tra di loro, come le morti e le posizioni.
5 Creare il programma principale che chiama le varie classi e funzioni e interagisce con l'utente. È possibile controllare i vari aspetti del tuo gioco da questo programma. Utilizzare questo codice per crearla:
int main () {
using namespace std;
// Clear the dungeon
CRolePlayingGame qGame;
bool bGameOver = false;
do {
qGame.PrintBoard();
// Get the next move
char cMove;
cout << "Use W, A, S, or Z to move:" << endl;
std::cin >> cMove;
// Check if the move is valid
if (qGame.MoveHero(cMove)) {
// If the hero is dead
if (qGame.HeroIsDead()) {
cout << "You have Died!" << endl;
bGameOver = true;
} else {
// Remove the dead monsters from the game
qGame.RemoveDeadMonsters();
// If all of the monsters are dead
if (qGame.AllMonstersDead()) {
cout << "Dungeon Cleared!" << endl;
bGameOver = true;
}
}
}
} while (!bGameOver);
return 0;
}
6 Compilare il nuovo progetto ed eseguirlo per verificare se funziona come previsto. Modificare varie impostazioni per migliorare il game-play finché non si è soddisfatti del risultato.