Skip to content

Please update your browser

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

Transcript

In this add-on, you’ll code the sprite to ask if the buyer wants to purchase the gadget, then react to the answer. First, ask the person watching the ad if he or she would like to buy the gadget. Select the android sprite. From the “sensing” menu, drag out an “ask” block. Write a question in the box. This example asks, “Would you like to buy the SportsWatch9000?” Test this by clicking the block. The sprite asks “Would you like to buy the SportsWatch9000?” and a box appears at the bottom of the screen.

Someone watching your project would answer the question in that box. Type some text in the box, and click enter. A variable named “answer” stores anything typed in the box. In the “sensing” menu, click the check mark next to “answer” to see it in the top left corner of the stage. It should say what you typed into the box. The sprite will act happy if the answer typed is “yes.” Making your program check what answer the user typed is similar to how you checked if a key was pressed or if the ball sprite was touching the player sprite in the net sports project in activity 3. From the control menu, place an “if” block under the “ask” block. To check if the answer is the same as “yes,” from the “operators” menu, place an “equals” block in the condition section of the “if” block. From the “sensing” menu, place “answer” on one side of the “if” block, and type “yes” on the other side. Inside the “if,” place the code for what will happen if the answer is “yes.” In this example, the sprite switches to the happy costume and says “Great choice, you’re going to love the SportsWatch9000.”

Click on the code. Answer “yes.” The sprite should say or do whatever cool thing you programmed.

Click the code and type “no” this time. Nothing happens.

To make something “else” happen if the user answers something other than “yes,” use the “if-else” block instead of the “if” block. Set the “if” block aside.

From the “control” menu, place the “if-else” block after the “ask” block. Drag the “equals” condition from the “if” block to the condition section of the “if-else” block. Then, drag your reaction code from the “if” block to the “if” section of the new “if-else” block.

The “else” section of the “if-else” block tells the program what to do if the condition isn’t true - so if the answer isn’t “yes.” Place the code for the sad sprite here. In this example, the sprite switches to the sad costumes and says “Oh, well maybe next time!”

Click the code, and type “no.”

The code inside “else” should run. Cool.

Finally, tell your program when to make this happen. Using “broadcast” and “receive” blocks help make it easier to read and reuse your code. Any time the sprite should ask for a purchase decision, these blocks will make the code you just wrote run.

From the “events” menu, drag a “broadcast and wait” block to at least one place on the block stack from the starter code. From the dropdown, select “new message” and type “purchase decision.” “Broadcast and wait” tells the program to wait until the code that receives the message is done running before the program runs more code.

“Broadcast” would let the rest of the code run while the sprite is asking if the audience wants to buy.

From the “events” menu, place a “when I receive” block on top of the code that makes the sprite ask the audience if they want to buy.

From the dropdown, select “purchase decision.”

Test the code by clicking the flag.

The sprite will ask its question when the program sends the “purchase decision” message. Here’s the game plan. Code the sprite to ask the buyer a question, then use an “if-else” statement to respond to the answer.

Choisir une extension
Danser
Programmer le lutin pour qu'il danse.
Changement de scène
Fais passer l'androïde sur différentes scènes pour voir comment le gadget réagit.
Imperméabilité
Ajoute des effets météorologiques pour montrer la solidité du gadget.
Décision d'achat
Demande à l'acheteur d'acheter l'objet.
Public admirateur
Ajoute au projet d'autres lutins qui interagiront avec l'athlète.
Entraîneur sportif
Code le lutin pour lui ajouter un pouls et pour le faire parler de son rythme cardiaque.
arrow_backward Retour
Suivant arrow_forward
Instructions
  1. Découvre les différents badges que tu peux gagner.
  2. Sélectionne les fonctionnalités qui t’intéressent !