–Visual Programming CS320n Functions Mike Scott

advertisement
CS320n –Visual Programming
Functions
Mike Scott
(Slides 6-1)
Thanks to Wanda Dann, Steve Cooper, and Susan Rodger
for slide ideas.
What We Will Do Today
• Learn about functions in Alice
Height
Visual Programming
Functions
2
Functionality
• A function
– receives value(s),
– performs some computation on the
value(s), and
– returns (sends back) a value.
• The values it receives or inputs are
parameters
• like a method in some ways
• like asking a question and getting
and answer
Visual Programming
Functions
3
Types of Functions
• Functions can be classified by the type of
value they return
–
–
–
–
a calculated value (a number)
a specific object
a color
others
42
Visual Programming
Functions
4
Built-in Functions
• We used one of Alice's built-in functions in the
skateAround method for the advancedSkater.
• asked for the distance between the advanced
skater and the object, target. (In the specific
example shown, target was a penguin.)
Distance between
center points.
Visual Programming
Functions
5
Another Example
• How do we make a realistic bouncing ball?
• Bounce it over a net
–
–
–
–
ball is 1 meter from net to start
ball should move forward
simultaneously ball should move up then down
parabolic motion
Note: This looks easy – but do not be deceived!
Visual Programming
Functions
6
Design Storyboard
• A possible storyboard
• To reach the top of the
net,
– we know the ball
should move forward 1
meter (we positioned
the ball 1 meter in front
of the net)
– but how far upward
should the ball move
to clear the net?
Visual Programming
Functions
World.ballOverNet:
Do in order
toyball turn to face the net
Do together
toyball move up
toyball move forward
Do together
toyball move down
toyball move forward
7
Height
• We can use the built-in height question to
determine the height of the net and move the
ball up that distance.
• Demo program. What happens?
Visual Programming
Functions
8
Problem
• The ball does not bounce over the net.
• The problem is that we cannot easily tell
"which way is up" or “which way is
forward” – from the perspective of the ball.
Visual Programming
Functions
9
Solution
• We think “up” and
“down” relative to the
ground
• may be different for
objects in the world
• orient ball to ground
• Now, when the code is
run, the ball will
bounce over the net.
Visual Programming
Functions
10
Realistic Motion
• Turn the camera to look at
the bounce from the side.
• Is it realistic?
• The default style for motion
is “gently”
• begin and end gently
• can get more realistic by
– making forward motion
abrupt,
– up motion end gently, and
– down motion begin gently
Visual Programming
Functions
11
Adjusting Style
• adjusting the style of motion makes the bounce
more realistic
Visual Programming
Functions
12
Rolling the ball
• How do we create a realistic rolling action
• Not a slide
• The ball must simultaneously move and roll.
Visual Programming
Functions
13
Attempts at Rolling
Why doesn’t
this work?
Will this?
Visual Programming
Functions
14
Revising the approach
• The ball is made to roll 1 revolution.
• What if we want the ball to roll a certain
distance? A class level method for rolling,
with the distance sent in as a parameter
• How can we make the ball roll the correct
number of revolutions to cover a given
distance along the ground?
Visual Programming
Functions
15
Math to the rescue!
• Number of revolutions
• The number of revolutions
depends on the size of the
ball
Big Ball
– The number of revolutions is
distance / ( Pi * diameter)
• But there is no built-in
question to return the number
of revolutions
• We will write our own function
Visual Programming
Functions
one revolution
four revolutions
Little Ball
16
Parameters
• We want to return the value computed as
distance / ( Pi * diameter of ball)
• What information is needed?
– the ball’s diameter
• the ball object has a built-in width function
– the distance the ball is to travel
• can be sent as a parameter to the function
Visual Programming
Functions
17
Building a numberOfRevolutions
function
Step 1
Step 2:
Drag distance
parameter to
replace 1 in return
Visual Programming
Functions
18
The numberOfRevolutions function
Step 3:
Select math and divide.
Select other and key
in 3.14
Visual Programming
Functions
19
The numberOfRevolutions function
Step 4:
Select the 3.14.
Pick math and 3.14 *
Select 1
Visual Programming
Functions
20
The numberOfRevolutions function
Step 5:
Replace the 1 with
the width of the ball
from the functions
Visual Programming
Functions
21
Demo: Calling the Function
10 is a test value.
We should run the animation with several test
values to be sure it works as expected.
What happens if you use a negative value?
Visual Programming
Functions
22
Creating a Roll Method
• Create a class level method for realistic roll
• problem: amount of time to roll. (Demo)
Visual Programming
Functions
23
Fixing Realistic roll
• Set duration of to function of distance.
– Try 2 meters per second
• Fixes one problem, but what happens with
negative numbers?
• What is result of -5 / 2?
• Fix, using a world level method that finds the
absolute value of a number
Visual Programming
Functions
24
Levels of Questions
• As with methods, you can write questions
as either class-level or world-level. (The
question just presented was class-level.)
• The guidelines for class-level methods
also apply to class-level questions:
– No references to other objects.
– No references to world-level questions you
have written (built-in world-level questions
are fine to use).
Visual Programming
Functions
25
Download