Text Based RPG using dynamic binding

May 3, 2016 at 2:28pm
Hey guys, I am making a RPG Game in which there are 6 different creatures with different abilities. I have the main coding down, I need a little help decrementing the health when hit. I would like to use Dynamic Binding for this.


Below is code from one of my creatures and the creature header file

I need help lowering the health when the snake uses whack



Snake.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "Snake.h"


Snake::Snake(int h) : Creature(h)
{
}

void Snake::Greet() const
{
	cout << "Snake says Hsssssssss! " << endl;
}

void Snake::Whack() const
{
	// decrement the heath?

	cout << "Take that!" << endl;
}

Snake::~Snake(void)
{
}


Creature.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma once

#include <iostream>
using namespace std;

// Abstract Class

class Creature
{
public:
	Creature(int h = 100);
	virtual void Greet() const = 0; // pure virtual function
	virtual void DisplayHealth() const;
	virtual void Whack() const;
	~Creature(void);
protected:
	int health;
};
Last edited on May 3, 2016 at 2:29pm
May 3, 2016 at 2:37pm
What is your creature whacking, exactly?
May 3, 2016 at 2:41pm
Is it whacking itself?
May 3, 2016 at 2:47pm
When the snake uses whack the health of the current object, so the snake's health will go down. Later I will add in health potions to increase his health I think. What would the coding look like to decrease his health though? I've been working with it forever trying to come up with something
May 3, 2016 at 2:59pm
Yeah, it's basically whacking itself kevin
May 3, 2016 at 3:01pm
Assuming Snake inherits the protected members of Creature, why not just do health--;?
May 3, 2016 at 3:04pm
You're right, I can get that to work, how do I decrement it a certain amount though? Say the health goes down 25? Thank you
May 3, 2016 at 3:04pm
health is a protected member of your base class, so Snake::Whack() has access to it. Simply decrement it by whatever amount you want.

1
2
3
4
void Snake::Whack()  // removed const.  Whack modifies the object.
{  health -= 10;  // Or however much you want
    cout << "Take that!" << endl;
}

Don''t forget to remove const from Creature::Whack() also.

Last edited on May 3, 2016 at 3:05pm
May 3, 2016 at 8:59pm
Thanks for reminding me of that! I almost missed it! Thanks for all your help guys :D
Topic archived. No new replies allowed.