yarringt/101/lab11-functionsandevents1.docx · web viewpart 1: tutorial on parameters (passing...

10
Part 1: Tutorial on Parameters (passing values into functions) and Events (controlling instruction flow by interacting with the computer) Note: For this lab, you must complete both part1 (this) and part 2 (in the second Word document). 1. Start a new world a. Add a Kangaroo and several animals of different heights, spread around the world i. I picked bunny, cow, horse1, ladybug, tortoise, turtle – any animals will do 2. Problem: The kangaroo wants to visit each animal where “visit” means that the kangaroo and the animal face each other, the kangaroo moves close in front of it, the kangaroo says “hi” and then the kangaroo jumps over it. However, the kangaroo can only jump 1 meter high. Parts of our algorithm: Kangaroo visit each friend Kangaroo either go around or jump over Create an event to click on animal to visit 3. We’ll make a method to have the kangaroo visit the cow. a. Create a “kangaroo” method called visit

Upload: others

Post on 11-Sep-2019

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: yarringt/101/lab11-functionsandevents1.docx · Web viewPart 1: Tutorial on Parameters (passing values into functions) and Events (controlling instruction flow by interacting with

Part 1: Tutorial on Parameters (passing values into functions) and

Events (controlling instruction flow by interacting with the computer)

Note: For this lab, you must complete both part1 (this) and part 2 (in the second Word document).

1. Start a new worlda. Add a Kangaroo and several animals of different heights, spread around the world

i. I picked bunny, cow, horse1, ladybug, tortoise, turtle – any animals will do

2. Problem: The kangaroo wants to visit each animal where “visit” means that the kangaroo and the animal face each other, the kangaroo moves close in front of it, the kangaroo says “hi” and then the kangaroo jumps over it. However, the kangaroo can only jump 1 meter high. Parts of our algorithm:

• Kangaroo visit each friend• Kangaroo either go around or jump over• Create an event to click on animal to visit

3. We’ll make a method to have the kangaroo visit the cow. a. Create a “kangaroo” method called visit

Page 2: yarringt/101/lab11-functionsandevents1.docx · Web viewPart 1: Tutorial on Parameters (passing values into functions) and Events (controlling instruction flow by interacting with

b. Enter the following code:

• For the third line• drop the move forward first• under “kangaroo”, “functions”, drag in “distance to” and drop over the

number

Page 3: yarringt/101/lab11-functionsandevents1.docx · Web viewPart 1: Tutorial on Parameters (passing values into functions) and Events (controlling instruction flow by interacting with

• Then click on the last white arrow on the line to select “Math” and then the minus and math and 2.2 (you can play with this number to get a distance you like).

• The kangaroo will stop 2.2 (or whatever distance you chose) meters in front of the center of the cow.

c. Add the remaining code:

• For the sixth line• drop the move forward first• under “kangaroo”, “functions”, drag in “kangaroo’s depth” and drop over

the number• Then click on the last white arrow on the line to select “Math” and then the

+ and math and 2.2 (it should be the same number you chose earlier)4. Now we will make this an event. We will start by making this an event that happens when you

hit the play button:a. In the events area, change “when the world starts” to Kangaroo.visit

b. Hit the play button.

Functions

5. Great! But what if we want to make the Kangaroo visit some of the other animals and jump over them? We could write a separate method for each animal the kangaroo could meet, but that would be long and tedious. Instead, we’ll change the method we just created to a function,

Page 4: yarringt/101/lab11-functionsandevents1.docx · Web viewPart 1: Tutorial on Parameters (passing values into functions) and Events (controlling instruction flow by interacting with

and add a parameter to be used inside the function. Parameters are like placeholders – we fill in what they hold when we call the function.

Note: for simplicity’s sake, Alice stores functions you create in the method display for that object. In other words, even though there are both function and method tabs at the top of the display area to the left of your editor, the functions you create will be stored under the method tab.

6. Add a parameter named friendToVisita. type should be Object b. click ok

c. Now click and drag friendToVisit on top of the four occurances of Cow

d. Click on the World.my first method tab. Add a kangaroo.visit method for each animal in your scene, and, when you do, choose each animal as your parameter.

Page 5: yarringt/101/lab11-functionsandevents1.docx · Web viewPart 1: Tutorial on Parameters (passing values into functions) and Events (controlling instruction flow by interacting with

e. Change your event from: When the world starts, do kangaroo.visit back to When the world starts, do world.my first method.

f. You should have a screen that looks something like this:

7. So far so good. But our kangaroo can only jump one meter high. You may have noticed that when the kangaroo visits someone taller than one meter high, the kangaroo jumps through his/her friend. We probably don’t want this. We probably want to check whether the animal being visited is taller than a meter high. If it is, then our kangaroo will go around the animal rather than over the animal. We will be using an if/else control statement.

a. Return to our kangaroo.visit method by clicking on the kangaroo.visit tab.b. Drag in an If/Else from the bottom of the window, placing after the “say”

Page 6: yarringt/101/lab11-functionsandevents1.docx · Web viewPart 1: Tutorial on Parameters (passing values into functions) and Events (controlling instruction flow by interacting with

c. Select “world” and its “functions” and scroll to find math functions, then click and drag “a<b”, selecting 1’s

d. Drag in “cow’s height” from the cow functions over the first 1 in the if statement, then drag and drop “friendToVisit” over cow

Page 7: yarringt/101/lab11-functionsandevents1.docx · Web viewPart 1: Tutorial on Parameters (passing values into functions) and Events (controlling instruction flow by interacting with

e. If the friend to visit’s height is less than 1 meter, we want the kangaroo to do the instructions we’ve written for jumping over the friend. So we’ll move the kangaroo’s jumping instructions under the condition in which the friend’s height < 1.

f. If, however, the friend’s height is not < 1 meter, we want to write instructions to have the kangaroo go around the friend. This involves having the kangaroo first turn right .25 revolutions, then turn left .5 revolutions. We add “as seen by Friend To Visit” so that the rotation is around the friend. Our code should now look like this:

g. Hit the play button to see what you’ve got so far.

8. You’ve written a method, a function, and used an if/else control statement. Now let’s make this program more event-driven. We’ll make it so that the kangaroo visits the friend you click on with your mouse.

a. First, let’s disable the code in world.my first method. You right-click on each instruction and select disable

b. Now create a new event, When the mouse is clicked on something

Page 8: yarringt/101/lab11-functionsandevents1.docx · Web viewPart 1: Tutorial on Parameters (passing values into functions) and Events (controlling instruction flow by interacting with

c. Drag the kangaroo’s visit method and drop it over the Nothing in the “When the mouse is clicked on anything, do nothing” statement in the events editor. Select “expressions”,

then “object under mouse cursor”

d. Now click “play” and click on different animals, but not the kangaroo. Congratulations! You’ve written a function, used an if/else statement, and created an event-driven program.

9. Move on to Part 2 of this lab. Close this document and open lab11-functionsandevents2.docx.