Você está na página 1de 7

Tutorial 6: Use the IF and WHILE Control Structures

Overview
In this tutorial, you will practice using the IF and WHILE control structures to control
the execution of procedures and functions based on a true or false condition.
Key Concepts
Use the IF control structure to affect execution of instructions
Learned
Use the WHILE control structure to create a conditional loop for repetitive
behavior
Difficulty
Advanced: This tutorial is appropriate for someone who has previously used Alice 3 to:
Level
Add and position objects in a scene
Code programming statements in the Code editor
Randomize movements
Declare a user-defined procedure
Create an animation that uses a control statement to control animation timing
Use functions to control movement based on a return value
Duration
60-90 minutes
Notes
This tutorial was built using Alice 3.1.81.
Part 1: Define the Scenario
Review the scenario and corresponding animation. In this tutorial, you are going to create an animation
from the scenario defined below.
Scenario
Animation
Walruses race to see who can get into the icy pond Two walruses race to a pond and the one that
first.
reaches the pond first dives in. Each time the
animation is played, a different walrus may win.
Part 2: Create the Scene
1.
Open the SNOW template.
Save the animation project with the file name Tutorial 6.
REMEMBER: SAVE FREQUENTLY AS YOU WORK!
2.
In the Gallery, go to the Browse Gallery by Theme tab
Select the SNOW theme.
3.
Add the following objects to the scene:
2 Baby Walruses
1 Baby Yeti with Tutu
1 Baby Yeti with Scarf
Note: Do not position the objects yet, as you will do so in later steps.
4.
In the Gallery, go to the Shapes/Text tab.
Add one Disc to the scene.
Select CYAN for the paint color.
5.

In the object tree, right-click on the disc and select Rename...


Rename the disc to pond.

1
Copyright 2014, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective
owners.

6.

Rename the Baby Yeti with the scarf to YetiBabyScarf.


Rename the Baby Yeti with the tutu to YetiBabyTutu.

7.
Use the Resize handle
be able to dive into it.

to resize the pond so it is large enough for the two baby walruses to

8.

Move the two baby yetis to the right of the pond (your right), slightly facing the pond.

9.

In the following steps, you will precisely position the baby walruses for the start of the race so that
each baby walrus is the exact distance away from the center of the pond. This ensures that the
baby walruses have a fair start.
Right-click on the WalrusBaby.
Select Procedures.
Select moveAndOrientTo. Select the pond.
This will move the walrus to the center of the pond. The Walrus may appear to sink into the pond.
You will fix this in a future step.

10.

Right-click on the WalrusBaby2.


Select Procedures.
Select moveAndOrientTo. Select the pond.
The two baby walruses should be on top of each other in the center of the pond. They may appear
to sink into the pond. You will fix this in a future step so they are sitting on the ground.
Right-click on the WalrusBaby.
Note: Use the object tree to select the correct baby walrus since they are on top of each other in
the scene.
Select Procedures.
Select move.
Select arguments: BACKWARD 10.0.
Right-click on the WalrusBaby2.
Select Procedures.
Select move. Select arguments: BACKWARD 10.0.

11.

12.

2
Copyright 2014, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective
owners.

13.

Both baby walruses should be precisely 10 meters away from the pond. Now you need to separate
them so they are standing next to each other.
Right-click on WalrusBaby in the object tree.
Select Procedures.
Select move.
Select arguments: LEFT 1.0.

14.

Right-click on WalrusBaby2 in the object tree.


Select Procedures.
Select move.
Select arguments: RIGHT 1.0.
Now you will move both baby walruses up so they are sitting on the ground and not sunk into the
ground.

15.

16.

17.

18.

Click on the Translation handle style.


Click on WalrusBaby. Use the up arrow that appears over the baby walruss head to move the
baby walrus up so it is sitting on the ground and you can see its feet.
Click on WalrusBaby2. Use the up arrow to move the baby walrus up so its sitting on the ground.
Now you will ensure that both walruses are turned to face the center of the pond.
Right-click on the WalrusBaby.
Select Procedures.
Select turnToFace. Select: pond.
Right-click on the WalrusBaby2.
Select Procedures.
Select turnToFace. Select: pond.
When you are finished, your scene should look like this:

You may add additional objects to the scene to make your scene unique, such as people, props, or
animals.
Part 3: Program the Animation
PART 1
1.
Go to the Code editor.
2.
Drag a Do In Order
into myFirstMethod.
3.
Drag a comment
into the Do In Order.
Enter text: Race Start
4.
Select the YetiBabyTutu from the instance menu.
5.
Drag a say procedure into the Do In Order.
3
Copyright 2014, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective
owners.

6.
7.
8.
9.

10.
11.
12.
13.
14.
15.
16.

Enter text: Are you ready to race?


Drag a Do Together
into the Do In Order.
Select the WalrusBaby from the instance menu.
Drag a say procedure into the Do Together.
Enter text: Ready!
Copy the say procedure. Paste it into the Do Together.
Change the object from WalrusBaby to WalrusBaby2.

Drag a Do Together into the Do In Order.


Select the YetiBabyTutu from the instance menu.
Drag a say procedure into the Do Together.
Enter text: Ready, set, go!
Drag a turn procedure into the Do Together.
Select arguments: LEFT 4.0.
Select YetiBabyScarf from the instance menu.
Drag a turn procedure into the Do Together.
Select arguments: RIGHT 4.0.
Run the animation to test the results of your programming statements.
At this point, your code should look like this:

PART 2
1.
Drag a Do In Order
into the myFirstMethod tab.
2.
Drag a comment
into the Do In Order.
Enter text: Walruses race each other to the pond. When one of the walruses reaches the pond,
the walruses stop.
3.
Drag a WHILE control statement into the Do In Order.
Select the true condition.

4.

Now you must specify the condition under which the while loop executes (while the walruses are
not colliding with the pond, they will continue to move forward a random amount). Once the
condition is no longer true, the while loop will stop executing.
4

Copyright 2014, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective
owners.

Select the down-pointing arrow next to the true condition.


Select EITHER true OR ???
Select true.

5.

Select the outermost down-pointing arrow next to the condition.


Select NOT EITHER true OR true.

6.
7.

Select the WalrusBaby from the instance menu.

8.

9.
10.

11.
12.
13.
14.

15.

Go to the Functions tab.


Drag the isCollidingWith function onto the first true condition in the while statement.
Select the pond.

Select the WalrusBaby2 from the instance menu.


Drag the isCollidingWith function onto the second true condition in the while statement.
Select the pond.

This complete condition states Either the WalrusBaby is not colliding with the pond or the
WalrusBaby2 is not colliding with the pond While either walrus is not colliding with the pond, the
while loop should continue executing.
Select the WalrusBaby from the instance menu.
Go back to the Procedures tab.
Drag a Do Together into the while statement.
Drag a move procedure into the Do Together.
Select arguments: FORWARD 1.0.
1.0 is a placeholder and will be changed in the next step.
Next, you will modify the distance argument in the move procedure so that a random value
between 0.25 and 2.0 is selected each time the move procedure is executed.
Click the arrow next to the 1.0 distance argument in the move procedure.
Select Random.
Select nextRandomRealNumberInRange ??? ???
5

Copyright 2014, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective
owners.

Select 0.25 for the low value.


Select 2.0 for the high value.

The programming statement should look like this:

16.

Copy the move procedure. Paste it into the Do Together.


Change the object from WalrusBaby to WalrusBaby2.

17.

Run the animation to test the results of your programming statements.


You will notice that the walruses stop when one of them reaches the pond. You will also notice that
they move forward a random amount, and there is a different outcome each time you run the
animation.
PART 2
1.
Drag a comment
into myFirstMethod.
Enter text: The walruses celebrate the end of the race. Then, they dive into the water.
2.
Drag an IF statement
into myFirstMethod.
Select the true condition.

3.
4.
5.

Select the WalrusBaby from the instance menu.


Click the Functions tab.
Drag the isCollidingWith function onto the true condition.
Select the pond.

6.
7.

Click the Procedures tab.


Drag a Do In Order into the section drop statement here below the true condition.
6

Copyright 2014, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective
owners.

8.

9.
10.
11.
12.

Ensure the WalrusBaby is still selected.


Drag a say procedure into the Do In Order.
Enter text: Yay! I won! Lets go for a swim.
Drag a move procedure into the Do In Order.
Select arguments: FORWARD 1.0.
Drag a Do Together into the Do In Order.
Drag a turn procedure into the Do Together.
Select arguments: FORWARD 0.25.
Drag a move procedure into the Do Together.
Select arguments: DOWN 2.0.

13.

Copy the Do In Order.


Paste it into drop statement here under the ELSE condition.
Change the object arguments in each procedure from WalrusBaby to WalrusBaby2.

14.

Run the animation multiple times.


Observe what happens when each Walrus touches the pond first. The walrus that touches the
pond first speaks and then dives into the pond. Then the animation ends.

Part 4: Give the Animation a Unique Ending


Now that the Walrus race is over, give the animation a unique ending. Here are some ideas to spur your
thinking:
Both walruses dive into the water and do synchronized swimming.
The yetis dive into the water and swim.
A shark pops out of the pond and swims around, scaring the walruses.
Note: If you plan on completing Tutorial 7, do not complete part 4 to give the animation a unique
ending, as tutorial 7 builds on the animation created in Tutorial 6.
7
Copyright 2014, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective
owners.

Você também pode gostar