Log In  


I'm brand new to coding in general and need help.
I'm trying to make it so that there is a 50/50 chance of getting a specific sprite to appear shortly after the game starts.
This is what I came up with, but it jus glitches and overlaps both sprites on top of eachother. Can someone let me know what I'm doing wrong?

if time()>8 then
end
if rnd(10)>5 then
cls()
map(0,0,0,0,16,12)
spr(017,px,py)
end
if rnd(10)<5 then
cls()
map(0,0,0,0,16,12)
spr(018,px,py)
end



1

Each time you call RND(), Pico8 generates a new random number.
I guess your code is called each frame.
This means every frame, your program generates two random numbers (because you called RND twice).
This happens very quickly, so the chance you display the sprite changes twice every 0,3 seconds. Your human eyes see both srpites top of each other.

You want to generate your random number once at the start of the program, then refer to its value.
Something like :
display_chance=rnd(10)

And then in each frame :
if display_chance>5 then
spr(017,px,py)
elseif display_chance<5 then
spr(018,px,py)
end

Edit : you should also consider the situation when the random number equals 5.00


1

There's a few things happening here. You're calling rnd(10) twice, so you will get two different results and one might be > 5 and the other might be < 5, so that's why you would get overlapping sprites. What you want to do is call rnd() once and store it in a variable, then check the variable. You also want to check for the very tiny chance that rnd(10) equals 5 exactly. Finally, since you want to cls() and map() every time, you can do those once and only use the if statement to check your random chance. So you end up with something like this:

if time()>8 then
end
local chance=rnd(10)
local rndspr=17
cls()
map(0,0,0,0,16,12)
if chance < 5 then
   rndspr=18
end

spr(rndspr,px,py)

Keep in mind that if you have this code in your _draw() or _update() functions, it will run every frame and so the chance variable will also change every frame. If you want to have the random value set once at the start of the game, remove the "local" keyword from the beginning of that line and put it into your _init() function instead.

There are ways to shorten this further, but I think this is more clear since you're newer to programming. Hope that helps!


Thank you both for the suggestions, here's what I ended up going with that worked for me

In my function init:

If time()>1 then
End
ChanceEgg=RND(10)
Rndspr=17
Cls()
Map(0,0,0,0,16,12)
If chanceegg>5 then
Rndspr=18
End

And in my function draw I have

If time()>8 then
Cls()
Map(0,0,0,0,16,12)
Spr(rndspr,px,py)
End
End

Also:
How do you do the code snippet like that?


1

Curious what the "if time()>1 then end" is meant to do. Since there's nothing inside the if-end block, nothing will happen whether or not time() > 1.

For code blocks on the BBS, you can use three back ticks on a line to start a code block and then three back ticks on a line to end it, then your code goes between those two back tick lines.


Good question, i don't remember why I initially put that there but the game runs the same without it currently so I cut it out.
The thing I'm trying to create is a digimon-esque v pet so I'm currently trying to figure out a few basic things like getting buttons on the screen for feeding/training/etc but eventually I want to make an aging system and it seems like the easiest thing is just to keep using time(), but I'd be curious to know if there's a simple way to make aging its own system separate from time, as well as adding variables that increase chances for certain evolutions.
But first things first, gotta get some buttons on the screen



[Please log in to post a comment]