Skip to main content
GameDev.net gamedev.net
🔒 Locked

First game using Classes

Started by bam104 Sep 27, 2006 at 5:44 PM 4 replies 750+ views
Original Post
bam104
bam104
Hello, I am teaching myself a little code to better understand game development as a whole. I have been making my way through a book on C++ and have hit classes and OO programming. I like to learn a new aspect of C++ then write a small game using what i have just learnt. But i am finding it hard to design a text based game using classes. Could anyone give me some ideas/help/links on designing a basic text based game using OO programming? Thanks, Dan.
Simian Man
Simian Man
How about your standard text adventure game where the player has to navigate a set of rooms with monsters and items in them? This can be modeled well with OOP. For example you can have a Player class (possibly deriving from a more general class), Monster classes, Item classes etc.

A lot of learning to use OOP is in trying to design things yourself and learning from your mistakes. It can be hard to wrap your head around inheiritance and composition at first.
bam104
bam104
Thanks.

I was hoping for something more detailed that i could base my first attempt from. Anyone have some old code they wrote at this sort of level?
Captain P
Captain P
You can see classes as just types. Only, instead of containing only a single value, you can define just what exactly they store.

For example, if you had power-ups spread across the level, you would create a Powerup class, containing it's type and location. You can now create multiple Powerups, just as you can use multiple ints or bools in your program:

Powerup a;

a.x = 5;
a.y = 10;

A next step would be to derive classes from other classes. Say, a sword is a specific Powerup type: besides a location and type, it also needs to store the strength and duration of the sword. So:

class Sword: public Powerup
{
// class definition
}

A Sword object can be treated as a Powerup, since essentially it is one, but it adds a few variabeles (and possibly functions) of it's own. This saves you writing code twice and makes maintenance easier, if done well of course.
There are far more aspects to OO programming, such as dividing responsibility - should the Paint class render all objects, or should each object have a Paint function itself? Rather than treating classes as data buckets, you should keep in mind they can have functionality attached.

I've put up the source of a small ascii game prototype I wrote a while ago here:
download. It features a scripting-system, allowing you to create levels for it with Notepad. I hope you can learn a few things from it. It's certainly not the only, or perfect, approach, but it sufficed at that time. :)
Palidine
Palidine
Quote:
Original post by bam104
I was hoping for something more detailed that i could base my first attempt from. Anyone have some old code they wrote at this sort of level?


You're honestly waaaaay better off just trying this out yourself to begin with and just coming back to the boards with questions. Looking at someone else's code and just copy - pasting or even just cribbing will dramatically slow down your learning curve.

Just take a stab and start writing code. You'll get confused pretty quickly and you can come here with good questions.

-me

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.