Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Maze Co-Ordinate Problem

jpgenovesejpgenovese Posts: 19Member
Hey i am designing several mazes in a game I am trying to do and I want that whenever the object hits the wall he will return to the previous position so that it would seem that the object bumped into the wall and not going in the wall. I am doing it by writing all the co-ordinates of the designed maze but this will take me alot of time. Is there another way of doing this? Thanks alot.

Comments

  • Phat NatPhat Nat Posts: 757Member
    : Hey i am designing several mazes in a game I am trying to do and I
    : want that whenever the object hits the wall he will return to the
    : previous position so that it would seem that the object bumped into
    : the wall and not going in the wall. I am doing it by writing all the
    : co-ordinates of the designed maze but this will take me alot of
    : time. Is there another way of doing this? Thanks alot.

    Yes ;)

    Okay, to do this, I need more info on how you're programming it. Some code snippets would help.
    Are you in text mode or graphics mode?
    How are your mazes drawn to the screen? Stored in a const array? Loaded from a file?
    Assuming text mode, are all the walls the same character (eg. "#") or are they different vertically, horizontally and at corners (eg. "
  • jpgenovesejpgenovese Posts: 19Member
    Oh first of all thanks for replying. I think I am going to use 1 character which will be the same for every part of the maze. I thought if there would be something like if G=(chr(1)) then it will bounce back ( I know how to do the bounce thing I just change the x or y co-ordinate back to the last value). I am currently doing this game by mentioning each co-ordinate of the maze which is taking me alottttt. I am using X and Y as variables for the movement of player combined with dx and dy (for an increase or decrease in the co-ordinate) This is an example.....


    Repeat



    dx1:=0;
    dy1:=0;
    dx2:=0;
    dy2:=0;

    Repeat

    Until Keypressed;
    choice:=Readkey;

    case choice of {movement of Player 1}

    'a': begin dx1:=-1;
    gotoxy(x1,y1);
    write (' ');
    x1:=x1+dx1;
    end;

    'w': begin dy1:=-1;
    gotoxy(x1,y1);
    write (' ');
    y1:=y1+dy1;
    end;

    'd': begin dx1:=+1;
    gotoxy(x1,y1);
    write (' ');
    x1:=x1+dx1;
    end;

    's': begin dy1:=+1;
    gotoxy(x1,y1);
    write (' ');
    y1:=y1+dy1;
    end;

    end; {case}


    gotoxy(x1,y1);
    write (chr(1));


    until (choice='q'); {end of game}



    Is this good to help you out what I'm doing?

  • Phat NatPhat Nat Posts: 757Member
    Sorry for the late reply. I somehow missed this thread when I looked through the last couple of times ???

    This does help a bit, but still not sure of how you're planning on storing your map. Are you saying that right now you use an if to check each X,Y that has a wall on it? If so this is not a good method as you would need to rewrite your code for every new maze.

    First off your player movement seems right, but in order to avoid repeating writing lines try this:

    [code]
    case choice of {movement of Player 1}
    'a': Begin dx1:=-1; dy1 := 0; End;
    'w': Begin dy1:=-1; dx1 := 0; End;
    'd': Begin dx1:=+1; dy1 := 0; End;
    's': Begin dy1:=+1; dx1 := 0; End;
    end; {case}
    gotoxy(x1,y1);
    write (' ');
    x1:=x1+dx1;
    y1:=y1+dy1;
    [/code]


    Also, try storing your mazes as 2D arrays. To start with make on maze that is just a CONST array. Then later you can switch it to a variable (or not) and load multiple mazes from a file if wanted.

    [code]
    CONST
    MazeSizeX = 5;
    MazeSizeY = 5;
    Maze : Array[ 1..MazeSizeY, 1..MazeSizeX] Of Char =
    (('*','*','*','*','*'),
    ('S',' ',' ','*','*'),
    ('*','*',' ','*','*'),
    ('*','*',' ',' ','E'),
    ('*','*','*','*','*'));

    TYPE
    Spots = record { Stores an X,Y variable }
    X : Byte;
    Y : Byte;
    End;
    [/code]
    Obviously you are going to make yours much larger, but this is the idea. Using this array, you can just use two loops to draw it to the screen in one procedure and in another you can use it to check if the player hit a wall.

    The drawing procedure would look like this (Also it will return start and end points for the maze):
    [code]
    PROCEDURE DrawMaze( VAR Startspot, EndSpot : Spots); { MAZENUMBER Unused right now }
    VAR
    X, Y : Byte;
    Begin
    For Y := 1 to MazeSizeY Do
    For X := 1 to MazeSizeX Do
    Begin
    Case Maze[Y,X] Of
    'S' : Begin
    StartSpot.X := X;
    StartSpot.Y := Y;
    End;
    'E' : Begin
    EndSpot.X := X;
    EndSpot.Y := Y;
    End;
    End; {Case}
    GotoXY( X+5, Y+5);
    Write( Maze[Y,X]);
    End;
    End;
    [/code]

    This would be a basic outline. You could then use any characters as walls and just use the SPACE (Chr(32)) as available movement spots.

    Hope this helps,
    Phat Nat

    PS - One thing I noticed was the use of letters for movement. You can use this for the arrow keys as well if wanted:
    [code]
    case choice of {movement of Player 1}
    { #0 for Extended characters (arrow keys, Insert, Home, PgUp, etc) }
    #0 : Begin
    choice := readkey;
    case choice of { movement of Player 1 with arrow keys }
    #75 : Begin dx1:=-1; dy1 := 0; End;
    #72 : Begin dy1:=-1; dx1 := 0; End;
    #77 : Begin dx1:=+1; dy1 := 0; End;
    #80 : Begin dy1:=+1; dx1 := 0; End;
    End;
    Choice := #1; { For safety }
    End;
    { movement of Player 1 with letters }
    'a': Begin dx1:=-1; dy1 := 0; End;
    'w': Begin dy1:=-1; dx1 := 0; End;
    'd': Begin dx1:=+1; dy1 := 0; End;
    's': Begin dy1:=+1; dx1 := 0; End;
    end; {case}
    [/code]
Sign In or Register to comment.