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 Trova la porta segreta

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.

Scegli un’estensione
Cammina
In questo video scoprirai come fare avvicinare il personaggio alla porta e come farlo allontanare.
Qual è la password?
Con questo componente, programmerai una password segreta che l'utente dovrà inserire per attraversare la porta segreta.
Fai piovere
Con questo componente, scoprirai come usare la valigetta per inserire nel progetto la pioggia che hai creato durante la terza giornata.
Trova la porta segreta
Con questo componente, farai muovere il personaggio su comando dell'utente con i tasti freccia.
Aggiungi un altro personaggio
Con questo componente aggiuntivo, inserirai nella storia altri personaggi.
Aggiungi un'altra scena
In questo componente ti guiderò nell'aggiunta di una nuova scena e ti darò qualche suggerimento su come programmarla.
arrow_backward Indietro
Avanti arrow_forward
Istruzioni
  1. Scegli un componente aggiuntivo e fai clic su "Guarda" per scoprire come costruirlo.
  2. Una volta terminato un componente aggiuntivo, scegline un altro da provare!