Trying it out - Be Reflective: Mirror Images

Objective

After completing this lesson, you will be able to apply the concepts of the previous lesson - Be Reflective: Mirror Images.

Exercise

Your Turn!

Now it’s your turn. Download this document for a recap of the blocks we covered in this lesson and some hands-on exercises for you to explore.

What You Have Learned in This Lesson

Parallelism

In this unit, you learned about parallelism. In Snap!, you can have multiple sprites that run scripts at the same time. With buttons in the sprite corral, you can add new turtle sprites, draw additional sprites or create new sprites with a picture from your webcam as a costume. Or you can duplicate your existing sprite by right-clicking it and select "duplicate" from its context menu. This will also copy all the existing scripts and costumes of the sprite you’re creating the duplicate from.

You can generate new sprites by clicking on any of the buttons in the sprite corral below the stage. Based on which button you click, you can immediately add one drawn costume or one webcam picture as a costume. If you right click a sprite in the corral, you can duplicate it from its context menu.

Additionally, you can run scripts simultaneously within one sprite, e.g. when scripts are triggered by the same hat block or when several scripts in a sprite contain a forever loop.

Both scrips of the sprite are triggered with the green flag and contain a forever loop. Since they are running in parallel, they result in the circle on the right.

User Interaction

You also learned that you can execute scripts based on a user’s input for example from the keyboard or the mouse. Snap! provides hat blocks that trigger the scripts below them on user action, like when (key) key pressed. There are predicates and reporters that report the mouse’s position or whether it is currently pressed. If you are interested in more user interaction, check out the "Sensing" category.

The hat block “When key is pressed” triggers the connected blocks when the selected key is pressed. The reporters and predicate from the “Sensing” category report the x- and y- position of the mouse or whether it is currently pressed.

Conditionals

Moreover, you learned how to use conditionals – blocks that execute parts of your program based on a condition.

Snap! has two types of conditionals, if _ and if_ else_. The blocks inside their c-shaped slot are executed depending on the condition that is used as an input to the hexagonal input slot. The condition in the hexagonal slot reports either trueor false, so-called Boolean values.

The if block runs the enclosed blocks only when the condition in the hexagonal input slot evaluates to true. If the condition is false, the script simply continues with the blocks that follow. In an if–else block, the blocks immediately after if run when the condition is true. If the condition is false, the blocks inside the else section are executed instead.

What we didn’t show you but might be helpful anyway is the following:

You can expand the ifblock to add more conditions. Clicking on the small arrowhead at the bottom of the ifblock will add an else if_ section to it.

Clicking on the arrowhead of the if block will add an else if section to the block. You can add as many if else sections as you need.

These conditions are evaluated from top to bottom and for the first one that is true, the corresponding blocks are executed. Not adding a specific condition to one of the Boolean input slots will make it work like the regular elsein the if else block. The blocks enclosed in that part will be executed, if none of the conditions above are true.

If you want to use several conditions, make sure that you have them in the right order.

In the following example you e.g. must start with the shortest distance, because distance to target < 100 will also report true, if the distance is smaller than 50 and 10. Therefore, the other parts of that conditional wouldn’t be evaluated as soon as the distance to target is smaller than 100.

Example for an expanded if block. The conditions are executed from top to bottom. If none of them are true, the blocks in the else if true part will run.