Good job, it looks like you figured out how to use loops and if-statements and have the primary basics of C++ figured out.
Look at your code;
int strenght, spellPower, health, playerLevel, choose, playerXp; int evilStrenght, evilSpellPower, evilHealth, evilLevel; playerLevel = 1; playerXp = 0; int playerClass;
The above code snippet contains variables all linked to one thing; the Player. Thus, it would make sense to somehow link this together, right? You can make objects in C++ by using 'classes'. In your case, making a 'Player' class would make sense.
Making a class goes like this in C++:
As you can see you start with the keyword 'class' followed by the name of the class ('Player'). Then the body of the class between '{' and '}', and finish with a ';'
You can add your variables to the class:
filename: Player.h
1 2 3 4 5 6 7 8 9 10 11 12
|
class Player {
public:
private:
int goodStrenght, evilStrenght;
int goodSpellPower, evilSpellPower;
int goodHealth, evilHealth;
int goodLevel, evilLevel;
int xp;
int class;
};
| |
The class variables (also called data members or fields) are under 'private', which means that you cannot directly access them from the outside. In more simple terms, only a Player can change these using public functions (which do not exist yet, as you can see, there is nothing below 'public:').
Let's say you want to create a Player object called 'seerex'. The following code will do this:
Player seerex;
This variable 'Player seerex' already has goodSpellPower, evilSpellPower, goodHealth, etcetera (all set to '0'). To change any of the values you need to add methods (or member functions) to the Player class. Let's think of a few:
filename: Player.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
class Player {
public:
int addXP(int add);
int addGHealth(int damage);
int addBHealth(int damage);
private:
int goodStrenght, evilStrenght;
int goodSpellPower, evilSpellPower;
int goodHealth, evilHealth;
int goodLevel, evilLevel;
int xp;
int class;
};
| |
I have added 3 member functions to the class: 'int addXP(int add);' , 'int addGHealth(int hp);', and 'int addBHealth(int hp);'.
A function is divided in 3 parts: a return type, a name, and parameters. For example 'int addXP(int add);' has return type 'int', name 'addXP', and one parameter 'int add'.
To implement the Player class we use a file 'Player.cpp' and include the class we made in the 'Player.h' file:
File: Player.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include "Player.h"
int Player::addXP(int add) {
xp = xp + add;
return xp;
}
int Player::addGHealth(int hp) {
goodHealth =+ hp;
return goodHealth;
int Player::addBHealth(int hp) {
return (badHealth=+ hp);
}
}
| |
This code uses the class definition in 'Player.h'. Here you implement the functions you described in the header file; In the header file you say 'Look, I have a member function called 'addXP' that uses an int parameter 'add' and a return type of 'int'. In the CPP file 'Player.cpp' you actually implement the function; you program what should happen when calling that function. In member function you can use the class variables from the Player class; you can change xp, goodHealth, badHealth, etcetera, because the member functions are part of the Player class and can thus access the 'private' parts of the class.
The above code shows three member function implementations.
To wrap it up:
filename: Player.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
class Player {
public:
int addXP(int add);
int addGHealth(int damage);
int addBHealth(int damage);
private:
int goodStrenght, evilStrenght;
int goodSpellPower, evilSpellPower;
int goodHealth, evilHealth;
int goodLevel, evilLevel;
int xp;
int class;
};
| |
File: Player.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include "Player.h"
int Player::addXP(int add) {
xp = xp + add;
return xp;
}
int Player::addGHealth(int hp) {
goodHealth =+ hp;
return goodHealth;
int Player::addBHealth(int hp) {
return (badHealth=+ hp);
}
}
| |
File: game.cpp
1 2 3 4 5 6 7 8 9 10 11
|
#include <iostream>
#include "Player.h"
using namespace std;
int main() {
Player seerex;
cout << "Seerex new XP: " << seerex.addXP(2) << "\n";
int gHP = seerex.addGHealth(40);
cout << "Seerex new Good Health: << gHP;
seerex.addBHealth(50);
}
| |
In this line "
cout << "Seerex new XP: " << seerex.addXP(2);
you can see the 'return type' of a function at work. In the Player.cpp file you see that the function returns the current value of xp:
return xp;
. You can print it, save it to a variable (as seen in
int gHP = seerex.addGHealth(40);
), or do nothing with it (
seerex.addBHealth(50);
).
I hope this helps a bit :)