Advertisement

Pascal - Maze

Started by March 12, 2002 03:45 PM
2 comments, last by sanguineraven 22 years, 9 months ago
since i am studying pascal for my computer science course, i thought i may aswell do it as best as i can. ive made a maze game (very very very basic) to test some things. However i cannot get ''collision detection'' to work so it stops moving into a wall.
  
program maze1a;

uses
    sysutils,crt;

{$R *.RES}

const
     up     = 72;
     down   = 80;
     left   = 75;
     right  = 77;

var
   number_moves  :integer;
   key_press     :char;

   temp_x        :integer;
   temp_y        :integer;

   main_area     :array[1..10,1..10] of char;
   load_stream   :text;


procedure load_maze;
var
   k:integer;
   l:integer;
begin
     assignfile(load_stream,''maze.dat'');
     reset(load_stream);
     for k:= 1 to 10 do
     begin
          for l:= 1 to 10 do
          begin
               read(load_stream,main_area[l,k]);
          end;
     end;
     for k:= 1 to 10 do
     begin
          for l:= 1 to 10 do
          begin
               write(main_area[l,k]);
          end;
     end;
end;

procedure move_up;
begin
     temp_x := wherex;
     temp_y := wherey;
     gotoxy(temp_x,temp_y);
     write('' '');
     gotoxy(temp_x,temp_y-1);
     write(''X'');
     gotoxy(temp_x,temp_y-1);
end;

procedure move_down;
begin
     temp_x := wherex;
     temp_y := wherey;
     gotoxy(temp_x,temp_y);
     write('' '');
     gotoxy(temp_x,temp_y+1);
     write(''X'');
     gotoxy(temp_x,temp_y+1);
end;

procedure move_left;
begin
     temp_x := wherex;
     temp_y := wherey;
     gotoxy(temp_x,temp_y);
     write('' '');
     gotoxy(temp_x-1,temp_y);
     write(''X'');
     gotoxy(temp_x-1,temp_y);
end;

procedure move_right;
begin
     temp_x := wherex;
     temp_y := wherey;
     gotoxy(temp_x,temp_y);
     write('' '');
     gotoxy(temp_x+1,temp_y);
     write(''X'');
     gotoxy(temp_x+1,temp_y);
end;


begin
     load_maze;
     number_moves := 0;
     gotoxy(2,2);
     write(''X'');
     gotoxy(2,2);
     repeat
     begin
          key_press := readkey;
          case key_press of
               chr(up)   : move_up;
               chr(down) : move_down;
               chr(left) : move_left;
               chr(right): move_right;
          end;


     end
     until number_moves = 100;
     readln;
end.
  
the way i tried implementing the collision detection is in the ''move right'' procedure i had it checking the cell to the right of its current location in the array. however,i ran it and i cudnt move,so i tried getting it to print wot was in the cell on the right somewhere else so i cud see whether it was recognising the space or a wall, and it recognised a space as a wall,
  
procedure move_right;
begin
     temp_x := wherex;
     temp_y := wherey;
     if main_area[temp_x+1,temp_y] = '' '' then
     begin
          gotoxy(temp_x,temp_y);
          write('' '');
          gotoxy(temp_x+1,temp_y);
          write(''X'');
          gotoxy(temp_x+1,temp_y);
     end;
end;
  
so its starting location is 2,2 and it shud check the array in 2,3 where it shud find a space: @@@@@@@@@@ @ @ @ @@@@@@ @ @ @@@@@@ @ @ @ @ @@ @@ @ @ @ @@@@@@@@@@ but it shows up a @ what is wrong? What about me? What about Raven?
i read through it and in the WHOLE thing i didn''t see u set a value to wherex,wherey,tempx, or tempy.

i don''t even see wherex,wherey even declared
if ur guy starts at 2,2 ... and u didn''t set it... tempx,tempy would both be 0 and it would be checking spot 1,0 if he was moving right.
Advertisement
gotoxy(2,2);

What about me? What about Raven?
what?? all that does is go to 2,2 and put an x.

i think ur confused.
saying gotoxy(2,2); does not mean it keeps those coordinates

u have to write tempx=2, tempy=2.

this is what i think it should go like

procedure move_right;
begin
if main_area[temp_x+1,temp_y] = '' '' then
begin
gotoxy(temp_x,temp_y);
write('' '');
gotoxy(temp_x+1,temp_y);
write(''X'');
temp_x := temp_x+1; //notice its now changing
end;
end;

This topic is closed to new replies.

Advertisement