Archived

This topic is now archived and is closed to further replies.

graveyard filla

OO programming design question

Recommended Posts

high, im pretty new to programming, only been doing it for about 4 1/2 months. anyway, im working on my third game (a pacman clone), and the game is starting to get huge. its probably close to 2000 lines of code, 6 pages of source, etc, etc. ive learned a lot from coding this game, the most important thing ive learned is that you MUST PLAN things out before you code. i didnt plan a single line of this code, one day i just started coding, and then added more stuff, more stuff, more stuff. this is bad!! anyway, this reflects in my design, and when i make my next game, im going to plan everything out in the begining. now heres my design questions: is it bad to EVER have a function / data member of a class, that a certain instance of that class will NEVER use. for example, in my pacman game i have a class called Entity. Entity has things like, xPos,yPos, frame_count, etc etc. this class is for my player and the enemies. but, some funcions of this class are ONLY for the player, and some are ONLY for the enemies. functions like : Draw_Scared() // only for the enemy, obviously Draw_Player_Dead() Draw_Enemy_Dead(), ETC.... is this bad design? obviously my player will NEVER use Draw_Scared(), and my enemies will NEVER use Draw_Player_Dead(). in situations like this, should i have 2 seperate classes, one called Player, one called Enemy? if i EVER have a function/data member that a specific instance of that class will NEVER use, is this a reflection of bad design? (i havent learned inheritence yet, but im sure this will be the solution to this problem, if in fact is IS wrong to do this) also, what about mixing up global functions with class functions? IE, should a class EVER call a global function? if i had a global function called Blah();, should i ever call Blah() from inside of one of my Entity''s functions? is this also a sign of bad-design? obviously global functions should make use of class''s, but what about the other way around? should i keep everything encapsulated to the class, and never have a class use "outside" parts? if i EVER have a class function that calls a global function, is this a sign of bad design? what about one class using another class''s functions? is this also a sign of bad design? also, if you guys have any other OOP design tips, please let me know. thank you for any help!!

Share this post


Link to post
Share on other sites
just make a base class with a Draw function. make a seperate class for the players and the enemies that override the draw function:


class BaseObject {
public:
virtual void Draw() = 0;
};

class Player : public BaseObject {
public:
void Draw_Player_Dead() {
//whatever

}

void Draw() {
if (AmDead)
Draw_Player_Dead();
else
BaseObject::Draw();
}
};

class Enemy : public BaseObject {
public:
void Draw_Enemy_Dead() {
//whatever

}

void Draw() {
if (AmDead)
Draw_Enemy_Dead();
else
BaseObject::Draw();
}
};


or


class BaseObject {
public:
virtual void DrawDead() = 0;
};


class Player : public BaseObject {
public:
void DrawDead() {
//move your Draw_PLayer_Dead code here

}
};


class Enemy : public BaseObject {
public:
void DrawDead() {
//move your Draw_Enemy_Dead code here

}
};


-me

Share this post


Link to post
Share on other sites
Seems like it would be better to have Entity only contain the members that are common to both player and enemy. Then inherit from entity to create your player and enemy classes and add the specific memebers.

Share this post


Link to post
Share on other sites