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 Secret Code

Transcript

In this video, you will create a secret key that only your friend can use to find out his or her perfect future!

This is a little harder than some of the other add-ons and teaches a new concept, so use your persistence and curiosity to complete it!

First, program the computer to figure out that the secret key has been pressed.

Computers use “conditional statements” to check whether something is true.

One common conditional statement is an “if” statement.

That’s what you will use in this add-on.

From the “control” menu, drag out an “if/then” block.

This block has a hexagon-shaped space where the condition goes.

For this project, the condition is pressing a specific key.

From the “sensing” menu, place a “key pressed” block inside the “if/then” block.

From the dropdown, select the key that will be the secret key.

This example uses “j.”

Next, tell the program what to do if that key is pressed.

It should switch the costume of the sprite to the one you chose for your friend.

From the “looks” menu, place the “switch costume to” block inside the “if/then” block.

From the dropdown, select a costume for your friend.

This example chooses the castle.

Place the “if” block inside the repeat until loop.

This makes the program keep checking if the secret key is pressed until the spacebar is pressed.

Test the code by clicking the green flag, then the secret key.

Wait!

The program doesn’t stop flipping through options!

It only shows the special future for a very short time.

To make the secret key also stop the program from switching costumes, go to the “control” menu, and place a “stop all” block inside the “if/then” block.

Not everything needs to stop!

Just this script.

“This script” means this stack of blocks, but not any others that might be running.

From the dropdown, select “this script.”

Test the code by clicking the green flag, then the secret key.

The program stops flipping this time!

But wait, the jobs sprite never starts flipping.

The script stopped before the program reached the code that broadcasts to the jobs sprite.

To fix this, go to the “events” menu, and place a “broadcast” block inside the “if/then” block before the “stop” block.

Test the code by clicking the green flag, then press the secret key.

The program picks the selected costume, then the jobs sprite starts spinning.

Great!

Try using a different keypress event to create a secret key for your friend’s job as well.

Now it’s your turn: Use the “if/then” “key pressed” and “switch costume to” blocks to choose the perfect future for your friend.

Add a “stop” block to the “if/then” block to make the home costumes stop flipping.

Finally, add a “broadcast” block to the “if/then” block to make the job sprite start flipping.

Choose an Add-On
A Bigger Future
Add Another Image (Part 1 of 2)
Add Another Image (Part 2 of 2)
Program Introduction
Talking about the Future
Secret Code
Sound!
arrow_backward Back
Next arrow_forward
Instructions
  1. Choose an Add-On, and click "watch" to learn how to build it.
  2. Once you finish one Add-On, try another one below the video!