Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Random Selection of Pre-set Coordinates?
- lilyrebecca
-
23 posts
Random Selection of Pre-set Coordinates?
Hi
FYI I joined Scratch (different account) about 3 years ago, but I haven't used Scratch for about 2 years, so I'm pretty rusty - forgive me if I say something stupid!
I'm creating a game which is a treasure hunt, and the treasure chests need to be randomly assigned to a box each time the game is restarted. Is there a way for the sprite to move to a randomly selected co-ordinate out of the pre-set ones (that match up with the middle of a box)?
Secondly, how can I prevent my character from moving out of the 8x8 grid? I tried using a ‘while’ statement so that the game will go on as long as the character is touching the grid, but it didn't quite work.
Here is the link to My project

I'm creating a game which is a treasure hunt, and the treasure chests need to be randomly assigned to a box each time the game is restarted. Is there a way for the sprite to move to a randomly selected co-ordinate out of the pre-set ones (that match up with the middle of a box)?
Secondly, how can I prevent my character from moving out of the 8x8 grid? I tried using a ‘while’ statement so that the game will go on as long as the character is touching the grid, but it didn't quite work.
Here is the link to My project
- deck26
-
1000+ posts
Random Selection of Pre-set Coordinates?
For the first question the easiest option might be lists of x and y values and you can then get a random value (1 to length of lists) and get the corresponding x and y value. If you prefer you could use a single list x1, y1, x2, y2 and choose your random number from 1 to list-length/2 so you double the random value for the y item number and take one less than that for the x value.
Is your character moving one grid square each time or can a move be less than one grid square? Is the grid all on screen at the same time?
Is your character moving one grid square each time or can a move be less than one grid square? Is the grid all on screen at the same time?
- ErnieParke
-
1000+ posts
Random Selection of Pre-set Coordinates?
Yes, there is a way to make a box go a list of pre-set coordinates, with lists!
First off, coordinates are made of two things: x and y. As such, we ill use two lists (although you could definitely use 1). For me, I will have:
List X: 10, -150, 40
List Y: 160, 100, -40
Now, we want the sprite to go to a random coordinate. There are three spots, and we want one:
Now, we transform that number into actual coordinates:
Ta da! Done.
I will look at your project and see if there is a way to force the character in the 8x8 box.
Pointing out,
ErnieParke
First off, coordinates are made of two things: x and y. As such, we ill use two lists (although you could definitely use 1). For me, I will have:
List X: 10, -150, 40
List Y: 160, 100, -40
Now, we want the sprite to go to a random coordinate. There are three spots, and we want one:
Now, we transform that number into actual coordinates:
Ta da! Done.
I will look at your project and see if there is a way to force the character in the 8x8 box.
Pointing out,
ErnieParke
Last edited by ErnieParke (Aug. 3, 2016 18:06:57)
- deck26
-
1000+ posts
Random Selection of Pre-set Coordinates?
OK, looking at your project, the best plan is to keep track of the current row and column. Then if a move is input which would take you outside the permitted range you reduce it to the maximum allowed.
So if row is 4 you can only move up by 4 rows or fewer and if 6 is entered you'd adjust it. All easy enough to do if you have a row and column variable.
So if row is 4 you can only move up by 4 rows or fewer and if 6 is entered you'd adjust it. All easy enough to do if you have a row and column variable.
- ErnieParke
-
1000+ posts
Random Selection of Pre-set Coordinates?
I checked out your project, and the simplest way would be this:
Make sense? If the character goes too far, fix it!
@deck26's method is also nice, if you want to try it.
Thoughts,
ErnieParke
Make sense? If the character goes too far, fix it!
@deck26's method is also nice, if you want to try it.
Thoughts,
ErnieParke
Last edited by ErnieParke (Aug. 3, 2016 18:16:14)
- lilyrebecca
-
23 posts
Random Selection of Pre-set Coordinates?
I tried this and it didn't seem to work on my project. Could you have a look at it? Thanks Yes, there is a way to make a box go a list of pre-set coordinates, with lists!
First off, coordinates are made of two things: x and y. As such, we ill use two lists (although you could definitely use 1). For me, I will have:
List X: 10, -150, 40
List Y: 160, 100, -40
Now, we want the sprite to go to a random coordinate. There are three spots, and we want one:
Now, we transform that number into actual coordinates:
Ta da! Done.
I will look at your project and see if there is a way to force the character in the 8x8 box.
Pointing out,
ErnieParke

- footsocktoe
-
1000+ posts
Random Selection of Pre-set Coordinates?
I tried this and it didn't seem to work on my project. Could you have a look at it? Thanks Yes, there is a way to make a box go a list of pre-set coordinates, with lists!
First off, coordinates are made of two things: x and y. As such, we ill use two lists (although you could definitely use 1). For me, I will have:
List X: 10, -150, 40
List Y: 160, 100, -40
Now, we want the sprite to go to a random coordinate. There are three spots, and we want one:
Now, we transform that number into actual coordinates:
After you set x pos and y pos you need this block
- deck26
-
1000+ posts
Random Selection of Pre-set Coordinates?
If you add the data to the lists each time you need to delete all from the lists first - otherwise the lists will get longer each time you run the project.
You only need one block to set position to a random number.
You need to fix your costume centres for the treasure chests. See my project https://scratch.mit.edu/projects/39988498/
You only need one block to set position to a random number.
You need to fix your costume centres for the treasure chests. See my project https://scratch.mit.edu/projects/39988498/
- deck26
-
1000+ posts
Random Selection of Pre-set Coordinates?
They're using rthe position to place the first treasure chest. What they're doing is fine but the costume centres are off.I tried this and it didn't seem to work on my project. Could you have a look at it? Thanks Yes, there is a way to make a box go a list of pre-set coordinates, with lists!
First off, coordinates are made of two things: x and y. As such, we ill use two lists (although you could definitely use 1). For me, I will have:
List X: 10, -150, 40
List Y: 160, 100, -40
Now, we want the sprite to go to a random coordinate. There are three spots, and we want one:
Now, we transform that number into actual coordinates:
After you set x pos and y pos you need this block
- lilyrebecca
-
23 posts
Random Selection of Pre-set Coordinates?
I think I've fixed the treasure chest centering (but please tell me if you think otherwise), but now half the treasure chests go to random squares as they should, and the other half all clump together on the same square. I can't work out why, because their scripts are seemingly identical.They're using rthe position to place the first treasure chest. What they're doing is fine but the costume centres are off.I tried this and it didn't seem to work on my project. Could you have a look at it? Thanks Yes, there is a way to make a box go a list of pre-set coordinates, with lists!
First off, coordinates are made of two things: x and y. As such, we ill use two lists (although you could definitely use 1). For me, I will have:
List X: 10, -150, 40
List Y: 160, 100, -40
Now, we want the sprite to go to a random coordinate. There are three spots, and we want one:
Now, we transform that number into actual coordinates:
After you set x pos and y pos you need this block
- deck26
-
1000+ posts
Random Selection of Pre-set Coordinates?
If the treasure chests are all the same there is no reason to randomise them - in fact by doing that you risk them being in the same place as each other. Just set the first one to the first x,y position, the second to the second x,y position and so on. If you do that you don't even need the list (which still should be cleared at the start of the project) - just hard code the positions.
- lilyrebecca
-
23 posts
Random Selection of Pre-set Coordinates?
I'm doing this for school and it says the treasure chests need to be in random places that therefore are new everytime you start the game again. If the treasure chests are all the same there is no reason to randomise them - in fact by doing that you risk them being in the same place as each other. Just set the first one to the first x,y position, the second to the second x,y position and so on. If you do that you don't even need the list (which still should be cleared at the start of the project) - just hard code the positions.
- 18KENNYJtgsb
-
1 post
Random Selection of Pre-set Coordinates?
could I have some help on getting my sprite to a random position as it doesn't seem to be working
- deck26
-
1000+ posts
Random Selection of Pre-set Coordinates?
Please create your own new topic rather than necroposting. Sharing what you have is also a good idea. could I have some help on getting my sprite to a random position as it doesn't seem to be working
- Discussion Forums
- » Help with Scripts
-
» Random Selection of Pre-set Coordinates?