Alice: Functions Alice Chapter 6 September 19, 2005.

Slides:



Advertisements
Similar presentations
Space Man Sam: Grammar Mistakes By Aleis Murphy Duke University, Under the direction of Professor Susan Rodger July 2010.
Advertisements

Princess & Dragon – Version 2 By Michael Hoyle under the direction of Professor Susan Rodger Duke University July 2012 Part 3: Billboards, Events, Sounds,
Getting Started With Alice By Ruthie Tucker under the direction of Prof. Susan Rodger Duke University, July
Alice Inheritance and Event Handling. Inheritance Concept Consider this hierarchy; parents describe properties of children Animals Vertebrates MammalsFish.
Events Chapter 7. Interactivity The real world is interactive User determines order of actions instead of programmer.
Events Chapter 7. Interactive Real world is interactive User determines order of actions instead of programmer.
Lets Play Catch! Keeping Score in Alice By Francine Wolfe Duke University Professor Susan Rodger May 2010.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Repetition Structures
Fall 2007ACS-1805 Ron McFadyen1 Functions and if-else A function is a collection of statement, similar to a method, but a function is defined to return.
CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
CS320n –Visual Programming Interactive Programs Mike Scott (Slides 5-1)
Interaction: Events and Event Handling
Functions and Conditionals in Alice 1 Stephen Cooper Wanda Dann Barb Ericson September 2009.
Variables and Functions Chapter Variables Named storage location in computer’s memory Programs may need to store data when running o Stored in.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley This week: Whew!!! The last homework was tough! The homework for this week.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Execution Control with If/Else and Boolean Functions
Making a Boat Racing Game in Alice By Jenna Hayes Under the direction of Professor Susan Rodger Duke University, July 2010.
Study Guide For Test Chapter 5, 6,& 7 Test is Friday, May 15th.
Interaction: Events and Event Handling
Changing Camera Views! Part 2: Simple Scene Change & Lighting Fixes By Bella Onwumbiko under the direction of Professor Susan Rodger Duke University July.
Introduction to Arrays. definitions and things to consider… This presentation is designed to give a simple demonstration of array and object visualizations.
Events Chapter 7 Part 2. While a Key is Pressed Event Specialized event An event occurs when you press a key and continues until you take your finger.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Events (2) (Alice In Action, Ch 6) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September 2012.
Today’s Agenda 1.Collect Pre-Lab 5 2.Collect Alice project storyboards 3.Events 4.Dummy Objects 5.Assign pair programming teams and meet upstairs for Lab.
Parameters and Event-Handler Methods Alice. Mouse clicks Interactive programs often allow the user to mouse click an object in the display. buttons in.
Interactive Programming Sec 49 Web Design. Objectives The student will: Understand the difference between movie mode and an interactive program Understand.
By Melissa Dalis Professor Susan Rodger Duke University June 2011 Multiplication Table.
Writing Our Own Functions Alice. Functionality A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.
What we will do today Learn about functions in Alice.
Functions Alice.
An Introduction to Alice (Short Version) – Extras! Yossra Hamid Under the Supervision of Professor Susan Rodger Duke University, June 2014 This is a continuation.
Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July
1 ball, 2 ball, red ball, blue ball By Melissa Dalis Professor Susan Rodger Duke University June 2011.
Mathematical Expressions, Conditional Statements, Control Structures
Interactive Programming Alice. Control of flow Control of flow -- how the sequence of actions in a program is controlled. What action happens first, what.
CompSci 4 Chap 5 Sec 2 Oct 18, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
Alice Learning to program: Part Two Writing Your Own Methods by Ruthie Tucker and Jenna Hayes Under the direction of Professor Susan Rodger Duke University,
Bunny Eat Broccoli Repetition – Simple loops and Conditional loops Susan Rodger Duke University July 2011.
Balancing the scales: Inequalities By Melissa Dalis Professor Susan Rodger Duke University June 2011.
Variables and Functions Alice. Naming is Important If you get a new pet one of the first things you do is name it Gives you a way to refer to the new.
Functions Sec 8-11 Web Design. Objectives The Student will: Understand what a function is Know the difference between a method and a function Be able.
Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value.
Interactive Programming Alice. Control of flow Control of flow -- how the sequence of actions in a program is controlled. What action happens first, what.
An Introduction to Alice By Jenna Hayes under the direction of Professor Susan Rodger Duke University, June 2009 Updated June 2014 by Ellen Yuan.
CompSci 4 Chap 5 Sec 2 Oct 9, 2008 Prof. Susan Rodger.
February 25,  The BDE(Begin-During-End) event.  Worksheet – Exercise # 9 Instructions  2nd Period Test.
Illuminating Computer Science CCIT 4-6Sep
CompSci 4 Chap 5 Sec 1 and 2 Oct 14, 2010 Prof. Susan Rodger.
Princess & Dragon Part 3: A Knight Comes Riding In—Cameras & Events By Elizabeth Liang under the direction of Professor Susan Rodger Duke University June.
CS320n –Visual Programming Execution Control with If / Else and Boolean Functions (Slides 6-2-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for.
An Introduction to Programming Using Alice 2.2, Second Edition Chapter 7 Recursive Algorithms.
Creating a UFO Rescue Game in Alice
Scratch for Interactivity
Functions Sec 51 Web Design.
Learn about functions in Alice
Creating your Function
Functions Sec 8-11 Web Design.
Creating a UFO Rescue Game in Alice
Learning Java with Alice 3.0 Game Design Kathy Bierscheid
Interactive Programming
Functions Alice.
Functions Alice.
Interactive Programming
Functions Alice.
Functions Alice.
Presentation transcript:

Alice: Functions Alice Chapter 6 September 19, 2005

Checkpoint 80 point quiz Abstract article - quick summary (What cool thing did you read about?)  Turn in to Ms. K Demos of methods & parameters animation Today I am going to have you follow along in Alice as I am presenting the material  Make a world with two toyballs (Sports) in it (one large, one small)

Writing your own functions Functions do not change the state of the world  Instead, they return a value Sometimes there are no built-in functions that do what you want to do One example: Let’s say you want to simulate a ball rolling forward  It’s more difficult than it seems!

Rolling Ball Try: Ball.turn(forward, 1 revolution)  The ball simply rotates in place Maybe try to move the ball forward at the same time that it turns forward  Try: Write a class-level method, realisticRoll: doTogether{ ball.move (forward, 1 meter) ball.turn(forward, 1 revolution) }  The ball still ends up in the same place

Rolling Ball The ball is moving relative to itself, but we want it to move relative to the ground, so let’s change our class-level method to: doTogether{ ball.move (forward, 1 meter, asSeenBy ground) ball.turn(forward, 1 revolution) }  This just guesses that 1 revolution looks right, but what happens if we have a large ball and a small ball rolling forward?

Rolling Ball The distance covered in one revolution of the ball is proportional to the diameter If we wanted the ball to roll for 10 meters, how many revolutions would a large ball need to make? How about a small ball? We want a way to calculate this, depending on the ball’s diameter  We need a new function

Create a new function Try: Create new function  Call it numberOfRevolutions and select Number for the return type You will see a new editor panel for your function with a Return line at the bottom

Returning a value Every function must have a Return statement In our problem, we are asking the function: “How many revolutions does the ball have to make to move a given distance along the ground?”  This depends on the distance traveled by the circumference of the ball in a single revolution numRevolutions = distance / (diameter * pi)

Writing out function To calculate numRevolutions = distance / (diameter * pi) we need to know:  Distance (a parameter sent in to the function)  Diameter (the width of the ball - a built-in function)  Pi (a constant value, we will approximate it to 3.14) We can use the built-in functions and mathematical operators to put this equation into the return part of this function

Another new function Let’s build a fly-space, with a biplane (Vehicle) and a helicopter (Vehicle) flying at approximately the same altitude near the airport (Buildings) Let’s build a function that checks for possible collisions when two vehicles are in the same fly- space  If they are TooClose, the flight controller can radio the planes to change their flight paths

isTooClose Need to know: aircraft1, aircraft2, minimumAllowedDistance We will return a Boolean (true/false) answer Here we need an if/else control structure  Check if absolute value of aircraft1.distanceAbove(aircraft2) < minimumAllowedDistance if so, return true, else return false

avoidCollision Then the world.avoidCollision method could be called if the airplanes were too close Would need to check which airplane was above the other  If airplane1 above, move airplane1 up & move airplane2 down  Else move airplane1 down & airplane2 up

Alice: Events Alice Chapter 7 September 20, 2005

Yesterday We looked at adding new functions  Remember, in Alice functions have return values We looked at doing a few slightly complicated statements within Alice We finished with a function and a method  isTooClose - boolean function  avoidCollision - method Let’s make sure we all “got” those

isTooClose Need to know: aircraft1, aircraft2, minimumAllowedDistance We will return a Boolean (true/false) answer Here we need an if/else control structure  Check if absolute value of aircraft1.distanceAbove(aircraft2) < minimumAllowedDistance if so, return true, else return false

avoidCollision Then the world.avoidCollision method could be called if the airplanes were too close Would need to check which airplane was above the other  If airplane1 above, move airplane1 up & move airplane2 down  Else move airplane1 down & airplane2 up

Some Cool Things RandomNumber (World Function>Random)  Returns a fractional value between 0 and 1  More button allows you to choose minimum and maximum for randoms  More again allows you to specify only integer values  Random Numbers allow you to create random motion in an animation OrientTo method - to have two objects move together, first you must synchronize their orientation Vehicle property - one object can “ride” on another object  More lets you set duration, style, and asSeenBy  You can use an object as the vehicle for the camera Making Objects Invisible  Use the opacity property or the isShowing property to change what is visible in the world

Checkpoint Are we okay with everything thus far? In Edit>Preferences, choose the General tab and switch the display to “Java Style in Color”  This will get you used to seeing things as they are in Java.  You need to restart Alice to make this change take effect. How many of you have done Tutorials 2 & 3?  If you can (if you have access to a PC), do these tonight if you haven’t already done them

Interactive Programming So far we have been writing sequential, non- interactive events  Before run-time we knew the exact sequence of what was going to happen in the animation With events, we no longer know exactly what sequence of events will happen  We need to link events to a responding event handling method All events are world-level because the world is listening for any event to happen

Initial Scene Let’s start with our airplane world from yesterday and get rid of one of the flying vehicles We will make an interactive flying simulation today

Keyboard Control We use keyboard commands (instructions) to control what events happen Designing a flying game - we may want  The spacebar to make the plan spin  The up arrow to move the plane forward  And so on

What to do 1: Create Methods First write the methods that will respond to events  These can be world-level or class-level methods, depending on what makes sense in the given situation biplane.flyForward  Move forward 2 meters for 2.5 seconds abruptly Biplane.barrel  Roll right 1 revolution abruptly for 2.5 seconds

What to do 2: Link Events Linking events  In the upper right corner is the “Events editor” window  You can “create new event” and select event type from popup menu “when a key is typed”  You will see “any key” and “nothing” as placeholders for what should happen Lets create our first event:  When up is typed  Do biplane --> flyForward Our second event”  When space is typed  Do biplane --> barrel

What to do 3: Test Run the program and make sure nothing happens until up arrow or spacebar are pressed But you should also make sure that the event handling methods work as you expected after you develop them

Events & Parameters What if we want the mouse to control what happens - like if the mouse clicks on an object, we want that object to do something  Obviously, we need parameters! Let’s say we were building a rescue mission with a firetruck and a burning building with a person stuck on each of the three floors  We want the user to click on a person and then have the firetruck rescue that person  The save person method would need to know whichFloor the person was on, whichPerson to save, and howFar to move the ladder up

Burning Buildings First we make a method, firetruck.savePerson with 3 parameters  whichFloor (so we know where to point the swivel to)  howFar (so we know how far to move the ladder forward)  whichPerson (so we know which person to move down the ladder) So we could create a new mouse event that when a user clicks on randomGirl3 (who lives on the 3rd floor), we would automatically be able to set the other parameters to call savePerson with:  firetruck.savePerson(whichFloor = building.thirdFloor, whichPerson = randomGirl3, howFar = 3) Testing - make sure to try all the possible combinations to make sure that the program works as expected

Other Alice Controls The While Loop is a conditional loop, which only perform what is inside if a given condition is true The Begin-During-End event  While: The conditional expression/function  Begin: Done once, at the time the condition is true  During: Put in the instructions or method that is done repeatedly as long as condition remains true  End: Done once, at the time the condition becomes false Dummy Objects - Can be used to mark locations where the camera will be used  Could set up a “frontView” camera and a “backView” camera and change the point of view to these dummy views

The Wrath of Zeus Demonstration of an event-driven world Was going to be the basis of your project, but several components of the environment were missing in this version of Alice  Still it shows the beginning of the scope of the project you should be doing

Alice Project We have learned most of the fundamental concepts available in Alice for programming  (Just not: infinite loops, lists, recursion, arrays, advanced use of variables) Now we are going to put it all together to make a big program  See handout Note: We have a quiz scheduled for this week - it’ll be on the concepts we’ve covered so far and will be on Monday

Project Work Wednesday - Ideas due Thursday & Friday: Class time on project Friday: Brief quiz review