In this video, we will program the player one sprite to keep moving while a key is pressed.
The player one 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 a different 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 the 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 press 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 a 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.
Run your code to see how this solution works.
The player one sprite should now move smoothly because it keeps moving as long as the up arrow key is pressed.
Finally program the repeat blocks for the other three events.
You can do those pretty easily by duplicating, or copying your code.
To do that right click on the repeat until block that you 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 a bunch of different numbers until you get a game play experience that you like.
For example control how fast the sprite moves forward by testing a few values in the move ten steps block.
This example uses four.
Well, that's a little too slow.
Great, that's much easier to control.
Once you've programmed the repeat until blocks in this step, switch to the player two sprite and program keyboard events to make it move.
Because you've already used the arrow keys, you will need to program player two using different keys.
This example uses the W, A, S, and D keys for player two.
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've programmed two players, find a neighbor and test your racing game.
Then move on to the add-ons to customize your game.
- Add a "repeat until" block to each event to make the sprite move smoothly around the track.