Object Oriented Programming Review
A Little More Information Than You Asked For¶
I wrote this to improve my programming skills. There are a few things in C++ that I hadn't used since college so I thought I should really work on that. Hopefully it will be helpful to someone else. An image of serial output has been included near the bottom. Down at the very bottom are ideas for expanding the program should you choose to do so.
Explanation¶
I will not do a step by step breakdown as the documentation and inline commenting pretty much does this already. The program should be fairly easy to read through.
This is a fairly basic demo of:
- class inheritance
- polymorphism
- Also (partially) included is a template function.
If you are very new to C++ this may very well be a little over your head, but if you have played around with it a bit and would like to learn about these topics I hope this will be very easy for you to follow and understand.
This is a fairly simple game. It is nothing special but it is intended to be something you can work forward from and make into something better. Hook up your mbed, use TeraTerm (or some terminal program) to communicate and you should see the game (after compile). It is a very simple Action RPG (lol). Everything happens automatically in this initial version and there is no user input at all.
Program Flow¶
The code works (in general) like this:
- Spawn player (set up all player variables)
- Roll (choose between) for basic enemy or elite enemy
- Delete currently spawned enemy
- Spawn enemy (basic or elite)
- Communicate enemy info to player class
Combat begins:
- Display round number
- Player rolls for damage (crit roll then damage roll)
- Current enemy takes damage (after dodge roll and if elite enemy, armour takes damage before health)
- Get updated enemy health info
- Send updated enemy health info to player class
- Enemy rolls for damage (crit roll then damage roll)
- User takes damage (one third of roll)
- Check to see if player has been killed
- If player is dead exit combat, otherwise increment round count
If player is dead:
- Make fun of player and pretend to do complicated bioengineering.
- Delete spawned player
- Spawn new player
- Reset round count
If user is still alive:
- Add experience points. This also adds bonus "rest" health.
- Check to see if the player has enough experience points to level up (and level up if there are enough points).
- Reset round count
- Roll for basic or elite enemy
- Delete current enemy
- Spawn new enemy
- Start combat again with new enemy
The Libraries¶
The following libraries are fully documented. See API documentations for explanation of class functions.
This is the base class that all living things (well, pretend living anyway) are derived from:
» Import this library into a programlife_entity
Life entity (base class). Written for OOP Review.
The player class:
» Import this library into a programplayer
Player class. Written for OOP Review. Derived from life_entity.
The basic enemy class:
» Import this library into a programenemy
Enemy class. Written for OOP Review. Derived from life_entity.
The elite enemy class:
» Import this library into a programarmoured_vehicle
Armoured vehicle class. For OOP Review. Derived from life_entity.
Example Program¶
» Import this programOOP_Review
Example program for Object Oriented Programing Review cookbook page.
» Import this program
/* Object Oriented Programing Review Refreshing my memory on things I haven't touched since college. This version is intended for public use. You may use this in any way you choose, free of charge. No permission is required (written or verbal), and you do not HAVE to attribute. It would however, be very nice if you would mention me in any product or project you may create with this. Aaron Goselin 2011 */ #include "mbed.h" #include "life_entity.h" #include "player.h" #include "enemy.h" #include "armoured_vehicle.h" Serial PC(USBTX, USBRX); // Pointer to player's class // Does not change to any other class // Only resets on death (delete then new) player *user = new player(); // Pointer to the current enemy. // Can change from enemy to armoured_vehicle (or the other way around) // Also resets on death (delete and new) // Pointer to user class is always passed to the current enemy's // constructor. life_entity *currentEnemy; int main() { PC.baud(230400); // Setting RTC for rand() set_time(1256729737); int enemyHealth = 0; char enemyLevel = 0x00; int roll = 0; // Damage roll int roundCount = 1; // Round counter char thereIsCake = 0x00; while(1) { srand ( time(NULL) ); // Spawn elite enemy if( (rand() % 1000) > 200 ) { delete currentEnemy; currentEnemy = new armoured_vehicle(user); } // Spawn normal enemy else { delete currentEnemy; currentEnemy = new enemy(user); } enemyHealth = currentEnemy->getHealth(); enemyLevel = currentEnemy->getLevel(); // This isn't really used in the version of code // being added to the site but you can use it // however you like. user->setCurrentEnemy(enemyHealth, enemyLevel); // Keep going through the rounds until someone is dead while(currentEnemy->getHealth() > 0) { printf("ROUND #%i\n", roundCount); printf("----------\n"); // User rolls for damage roll = user->rollDamage(); // Current enemy takes the hit currentEnemy->takeDamage(roll); // Get current enemy health enemyHealth = currentEnemy->getHealth(); // Send update on enemy health status to user user->setCurrentEnemy(enemyHealth); // Enemy rolls for damage roll = currentEnemy->rollDamage(); // User takes the hit (one third of it anyway) user->takeDamage(roll / 3); // Check to see if the user is dead thereIsCake = user->isDead(); // Exit if user is dead (they promised cake though) if(thereIsCake) break; wait(.5); roundCount++; } // If the user dies restart everything if(thereIsCake) { delete user; user = new player(); roundCount = 1; } // User is still alive. Crap, no cake yet. else { // User won, so add xp (also adds a bit of health) user->addExperience(); // Check to see if user leveled up user->isLevelUp(); // Reset round counter roundCount = 1; } } }
Screenshot¶

What's Next?¶
So, you've read through this junk gagging through my idiot sense of humor all the while and now you want to modify the program and add to it until it is the most awesome game ever to be on the mbed? Cool! I think you should go for it.
If that's true but you aren't quite sure where to go from here then keep reading for an extra few seconds (or minutes depending on how slow/fast you read).
Ideas for expansion of this program:
- Really, playing a game in terminal isn't ideal. Consider any sort of external display/interface device (LCD, OLED, LED matrix, motorcycle engines and pulleys hooked up to punch you in the face when you lose, whatever you can think of).
- Health bars are nicer than numbers (although you can draw numbers over or by the bars)
- Same with experience bars
- Items! Create a new base class for items, then create derived classes for your items (health packs, guns(damage modifiers?), armour vests, etc.)
- Events! Create yet another base class for events, and again create derived classes this time for your events. There could be traps (roll for traps), bonus events (meaning good things like health refills or extra crit chance), etc.
- More than one enemy at a time. Final Fantasy style combat anyone?
- Again with the FF gameplay, AI teammates.
- Improvements to xp and health/armour calculations (lets be honest, I didn't put a lot of effort into them).
- More xp for enemy's higher than your level, and less for enemy's lower than your level.
- User input! Obviously that's a no brainer.
- Dialog options (chat with the enemy...annoy the enemy).
- Anything
- Seriously, anything. If you can think it up, go ahead and throw it in.
class,
game,
inheritance,
OOP,
output,
polymorphism,
Serial,
simple,
tutorial



Please login to post comments.