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. Die Figur nach links und rechts bewegen

Transcript

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 Zurück
Weiter arrow_forward
Anweisungen
  1. Wählt die Figur für Spieler 1 aus und erstellt Ereignisse für die Pfeile nach rechts und links.
  2. Fügt zwei Blöcke „drehe dich“ hinzu, um die Figur nach links und rechts abbiegen zu lassen.
  3. Lasst die Figur von Spieler 1 gleichmäßig abbiegen, indem ihr die Blöcke „wiederhole bis“, „nicht“ und „Taste gedrückt“ zu jedem Stapel „drehe dich“ hinzufügt.