Sign in to follow this  
andyhall89

Help with an arena game

Recommended Posts

Hi everyone, i'm building a small arena game where the player fights enemies etc. It's just for personal interest really. Anywhere here's all the code:

[code]
// Arena.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
#include "time.h"
#include "string"

using namespace std;


int PlayerHealth = 40;
int PlayerDamage = ((rand() % 30) + 1);
int PlayerArmour = 10;
int PlayerExp = 0;
int PlayerLevel = 1;


class Enemy
{
public:
Enemy(int health = (PlayerLevel * (PlayerHealth - PlayerArmour)) * 0.75, int damage = ((rand() % 20) + 1), int exp = );
void virtual enemyAttack() const;
int virtual enemyIsAttacked() const;
void virtual enemyDied() const;
int m_Health;
int m_Damage;
int m_Exp;

private:

};

Enemy::Enemy(int health, int damage, int exp):
m_Health(health),
m_Damage(damage),
m_Exp(exp)
{}

void Enemy::enemyAttack() const
{
int PlayerDamage = (m_Damage % PlayerArmour + 1);
PlayerHealth = PlayerHealth - PlayerDamage;
--PlayerArmour;
}

int Enemy::enemyIsAttacked() const
{
m_Health - PlayerDamage;

if (m_Health <= 0)
{
PlayerWon();
}

return m_Exp;
}

void Enemy::enemyDied() const
{
// to do
}


void PlayerWon()
{
cout << "\n\nCongratulations! You've defeated an enemy!";
cout << "\n\nYou gained: " << m_Exp << " experience!";
PlayerExp + m_Exp;
while (PlayerExp >= 100){
PlayerExp - 100;
++PlayerLevel;
}


void _tmain(int argc, _TCHAR* argv[])
{
srand(time(0));

string playerName;

cout << "\n\nWelcome to the Arena!";
cout << "\n\nHere you will battle against the mightiest of foes!";
cout << "\n\nPlease tell us your name, mighty warrior: ";

cin >> playerName;

cout << "\n\nExcellent, a fine name for such a noble warrior!";

}
[/code]

A lot of it, especially main, isn't fully finished. But i'm getting a problem where some of the functions regarding the enemy class aren't able to reference the exp, health and damage ints of the enemy. What am i doing wrong? The errors are something along the lines of 'm_Exp is undefined' or 'This non-static object is not modifiable'. What am i doing wrong?

Thanks.

Share this post


Link to post
Share on other sites
First of all: write the following around your code (but without spaces):
[ c o d e ]
[ / c o d e ]

I think the problem is that you wrote PlayerWon instead of Enemy::PlayerWon.

Share this post


Link to post
Share on other sites
[quote name='Texus' timestamp='1306676133' post='4817093']
First of all: write the following around your code (but without spaces):
[ c o d e ]
[ / c o d e ]

I think the problem is that you wrote PlayerWon instead of Enemy::PlayerWon.
[/quote]

Oh so i need to make playerWon() a member of the Enemy class? What happens if i have a function outsie of Enemy but want to access some of the data inside there?

Thanks.

Share this post


Link to post
Share on other sites
You should have something like this:
[code]
Enemy mEnemy;
[/code]
Then you can access its data:
[code]
mEnemy.m_Exp;
[/code]
For calling e.g. enemyAttack from _tmain you will also need to do this:
[code]
mEnemy.enemyAttack()
[/code]

Share this post


Link to post
Share on other sites
Oh okay, thanks :).

Also, when an enemy is killed, what do you think would be the best way to go about that? Would it to literally call up a new enemy as you just described?

Share this post


Link to post
Share on other sites
I can't really help you with it, because you will handle the death of an ememy differently in some situatations e.g. multiple enemies or not.

If you want the enemy to respawn when he dies then you can continue working with mEnemy.
You just have to recalculate its health and things like that and then you can fight it again.


I have noticed that u are using a class, but all the members of the class are public.
I suggest to work with a struct, because it has public members by default.

Share this post


Link to post
Share on other sites
[quote name='Texus' timestamp='1306677894' post='4817111']
I can't really help you with it, because you will handle the death of an ememy differently in some situatations e.g. multiple enemies or not.

If you want the enemy to respawn when he dies then you can continue working with mEnemy.
You just have to recalculate its health and things like that and then you can fight it again.


I have noticed that u are using a class, but all the members of the class are public.
I suggest to work with a struct, because it has public members by default.
[/quote]

Ah right okay. You see you can tell i'm new because i don't actually know what a struct is lol. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this