-
A "Compass" for text RPG
Hello fellow coders!,
I want to create a simple text-text based RPG. The player will move on a 2d grid (x,y coords) with the options of n, s, e, w (north, south, etc.). What I want to do is design a Compass class that, depending on the x/y coords of the player, will output "NW", "E", or whatever the current direction happens to be. I thought about simply saying, for example:
// north-west movement
if ((yPosition > 0) && (xPosition > 0)
{
cout << "NW";
}
// northern movement
if (yPosition > 0)
{
cout << 'N';
}
etc., but it doesn't work; I get conflicting messages. Suggestions?
Ah, but there is ONE more thing I want to do, but am having problems with this as well. Within certain x/y coord. ranges, I want to "name" sections of land. For example if y is between 1-4 and x is between 1-4, the text "Village of Neutrality" would be shown on the screen. Then if they player moved and the coords. were, say, y between 5-100, x between 5-100 (NE movement), it might output "Forest of Evil Stuff" or something. In other words I'd like to "spruce up" the game because it is simple text (I'm just getting into game dev.) adventure, and I thought that the implementation of a compass along with a "map" informing the player as to their location might be something to make the game more interesting. The main purpose of the compass and map are to test my programming skills. Challenges help me advance, so I welcome them. :)
Any suggestions, codes, comments, ANYTHING? Please respond ASAP, and thx in advance for your help! ;)
cpp4ever
-
reply
try this:
if(...)
{
}
else if(...)
{
}
else if(...)
{
}
... and so on
instead of:
if(...)
{
}
if(...)
{
}
... and so on
-
You need to create an engine that will run all of this for you.
First you need to decide what you want to be able to do within the game, and put that in the engine. The engine is the workhorse of the game, whereas the game itself is usually just a lot of data files that the engine uses. These files can be changed/edited by an editor (again you must program the editor) to create an entirely different game, but based on the same engine.
Your movement scheme could be coded like this:
I use row and col instead of x,y - it is easier to use row,col when dealing with arrays than x,y which is usually y,x for an array.
Code:
class Location
{
int row;
int column;
const char *LocationName;
public:
Location (int trow,int tcol)
};
class Area
{
int endrow;
int endcolumn;
char *AreaName;
public:
Area(int trow,int tcol,int tendrow,tendcol);
};
class Map
{
Location *Map;
Area *AreasInMap;
int width;
int height;
public:
Map(int twidth,int theight);
int GetMapValue(int row,int col) {return Map[row*width*col];};
Area *GetArea(int row,int col); //return area that row,col lies in
};
You could use base classes and derive from Location, I just chose not to. This is not enough for your game engine, nor is it necessarily error-free as I just typed this in quickly. But it should help you a bit with the structure.
I usually code directions right into a room. Let's say that at map [row*width+col] there is a number 4. That number corresponds to an array element in the room array. The direction array matches the room array exactly (as it pertains to order), so I search the direction array at element 4 to get the valid exits from the room. A 1 usually indicates that direction is valid, and a 0 that it is not. A value higher than 1 could represent locked doors, hidden passages, or any number of things.
Directions can be held in binary and you can use bitwise comparisons to find out which directions are valid for the current room. If you want locked doors, etc., bitwise comparisons are not the best system since they only work with 1's and 0's in your direction number.
-
In the source, Map[row*width*col] should be Map[row*width+col]. The first will probably crash your system (if you write to it) and return spurious data (if you read from it) since it will massively overrun the array bounds.
-
RPG programming sites
Hello all,
Thx for your replies, first off. :)
Do any of you know of sites that focus on RPG programming (i.e. not just design)? I have "Swords & Circuitry" book (really good!) but it is mostly design and doesn't focus on coding. I know of a site http://www.2dgame.nl/Home.asp that focuses on a tutorial to make a 2d sidescroller; I guess I'm looking for an RPG site like this one, to make myself clear.
cpp4ever
-
It isn't devoted to that genre alone, but www.allegro.cc has a ton of RPGs, most of which are open source.