1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
#include <iostream>
using namespace std;
// Here is the Battle function. It's the only
// one you need for the entire battle system.
// !! FOR NOW, READ THE NOTES IN int main() !!
void Battle(int &P_HP, int &E_HP, int &Action)
{
// Because we sent those variables in from int main(), we can use them
// in here as if we made them here. You can pretend we're in int main().
// All of the changes we make in here are kept when we go back to main,
// because we put the & symbol before the variable names in this function
// delceration. Read it as "void Battle(int TheVariableFromMain, ...)"
switch(Action) // If the action you chose was...
{
case 1: // ..1 (Sword Attack)
// WHAT THE PLAYER DOES:
cout << "\nYou hit the enemy with your sword! (20 Damage)";
E_HP -= 20; // This is the same as writing "E_HP = E_HP - 20;".
// WHAT THE ENEMY DOES:
cout << "\nThe enemy hits you back with his fist! (10 Damage)";
P_HP -= 10;
break;
case 2: // ..2 (Gun Attack)
// WHAT THE PLAYER DOES:
cout << "\nYou shoot the enemy with your gun! (12 Damage)";
E_HP -= 12;
// WHAT THE ENEMY DOES:
cout << "\nThe enemy bandages the gunshot wound! (+8 Health)";
E_HP += 8;
break;
case 3: // ..3 (Healing Potion)
// WHAT THE PLAYER DOES:
cout << "\nYou quickly drink a healing potion! (+30 Health)";
P_HP += 30;
// WHAT THE ENEMY DOES:
cout << "\nThe enemy heals then quickly attacks! (+10 Health, 6 Damage)";
E_HP += 10;
P_HP -= 6;
break;
}
// This is all we need to do in the battle function.
// We took what the action was, then made the player
// and the enemy do something, depending on what was
// chosen. Now, go back to read notes in int main().
}
int main()
{
// All the variables we need.
int P_HP = 200, E_HP = 150, Action, Round = 1;
cout << "\nSimple Battle System\n--------------------"; // "\n" means new line like "endl".
// This is our main loop which will go around forever until you make it stop.
// We left the variables outside of this so they don't get set back to their
// Default values every time it goes round (which means HP would go back to full).
while(true)
{
cout << "\n\nROUND " << Round << ":\n-------"; // display the round.
cout << "\nPlayer HP: " << P_HP << "/200"; // displays your HP.
cout << "\nEnemy HP: " << E_HP << "/150"; // displays enemy HP.
cout << "\n\nChoose your action:";
cout << "\n1) Sword Attack, 2) Gun Attack, 3) Healing Potion"; // show the options.
cout << "\n> ";
cin >> Action; // choose an action.
// Here, instead of just calling the battle function, we're going to give it
// some of the variables we made earlier: P_HP, E_HP and Action. This means
// that we can use them in the function even though we didn't make them there.
Battle(P_HP, E_HP, Action);
// Now read the notes in the Battle() function.
// Since the Battle() function is in our while loop, the prompt for which action
// to take and all of the battle stuff will happen over and over, forever. We
// want it to stop, though, when someone dies (HP reaches 0). We have to add a
// couple of ifstatements to check if the HP is down to 0.
if(P_HP < 1){ // if player is dead:
cout << "\n\n\n---------------------------";
cout << "\nYou died! Ending Program...";
cout << "\n---------------------------\n\n";
break; // breaks out of the while loop, ends the program.
} else if(E_HP < 1){ // if the enemy is dead:
cout << "\n\n\n-----------------------------------------";
cout << "\nYou killed the monster! Ending Program...";
cout << "\n-----------------------------------------\n\n";
break; // breaks if this is the case too.
}
// If neither the player nor enemy are dead, the program will loop,
// This means "Round = Round + 1". It's just to
// show the player what round the player is up to.
Round++;
}
}
| |