Goal: Program a button to create new scenes every time you click it.
Vocabulary: random, se (sentence)
Your colors and your turtle should have instructions as in Obstacle Course 6.
In the make_a_scene
procedure, we will revise the code for the setpos
commands that position the rocks, so that they will be placed randomly on the page.
- We need to use the word random along with a number for the X coordinate and the Y coordinate. Random is a special word which is used after a command and before an input number. It means, "Choose any number that is 0 or higher and less than the input number."
- The setpos command needs a list of two numbers, we will use the se command to prepare that list on the fly in the procedure. Se which stands for sentence, receives some values and reports the list of them.
- Setpos se (random 215) (random 50) would mean: "Choose an X coordinate that is 0 or higher, but less than 215, and choose a Y coordinate that is 0 or higher, but less than 50." However, this command will only place rocks in the upper right area of the page. If we want to place the rocks all over the page, we need to use negative numbers, too.
- Setpos se (-240 + random 215) (5 + random 50) means:
"Start with an X coordinate of -240 and add a random number that is 0 or higher but less than 215. Start with a Y coordinate of -5 and add a random number that is 0 or higher but less than 50."
- You may choose different numbers depending on your project size and the outer limits of where you might like the rocks to be placed.
- When we use the random command, we never know what number the computer will choose. Sometimes the rocks might be spaced very close together or very far apart. If you don't like your new scene, just click the make_a_scene button again and again until you do. It is fun to be surprised.
Try writing different starting numbers before random.
Try writing different maximum numbers after random.
Can you give each rock a different range of numbers after setpos so that you can be sure the rocks will never touch each other?
Can you place more rocks randomly in the scene?
On Your Own - Project Ideas
Add a different type of obstacle. Use different ranges of numbers after setpos so that the new obstacles are not mixed in with the rocks.