I made a variable called lastpressed that keeps track of the last key that was pressed. I also added an array called Beenthere, that tracks where you have been and leaves breadcrumbs on the map. Let me know if you have any questions!
` ASCII Maze
set display mode 800,600,32,1
sync rate 30
sync on
direction as string
player as string = "@"
wall as string = "#"
block as integer
player_x as integer = 2
player_y as integer = 2
fontsize as integer = 24
maze_width as integer = 16
maze_height as integer = 16
lastpressed as string
dim BeenThere (maze_width,maze_height) as string
for x=1 to maze_width
for y=1 to maze_height
BeenThere(x,y)=" "
next y
next x
direction = "N"
dim maze(maze_width, maze_height)
restore maze1
for y = 1 to maze_height
for x = 1 to maze_width
read block
maze(x,y) = block
next x
next y
set text font "Courier"
set text size fontsize
` Main Loop
do
cls 0
text 750, fontsize, direction
text 600, fontsize, "ASCII Maze"
text 600, fontsize + 80, "X:" + str$(player_x)
text 680, fontsize + 80, "Y:" + str$(player_y)
BeenThere(player_x,player_y) = "."
gosub drawmap
for x=1 to maze_width
for y=1 to maze_height
if x<>player_x or y<>player_y then text x*fontsize, y*fontsize, BeenThere(x,y)
next y
next x
text player_x * fontsize, player_y * fontsize, player
if upkey() and lastpressed<>"up"
lastpressed="up"
` Can the player move up?
if direction = "N"
if maze(player_x, player_y - 1) = 0
dec player_y
endif
endif
if direction = "W"
` Can the player move left?
if maze(player_x - 1, player_y) = 0
dec player_x
endif
endif
if direction = "S"
` Can the player move down?
if maze(player_x, player_y + 1) = 0
inc player_y
endif
endif
if direction = "E"
` Can the player move right?
if maze(player_x + 1, player_y) = 0
inc player_x
endif
endif
endif
if downkey() and lastpressed<>"down"
lastpressed="down"
endif
if leftkey() and lastpressed<>"left"
lastpressed="left"
if direction = "N"
direction = "W"
goto blergh
endif
if direction = "W"
direction = "S"
goto blergh
endif
if direction = "S"
direction = "E"
goto blergh
endif
if direction = "E"
direction = "N"
goto blergh
endif
endif
blergh:
if rightkey() and lastpressed<>"right"
lastpressed="right"
if direction = "N"
direction = "E"
goto blergh2
endif
if direction = "W"
direction = "N"
goto blergh2
endif
if direction = "S"
direction = "W"
goto blergh2
endif
if direction = "E"
direction = "S"
goto blergh2
endif
endif
blergh2:
if direction = "N" then player = "^"
if direction = "S" then player = "v"
if direction = "E" then player = ">"
if direction = "W" then player = "<"
if scancode()=0 then lastpressed=""
sync
loop
maze1:
data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
data 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1
data 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1
data 1,0,1,1,1,1,0,0,1,0,0,1,1,1,1,1
data 1,0,1,0,0,0,0,0,1,1,1,1,0,0,0,1
data 1,0,1,0,0,0,0,0,0,0,0,1,0,0,0,1
data 1,0,1,0,0,0,0,0,0,0,0,1,0,0,0,1
data 1,0,0,0,0,0,1,1,1,0,0,1,0,0,0,1
data 1,0,0,0,0,0,1,0,0,0,0,1,0,0,1,1
data 1,0,0,0,0,0,1,0,0,0,0,1,0,0,1,1
data 1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1
data 1,0,0,0,1,1,1,0,0,1,0,1,0,0,0,1
data 1,0,0,0,0,0,1,0,0,1,0,1,0,0,0,1
data 1,0,0,0,0,0,1,1,1,1,0,1,0,0,0,1
data 1,0,1,1,1,0,1,0,0,0,0,0,0,0,1,1
data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
drawmap:
for y = 1 to maze_height
for x = 1 to maze_width
if maze(x,y) = 1
' text x * fontsize, y * fontsize, wall
endif
next x
next y
if direction = "N"
if maze(player_x-1, player_y) = 1
gosub leftofplayer
else
gosub leftofplayerelse
endif
if maze(player_x+1, player_y) = 1
gosub rightofplayer
else
gosub rightofplayerelse
endif
if maze(player_x, player_y - 1) = 1
gosub infrontofplayer
else
gosub infrontofplayerelse
endif
endif
if direction = "E"
if maze(player_x, player_y+1) = 1
gosub rightofplayer
else
gosub rightofplayerelse
endif
if maze(player_x+1, player_y) = 1
gosub infrontofplayer
else
gosub infrontofplayerelse
endif
if maze(player_x, player_y - 1) = 1
gosub leftofplayer
else
gosub leftofplayerelse
endif
endif
if direction = "S"
if maze(player_x-1, player_y) = 1
gosub rightofplayer
else
gosub rightofplayerelse
endif
if maze(player_x+1, player_y) = 1
gosub leftofplayer
else
gosub leftofplayerelse
endif
if maze(player_x, player_y + 1) = 1
gosub infrontofplayer
else
gosub infrontofplayerelse
endif
endif
if direction = "W"
if maze(player_x-1, player_y) = 1
gosub infrontofplayer
else
gosub infrontofplayerelse
endif
if maze(player_x, player_y-1) = 1
gosub rightofplayer
else
gosub rightofplayerelse
endif
if maze(player_x, player_y + 1) = 1
gosub leftofplayer
else
gosub leftofplayerelse
endif
endif
return
leftofplayer:
set cursor 330,370
print "\"
set cursor 330,390
print " \"
set cursor 330,410
print " \"
set cursor 330,430
print " "
set cursor 330,450
print " "
set cursor 330,470
print " "
set cursor 330,490
print " "
set cursor 330,510
print " /"
set cursor 330,530
print " /"
set cursor 330,550
print "/"
return
leftofplayerelse:
set cursor 360,370
print " "
set cursor 360,390
print "__ "
set cursor 360,410
print " |\"
set cursor 360,430
print " | "
set cursor 360,450
print " | "
set cursor 360,470
print " | "
set cursor 360,490
print " | "
set cursor 360,510
print "_|/ "
set cursor 360,530
print ""
return
rightofplayer:
set cursor 510, 370
print " /"
set cursor 510, 390
print " / "
set cursor 510, 410
print "/ "
set cursor 510, 430
print " "
set cursor 510, 450
print " "
set cursor 510, 470
print " "
set cursor 510, 490
print " "
set cursor 510, 510
print "\ "
set cursor 510, 530
print " \ "
set cursor 510, 550
print " \"
return
rightofplayerelse:
set cursor 510, 370
print " "
set cursor 510, 390
print " __"
set cursor 510, 410
print "/| "
set cursor 510, 430
print " | "
set cursor 510, 450
print " | "
set cursor 510, 470
print " | "
set cursor 510, 490
print " | "
set cursor 510, 510
print "\|_"
set cursor 510, 530
print " "
return
infrontofplayer:
set cursor 400,410
print " _______"
set cursor 400,430
print "| |"
set cursor 400, 450
print "| |"
set cursor 400,470
print "| |"
set cursor 400, 490
print "|_______|"
return
infrontofplayerelse:
set cursor 400,430
print "\ / "
set cursor 400, 450
print " \ / "
set cursor 400,470
print " / \ "
set cursor 400, 490
print "/ \ "
return