The simplest way is probably used by games like bomberman and sokoban, where you can only move one tile at a time. Here's an example, press esc to switch to smooth movement:
Code:
' Walls for something like bomberman or sokoban.
set window "Walls 1", 256, 240, false, 3
set redraw off
' Level map[y][x], 0 = nothing, 1 = obstacle, 2 = player start.
map = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1],
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1],
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
' Width and height of map.
mapHeight = sizeof(map)
mapWidth = sizeof(map[0])
' Find player starting position.
playerX = unset
playerY = unset
for y = 0 to mapHeight - 1
for x = 0 to mapWidth - 1
if map[y][x] = 2
playerY = y
playerX = x
map[y][x] = 0
break
endif
next
next
' 'assert' stops the program with an runtime error if the first parameter is not true.
assert typeof(playerX), "Player not found in map!"
' Game loop without player movement animation.
moveDelay = 0
while not keydown(KEY_ESCAPE, true)
' Only let player move if moveDelay is 0.
moveDelay = max(moveDelay - 1, 0)
if moveDelay = 0
dx = 0
dy = 0
' Check for arrow keys and level bounds.
if keydown(KEY_LEFT) and playerX > 0 dx = -1
elseif keydown(KEY_RIGHT) and playerX < mapWidth - 1 dx = 1
elseif keydown(KEY_UP) and playerY > 0 dy = -1
elseif keydown(KEY_DOWN) and playerY < mapHeight - 1 dy = 1
' Can player move to that location, or is it occupied?
if (dx or dy) and map[playerY + dy][playerX + dx] = 0
playerX = playerX + dx
playerY = playerY + dy
moveDelay = 15
endif
endif
' Draw map.
set color 0, 0, 0
cls
set color 128, 96, 32
for y = 0 to mapHeight - 1
for x = 0 to mapWidth - 1
if map[y][x] = 1
draw rect x*16, y*16, 16, 16, true
endif
next
next
' Draw player.
set color 16, 255, 32
draw rect playerX*16, playerY*16, 16, 16, true
' Instructions.
set color 255, 255, 255
set caret width(primary)/2, 2
center "Move with arrow keys"
center "Press escape to continue"
redraw
fwait 60
wend
' Game loop with player movement animation.
' Use screen coordinates for player now.
playerX = playerX*16
playerY = playerY*16
' Player movement.
playerDX = 0
playerDY = 0
playerSteps = 0
while not keydown(KEY_ESCAPE, true)
' Only let player start new move if not already moving.
if playerSteps = 0
dx = 0
dy = 0
' Check for arrow keys and level bounds.
mapX = playerX/16
mapY = playerY/16
if keydown(KEY_LEFT) and mapX > 0 dx = -1
elseif keydown(KEY_RIGHT) and mapX < mapWidth - 1 dx = 1
elseif keydown(KEY_UP) and mapY > 0 dy = -1
elseif keydown(KEY_DOWN) and mapY < mapHeight - 1 dy = 1
' Can player move to that location, or is it occupied?
if (dx or dy) and map[mapY + dy][mapX + dx] = 0
' Set direction and a counter for 16 steps.
playerDX = dx
playerDY = dy
playerSteps = 16
endif
' Move player.
else
playerSteps = playerSteps - 1
playerX = playerX + playerDX
playerY = playerY + playerDY
endif
' Draw map.
set color 0, 0, 0
cls
set color 32, 96, 192
for y = 0 to mapHeight - 1
for x = 0 to mapWidth - 1
if map[y][x] = 1
draw rect x*16, y*16, 16, 16, true
endif
next
next
' Draw player.
set color 16, 255, 32
draw rect playerX, playerY, 16, 16, true
' Instructions.
set color 255, 255, 255
set caret width(primary)/2, 2
center "Move with arrow keys"
center "Press escape to exit"
redraw
fwait 60
wend