Cant Move North and south
High all,
I''m making a text based rpg engine and so far I can move east and west in a 1d array, but now that I am testing 2d movement, I can''t move south and north properly for some reason. I will post the code here so you can compile and try to work with the engine, you will see what I mean.
The commands are ''n'' ''s'' ''e'' and ''w'' for moving north south east and west, a error message saying "you can''t move through the wall" will show up if you try to go to far east or west or north or south.
THx in advance for all help guys
Cheers, Armed.
If your array is one dimensional then you have to use this formula to tell where the character is at in it:
ArrayPos=YLoc*MapWidth+XLoc;
That assumes the map is rectangular. You may want to just switch to a 2D array to make it easier.
Ben
http://therabbithole.redback.inficad.com
ArrayPos=YLoc*MapWidth+XLoc;
That assumes the map is rectangular. You may want to just switch to a 2D array to make it easier.
Ben
http://therabbithole.redback.inficad.com
oops, I just realized I forgot to post the code.
Btw it is in a 2d array already and I keep track of the players position in an array with 2 locations, one for the x pos and one for the y pos
HEres the code:
Btw it is in a 2d array already and I keep track of the players position in an array with 2 locations, one for the x pos and one for the y pos
![](smile.gif)
HEres the code:
May 21, 2001 03:42 PM
oops, I just realized I forgot to post the code.
Btw it is in a 2d array already and I keep track of the players position in an array with 2 locations, one for the x pos and one for the y pos
HEres the code:
#include
#include
//used to set flags in the exits variable when the game is initiallized
//ie you set exits[N] = 1; to say that there is a exit to the north at
//that location, soon to be expanded to include north east, etc.
const N=1;
const E=2;
const W=3;
const S=4;
//the location of the descriptions in the description array
int map_addresses[3][3] = {0,0,0,
0,1,37,
0,80,107,};
//individual description of a location in the game
struct location{
char *description;
int exits[4];
} map[3][3]; //holds descriptions for all locations in the game
char descriptions[] = {"A dark room with an exit to the east/Another dark room with an exit to the west/Exit to the North and west/Exit to the North and East/"};
//these two variables set the size of the map, right now it is
// 2x2 note the 0 locations are not used in order to maintain
// simplicity
int mapsize_x = 3;
int mapsize_y = 3;
//holds the players info
struct character {
int x_pos;
int y_pos;
} player;
void NewGame(void); //contains game code
void InitializeGame(void); //sets up the map and sets player
// position
void PrintDescription(void); //prints out the location
// description
bool GetCommands(void); //get the players commands
int main(void) {
char choice = ''l'';
//print menu and get command
printf("welcome to my C text game");
printf("make a choice: \n (P)lay \n (E)xit \n : ");
do {
scanf("%c", &choice);
switch(choice) {
case ''P'':
case ''p'':
NewGame(); //start a new game
break;
case ''E'':
case ''e'':
return 0; //exit
break;
default:
printf("please enter a valid choice");
break;
}
}while(choice != ''P'' && choice != ''p'' && choice != ''e''
&& choice != ''E'');
return 0;
}
void NewGame(void) {
bool exit = false;
InitializeGame(); //setup the map and such
//Game loop follows:
do {
PrintDescription(); //print the location
// description
exit = GetCommands(); //get the players
// commands
}while(exit != true);
}
void InitializeGame(void) {
//setup all the locational information in the game
//will soon be read in from a file
map[1][1].description = descriptions; //set description
// to point to first description
map[1][1].exits[E] = 1; //exit to the east
map[1][1].exits = 1; //exit to the south<br><br> map[1][2].description = descriptions + 37; //set <br> // description to point to the second description <br> map[1][2].exits = 1; <br> map[1][2].exits[W] = 1; //an exit to the west<br><br> map[2][1].description = descriptions + 80;<br> map[2][1].exits[N] = 1; //an exit to the north<br> map[2][1].exits[E] = 1; //an exit to the east<br><br> map[2][2].description = descriptions + 107; <br> map[2][2].exits[N] = 1; //exit to the north<br> map[2][2].exits[W] = 1; //exit to the west<br><br> //the players start position in the map array<br> player.x_pos = 1; <br> player.y_pos = 1; <br>}<br><br>void PrintDescription(void) { <br> <br> char *original_position_in_description = <br> map[player.x_pos][player.y_pos].description;<br><br> do {<br> //this loop steps through the <br> //descriptions string printing the <br> //current locations description character by character <br> // until <br> //a ''/'' is found which denotes the end of the description<br> <br> printf("%c", <br> *map[player.x_pos][player.y_pos].description); <br> <br> map[player.x_pos][player.y_pos].description++;<br> }while(*map[player.x_pos][player.y_pos].description !<br> = ''/'');<br> <br> map[player.x_pos][player.y_pos].description = original_position_in_description; //reset the description <br> //pointor to point the correct position. <br> printf("\n");<br>}<br><br>bool GetCommands(void) { <br> char command = ''z''; <br><br> printf("enter a command q to exit: \n"); <br> <br> do { <br> scanf("%c", &command); <br> //clear the buffer, used <br> //to print invalid command all the time this fixes that <img src="smile.gif" width=15 height=15 align=middle><br> scanf("%c", &command); //get the actual command<br> <br> if (command == ''e'' || command == ''E'') {<br> if (player.x_pos < mapsize_x) <br> //can still move to right, not at the last x<br> player.x_pos += 1; //move the player east in the array ie to the right.<br> else printf("you cannot move through the " <br> + "wall\n"); //all the way to the right of <br> //the array<br> }<br> if (command == ''w'' || command == ''W'') { <br> if (player.x_pos > 1) <br> //not at the last left can still move<br> player.x_pos -=1; <br> //move left or west.<br> else printf("you cannot move through" <br> + "the wall\n");<br> }<br> if (command == ''s'' || command == ''S'') { <br> if (player.y_pos < mapsize_y)<br> player.y_pos += 1; //move the player down a postion in the array<br> else printf("you cannot move throughthe"<br> + " wall\n"); <br> }<br> if (command == ''n'' || command == ''N'') {<br> if (player.y_pos > 1) <br> player.y_pos -= 1; <br> else printf("you cannot move through"<br> +" the wall\n"); <br><br> }<br> else if (command != ''n'' && command != ''N'' && command !=''s'' && command != ''S'' && command != ''E'' && command != ''e'' && command != ''W'' && command != ''w'' && command != ''q'' ) {<br> printf("please enter a valid command"); <br> }<br> }while(command != ''E'' && command != ''e'' && command != ''W'' && command != ''w'' && command != ''q'' ); <br> <br> if (command == ''Q'' || command == ''q'') return true; <br> else return false; <br>}<br>
Btw it is in a 2d array already and I keep track of the players position in an array with 2 locations, one for the x pos and one for the y pos
![](smile.gif)
HEres the code:
#include
#include
//used to set flags in the exits variable when the game is initiallized
//ie you set exits[N] = 1; to say that there is a exit to the north at
//that location, soon to be expanded to include north east, etc.
const N=1;
const E=2;
const W=3;
const S=4;
//the location of the descriptions in the description array
int map_addresses[3][3] = {0,0,0,
0,1,37,
0,80,107,};
//individual description of a location in the game
struct location{
char *description;
int exits[4];
} map[3][3]; //holds descriptions for all locations in the game
char descriptions[] = {"A dark room with an exit to the east/Another dark room with an exit to the west/Exit to the North and west/Exit to the North and East/"};
//these two variables set the size of the map, right now it is
// 2x2 note the 0 locations are not used in order to maintain
// simplicity
int mapsize_x = 3;
int mapsize_y = 3;
//holds the players info
struct character {
int x_pos;
int y_pos;
} player;
void NewGame(void); //contains game code
void InitializeGame(void); //sets up the map and sets player
// position
void PrintDescription(void); //prints out the location
// description
bool GetCommands(void); //get the players commands
int main(void) {
char choice = ''l'';
//print menu and get command
printf("welcome to my C text game");
printf("make a choice: \n (P)lay \n (E)xit \n : ");
do {
scanf("%c", &choice);
switch(choice) {
case ''P'':
case ''p'':
NewGame(); //start a new game
break;
case ''E'':
case ''e'':
return 0; //exit
break;
default:
printf("please enter a valid choice");
break;
}
}while(choice != ''P'' && choice != ''p'' && choice != ''e''
&& choice != ''E'');
return 0;
}
void NewGame(void) {
bool exit = false;
InitializeGame(); //setup the map and such
//Game loop follows:
do {
PrintDescription(); //print the location
// description
exit = GetCommands(); //get the players
// commands
}while(exit != true);
}
void InitializeGame(void) {
//setup all the locational information in the game
//will soon be read in from a file
map[1][1].description = descriptions; //set description
// to point to first description
map[1][1].exits[E] = 1; //exit to the east
map[1][1].exits
Well I guess I just wasted another thread
I managed to start over and program the whole map traversing code, ie moving north south east and west all in one try
.
Now on to working with files.
Sorry for the false alarm guys : )
Cheers, Armed.
![](smile.gif)
I managed to start over and program the whole map traversing code, ie moving north south east and west all in one try
![](smile.gif)
Now on to working with files.
Sorry for the false alarm guys : )
Cheers, Armed.
May 21, 2001 06:47 PM
Well I guess I just wasted another thread
I managed to start over and program the whole map traversing code, ie moving north south east and west all in one try
.
Now on to working with files.
Sorry for the false alarm guys : )
Cheers, Armed.
![](smile.gif)
I managed to start over and program the whole map traversing code, ie moving north south east and west all in one try
![](smile.gif)
Now on to working with files.
Sorry for the false alarm guys : )
Cheers, Armed.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement