Map with player and collision detection SdlBasic.


const BOARD_HEIGHT = 15
const BOARD_WIDTH  = 15
const TILE_W = 32
const TILE_H = 32
const WALL = 1
const FLOR = 0
const PLAYER_SPEED = TILE_W
WIDTH  = (TILE_W * BOARD_WIDTH)
HEIGHT = (TILE_H * BOARD_HEIGHT)
old_pos_x = 0
old_pos_y = 0

setdisplay(WIDTH, HEIGHT, 32, 1)

dim img[BOARD_WIDTH, BOARD_HEIGHT] ' to load the map with the img positions
dim m[] ' our OG map fill with a 0 and 1 WALL nad FLOR(FLOOR is reserved word)

player = loadimage("player24x24.png")
player_x = 0
player_y = 0
player_index_x = 0 ' will serve to check the map array to see if 0 or 1
player_index_y = 0 ' same as above

sub loadMap(filename)
if(fileexists(filename)) then
open filename for input as 1
i = 0
while eof(1)=0
input 1, $a
m[i] = $a
i = i + 1
wend
close(1)
end if
end sub

sub fillMap()
for y = 0 to BOARD_HEIGHT
for x = 0 to BOARD_WIDTH
if(m[x+y*BOARD_WIDTH] = FLOR) then
img[x, y] = loadimage("floor.bmp")
else
img[x, y] = loadimage("wall.bmp")
end if
next
next
end sub

sub drawMap()
for y = 0 to BOARD_HEIGHT
for x = 0 to BOARD_WIDTH
pasteIcon(TILE_W * x, TILE_H * y, img[x, y])
next
next
end sub

sub updatePlayer()
old_pos_x = player_x 'if wall get back to OG position
old_pos_y = player_y 'same as above
if(key(k_right)) then
if(player_x+PLAYER_SPEED < WIDTH) then ' same all same sh...
player_x=player_x+PLAYER_SPEED
end if
elseif(key(k_left)) then
if(player_x-PLAYER_SPEED >= 0) then ' well you guess it
player_x = player_x-PLAYER_SPEED
end if
end if
if(key(k_down)) then
if(player_y+PLAYER_SPEED < HEIGHT) then ' is less then height then go else stay
player_y = player_y+PLAYER_SPEED
end if
elseif(key(k_up)) then
if(player_y - PLAYER_SPEED >= 0) then ' it's bigger then 0 the go else stay
player_y = player_y-PLAYER_SPEED
end if
end if
player_index_x=int(player_x / TILE_W) ' check the position inside the map (0 / 1)
player_index_y=int(player_y / TILE_H) ' same as above
'here is the part that checks if you are in the path or wall, this checks the colision by index of array, simple.
if(m[player_index_x+player_index_y*BOARD_WIDTH]=WALL) then ' it's a wall? then stay in the old position
player_x = (old_pos_x)
player_y = (old_pos_y)
end if
end sub

sub setSpritePosition(x, y)
bob(player, x, y, player) ' display the bob or sprite or image, no need for collision detection with this tecnique
end sub

loadMap("map.dat") ' load the map
fillMap() ' fill it

while(not key(27))
cls()
drawMap() 'draw it
updatePlayer() ' update the player position
setSpritePosition(player_x, player_y) ' draw the player
waitvbl()
wend

player_map

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s