Discuss Scratch

DIUUAcod1ng
Scratcher
7 posts

Randomly choosing between 4 set coordinates

I've been busy working on a school project and I was wanting to have my sprite to go to a random selection of 1 of 4 coordinates, can someone please help me?
DIUUAcod1ng
Scratcher
7 posts

Randomly choosing between 4 set coordinates

nsuarezl26
Scratcher
17 posts

Randomly choosing between 4 set coordinates

You can have a list that has all of the coordinates (one list has to be the x coordinates and the other the y coordinates) and then make this script.



when I receive [random]
set x to (item (pick random (1) to (4)) of [x cords] :: list)
set y to (item (pick random (1) to (4)) of [y cords] :: list)

deck26
Scratcher
1000+ posts

Randomly choosing between 4 set coordinates

nsuarezl26 wrote:

You can have a list that has all of the coordinates (one list has to be the x coordinates and the other the y coordinates) and then make this script.



when I receive [random]
set x to (item (pick random (1) to (4)) of [x cords] :: list)
set y to (item (pick random (1) to (4)) of [y cords] :: list)

But if you want the corresponding x and y values set a variable to the random number and use that for both lists. The script above will give you an x value randomly selected from the list and a similar, but unrelated, y value.
nsuarezl26
Scratcher
17 posts

Randomly choosing between 4 set coordinates

deck26 wrote:

nsuarezl26 wrote:

You can have a list that has all of the coordinates (one list has to be the x coordinates and the other the y coordinates) and then make this script.



when I receive [random]
set x to (item (pick random (1) to (4)) of [x cords] :: list)
set y to (item (pick random (1) to (4)) of [y cords] :: list)

But if you want the corresponding x and y values set a variable to the random number and use that for both lists. The script above will give you an x value randomly selected from the list and a similar, but unrelated, y value.

Yeah i forgot about that
when I receive [random]
set [random] to (pick random (1) to (4))
set x to (item (random) of [x cords] :: list)
set y to (item (random) of [y cords] :: list)



I fixed it

Last edited by nsuarezl26 (Sept. 19, 2022 15:21:23)

DIUUAcod1ng
Scratcher
7 posts

Randomly choosing between 4 set coordinates

Thank you. I will see if this works
nsuarezl26
Scratcher
17 posts

Randomly choosing between 4 set coordinates

DIUUAcod1ng wrote:

Thank you. I will see if this works

Just make sure to use the fixed version.
DIUUAcod1ng
Scratcher
7 posts

Randomly choosing between 4 set coordinates

To add the coords to the list do I just use the the add to list block?
DeveloperTools
Scratcher
100+ posts

Randomly choosing between 4 set coordinates

DIUUAcod1ng wrote:

To add the coords to the list do I just use the the add to list block?
Yep! you can also show the list and then click the “+” button to manually add one.
DIUUAcod1ng
Scratcher
7 posts

Randomly choosing between 4 set coordinates

Thank you all for the help! I decided to use that original design but change the 1-4 to a 1-2 because I had two coords in the x list and y list each. This also fixed the problem of it occasionally 0 0 which I didn't want. Will credit you all when I'm done.
RT_Borg
Scratcher
1000+ posts

Randomly choosing between 4 set coordinates

Hi DIUUAcod1ng,

If you only have a small number of coordinate choices, lists are more than you need. You can just do (for the 4 point version):

... // something happened, time to move to a random coordinate
set [random v] to (pick random (1) to (4))
if <(random) = (1)> then
go to x: (-300) y: (-100) // or wherever
end
if <(random) = (2)> then
go to x: (-300) y: (100) // or wherever
end
if <(random) = (3)> then
go to x: (300) y: (-100) // or wherever
end
if <(random) = (4)> then
go to x: (300) y: (100) // or wherever
end

I hope this helps. Happy to answer any questions.

– RT_Borg
DIUUAcod1ng
Scratcher
7 posts

Randomly choosing between 4 set coordinates

I was too lazy to change the code but I decided to add sharks which if killed would get points deducted so I'll use this basic layout RT_Borg

Powered by DjangoBB