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 heor 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 exampleasks, “Would you like to buy the SportsWatch9000?” Test this by clicking the block. The spriteasks “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 inthe box, and click enter. A variable named “answer” stores anythingtyped in the box. In the “sensing” menu, click the checkmark next to “answer” to see it in the top left corner of the stage. It should saywhat you typed into the box. The sprite will act happy if the answer typedis “yes.” Making your program check what answer the user typed is similar to how youchecked if a key was pressed or if the ball sprite was touching the player sprite in thenet sports project in activity 3. From the control menu, place an “if” block underthe “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 theother side. Inside the “if,” place the code for whatwill happen if the answer is “yes.” In this example, the sprite switches to the happycostume 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” sectionof the new “if-else” block.
The “else” section of the “if-else” block tells the program what to do if thecondition isn’t true - so if the answer isn’t “yes.” Place the code for thesad 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 askfor 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 onthe block stack from the starter code. From the dropdown, select “new message” andtype “purchase decision.” “Broadcast and wait” tells the program to wait untilthe 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 theaudience wants to buy.
From the “events” menu, place a “when I receive” block on top of the code thatmakes 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 sendsthe “purchase decision” message. Here’s the game plan. Code the sprite toask the buyer a question, then use an “if-else” statement to respond to the answer.
Choose an Add-On
Program the sprite to dance.
Move the android through different scenes to see how the gadget reacts.
Add weather effects to show how rugged the gadget is.
Ask the buyer to purchase the object.
Add more sprites to the project to interact with the athlete.
Code the sprite to have a pulse and talk about its heart rate.