Friday, July 31, 2009

Game Idea C++ help with map?

I have a plan to create a simple interactive video game with c++. The game will have no graphics except ASCII characters. I wanted to initialize a map and have mountains return a char value of '^', grass a char value of '.'. The character will be a '@'.





I've only been studying the basic syntax of c++ and have yet to get into the actual implementation of this syntax to create something worthwhile and enjoyable.





My problem however is defining a map, and when defining the map how will the character move within it. How can I create a map that will define an x and y environment in which if my character presses 'w' on the keyboard he'll move north or y+1. Have 'a' move me x - 1 and etc.





Should I use a structure? A multi-dimensional array? If you answer could you please give me some detailed instructions as well. And as always any help would be greatly appreciated. Thanks. :)

Game Idea C++ help with map?
I would use a 2D array, and make the number of columns and rows configurable, like this:








class Map


{


private:


char* map;


int numCols, numRows;


int charPositionX, charPositionY;





public:


Map(int columns, int rows) : numCols(columns), numRows(rows), charPositionX(0), charPositionY(0)


{


map = new char[rows][columns];


}





void setCharPos(int x, int y)


{


// do some bounds checking


charPositionX = x;


charPositionY = y;


}





char getMapChar(int x, int y)


{


// do some bounds checking


return map[y * numRows + x];


}





void getMapRow(int y, char* rowOut)


{


// do some bounds checking, and null checking


// or use a std::string reference here to avoid


// buffer overruns


memcpy(rowOut, map[y * numRows], numCols);


}





void moveCharUp()


{


if (charPositionX %26gt; 0)


charPositionX--;


}





void moveCharDown()


{


if (charPositionX %26lt; (numRows - 1))


charPositionX++;


}





//and so forth





};
Reply:for this you would use a Multi dimentional array...


Depending on how you want to code it you can use that to define the landscape. This could be a predefinded landscape or one generated at runtime....





then as the object moves accross you can use its coordinates with the landscape'd data for that position and perform the nessesary function...

wedding flowers

No comments:

Post a Comment