Discuss Scratch

FORBIDENxKROSS
New Scratcher
16 posts

How to randomly generate sprites.

This comes in handy in many types of projects. For example, my Flappy Bird Remake it randomly deploys obstacles so no matter how many times you play the obstacle course is never the same.

Doing this is very easy you make the program pick a random number and when it lands on a number you use an If statement to make it do something.

Example Code:

when green flag clicked
forever

set [Random v] to [(pick random (1) to (2)) ]
if <(Random) = [1 ]> then
move (10) steps

end
if <(Random) = [2 ]> then
move (-10) steps

end

wait (1) secs
end
xsnail
Scratcher
90 posts

How to randomly generate sprites.

Here is another way, as long as it doesn't land on 0
when green flag clicked
forever
set [Random v] to [(pick random (-1) to (1)) ]
move ((random) * (10)) steps
wait (1) secs
end

Last edited by xsnail (Oct. 3, 2014 21:24:55)

FORBIDENxKROSS
New Scratcher
16 posts

How to randomly generate sprites.

xsnail wrote:

Here is another way, as long as it doesn't land on 0
when green flag clicked
forever
set [Random v] to [(pick random (-1) to (1)) ]
move ((random) * (10)) steps
wait (1) secs
end


My way you don't have to worry about it landing on zero whatever number the computer picks gets assigned to a task.

Last edited by FORBIDENxKROSS (Oct. 3, 2014 22:44:51)

jdudedragon
Scratcher
35 posts

How to randomly generate sprites.

Or,

when green flag clicked
set [random v] to [(pick random (-1) to (1))]
move ((random) * (10)) steps
wait (1) secs
when green flag clicked
forever

if <(random) = [0]> then
set [random v] to (pick random (-1) to (1))

end

end
RPFluffy
Scratcher
1000+ posts

How to randomly generate sprites.

jdudedragon wrote:

Or,

when green flag clicked
set [random v] to [(pick random (-1) to (1))]
move ((random) * (10)) steps
wait (1) secs
when green flag clicked
forever

if <(random) = [0]> then
set [random v] to (pick random (-1) to (1))

end

end

Still it would be better like this:

when green flag clicked
forever

set [Random v] to [(pick random (1) to (2)) ]
if <(Random) = [1 ]> then
move (10) steps

end
if <(Random) = [2 ]> then
move (-10) steps

end

wait (1) secs
end
And so on.

Last edited by RPFluffy (Oct. 4, 2014 00:22:23)

Sushithing
Scratcher
1 post

How to randomly generate sprites.

ghande
-ShadowOfTheFuture-
Scratcher
1000+ posts

How to randomly generate sprites.

Sushi thing wrote:

ghande

Please don't necropost or spam. This topic is from 2014.

Curse you, 60-second rule…

EDIT: The bad word filter was blocking out the username for some reason…

Last edited by -ShadowOfTheFuture- (Nov. 22, 2017 18:27:13)

TheProgrammedPhoenix
Scratcher
1 post

How to randomly generate sprites.

I'm using this in 2018 so the randomizer doesn't do anything
Thefrenchpeople
Scratcher
500+ posts

How to randomly generate sprites.

TheProgrammedPhoenix wrote:

I'm using this in 2018 so the randomizer doesn't do anything
Please don't necropost, this topic is from 2017.
Explosive327
Scratcher
1 post

How to randomly generate sprites.

Is there another way to randomly generate sprites?
ShinigamiBlacky
Scratcher
100+ posts

How to randomly generate sprites.

Explosive327 wrote:

Is there another way to randomly generate sprites?
This is the third necropost in a row! WHAT?!?
But Im gonna help you anyway…
say (item (random v) of [What you want to be said v] :: list)

Last edited by ShinigamiBlacky (July 28, 2018 09:05:17)

SuparSaiyajinBlue
New Scratcher
1 post

How to randomly generate sprites.

I Want To Know How To Randomly Generate DIFFERENT Sprites. Like Flappy Bird How All The Walls He Goes Through Are Different And Randomly Generated.
xMystic416
Scratcher
100+ posts

How to randomly generate sprites.

SuparSaiyajinBlue wrote:

I Want To Know How To Randomly Generate DIFFERENT Sprites. Like Flappy Bird How All The Walls He Goes Through Are Different And Randomly Generated.
Welcome to the forums! Next time if you need help, please create your own topic instead of necroposting. Thanks!
surfingisfun123
Scratcher
16 posts

How to randomly generate sprites.

Welcome to the forums! Next time if you need help, please create your own topic instead of necroposting. Thanks!
TheAnomalousPseudo
Scratcher
1000+ posts

How to randomly generate sprites.

surfingisfun123 wrote:

Welcome to the forums! Next time if you need help, please create your own topic instead of necroposting. Thanks!
That's right.
CityPickle
New Scratcher
1 post

How to randomly generate sprites.

I came up with a way to show different sprites based on random numbers. It's pretty simple, but it is doing the trick.

1) I gave each sprite a number in their name, just so I could keep track of them: sprite_1, sprite_2, sprite_3, and so forth.

2) On the event that I wanted a random sprite to appear, I run a function to generate a random number. If I have 4 sprites, I generate a random number between 1 and 4. This number is saved in a variable called “randomNumber”. Then I set a broadcast message: “Random SpriteNumberGenerated”.

define generateRandomSprite
set [ randomNumber] to [(pick random (1) to (4)]
broadcast [ RandomSpriteNumberGenerated]

3) Each sprite receives the broadcast message. Underneath "when I receive “, I put an ”if / else“ condition. ”If randomNumber = – go to front layer, show – else – hide.

Presuming I am in the Code for “sprite_2”::
when I receive [ RandomSpriteNumberGenerated]
if (randomNumber) = 2 then
go to front
show
else
hide
end

And it works!

Kind of oversimplified and hacky feeling, but it works!

Powered by DjangoBB