IdeaMonk

thoughts, ideas, code and other things...

Thursday, October 02, 2008

How object oriented programming saved me from working for hours!

A week back I was meddling with my 2-D game called nincompoop to be put to Pwned!- the game programming contest at Shaastra 2008, iit madras. Just 2 days left for submission... and I was done with the basic class for hero and level collisions. What to do?? How much still left to do?? Will this ever finish?! These were the questions daunting me hour by hour...
Untill a simple idea struck my mind! I asked to myself... What are enemies in the game? and the lord said "Nothing but a hero who is controlled by the computer" and that was precisely the point where I felt the game was almost done! What basically I mean here is that, remove all the keyboard controls from a hero class, automate its motion, and to do that we just have to modify the hero class a bit.
Here's how it works -
// The hero class
class nincompoops {
public:
// *** Bitmap & allegro assets for hero
BITMAP *sprite_walk[4][12]; // 12 frame animation for walk

// *** Motion variables
bool walking, onGround,jumping,attacking;
int heroFrame, direction,oldDirection;
int jumpVel;
int xvel, yvel;
int recoil;
.
.
.

nincompoops(){
// *** initial motion settings
score=0;
jumping=onGround = walking = dropping = false;
velMax = 3;
.
.
.
.

}
bool loadSprites();
void levelCollision(BITMAP *lev);

void draw (BITMAP *bmp);

void decideKeys();
};
And the enemies class is a very similar class with some modifications in the decideKeys() functions, where desideKeys() actually never checks for keys.
class enemies {
public:
// *** Bitmap & allegro assets for enemy
BITMAP *sprite_walk[2][12]; // 12 frame animation for walk

// *** Motion variables
bool walking, onGround,jumping,attacking;
int heroFrame, direction,oldDirection;
int jumpVel;
int xvel, yvel;
int recoil;
int MaxFrames;
.
.
.

enemies(){
// *** initial motion settings
score=0;
jumping=onGround = walking = dropping = false;
velMax = 3;
.
.
.
.

}

bool loadSprites(int type);

void levelCollision(BITMAP *lev);

void draw (BITMAP *bmp);

void decideKeys() {

// doesn't look for keyboard keys, manages by collisions etc to the level
... rest of the hero class code
}
};
So, what about the items in the game!! The answer is simple again, items are nothing but enemies that don't move! So, I come up with an item class in another 10 or so minutes. :)

Finally we do something like this -
vector<enemies> enemyset;
vector<enemies>::iterator ei;

vector<items> itemset;
vector<items>::iterator ii;
And load all enemies and items from a file, and the game loop goes like this
// pseudocode
while (!key[KEY_ESC]) {
nin.decideKeys();
nin.levelCollision (cmap);

for_each item in itemset {
item.draw(work,x,y);
item.testCollision(hero);
}


for_each enemy in enemyset {
enemy.draw(work,x,y);
enemy.decideKeys();
enemy.testCollision(hero);
}

blit_all_to_screen();
}
The game finally finished around 1 AM on 30th of September which happened to be the last date. Thanks to understanding machhas @ iit, they had no problem for late submissions. And thanks to OOP, I finished major chunk of game within four hours... and that means I couldn't work on giving finishing touches, adding a story, animations, humour etc. more about the fest soon...

Labels: , , , ,

1 Comments:

At December 23, 2009 at 4:35 AM , Blogger Abhishek Mishra said...

I wish I had some more time to code this, could've used inheritance and designed more elegantly :)

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home