Thread: gotoxy

  1. #1
    Sekti
    Guest

    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

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    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},
    };

  3. #3
    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;
    }
    in your movement code, add this:

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

    does that answer your question?
    Last edited by frenchfry164; 08-10-2002 at 08:00 PM.

  4. #4
    Registered User Sekti's Avatar
    Join Date
    Feb 2002
    Posts
    163
    one thing what does return false do
    Last edited by Sekti; 08-10-2002 at 08:41 PM.
    +++
    ++
    + Sekti
    ++
    +++

  5. #5
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    returns a false value from a function... see how hte return type is bool?
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    Boolean:
    false = 0
    true = 1 (or more)

  7. #7
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    d00b's right. but true can be any non-zero value also
    false = 0
    true != 0
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  8. #8
    Registered User Sekti's Avatar
    Join Date
    Feb 2002
    Posts
    163

    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
    +++
    ++
    + Sekti
    ++
    +++

  9. #9
    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
    }

  10. #10
    fry
    Guest
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is gotoxy??
    By Beachblue in forum C Programming
    Replies: 6
    Last Post: 11-23-2008, 01:24 AM
  2. Tic Tac Toe movement
    By $l4xklynx in forum Game Programming
    Replies: 4
    Last Post: 11-06-2008, 07:22 PM
  3. gotoxy(); help!!
    By clique in forum C Programming
    Replies: 2
    Last Post: 10-07-2008, 04:08 AM
  4. Want to see if I am using gotoxy the right way ...
    By o0obruceleeo0o in forum C++ Programming
    Replies: 5
    Last Post: 04-22-2003, 04:17 PM
  5. Is gotoxy ansi?
    By MeneLaus in forum C Programming
    Replies: 2
    Last Post: 05-18-2002, 02:48 PM