I like to use bitwise operations to help with setting a player's direction. I knocked together some code to show how you can do this:
sync on : sync rate 60
autocam off : hide mouse
Type MapBlock
ObjectID,X, Y
BlockType as String //this names and also picks later what 3d model to use for the map section
Endtype
Global Dim Maplayout(10,10) as MapBlock
type PlayerData
ObjectID,x,y, MoveDelay
endtype
player as PlayerData
PlayerMove as byte
rem we will use the rightmost four bits of PlayerMove to show which direction they can go
rem if bit 0 is set, then the player can move up
rem bit 1 is set = can move down
rem bit 2 is set = can move right
rem bit 3 is set = can move left
rem bit values: bit 0 = 1, bit 1 = 2, bit 2 = 4, bit 3 = 8
rem so if the player can move up or down only, PlayerMove would be set to 3
rem if the player can move up, left and right, PlayerMove = 13
rem just add the values of the individual bit(s) together to obtain the value
gosub InitMap
gosub InitPlayer
GameOver = 0
repeat
gosub MovePlayer
gosub Debug
sync
if spacekey() = 1 then GameOver = 1 : // quit game using ESC or spacebar
until GameOver = 1
rem delete objects and clean up
for i = 100 to (NextObject - 1)
if object exist(1) = 1
delete object i
endif
next i
end
Debug:
text 10,10,"FPS: " + str$(screen fps() )
text 10,100,"PlayerMove: " + str$(PlayerMove)
return
InitMap:
NextObject = 100
For y = 1 to 10
for x = 1 to 10
Maplayout(x,y).ObjectID = NextObject
inc NextObject
Maplayout(x,y).X = x * 25
Maplayout(x,y).Y = y * 25
make object cube Maplayout(x,y).ObjectID,25
position object Maplayout(x,y).ObjectID,Maplayout(x,y).X,0.0,MapLayout(x,y).Y
Maplayout(x,y).BlockType = "Dirt" : color object Maplayout(x,y).ObjectID,rgb(137,85,56)
if y = 2 then Maplayout(x,y).BlockType = "Grass" : color object Maplayout(x,y).ObjectID,rgb(0,185,0)
Next x
next y
rem set initial position of the camera
position camera Maplayout(5,5).X,300.0,Maplayout(5,5).Y
rem point camera towards the map objects
point camera Maplayout(5,5).X,0.0,Maplayout(5,5).Y
return
InitPlayer:
player.x = rnd(9) + 1
player.Y = rnd(9) + 1
player.ObjectID = NextObject
inc NextObject
make object sphere player.ObjectID,25
color object player.ObjectID,rgb(255,0,0) : // red player
position object player.ObjectID,player.x * 25,12.5,player.y * 25
player.MoveDelay = timer()
return
MovePlayer:
if timer() < player.MoveDelay then return
rem check all around player and check boundaries too
PlayerMove = 0 : rem initialize value
if player.x > 1 then PlayerMove = PlayerMove || %1000 : // player can move left
if player.x < 10 then PlayerMove = PlayerMove || %0100 : // right ok
if player.y < 10 then PlayerMove = PlayerMove || %0001 : // up okay
if player.y > 1 then PlayerMove = PlayerMove || %0010 : // down okay
if upkey() = 1 and (PlayerMove && %0001) > 0
rem player can move up
inc player.y
endif
if rightkey() = 1 and (PlayerMove && %0100) > 0
rem player can move right
inc player.x
endif
if downkey() = 1 and (PlayerMove && %0010) > 0
// down
dec player.y
endif
if leftkey() = 1 and (PlayerMove && %1000) > 0
// left
dec player.x
endif
position object player.ObjectID,player.x * 25,12.5,player.y * 25
Player.MoveDelay = timer() + 100 : // wait 1/10 of a second in-between player moves
return
This creates cubes that represent the map values. The player is a red sphere, the "Dirt" cubes are brown and the "Grass" ones are green. Note that the Y works from the bottom up and not top down.
There are other ways of doing it, but this is what I typically will use. If bitwise operation isn't for you, let me know and I can show you another way.
So many games to code.....so little time.