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. Déplacer l'athlète

Transcript

In this video, you’ll make the athlete sprite move when the user presses the arrow keys.

You’ll create two conditionals: one to move the sprite left, and one to move it right.

From the control menu, drag out two "if" blocks, and snap them together.

Go to the sensing menu, and drag a "key pressed" block into each conditional. For the first one, select "left arrow" in the dropdown. Select "right arrow" for the second one.

Next, from the "motion" menu, place a “point in direction” block into each of the conditionals.

The first conditional moves the sprite left, so select negative ninety in the dropdown.

In the second, select ninety. To test it, hold down either the left or right arrow key, and click the block stack. Try to get the sprite to turn around!

As you can see from the test, pressing the left arrow flips the sprite upside down. Fortunately, Scratch has a block that will make the sprite turn side to side, but not upside down. From the motion menu, drag out the “set rotation style” block, and place it at the top of this code stack. Select “left-right” from the dropdown menu. Test again. Make sure to hold the key while you click the code. The sprite stays right-side up. Nice job!

Next, make the sprite move in the direction it's pointing. Place a "move" block under each conditional. Make this code keep running as long as a key is being pressed. Snap a “forever” loop around the block stack.

Click this to test it. Press the left and right arrow keys, and watch the athlete move left and right. Pretty sweet. Finally, use a “when flag clicked” event to start the athlete's code. Snap it on top of the stack, then click the flag to start the game.

Here’s the game plan: Make the athlete sprite move left and right when the left and right arrow keys are pressed using “if” blocks and “key pressed” events. Then, add a “set rotation style” block to keep the sprite from flipping upside-down. Make the sprite move in the direction it’s facing with a “move” block. Use a “forever” loop to make the athlete keep moving as long as a key is pressed. Start the game with a “when flag clicked” event.

arrow_backward Retour
Suivant arrow_forward
Instructions
  1. En utilisant des blocs "Si" et des événements "Touche cliquée", fais se déplacer le lutin Athlète vers la gauche et vers la droite lorsqu’un utilisateur appuie sur les touches flèche gauche et flèche droite.
  2. Ensuite, ajoute un bloc "Fixer le sens de rotation" pour empêcher le lutin de basculer à l'envers.
  3. Fais avancer le lutin dans la direction à laquelle il fait face à l’aide d’un bloc "Avancer de".
  4. Utilise une boucle "Répéter indéfiniment" pour faire continuer l'athlète à avancer tant qu'une touche est cliquée.
  5. Démarre le jeu avec un événement "Quand drapeau cliqué".