In this video, you will use a repeat loop to fill the blank stage with different colored objects.
Watch a video demonstration first then you get to try it on your own.
Your starter project already has a sprite with several colored objects that will be used to fill the stage.
Click on sprite to add the code blocks that will give the sprite instructions.
This sprite will move around stamping copies of itself onto the stage until the stage is full.
To make that happen click the motion menu and choose a go to block.
This will move your sprite to a location on the stage when you click the block.
The sprite needs to move all over the screen until the stage is full, and you don't want to waste time to make that happen.
To speed up the process click on the operators menu and drag the pick random block into the go to x-value.
If you click the block now you will see that the sprite moves, just a little every time you click it.
The value in x indicates the position that the sprite should move to the left or right on the stage.
The width of the stage stretches from negative 240 to positive 240, so you add those values to the pick random block.
Now, the computer can pick a random number anywhere from the far left to the far right on the stage.
You'll also do the same thing for the y-value so that the sprite can move up and down on the stage.
The height of the stage stretches from negative 180 to positive 180.
When the block is clicked multiple times, the sprite moves all over the stage.
Next, you need to make it copy itself.
To do that, go to the pen menu, and add the stamp block to your stack.
It's always a good idea to test your code every step of the way to make sure it is doing what you like.
Go ahead and click on this stack to make sure the sprite is moving and stamping.
Of course, you don't want to click the stack yourself to fill the screen, so under the control menu, grab the repeat 10 block and put it around your block stack.
If you click the stack now, you see that the block stack runs 10 times, moving and stamping itself across the screen.
The person using your project should be able to start it easily, so add the when flag clicked block to the top of your stack.
This will start the code as soon as someone clicks the flag.
You'll also want the project to start with a blank stage.
To do this, click on the pen menu and use the clear block.
Place the clear block right under the when flag clicked block so that the screen is cleared as soon as the project is started.
Each time the flag is clicked, the screen clears and a new set of colorful objects is stamped.
But, you might want to add more objects to fill up the space on the screen, so tinker with the value in the repeat block until you find one you like.
Changing the repeat number will change the number of times the blocks and side to repeat loop are run and the number of times your sprite is stamped.
The best way to catch bugs in your program and fix them is to test your code often.
If the first solution you try doesn't work, try again.
It's one of the ways computer scientists like you create projects that they are proud of.
Now it's your turn.
Click on sprite with all of the objects before adding your code.
Make the sprite move using the go to and pick random blocks.
Then, make the sprite stamp itself and use a repeat loop to run the code many times.
Use the when flag clicked block and clear block to start your project.
- Use the "go to" block to make the sprite go to a specific spot.
- Make the sprite go to a random spot.
- Make the sprite leave a stamp of itself.
- Make the sprite repeat going to a random spot and stamping itself.
- Start with a clear screen when the green flag is clicked.