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
pointindirection90*pickrandom-1to2move10steps
?
MouseclickHD
Scratcher
6 posts

Random Direction Script?

whenclickedforeveriftouching"edge"or"maze"thenset"direction"torandom1to4if"direction"=1pointupandsoonandsoonelsemove10steps

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

Random Direction Script?

MouseclickHD wrote:

whenclickedforeveriftouching"edge"or"maze"thenset"direction"torandom1to4if"direction"=1pointupandsoonandsoonelsemove10steps
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
iftouchingmaze?ortouchingedge?thenmove-10stepspointindirection90*pickrandom-1to2elsemove10steps
to make sure it goes back to path when touching the walls. To make it a little bit faster you can also use
turn90*pickrandom1to3degrees
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!
sayThanks!
moss-shadow
Scratcher
500+ posts

Random Direction Script?

This is the most simple way to do it in all directions:
pointindirectionpickrandom-180to180

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

Powered by DjangoBB