Log In  

hello, I'd like to know if anyone can help me. I wanted to make a bomb sprite appear 6 times on 6 random places on the screen (except a border that I made), so I made a function for that:

function randombomb()
 for i = 1,6 do
 spr(4,rnd(104)+8,rnd(104)+8)
 end
 end

I called this function in the _DRAW loop function, which was a mistake because the screen turned into a one big chaotic mess...

if it wasn't clear, I want the function to happen only 1 time and not infinitely. can anyone please help me?

P#65502 2019-07-01 18:54

You need something like a timer + some state variable(s) - eg. something that does something for some time, then stops.

Example:

-- half a sec @ 30fps
local bomb_timer=15
local bomb_count=5
-- pick a random initial position
local x,y=rnd(128),rnd(128)

function _update()
 timer-=1
 if timer<0 and bomb_count>0 then
  -- pick another position
  x,y=rnd(128),rnd(128)
  -- one less try
  bomb_count-=1
  -- reset timer
  timer=15
 end
end

function _draw()
 cls()
 spr(4,x,y)

 print("bomb #"..bomb_count.."\ntimer: "..timer,2,2,7)
end
P#65507 2019-07-01 19:20 ( Edited 2019-07-01 19:21)
1

Just don't go thinking a "sprite" is something special - once you've drawn the bombs this way, they almost might as well be random pixels on the screen.

If you don't plan on checking the color of screen pixels all the time, you might want to set up an array to keep track of the bombs to make it easier to check for collisions, add or remove bombs, etc. Sometimes it's just as easy to put the sprite information into the map tile data. (usually for less-random situations - see jelpy)

That said, there's nothing wrong with checking screen pixels. If you only have a single playfield that isn't scrolling and you don't need to redraw it for any special fx or other reasons, pixel checking is the simplest way.

P#65508 2019-07-01 19:31 ( Edited 2019-07-01 19:34)
1

I think an easier way of thinking of sprites in a succinct manner as tyrony explains, it's a copy-paste action with premade pictures you drew.

The reason they keep moving is because you're, for every frame, picking a new location for them along with not clearing the screen. So your code is literally 'copy and paste the bomb sprite 6 times every frame'.

To track bombs and draw them in the SAME place every frame, yes, you need some code that tracks the bomb locations. You can think of the sprite as a representational graphic of something that is described.

Imagine it a bit more like this (I hope this doesn't make things more confusing so I apologize in advance if it does!)

If you had a spot in your room that you said, 'This is perfect for a couch, a couch should go here', you have identified it's x and y coordinate, but there's no couch. BUT you wrote that information on a piece of paper. You hand the paper to your movers and say, 'put the couch here!' and they say 'okay!' and then put the couch there. The couch itself is the 'sprite' and in Pico8 world, you can't sit on it or anything, BUT you know where it goes and the movers put it there. To sit on it, since you know it's x/y location, you can say now, when I sit on this location, I will be on a couch. etc. etc.

This happens every frame that's drawn, which is by default 30 times per second.

Just remember if you change the location of where the couch goes, you need to erase the old location. Typically, we use CLS() which erases everything on screen to pitch black. That way if the coordinates of an object which has a sprite that needs to get painted there, then that's taken care of and there won't appear to be two of the same thing on screen.

I sincerely hope this makes sense and that you don't think that I am 'talking down' or anything. 'Sprites' when talking in general about playing videogames is different than how they are treated in programs. In playing videogames, we think they are 'solid entities' but in reality, they're programmed to be as such and most if not all hardware treats whatever's on screen as a collection of pixels and it's data that is not drawn at all that makes them behave the way they should.

It's all an illusion...go be a magician!

P#67931 2019-09-20 07:08 ( Edited 2019-09-20 07:10)

[Please log in to post a comment]

Follow Lexaloffle:        
Generated 2020-01-25 08:33 | 0.010s | 2097k | Q:20