Skip to content

Please update your browser

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

2. Lanciare uno sprite


In the previous screencast, you should have added one sprites to the starting project.

If you haven’t done this yet, please pause this screencast and do so now.

In this screencast, you’ll program the player sprite to “launch,” or repeatedly move forward, when the spacebar is pressed and program the arrow keys to control left and right turns. Go ahead and watch this screencast first to learn how to do it, then you’ll get the chance to try it on your own. First, you need to create a program that will cause the sprite to move when someone presses the spacebar.

You can do this by adding a “when space key pressed” event and a “move 10 steps” block.

Test that code to see how it works.

Good! But, you want the sprite to “launch” when the space key is pressed, meaning that it will keep moving forward without stopping.

Click on control.

You don’t need the sprite to repeat moving forward forever, just until it reaches the finish line. So select a “repeat until” block, and place it around the “move 10 steps” block. The sprite should repeat moving forward until it touches the finish line. Click the sensing menu, drag a “touching” block into the “repeat until” block, and change the value to “finish line.”

Check what happens when you test the code.

The sprite moves forward and stops once it reaches the finish line.

That’s fine. But how could the game be played again?

Sometimes problems become obvious when you test your code. As a computer scientist, it’s important to test your code often so these kinds of problems.

To make it so the player can continue playing the game, you can use a “go-to” block to move the sprite back to the start.

Drag the sprite to where you want it to start, then place the “go-to” block below the “repeat until” block.

Now, when you press the spacebar, the sprite moves forward until it reaches the finish line, then it goes back to the start.

In the next screencast, you’ll program the sprite to turn left and right with the arrow keys.

arrow_backward Indietro
Avanti arrow_forward
  1. Seleziona uno sprite Giocatore 1 e crea una situazione da far accadere quando viene premuta la barra spaziatrice.
  2. Aggiungi un blocco "Movimento" per far muovere in avanti lo sprite.
  3. Aggiungi un blocco "Ripeti fino a quando" alla colonna di blocchi.
  4. Aggiungi un blocco "Sta toccando il traguardo" come condizione.
  5. Imposta una posizione di partenza per il tuo sprite utilizzando un blocco "Vai a".