Skip to content

Please update your browser

Your browser version is not supported by this site. CS First may not work properly. Learn more

3. Girare lo sprite a sinistra e destra


Your sprite is on the move, but as of now it only moves in a straight line.

That won’t get it past the enemies! Now that you’ve programmed the sprite to “launch” when the spacebar is pressed, you need to program it to turn when the arrow keys are pressed.

Watch this screencast to learn how to create that programming, then try it on your own.

Drag out 2 “when key pressed” events, and change the values to left arrow and right arrow.

Then, add the appropriate turn blocks to each arrow.

Test your code to make sure the the sprite is turning properly when you press the arrow keys.

You’ll notice that there’s a problem here that you’ve run into before. The sprites turn when the arrow key is held down, but they don’t turn smoothly. To fix this, you’ll need to program this to say, “When the arrow key is pressed, repeat turning until the arrow key is no longer being pressed.”

This will ensure that the sprite turns smoothly when the key is pressed and held down. Go to the control menu and select a “repeat until” block to put around one of the turns.

Because you’ll have to program this twice, it’s a good idea to try it on one turn, see if it works, then copy the code for the other turn.

Go to the operators menu and select a “not” block.

Then, go to the sensing menu, select a “key pressed” block, and change the value to the correct arrow key.

Run the code to see how it works.

Remember, as a computer scientist it’s important to test your code at every step, to make sure that it’s doing what you want it to do.

It’s much easier to find and correct problems when you test code early and often before multiple problems pile up.

In the next screencast, you’ll program this game to level up with spawning enemies!

arrow_backward Indietro
Avanti arrow_forward
  1. Seleziona lo sprite Giocatore 1 e crea delle situazioni per i tasti freccia sinistra e freccia destra.
  2. Aggiungi due blocchi "Ruota" per far ruotare a sinistra e a destra lo sprite.
  3. Fai sì che lo sprite Giocatore 1 ruoti agevolmente aggiungendo i blocchi "Ripeti fino a quando", "Non " e "Tasto premuto".