I’ve started making another simple game for practice, and the next thing I need to do is make a flower bud (sprite 2) spawn randomly anywhere on the map except the wall tiles marked with Flag 0. I’ve looked at solutions online… but they’re given all at once. I need help to break them into tiny pieces so I understand what I’m doing instead of just copying it. Anyway, here’s my game so far.
Tab 0
function _init()
p={}
px=53
py=63
fx=rnd(120)
fy=rnd(120)
end
function _update()
move_plr()
collide()
end
function _draw()
cls(1)
map()
spr(1,px,py)
spr(2,fx,fy)
end
Tab 1
--move player--
function move_plr()
lx=px --saves player's x location
if btn(⬅️) then
px-=1
elseif btn(➡️) then
px+=1
end
if collide() then --sends player back to last x
px=lx
end
ly=py --saves player's y location
if btn(⬆️) then
py-=1
elseif btn(⬇️) then
py+=1
end
if collide() then
py=ly -- sends player back to last y
end
if px>=120 then
px=120
elseif px<=0 then
px=0
end
if py>=120 then
py=120
elseif py<=0 then
py=0
end
end
Tab 2:
--collision--
function collide()
local tl={x=px,y=py} --top left corner
local br={x=px+7,y=py+7} --bottom right corner
--get flag 0 for all corners of sprite
local c1=fget(mget(tl.x/8,tl.y/8),0)
local c2=fget(mget(tl.x/8,br.y/8),0)
local c3=fget(mget(br.x/8,tl.y/8),0)
local c4=fget(mget(br.x/8,br.y/8),0)
return c1 or c2 or c3 or c4
end