r/pico8•
s_p_oo_k_ee

Cannot work out spawning at random times!

:p8:I Need Help:p8:

https://reddit.com/link/1rlfel1/video/v2q5ws8vq7ng1/player

Hello! I'm currently trying to make my first game from scratch (no following tutorials, just using the bits I've learned and experimenting). I've been scratching my head the last few evenings trying to work out why my 'snacks' (the green things) are spawning in this strange sluggish way. I think (think!) I'm using a spawn_timer variable correctly and then have used tables to add in instances of 'snacks' but something about it isn't working?

For reference, Im just trying to make them spawn in from the right side of the screen and travel in a straight line toward the left, hopefully without overlap and when they exit the screen they can be deleted. Any help would be much appreciated! I've been really enjoying trying to problem solve through reading Nerdy Teachers and the documentation, but haven't been able to crack it. Pasted my code below;

function _init()
print("press start",40,62,7)
game_start=false
make_head()
make_tummy()
snacks={}
spawn_timer=30+rnd(90)
end

function spawn_snacks()
local sn={
active=true,
x=128,
y=18,
spr=3,
speed=5
}
--add snacks to the table
add(snacks,sn)
end

function _update()
move_head()
tummy_shrink()
spawn_timer-=1
if (spawn_timer<=0) then
spawn_snacks()
foreach(snacks,update_snacks)
--set next spawn interval
spawn_timer=30+rnd(90)
end
end

function _draw()
cls()
make_target()
draw_neck()
draw_head()
draw_tummy()
foreach(snacks,draw_snacks)
end

--update exisiting snacks
function update_snacks()
for s in all(snacks) do
s.x-=s.speed
if s.x<0 then del(snacks,sn)
end
end
end

function draw_snacks()
for s in all(snacks) do
spr(s.spr,s.x,s.y)
end
end

Posted Mar 5, 2026, 11:39 AM UTC

65
Loading comments…