15-101 built-in functions & arithmetic expressions

23
15-101 Built-in Functions & Arithmetic Expressions

Upload: cathal

Post on 24-Feb-2016

43 views

Category:

Documents


0 download

DESCRIPTION

15-101 Built-in Functions & Arithmetic Expressions. Recall :. A curupira is playing soccer in the rainforest. The curupira has a soccer ball and will kick the soccer ball toward a pond in the forest. Each kick causes the soccer ball to move forward a random distance . - PowerPoint PPT Presentation

TRANSCRIPT

Programming: Putting Together the Pieces Built-in Questions and Expressions

15-101Built-in Functions & Arithmetic Expressions1A curupira is playing soccer in the rainforest. The curupira has a soccer ball and will kick the soccer ball toward a pond in the forest. Each kick causes the soccer ball to move forward a random distance.

If the curupira gets the soccer ball in the water, the curupira scores a point and does a dance, or else he disappears in disgust

Recall:22The Curupira (Portuguese pronunciation:[kuupi]) is a mythological creature of Brazilian folklore. This creature blends many features of West African and European fairies but was usually regarded as a demonic figure. His name comes from the Tupi language kuru'pir, meaning "covered in blisters". According to the cultural legends, this creature has bright red/orange hair, and resembles a man or a dwarf, but its feet are turned backwards. -- http://en.wikipedia.org/wiki/CurupiraTo startDownload and open the world you were working on from last session:- or -

CurupiraSoccerRunToObject.a3p

3Where we left offWorking on running to the soccer ball

Do in ordercurupira runs to soccer ballcurupira kicks the soccer ball a random distanceIf soccer ball is in the water, score 1 pointcurupira danceselsecurupira disappears 4Our implementationCurupira moves forward, but how far?How can we be sure that 1 meter is the correct distance?

5Curupira does not run far enough

If video does not play, see Resources: Videos: 04-3PresentationVideos: video_01.mov.mp4

6In the PenguinBowling project, the penguin was positioned exactly 1 m from the pen a known distance.pushObject procedure moves this Penguin object forward 1 meter to collide with the target object

In the CurupiraSoccer project, the distance between the soccer ball and the curupira is unknown.

Known vs Unknown Distance

7Known distance was convenient for a first project. But, in many animations the objects are moving around and the distance between objects is often unknown at runtime.FunctionsA function asks a question (to check a condition or compute a value).

In Alice, a function is used to get the information we need concerning

the properties of objects Is the water blue?

the relationship of one object to another What is the distance between the curupira and the soccer ball?

8

Alice 3 Methods PanelPerforms an action; does NOT compute and return a valueComputes and returns a value; does NOT perform an actionSets or gets a value that is stored in a property variable.9Return a value of a specific data typeBooleanDecimal NumberJointsStringBuilt-in functional methods

10Calling a functional methodWe use the getDistanceTo function to determine the distance between the curupira and the

11Calling a functional methodVideo demonstration of adding getDistanceToTile

If video does not play, see Resources: Videos: 04-3PresentationVideos: video_02.mov

12Calling a functional methodA call to a function is embedded within a larger statement (that is, a call to a function is NOT a syntactically complete statement)Example: Calling getDistanceTo The return value (distance value returned by the function) is used as an argument in the move statement

13Problem: CollisionWhen our program is run, the curupira seems to move through the ball too much collision.

If video does not play, see Resources: Videos: 04-3PresentationVideos: video_03.mov

14

The problem is that distance between two objects is measured center-to-center.

One way to solve this problem is to subtract a small number from the distance.

12/18/091515Arithmetic ExpressionsTraditional math and formula computations are implemented as arithmetic expressions in a computer program.

An arithmetic expression is a combination of operators and operands evaluates to a numeric value12/18/0916

A numeric operand can be either A whole number (integer)

Or a floating pt number (decimal number).

12/18/0917

Arithmetic operators are symbols that can easily be typed on a keyboard

Addition+Subtraction-Multiplication*Division/Remainder (mod)%12/18/0918

Arithmetic expressions in AliceClick pull-down menu arrow for getDistanceTo and select MathBuild expression from the cascading menus.

12/18/0919Arithmetic expressions in AliceVideo demonstration of building an arithmetic expression

If video does not play, see Resources: Videos: 04-3PresentationVideos: video_04.mov

20

Binary operationCascading menus assume a binary operation Operand Operator Operand

Resulting statement:

12/18/0921Complete runToBall procedure

12/18/0922Curupira does not move through the soccer ball

If video does not play, see Resources: Videos: 04-3PresentationVideos: video_05.mov

23