3. Moving More Smoothly with "Repeat Until"
In this video, you will program the player 1 sprite to keep moving while a key is pressed.
So, the player 1 sprite moves when you press and hold down a key, but the movement isn’t smooth. Also, if you’re holding down a key, like the up arrow, and you press another key, like the right arrow, the sprite stops moving forward. This racing game would be a lot more fun if you could move forward and turn at the same time, so you’ll program that in this step. To make that happen, use a “repeat until” block from control. You want each movement to repeat, or keep happening, until the user stops pressing the key. To do this, attach the “repeat until” block to the up arrow event, and place the “move forward” block inside it.
Next, tell the sprite when to stop moving forward. The sensing category contains many different blocks that let the computer detect if something is happening. For example, the “key space pressed” block can let the computer know if a key is being pressed. You can change the value of this block to up arrow and add it to the “repeat until” block.
Now this reads: “When the up arrow key is pressed, repeat moving forward until the up arrow key is pressed.” Make the sprite keep moving forward until the up arrow key is not pressed. With computers, it’s important to give exact instructions, because the computer will do exactly what you tell it to do.
To fix this, choose the “not” block from operators, and place it in the block stack.
Now it reads: “When the up arrow key is pressed, repeat moving forward until the up arrow key is not being pressed.” Much better! Run your code to see how this solution works.
The player 1 sprite should now move smoothly, because it keeps moving as long as the up key is pressed.
Computer scientists like you often need to test several solutions to a tough problem before they find the one that works. If your code isn't working correctly, keep trying different fixes until you solve the problem.
Finally, program these repeat blocks for the other three events. You can do this pretty easily by duplicating, or copying, your code. To do that, right click on the “repeat until” block just created, and select duplicate. Drag the duplicated code over to your right arrow event... place the right turn movement inside... and change the “key press” value to “right arrow.” Try this event by pressing the right arrow.
Great! Your sprite should turn smoothly now. Now that each movement is coded, you can tinker with the values of the movement blocks to make sure that the sprite moves at the speed you want. Try out a bunch of different numbers until you get a gameplay experience that you like. For example, control how fast the sprite moves forward by testing a few values in the “move 10 steps block.” This example uses 4. That’s a little too slow. Try 8.
Great! That’s much easier to control. Once you’ve programmed the “repeat until” blocks in this step, switch to the player 2 sprite and program keyboard events to make it move. Because you’ve already used the arrow keys, you will need to program player 2 using different keys. This example uses the W,A,S, and D keys for player 2.
Stuck? Ask the person next to you for help. If you can’t figure it out together, ask your Guru.
Share your project and write a description of it before you leave today, so Scratch users everywhere can experience the awesome program you made.
This is your chance to share your project with others and tell them how it works. Write clear instructions so any Scratch user can play with your project and make it work.
Now, it’s your turn! Make the sprite move smoothly with “repeat until” blocks. Then, program the player 2 sprite to move when different keys are pressed.
Once you have programmed two players, find a neighbor and test your racing game!
Then, move on to the add-ons to customize your game.