Discuss Scratch

MouseclickHD
Scratcher
6 posts

Random Direction Script?

Hi, I'm making a maze solver, and I need a script which basically goes “If this sprite is touching either the edge or the maze, make it turn either left, right, up, or down (chosen randomly)”.

I've tried so that when it is touching “edge” or “maze” it changes a variable called “direction” to an integer from 1 to 4 (so either 1,2,3 or 4).
If it's 1 it points up, if 2 it points down, if 3 left, if 4 right. This is inside an “if/else” script, in the “if” field, where the “else” field is “move 10 steps”.

But it doesn't work.

If anybody has a “random left/right/up/down direction” script they are willing to share, please do!

Thanks!
-Scratcher-
Scratcher
100+ posts

Random Direction Script?

MouseclickHD wrote:

This is inside an “if/else” script, in the “if” field, where the “else” field is “move 10 steps”.
Maybe you could post the scripts here, so it's easier to understnad. Do you mean like
point in direction ((90) * (pick random (-1) to (2)))
move (10) steps
?
MouseclickHD
Scratcher
6 posts

Random Direction Script?

when green flag clicked
forever

if <touching "edge" or "maze"> then
set "direction" to random (1) to (4)
if "direction" = 1
point up
and so on and so on
else
move 10 steps

end

end

I would try to show it easier, but Scratchblocks won't work hmph
-Scratcher-
Scratcher
100+ posts

Random Direction Script?

MouseclickHD wrote:

when green flag clicked
forever
if <touching "edge" or "maze"> then
set "direction" to random (1) to (4)
if "direction" = 1
point up
and so on and so on
else
move 10 steps
end
end
Oh okay. You could replace the whole part inside “if” with the point in direction block, that makes it a lot easier. I see one problem with the sensing, because if the sprite touches an object and then changes it's direction, it still touches the object, so it would probably just rotate all the time. Is your maze grid based?
First, you could try
if <<touching [maze v] ?> or <touching [edge v] ?>> then
move (-10) steps
point in direction ((90) * (pick random (-1) to (2)))
else
move (10) steps
end
to make sure it goes back to path when touching the walls. To make it a little bit faster you can also use
turn ccw ((90) * (pick random (1) to (3))) degrees
Because the other block also contains the direction it already had before, but it should rather try the other three directions.

Last edited by -Scratcher- (May 23, 2015 14:50:46)

G0577
Scratcher
55 posts

Random Direction Script?

this thread is handy, thank you for asking and getting answered!
say [Thanks!]

(([sqrt v] of ([cos v] of (item (process) of [distances v] :: list))) * (50))
moss-shadow
Scratcher
500+ posts

Random Direction Script?

This is the most simple way to do it in all directions:
point in direction (pick random (-180) to (180)

Last edited by moss-shadow (Jan. 1, 2016 01:42:10)


Moss | 21 | He/They | Non-Binary | Abroromantic Ace | West Woods Founder | Over Borders in Development

ー ♠ • ♥ ー ー You ー ー Are ー ー Valid ー ー ♣ • ♦ ー

Powered by DjangoBB