So, I'm a newbie at c. Started watching videos constantly and reading books a week ago and decided to try out making an RPG game for command prompt. I didn't know where to start so I took an example of somebody's code online and went from there. I have added 3 more player classes, gold, and a leveling system. It all worked last night perfectly fine, but as soon as I went to play it again for some reason all the game would load to was after you chose what class you wanted to play. I eventually got it to load to the part where you choose what monster you can fight, but now you can only choose option 1, and as soon as you kill the enemy the game ends. Also, you start out with a HUGE amount of gold, which I did not put in on purpose. Usually you start off with -2 and earn 5 per kill.
I will post my code below, and if anyone knows whats wrong with it please let me know. I have a feeling its something very simple that I am missing, but then again, with as many problems that I have been having lately, its probably something huge.
By the way, don't be to cruel on my code, this is the very first thing I am making besides hello world and a calculator ;).
#include <iostream>
#include <string>
using namespace std;
int main()
{
int strength, spellPower, health, playerLevel, playerName, choose, playerXp, arrowPower, gold;
int evilStrength, evilSpellPower, evilHealth, evilLevel;
playerLevel = 1;
playerXp = 0;
cout << "Welcome to Crypt!" << endl;
cout << "A console RPG game where you choose your playstyle and fight in an arena." << endl;
cout << "Choose a class." << endl;
cout << "1. Knight" << endl;
cout << "2. Archer" << endl;
cout << "3. Spellbreaker" << endl;
cout << "4. Healer" << endl;
cout << "5. Zombie" << endl;
int playerClass;
cin >> playerClass;
if (playerClass == 1)
{
cout << " ===|====>" << endl;
strength = 10;
spellPower = 0;
health = 100;
cout << "you choose The Knight! here is your attributes:" << endl;
cout << "Strength = " << strength << endl << "Health = " << health << endl;
}
else if (playerClass == 2)
{
cout << " |)" << endl;
strength = 8;
arrowPower = 25;
health = 90;
cout << "you choose the Archer! here is your attributes:" << endl;
cout << "Strength = " << strength << endl << "Bow and Arrow = " << arrowPower << endl << "Health = " << health << endl;
}
else if (playerClass == 3)
{
cout << " ----(o" << endl;
strength = 7;
spellPower = 20;
health = 95;
cout << "you choose the Spellbreaker! here is your attributes:" << endl;
cout << "Strenght = " << strength << endl << "Spell Power = " << spellPower << endl << "Health = " << health << endl;
}
else if (playerClass == 4)
{
cout << " <3" << endl;
strength = 5;
spellPower = 18;
health = 150;
cout << "you choose the Healer! here is your attributes:" << endl;
cout << "Strength = " << strength << endl << "Spell Power = " << spellPower << endl << "Heatlh = " << health << endl;
}
else if (playerClass == 5)
{
cout << " brrraiiinssss...." << endl;
strength = 9;
spellPower = 0;
health = 90;
cout << "you choose the Zombie! here is your attributes:" << endl;
cout << "Strength = " << strength << endl << "Spell Power = " << spellPower << endl << "Heatlh = " << health << endl;
}
{
}
{
cout << "|_|_|_|_|__|__|__|_|_|_|_|" << endl;
cout << "|_______|________|_______|" << endl;
cout << "| ___ | | ___ |" << endl;
cout << "| [ ] | | [ ] |" << endl;
cout << "| [___] | ____ | [___] |" << endl;
cout << "| | | | | |" << endl;
cout << "| | | | | |" << endl;
cout << "--------------------------" << endl;
cout << "Welcome to the Crypt Arena!" << endl;
cout << "You are level " << playerLevel <<endl;
cout << "You have " << gold << " Gold" << endl;
cout << "---------------------------" << endl;
cout << "What do you want to fight?" << endl << "\n 1. Bandit" << endl << "\n 2. Troll" << endl;
cin >> choose;
}
if (choose == 1)
{
{
cout << string( 100, '\n' );
cout << "fighting level 1 Bandit!! Prepare for combat!" << endl;
evilHealth = 40;
evilStrength = 4;
evilSpellPower = 0;
while (health > 0)
{
if (evilHealth < 0)
{
playerXp += 10;
gold += 5;
cout << "You killed the Bandit! You are awarded 10 experience points! you now have " << playerXp << " experience points!" << endl;
cout << "You are awarded 5 Gold! You now have " << gold << " Gold!" << endl;
cout << "Type any number to continue." << endl;
cin >> choose;
cout << string( 100, '\n' );
break;
}
health -= evilStrength * 2;
cout << "the Bandit strikes and deals " << evilStrength * 2 << " damage to you!" << "\nYou have " << health << " health left!" << endl;
cout << "Choose an action!" << "\n 1. Attack" << "\ 2. Cast fireball" << endl;
cin >> choose;
if (choose == 1)
{
evilHealth -= strength * 2;
cout << "you swing your weapon and deal " << strength * 2 << " damage to the Bandit!" << endl;
cout << "the Bandit has " << evilHealth << " health left!" << endl;
}
if (choose == 2)
{
evilHealth -= strength * 2;
cout << "You cast a fireball and deal " << strength * 2 << " damage to the Bandit!" << endl;
cout << "the Bandit has " << evilHealth << " health left!" << endl;
}
}
}
if (choose == 2)
{
{
cout << string( 100, '\n' );
cout << "fighting level 1 Troll!! Prepare for combat!" << endl;
evilLevel = 2;
evilHealth = 40;
evilStrength = 4;
evilSpellPower = 0;
while (health > 0)
{
if (evilHealth < 0)
{
playerXp += 10;
cout << "You killed the Troll! You are awarded 10 experience points! you now have " << playerXp << " experience points!" << endl;
cout << "Type any number to continue." << endl;
cin >> choose;
cout << string( 100, '\n' );
break;
}
health -= evilStrength * 2;
cout << "the Troll strikes and deals " << evilStrength * 2 << " damage to you!" << "\nYou have " << health << " health left!" << endl;
cout << "Choose an action!" << "\n 1. Attack" << "\ 2. Cast fireball" << endl;
cin >> choose;
if (choose == 1)
{
evilHealth -= strength * 2;
cout << "you swing your weapon and deal " << strength * 2 << " damage to the Troll!" << endl;
cout << "the Troll has " << evilHealth << " health left!" << endl;
}
}
}
}
}
return 0;
}
Hey man, i commend you for attempting it, but I would strongly recommend you use functions and classes, if necessary. I know you are just starting and this is a simply program, but you should definately learn OOP right off the bat. I did not learn object oriented programming at first, then i had to learn it later which was OK, but i would have rathered to learn it initially.
For this rpg, you should definately use functions instead of having one bunch of code procedured.
IF you need further help or do not understand what to do, feel free to write me back on this post :)
I will be getting my c++ for dummies book in the mail in a few days, I am sure that will help me out, but until then, mind telling me how to make a class, and how to implement it into this?
I know what classes are, and I kind of know how they work, but its still a bit shady.
I also commend you for having a good go - Well done. Shaggy is right though - you need to learn more about the basics.
My comment about classes is they introduce a lot more complexity. I know it sounds boring, but it would be IMO worth doing more procedural (using functions) stuff first. I have seen it before - people with a basic knowledge moving on too quickly, then being really confused. It's like learning to drive a car - you don't start with a Formula One car, you might start with a go kart first, or an ordinary small car.
With your code, consider using a switch instead of a load of else if's.
Can you edit your post with code tags - the <> button, instead of quote tags?
There is also a suggestion that code should not be more than 80 chars per line, so that I don't have to scroll sideways all the time.
Take a look at the reference & articles section on this site - go to the top left of this page.
Hm, I will take some time this weekend to learn what classes are and how to use them, seeing as I'm just coding in my spare time :P.
But if somebody could look at my code and tell me why it suddenly broke and won't work all the way like its supposed to do I would REALLY appreciate it :).
You don't ever set gold to anything initially, which is why you're seeing an odd amount of gold when starting. Strangely enough when you compile the code the compiler warns of this. Pay attention to your compiler warnings.
Your bracketing and indentation levels are messed up so the bottom most if (choose==2) line is inside the first if (choose == 1) body.
I don't know if anyone will still be willing to help me with this, but I took cire's advice and I fixed the bracket problem, but now I don't know why the game ends right after I kill something. I think I deleted a wrong code that prevented this from happening a while back.