Skip to content

Please update your browser

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

arrow_back Trouver la porte secrète (défi)

Transcript

This add-on is slightly more challenging than some of the others. After program this add-on, the user will be able to control the movements of the main character. Also, the main character will only notice the secret door once it touches it.

First, program the character to move with the arrow keys.

Drag out a “move 10 steps” block, and click on it.

The character will probably move right.

Place a “when key pressed” event above the “move 10 steps” block, click the dropdown, and select “right arrow.” Test this. Press the right-arrow key. The character should move right. Great. Now, create similar code to make the character move left. Right click on the code to move right, and click “duplicate.” Change the dropdown to “when left arrow clicked.” Negative numbers in Scratch do the opposite of positive numbers. If “move 10 steps” moves a sprite right, “move negative 10 steps” moves it left. Add a negative sign in front of the 10 in “move 10 steps” to code for the left-arrow key. Test your code. Pressing the left arrow should move the sprite left, and pressing the right arrow should move the sprite right.

This is a basic way to make characters move. If you’d like more information about how to make it look like sprites are actually walking, watch the add-on “Walking.” Instead of putting the walking code inside an “if-else” statement, put it under the appropriate events.

Next, make sure the sprite starts in the same spot each time the story is told, regardless of where it gets moved during the story. To do this, move the character to where it will start. Then, place the “go to x y” block just under the “when green flag is clicked” block. Now the main character walks, but it still immediately mentions the secret door, rather than waiting until it is touching the door to notice it. To make your program wait for a condition, use the “wait until” block. First, fill out the condition.

The sprite can see the door when it is touching it.

The door is part of the background, however, it is the only part that is black. The character can tell it is touching the door if it is touching the color black.

Drag the “touching color” condition into the “wait until” block.

Next, make sure the color in the condition matches the color of the door. Click on the color, then click on the door. Now the block should read “wait until touching color black.”

Figure out where to place the “wait until” block.

Watch what happens when the “wait until” block is placed immediately after the “when green flag is clicked” block. The character doesn’t reset its position, and the scene doesn’t reset to the brick wall!

That happened because when the green flag is clicked, the program waits until the character is touching black before doing anything else. Instead, try moving the wait block after all the set-up, placing it before “should I go through the secret door?”

Test the code. The character begins in its starting position, and the background starts as the brick wall. Move the character. As soon as it touches the black door, it asks if it should go through it. Great. Next, tell the user how to make your program work using the right and left arrow keys. To do this, add a “say” block after the “go to” block. In this example, the character goes to its starting position, then says, "Use the arrow keys to make me move!”

Then, once it touches the black door, it asks the user if it should go through the door. Computer scientists rarely program what they want correctly the first time. They have to be persistent and keep trying until they solve the problem. When you keep working at a problem to figure out how to fix it rather than giving up, you are acting like a computer scientist! Now, it’s your turn: Use the “move” and “when key pressed” event blocks to make the character move when the arrow keys are pressed. Use the “wait until” block to make the character wait until it touches the door before asking if it should go through it.

Make the condition in the “wait until” block “touching color black.”

Tell the user to press the arrow keys to move your sprite by adding a “say” block after the “go to” block.

Choisir une extension
Marcher
Dans cette vidéo, tu vas découvrir comment créer l'effet que le personnage marche en direction de la porte ou s'en éloigne.
Quel est le mot de passe ?
Dans cette extension, tu vas programmer un mot de passe secret que l'utilisateur devra taper pour pouvoir franchir la porte secrète.
Faire tomber la pluie
Dans cette extension, tu vas apprendre à utiliser le sac à dos pour ajouter la pluie de ton projet de l'activité 3 dans ce projet.
Trouver la porte secrète (défi)
Dans cette extension, tu vas programmer le personnage principal pour que le public puisse utiliser les touches fléchées pour trouver la porte secrète.
Ajouter un autre lutin
Dans cette extension, tu vas ajouter d'autres personnages à ton histoire.
Ajouter une autre scène (défi)
Dans cette extension, tu vas être invité à ajouter une autre scène et tu recevras des idées sur la façon de la programmer.
arrow_backward Retour
Suivant arrow_forward
Instructions
  1. Choisis une extension, et clique sur "Regarder" pour découvrir comment la créer.
  2. Une fois que tu as terminé une extension, essayes-en une autre !