# gotoxy

• 08-10-2002
Sekti
gotoxy
Ok im makeing a game in dos and so far i can have the @ move around the screen but i cant figure out how to make walls and have it to where you cant go through them any help would be nice
• 08-10-2002
Crimpy
Mark a table with say, 0's and 1's that tell you if you can or can't move. If you can then the @ goes where you tell it to, if you can't then it doesn't move. Build a maze of 1's and you can move to 0's. :)
Code:

```int maze[][4] = {     {1,0,1,1},     {1,0,0,1},     {1,1,0,0},     {1,1,0,1}, };```
• 08-10-2002
frenchfry164
well I would say make a map file and have something parse it, but I think you want to keep this simple, so you could just use a 2D bool array (or int if you want to have more than just walls).

Kind of like this (you could make this any size you want, I'm just making it 5x5 for simplicity)
Code:

```bool Map[5][5] { {true,true,true,true,true}, {true,false,false,false,true}, {true,false,false,false,true}, {true,false,false,false,true}, {true,true,true,true,true}, };```
and then have a function to draw the map by reading the values, and if it reads true, it draws a wall. Maybe use the square ASCII character for a wall (or if you don't know how to make one, just use a '#' or something)

for the part where you make the '@' not go through walls, it is called (as you probably know) collision detection. There are several ways to do this. One simple way that would be practicle for your use would be the "Simulated Movement Detection". Basically everytime you move, the program will simulate your movement before you actually move. Then it checks if the tile you are going to move to is solid. If it is, the '@' won't move. If it isn't, the '@' will move. Here is a simple way. If you use this, make sure you define NORTH, SOUTH, WEST, and EAST.
Code:

```bool CollisionDetection(short int direction) {  switch (direction)  {   case NORTH:     if (Map[x][y-1] == true)     return false;   case SOUTH:     if(Map[x][y+1] == true)     return false;   case EAST:     if(Map[x+1][y] == true)     return false;   case WEST:     if(Map[x-1][y] == true)  }  return false; }```

Code:

```if (CollisionDetection(direction)) {  movement code goes here }```
direction is the direction you are about to move.
-------------------------------------------------------------------

• 08-10-2002
Sekti
one thing what does return false do
• 08-10-2002
GrNxxDaY
returns a false value from a function... see how hte return type is bool?
• 08-11-2002
d00b
Boolean:
false = 0
true = 1 (or more)
• 08-11-2002
GrNxxDaY
d00b's right. but true can be any non-zero value also
false = 0
true != 0
• 08-12-2002
Sekti
ok
i have been working on it and it compiles fine but when you run it you cant move the guy unless you like go into the wall i dont know its wierd

Code:

```#include <iostream.h> #include <conio.h> int level [4][4] = { {0,0,0,0}, {0,1,1,0}, {0,1,1,0}, {0,0,0,0}, }; int x = 1; int y = 1; int oldx; int oldy; char choice; void cd (char choice_tmp); void move (); void input (); int main() { cout<<"    "<<endl; cout<<" ## "<<endl; cout<<" ## "<<endl; cout<<"    "<<endl; gotoxy(x,y); cout<<"@"; input(); return 0; } void cd(char choice_tmp) { switch(choice_tmp) { case '2': if(level[x][y+1] == 0) move(); break; case '4': if(level[x-1][y] == 0) move(); break; case '6': if(level[x+1][y] == 0) move(); break; case '8': if(level[x][y-1] == 0) move(); break; } } void move() { oldx = x; oldy = y; gotoxy(oldx,oldy); cout<<" "; switch(choice) { case '2': gotoxy(x,++y); cout<<"@"; input(); break; case '4': gotoxy(--x,y); cout<<"@"; input(); break; case '6': gotoxy(++x,y); cout<<"@"; input(); break; case '8': gotoxy(x,--y); cout<<"@"; input(); break; } } void input() { while(x || y != 0 || 5) { choice = getch(); cd(choice); } }```
try it and tell me whats wrong
• 08-12-2002
frenchfry164
oh I'm sorry. it can be fixed with one line of code.

change

if (CollisionDetection(direction))
{
movement code goes here
}

to

if (!CollisionDetection(direction))
{
movement code goes here
}
• 08-12-2002
fry
"level[x][y+1]"

i believe that this should be "level[y+1][x]" as it goes by row and then by column...

i fiddled around with it also, but was unable to get it to work correctly, as i ran out of time.

also, you should initiate x and y at 0,0 not 1,1 --> your map has this as a wall :)