The enemy sprite moves along the screen with my player as the map scrolls. How do i get the enemy to stay at the assigned coordinates as the map scrolls? I have tried setting sprite priority but that does not change anything. Here is the code i have so far. Any comments on the code structure and layout would be great aswell. I have had DBP for a long time but never really got into it until now.
`set screen resolution
set display mode 1024, 768, 32
backdrop on
sync on
sync rate 60
`********************************************************************************
gosub loadmedia
gosub declarevariables
gosub loadmapdata
`********************************************************************************
do
drawMap(MapOffsetX, MapOffsetY)
gosub player
gosub enemies
gosub controls
gosub handleweapon
gosub scrollmap
gosub playerhealth
gosub display
sync
loop
`********************************************************************************
loadmedia:
`player
create animated sprite 101, "media\player\skeleton_walk_001.png",24,3,101
`enemies
create animated sprite 103, "media\player\skeleton_walk_001.png",24,3,103
`sounds
load sound "media\sounds\arrow_001.wav",1
`map tiles
load image "media\level\sand_001.bmp",1,1
load image "media\level\slate_001.bmp",10,1
load image "media\level\water_001.bmp",11,1
`weapons
create animated sprite 200, "media\player\axe_001.png",8,1,200
create animated sprite 300, "media\player\arrow_001.png",8,1,300
`display
load image "media\hud\healthbar_001.bmp",102,102
return
`************************************************************************************
declarevariables:
#constant screensizex 1024
#constant screensizey 768
Global MapWidth = 40
Global MapHeight = 40
Global MapTileSize = 64
Global MapOffsetX = 0
Global MapOffsetY = 0
Global MapViewportWidth = 0 : MapViewportWidth = 1024 / MapTileSize
Global MapViewportHeight = 0 : MapViewportHeight = 768 / MapTileSize
`maximum amounts the map can scroll in world coordinates
maxScrollX = MapWidth*MapTileSize - 1024
maxScrollY = MapHeight*MapTileSize - 768
`player's on-screen boundaries
scrollBoundX1 = 70 : scrollBoundX2 = 950
scrollBoundY1 = 70 : scrollBoundY2 = 690
`store the coordinates that will make the player be at the centre of the screen
px# = screensizex / 2
py# = screensizey / 2
`maximum bullets
maxnumofbullets = 299
Dim Bullets(maxnumofbullets,200)
BulletWait = 400 `Number of milliseconds between each bullet
max_health = 100
current_health = max_health
return
`************************************************************************************
loadmapdata:
`build map
dim map(MapWidth,MapHeight)
for x = 1 to MapWidth
for y = 1 to MapHeight
read map(x,y)
next y
next x
return
`************************************************************************************
player:
sprite 101, px#, py#, 101
offset sprite 101,sprite width(101) /2,sprite height(101) /2
set sprite priority 101,1
return
`************************************************************************************
enemies:
sprite 103, 300, 300, 103
offset sprite 103,sprite width(101) /2,sprite height(101) /2
play sprite 103, 25, 48, 100
return
`************************************************************************************
controls:
`Idle animation
if keystate(17) = 0
`If sprite's current frame is out of range, set it to first frame within range
if sprite frame(101) < 25
set sprite frame 101, 25
endif
`play idle animation
play sprite 101, 25, 48, 100
endif
`rotates sprite left (A)
if keystate(30) then angle#=wrapvalue(angle#-4)
`rotates sprite right (D)
if keystate(32) then angle#=wrapvalue(angle#+4)
`Moves sprite forward (W)
if keystate(17)
`If sprite's current frame is out of range, set it to first frame within range
if sprite frame(101) > 24
set sprite frame 101, 1
endif
`play walk animation
play sprite 101, 1, 24, 40
py# = py# - sin(angle#)*3
px# = px# - cos(angle#)*3
endif
rotate sprite 101, (angle#)-90
return
`************************************************************************************
handleweapon:
for b = 200 to MaxNumOfBullets
`if it doesn't exist then create the animated sprite
if sprite exist(b) = 0
create animated sprite b, "media\player\axe_001.png",8,1,200
endif
`fire sprite
if spacekey()=1 and Bullets(b,101)=0 and (timer()-LastCreated) > BulletWait
sprite b, sprite x(101),sprite y(101),200
scale sprite b,30
play sprite b,1,8,50
offset sprite b, sprite width(101) /2, sprite height(101) /2 +40
rotate sprite b, sprite angle(101)
Bullets(b,101)=1
Bullets(b,200)=0
show sprite b
move sprite b,6
LastCreated = timer()
endif
`if fired while moving
if Bullets(b,101)=1
play sprite b,1,8,50
move sprite b,6
l = bullets(b,200)
inc 1
Bullets(b,200)=l
endif
next b
return
`************************************************************************************
scrollmap:
`keep player within its boundaries
if px# > scrollBoundX2-64 then px# = scrollBoundX2-64 : inc MapOffsetX,3
if px# < scrollBoundX1+64 then px# = scrollBoundX1+64 : dec MapOffsetX,3
if py# > scrollBoundY2-64 then py# = scrollBoundY2-64 : inc MapOffsetY,3
if py# < scrollBoundY1+64 then py# = scrollBoundY1+64 : dec MapOffsetY,3
`scroll map according to player
if MapOffsetX < 0 then MapOffsetX = 0
if MapOffsetY < 0 then MapOffsetY = 0
if MapOffsetX > maxScrollX then MapOffsetX = maxScrollX
if mapOffsetY > maxScrollY then MapOffsetY = maxScrollY
return
`************************************************************************************
playerhealth:
if downkey() = 1 then dec current_health, 1
if current_health < 0 then current_health = 0
if upkey() = 1 then inc current_health, 1
if current_health > 100 then current_health = max_health
if keystate(12) = 1
hide sprite 102
endif
if keystate(13) = 1
show sprite 102
endif
ch# = current_health
mh# = max_health
scale_x# = (ch# / mh#) * 100
sprite 102, px#-32, py#+32, 102
stretch sprite 102, scale_x#, 100
return
`************************************************************************************
display:
`show player's boundaries. Pushing beyond the boundaries will scroll the map
`ink rgb(255,0,0),0
`line scrollBoundX1,scrollBoundY1,scrollBoundX2,scrollBoundY1
`line scrollBoundX1,scrollBoundY2,scrollBoundX2,scrollBoundY2
`line scrollBoundX1,scrollBoundY1,scrollBoundX1,scrollBoundY2
`line scrollBoundX2,scrollBoundY1,scrollBoundX2,scrollBoundY2
ink rgb(255,255,255),0
text 0,10,"FPS: "+STR$(Screen FPS())
text 0,30,"W A S D to move"
text 0,50,"space key to shoot"
text 0,70,"turn on & off health with + & - key"
text 0,90,"increase & decrease health with up & down key"
set cursor 0,110
print "Player World X: ",MapOffsetX+px#
print "Player World Y: ",MapOffsetY+py#
return
`************************************************************************************
function drawMap(offsetX as float, offsetY as float)
sx = int(offsetX / MapTileSize)
sy = int(offsetY / MapTileSize)
ox = offsetX mod MapTileSize
oy = offsetY mod MapTileSize
ex = sx + MapViewportWidth+1
ey = sy + MapViewportHeight+1
if ex >= mapWidth then ex = mapWidth-1
if ey >= mapHeight then ey = mapHeight-1
for y = sy to ey
for x = sx to ex
ix = (x - sx)*MapTileSize - ox
iy = (y - sy)*MapTileSize - oy
paste image map(x+1,y+1), ix, iy
next x
next y
endfunction
`************************************************************************************
`************************************************************************************
`store tile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1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,10
data 10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,10
data 10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,10
data 10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,10
data 10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,10
data 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10
`********************************************************************************
remstart
sprites
101 = player
103 = enemy
200-299 = axe
300-399 = arrow
images
1 = sand
10 = slate
11 = water
102 = health bar
sounds
1 = arrow
remend
`********************************************************************************