Discuss Scratch

JmungusHumungous
New Scratcher
2 posts

randomised movement

I'm currently making an like randomized movement for a game i'm making but when I try to make it move the full amount of steps (which is about 7), it goes ahead and changes what direction it moves. How do I make it so that it does the full amount of steps before it makes another?



The code I did if I didn't explain it well enough.
when green flag clicked
forever
set [randomized movement v] to (pick random (1) to (2))
if <[randomized movement] = [1]> then
point in direction (90 v)
move (pick random (1) to (7)) steps
if <[randomized movement] = [2]> then
point in direction (-90 v)
move (pick random (1) to (7)) steps
end
end
end
102acrimea
Scratcher
100+ posts

randomised movement

Your current code (assuming the second if statement is outside the first one) currently either picks left or right (90 or -90) and moves 1 to 7 steps in that direction before re randomizing. Instead, have the move X steps inside a repeat block like this:

when green flag clicked
forever
if <(pick random (1) to (2)) = [2]> then
point in direction (90 v)
repeat (pick random (1) to (7))
move (1) steps
end
else
point in direction (-90 v)
repeat (pick random (1) to (7))
move (1) steps
end
end
wait (0.5) secs
end

Wait block is so you can observe its movement frequency

Powered by DjangoBB