CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper,...

25
CS320n –Visual Programming Functions Mike Scott (Slides 6-1) hanks to Wanda Dann, Steve Cooper, and Susan Rodger or slide ideas.

Transcript of CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper,...

Page 1: CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

CS320n –Visual Programming

Functions

Mike Scott

(Slides 6-1)

Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Page 2: CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Functions 2

What We Will Do Today• Learn about functions in Alice

Height

Page 3: CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Functions 3

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

Page 4: CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Functions 4

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

Page 5: CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Functions 5

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.

Page 6: CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Functions 6

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!

Page 7: CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Functions 7

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?

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

Page 8: CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Functions 8

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?

Page 9: CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Functions 9

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.

Page 10: CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Functions 10

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.

Page 11: CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Functions 11

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

Page 12: CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Functions 12

Adjusting Style• adjusting the style of motion makes the bounce

more realistic

Page 13: CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Functions 13

Rolling the ball• How do we create a realistic rolling action• Not a slide• The ball must simultaneously move and roll.

Page 14: CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Functions 14

Attempts at Rolling

Why doesn’t this work?

Will this?

Page 15: CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Functions 15

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?

Page 16: CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Functions 16

Math to the rescue! • Number of revolutions • The number of revolutions

depends on the size of the 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

one revolution

four revolutions

Big Ball

Little Ball

Page 17: CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Functions 17

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

Page 18: CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Functions 18

Building a numberOfRevolutions function

Step 1

Step 2: Drag distanceparameter to replace 1 in return

Page 19: CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Functions 19

The numberOfRevolutions function

Step 3: Select math and divide.Select other and keyin 3.14

Page 20: CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Functions 20

The numberOfRevolutions function

Step 4: Select the 3.14.Pick math and 3.14 *Select 1

Page 21: CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Functions 21

The numberOfRevolutions functionStep 5: Replace the 1 withthe width of the ballfrom the functions

Page 22: CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Functions 22

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?

Page 23: CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Functions 23

Creating a Roll Method• Create a class level method for realistic roll

• problem: amount of time to roll. (Demo)

Page 24: CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Functions 24

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

Page 25: CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Functions 25

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).