kopen van 2 Maak een nieuwe klasse die vertegenwoordigt uw held en de monsters in je " kerker " door het gebruik van deze code :
class 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 ) { //Genereer een getallen tussen 1 en 100unsigned int uiAttack1 = ( rand ( ) % 100 ) + 1 ; unsigned int uiDefense1 = ( rand ( ) % 100 ) + 1 ; unsigned int uiAttack2 = ( rand ( ) % 100 ) + 1 ; unsigned int uiDefense2 = ( rand ( ) % 100 ) + 1 ;//Heeft de aanvaller ( held ) hit als ( uiAttack1 < muiAttack && uiDefense1 > qrDefender ? . muiDefense ) { - . qrDefender muiHitPoints ; std :: cout << " ! Monster Hit " << std :: endl ; } else { std :: cout << "Monster Gemist ! " ; << std :: endl ; } //Controleer of de monsterhit uw characterif ( uiAttack2 < qrDefender.muiAttack && uiDefense1 > muiDefense ) { - muiHitPoints ; std :: cout << " , Hero Hit " << std :: endl ; } else { std :: cout << " ! Hero Gemist " << std :: endl ; ! } std :: cout << , " Uw Hitpoints : " << muiHitPoints << std :: endl ; std :: cout << " Monster Hitpoints : " << qrDefender.muiHitPoints << std : : endl ; } bool IsDead ( ) {return ( muiHitPoints == 0 ) ; } private : unsigned int muiAttack ; unsigned int muiDefense ; unsigned int muiHitPoints ;} ;
Deze klasse initialiseert de drie attributen : aanval, verdediging en hit - points . Het gebruikt dan een aanval algoritme om te bepalen of de aanval succesvol was , de schade van de aanval en de resterende hit - points .
3
Maak een nieuwe klasse die je " dungeon " vertegenwoordigt als een bi - dimensionale array met behulp van deze code :
class CDungeon
{public : CDungeon ( ) { //Maak een lege mazechar caaMaze [ 10 ] [ 11 ] = { " ******* *** " , " ****** " , " ***** " " ** " , " ****** " , " *** **** " , " **** " , " **** " , " ****** " , " ********** " } , voor ( 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 ] ; } private : char mcaaMaze [ 10 ] [ 10 ] ;} ;
U kunt verandert de manier waarop je " dungeon " ziet eruit als door het veranderen van de positie van de " * " tekens
4
Maak de klasse die de game-play controleert met behulp van deze code : .
class CRolePlayingGame {public : CRolePlayingGame ( ) { //Initlialize de random number generatortime_t Qtime ; tijd ( & Qtime ) ; srand ( ( unsigned int ) Qtime ) ;
//Initialiseer de kerker te zijn emptyfor ( unsigned int uiRow = 0 ; uiRow < 10 ; + + uiRow ) { for ( unsigned int uiCol = 0 ; uiCol < 10 ; + + uiCol ) { mqpaaCreatures [ uiRow ] [ uiCol ] = 0 ; } } < br >
//Creëer een herobool bFoundSpot = false; ( ! bFoundSpot ) , terwijl { unsigned int uiRow = 1 + ( rand ( ) % 8 ) ; unsigned int uiCol = 1 + ( rand ( ) % 8 ) if ( QueryLocation ( uiRow , uiCol ) == " " ) { bFoundSpot = true; mqpaaCreatures [ uiRow ] [ uiCol ] = &mqHero; } } //Maak 10 monstersbFoundSpot = false; unsigned int uiMonster = 0; ! terwijl ( 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; } } } } char QueryLocation ( unsigned int uiRow , 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 ( const char kcDirection ) { unsigned int uiHeroRow ; unsigned int uiHeroCol ; LocateCreature ( uiHeroRow , uiHeroCol , & , mqHero ) ; unsigned int uiNextRow = uiHeroRow ; unsigned int uiNextCol = uiHeroCol ; schakelaar ( 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 ; } } char 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 printplaat ( ) { using namespace std ; for ( unsigned int uiRow = 0 ; uiRow < 10 ; + + uiRow ) { for ( unsigned int uiCol = 0 ; uiCol < 10 ; + + uiCol ) { cout << QueryLocation ( uiRow , uiCol ) ; } cout << endl ; } } bool HeroIsDead ( ) { return mqHero.IsDead ( ) ; } void RemoveDeadMonsters ( ) { for ( unsigned int uiIndex = 0 ; uiIndex < 10 ; + + uiIndex ) {if ( mqaMonsters [ uiIndex ] IsDead ( . ) ) { unsigned int uiRow ; unsigned int uiCol if ( LocateCreature ( uiRow , uiCol , & ( mqaMonsters [ uiIndex ] ) ) ) { mqpaaCreatures [ uiRow ] [ uiCol ] = 0 , std :: cout << "Monster gedood ! " << std :: endl ; } } } } bool AllMonstersDead ( ) { bool bAllDead = true; for ( unsigned int uiIndex = 0 ; uiIndex < 10 ; + + uiIndex ) {if ) { bAllDead = false; } ( mqaMonsters [ uiIndex ] IsDead ( ! . ) } return bAllDead ; }
prive : 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 ] ;} ; < br >
Deze klasse bestuurt bewegingen van je held . U gebruikt " W ", " A ", " S " en " Z " om je personage te verplaatsen . Paait ook 10 monsters en je held in je " dungeon " en regelt de interactie tussen hen, zoals sterfgevallen en posities .
5
Maak het hoofdprogramma dat de verschillende klassen en functies oproepen en interageert met de gebruiker . U kunt de verschillende aspecten van je spel uit dit programma te controleren . Gebruik deze code om het te maken :
int main
( ) { using namespace std ;//Maak de dungeonCRolePlayingGame qGame ; bool bGameOver = false; doen { qGame.PrintBoard ( ) ;//Verkrijg de volgende movechar cMove ; cout << " Gebruik W , A , S of Z om te bewegen : " << endl ; std :: cin >> cMove ;//Controleer of de verhuizing is validif ( qGame.MoveHero ( cMove ) ) { //Als de held is deadif ( qGame.HeroIsDead ( ) ) { cout << " Je hebt stierf! " << endl ; bGameOver = true; } else { //de doden verwijderen monsters uit de gameqGame.RemoveDeadMonsters ( ) ;//Als alle van de monsters zijn deadif ( qGame.AllMonstersDead ( ) ) { cout << "Dungeon gewist ! " << endl ; bGameOver = true ; } } } } while ( bGameOver ! ) return 0; }
6
Stel uw nieuwe project en voer het uit om te testen of het werkt zoals je verwacht . De verschillende instellingen aan game-play te verbeteren tot je tevreden bent met het resultaat .