Need help with my basic game (attack/health/etc.)

I'm learning about Inheritance and Polymorphism right now, and I am using TheNewBoston's tutorial as a basis for what I am doing. I didn't really do much, but I wanted to add a Health Function. Once an enemy attacks, I want the health to be reduced. This is working except for the second enemy. I am returned a health of "50," instead of what it should be (i.e. "25").

I'm looking forward to expanding on this once I'm done figuring out Health.

Thanks in advance.

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

#include <iostream>
#include "Enemy.h"

using namespace std;

int main()
{
    Ninja n;
    Monster m;

    Enemy *Enemy1 = &n;
    Enemy *Enemy2 = &m;

    Enemy1->setAttackDamage(25);
    Enemy2->setAttackDamage(50);

    n.attack();
    m.attack();

    return 0;
}


#include <iostream>

using namespace std;

class Enemy{

protected:
int attackDamage;
int Health = 100;

public:
    void setAttackDamage(int attack){
    attackDamage = attack;

    if (attackDamage > 0)
        setHealth(attackDamage);
    }

     void setHealth(int attackDamage){
     Health = Health - attackDamage;
     }


};

class Ninja: public Enemy {

public:

    void attack(){
    cout << "I am a ninja, ninja chop!! - " << attackDamage << " POINTS!" << endl;

    cout << "Updated health is " << Health << "!" << endl;

    }
};

class Monster: public Enemy{

public:
    void attack(){

    cout << "\nMonster must eat you!! - " << attackDamage << " POINTS!" << endl;

    cout << "Updated health is " << Health << "!" << endl;

    }
};
Last edited on
each object has its own data member health, so each attack is decreasing its own objects health. Is the health suppose to be the "players" health indicated by "attack", because its defined as the enemies health, which makes sense, each enemy will have a health too.
Last edited on
Hey thanks,

Yeah I see that you're right here...Would it be best to create a "Player" class, then, and give them a health? I just need to make sure their health updates every time an attack takes place. I guess that's what I'm trying to figure out right now.
Last edited on
As players and monsters share lots of data types and capabilities, such as spawning, moving, attacking, taking damage, dying, health, position, etc., a person class from which a player class and a monster class can inherit might be a decent base class.
Last edited on
Topic archived. No new replies allowed.