Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 Visual ProgrammingFunctions2 What We Will Do Today Learn about functions in Alice Height

3 Visual ProgrammingFunctions3 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

4 Visual ProgrammingFunctions4 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

5 Visual ProgrammingFunctions5 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.

6 Visual ProgrammingFunctions6 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!

7 Visual ProgrammingFunctions7 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

8 Visual ProgrammingFunctions8 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?

9 Visual ProgrammingFunctions9 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.

10 Visual ProgrammingFunctions10 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.

11 Visual ProgrammingFunctions11 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

12 Visual ProgrammingFunctions12 Adjusting Style adjusting the style of motion makes the bounce more realistic

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

14 Visual ProgrammingFunctions14 Attempts at Rolling Why doesn’t this work? Will this?

15 Visual ProgrammingFunctions15 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?

16 Visual ProgrammingFunctions16 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

17 Visual ProgrammingFunctions17 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

18 Visual ProgrammingFunctions18 Building a numberOfRevolutions function Step 1 Step 2: Drag distance parameter to replace 1 in return

19 Visual ProgrammingFunctions19 The numberOfRevolutions function Step 3: Select math and divide. Select other and key in 3.14

20 Visual ProgrammingFunctions20 The numberOfRevolutions function Step 4: Select the 3.14. Pick math and 3.14 * Select 1

21 Visual ProgrammingFunctions21 The numberOfRevolutions function Step 5: Replace the 1 with the width of the ball from the functions

22 Visual ProgrammingFunctions22 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?

23 Visual ProgrammingFunctions23 Creating a Roll Method Create a class level method for realistic roll problem: amount of time to roll. (Demo)

24 Visual ProgrammingFunctions24 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

25 Visual ProgrammingFunctions25 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).


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

Similar presentations


Ads by Google