Alice: Functions Alice Chapter 6 September 19, 2005.

30
Alice: Functions Alice Chapter 6 September 19, 2005

Transcript of Alice: Functions Alice Chapter 6 September 19, 2005.

Page 1: Alice: Functions Alice Chapter 6 September 19, 2005.

Alice: Functions

Alice Chapter 6

September 19, 2005

Page 2: 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)

Page 3: Alice: Functions Alice Chapter 6 September 19, 2005.

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!

Page 4: Alice: Functions Alice Chapter 6 September 19, 2005.

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

Page 5: Alice: Functions Alice Chapter 6 September 19, 2005.

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?

Page 6: Alice: Functions Alice Chapter 6 September 19, 2005.

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

Page 7: Alice: Functions Alice Chapter 6 September 19, 2005.

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 <None> line at the bottom

Page 8: Alice: Functions Alice Chapter 6 September 19, 2005.

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 revolutionnumRevolutions = distance / (diameter * pi)

Page 9: Alice: Functions Alice Chapter 6 September 19, 2005.

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

Page 10: Alice: Functions Alice Chapter 6 September 19, 2005.

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

Page 11: Alice: Functions Alice Chapter 6 September 19, 2005.

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

Page 12: Alice: Functions Alice Chapter 6 September 19, 2005.

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

Page 13: Alice: Functions Alice Chapter 6 September 19, 2005.

Alice: Events

Alice Chapter 7

September 20, 2005

Page 14: Alice: Functions Alice Chapter 6 September 19, 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

Page 15: Alice: Functions Alice Chapter 6 September 19, 2005.

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

Page 16: Alice: Functions Alice Chapter 6 September 19, 2005.

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

Page 17: Alice: Functions Alice Chapter 6 September 19, 2005.

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

Page 18: Alice: Functions Alice Chapter 6 September 19, 2005.

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

Page 19: Alice: Functions Alice Chapter 6 September 19, 2005.

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

Page 20: Alice: Functions Alice Chapter 6 September 19, 2005.

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

Page 21: Alice: Functions Alice Chapter 6 September 19, 2005.

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

Page 22: Alice: Functions Alice Chapter 6 September 19, 2005.

What to do1: 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

Page 23: Alice: Functions Alice Chapter 6 September 19, 2005.

What to do2: 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

Page 24: Alice: Functions Alice Chapter 6 September 19, 2005.

What to do3: 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

Page 25: Alice: Functions Alice Chapter 6 September 19, 2005.

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

Page 26: Alice: Functions Alice Chapter 6 September 19, 2005.

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

Page 27: Alice: Functions Alice Chapter 6 September 19, 2005.

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

Page 28: Alice: Functions Alice Chapter 6 September 19, 2005.

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

Page 29: Alice: Functions Alice Chapter 6 September 19, 2005.

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

Page 30: Alice: Functions Alice Chapter 6 September 19, 2005.

Project Work

• Wednesday - Ideas due

• Thursday & Friday: Class time on project

• Friday: Brief quiz review