Chapter 2 part 1

advertisement
Programming in Alice
Chapter 2
Today’s Agenda





Designing a Program
Writing Methods
Executing Instructions Simultaneously
Comments
Tips for Setting Up an Initial Scene
2-2
Steps in Creating Programs
Four step process
1.
2.
3.
4.
What is problem?
Design or plan ahead
Implement or write the program
Test to see if it works
2-3
Scenario



Problem statement or story
Also called requirement specifications
In Alice consists of
o
o
o
World scene
Objects
Actions
2-4
Scenario Provides Answers To


What story is to be told?
What objects are needed?
o
o

Primary objects
Background objects
What actions are to take place?
o
Become instructions in program
2-5
Scenario: First Encounter







After traveling through space, a robot-manned craft
just landed on a moon
The robot sets up a camera so scientists in Houston
can view this historic event
The camera view shows the robot, lunar lander and
nearby rocks
An alien surprises the robot when it peeks out from
behind a rock
The robot sees the alien and walks over for a closer
look
The alien is frightened and hides behind rocks
The robot calls Houston to report alien
2-6
Step 1: What is Problem?

What story will be told?
o

What objects are used?
o

A robot’s first encounter with an alien on a distant moon
Robot, lunar lander, alien, lunar background
What actions take place?
o
o
o
o
Alien peeks out from behind rock
Robot turns head around & moves toward alien
Alien hides behind rocks
Robot sends message back to earth
2-7
Pseudo Code







List of English worded steps to perform a task
Provides algorithmic structure
Breaks problem down into smaller tasks
List all things that will be done in a scene
You can also make basic drawing to better
illustrate scene you wish to create
Book refers to pseudo code
Algorithm will be used in this class since it is a
more familiar word and has similar meaning
2-8
Example Sketch For Scenario
2-9
Scenario Scene

Not intended to be artistic
o
o

Can use simple circles, squares and lines
Label color of shapes
Just give an idea of what scene will look like
2-10
Create Initial World



Use Alice scene editor to add objects
Arrange objects in various poses
Capture screen as each successive scene is
created for storyboard
2-11
Algorithm for First Encounter Scenario












Do following steps in order
Alien moves up
Alien says "Slithy toves?"
Robot's head turns around
Entire Robot turns to look at alien
Do together
Robot moves toward alien
Robot legs walk
Alien moves down
Robot turns to look at camera
Robot’s head turns red to signal danger
Robot says “Houston, we have a problem!”
2-12
Step 3: Algorithm Implementation


Translate algorithm steps into program
Program
o

A list of instructions to have objects perform
animation actions from algorithm
When programming you need to be concerned
with
o
Syntax – statement structure and punctuation


o
o
Alice takes care of this for you
You drag instruction to method editor
Semantics – meaning of statements
Sequence – order of programming instructions
2-13
Translating the Design

Some algorithm steps can be written as a single
instruction
o

Ex: robot turns to face alien
Other steps are composite actions that require
more than one instruction
o
To make the robot legs walk you need more than one
instruction to cause multiple legs to move forward and
bend at a joint
2-14
Writing Methods
•
What is a method?
o
o
o
o
Set of instructions
Create methods by
dragging tiles from
details pane into the
Method Editor
These tiles are the
instructions
Method Editor not
available in Scene
Editor mode
my first method
Method Editor
2-15
Dragging Method Tile to Editor
•
•
•
To place instruction in method editor drag from details pane
Pop-up menu appears asking for direction, select one
Pop-up menu then asks for amount of distance to move
o
Example: move instruction

Arguments used in example


Distance amount (use predefined values or other (enter value with keyboard))
Direction
Drag method to editor
2-16
Method with Instruction Tiles
•
Methods execute the instructions (tiles) that are
dragged into the Method Editor
2-17
Terminology
•
Calling a method - executing a method
Parameter – tells method more information on what to do
•
Argument – information sent to method
•
•
For method hare.move what direction to move?
•
•
•
Possibilities for direction parameter are up, down, left, right
How far to move is another argument to send move method
Passing arguments
•
A method is called with arguments sent to its parameters
argument
object
method name
argument
editing tag
2-18
Entering Custom Number
•
•
When other is chosen
A number pad pops up
o
o
Can enter number with
keyboard
Click on number pad

o
o
Does not always work
Click Okay or Cancel when
done
‘/’ is for fractions instead
of manually calculating it
2-19
Editing Tag
•
•
Holds optional arguments
What appears depends on the method
•
Ex: Duration - amount of time for action to take place
•
Default - 1 second
2-20
Duration Argument

Each instruction takes 1 second to execute by default
Can change time with duration option

Increase or decrease duration for realism

o
o
o

Increase to show movement over long distance and time
Decrease to speed up action
Synchronize objects together
Example: Make leg move in sync with lunar robot
o
o
o
Lunar robot moved 1 sec at time
Each leg needed to move forward and backward in 1 sec
Therefore move forward ½ sec and backward ½ sec
2-21
Style Argument

Specifies how smoothly one movement will
blend into next movement
o
o
Gently – begins and end gradually
Abruptly – begins and ends instantly

o
o
Sometimes needed for smooth transition since gently
shows what appears to be an abrupt end and beginning
Begin gently – begins gradually, ends instantly
End gently – begins instantly, ends gradually
2-22
Instructions (Methods)

Methods available to use are:
o
Move – forward, backward, up, down, left, right

o
o
o
o
o
In meters
Turn – left, right, forward, backward (revolutions)
Roll – left, right (revolutions)
Resize – increase or decrease size
Say – put thought bubble over object
Turn to face – select who to turn to face
2-23
Primitive Methods
 All objects have common set of



built-in methods for performing
actions
These “primitive methods” get
objects to move, turn, change
size, etc
Seen in methods tab of details
pane
See list in Table 2-1 (pp. 65-66)
with explanation of each one
dot notation: hare.move
2-24
Control Statements




Determines the sequence
instructions are executed
Drag instruction to code editor
Sequential Action Block
o Actions occur one after
another
Simultaneous Action Block
o Actions occur at same time
Drag block into editor
Do in order
2-25
Do in Order


By default, instructions
are executed one after
the other in the order
they appear in the
Method Editor
It is best to place
instructions in “Do in
order” block
o
Will be easier to move
group of instructions if
needed later
2-26
2-26
Do Together
•
•
•
For simultaneous actions, drag
“Do together” into the Method
Editor
Place instructions to be executed
simultaneously in “Do together”
block
Make sure the duration for all
items in “Do together” are same
•
Otherwise some will finish quicker
than others
2-27
2-27
Nesting
•
•
•
•
Sometimes two groups of
instructions need to be executed
simultaneously
Done by placing a “Do in order”
block within a “Do together” block
Example has brother penguin turn
head right and left at same time as
sister penguin does
If head was turned left and right
together nothing would happen
•
Head would turn right and left at exact
same time
2-28
Create Program

Choose which sequence to execute instructions
o
o

At same time
Consecutively
Refer to algorithm to find instruction order
2-29
FirstEncounter Program
2-30
Demonstrate Program

Demonstrate creating FirstEncounter
2-31
Step 4: Testing

Important step in creating a program is to run it
o

To be sure it does what you expect it to do
Use an incremental development process
o
o
o
o
Write a few lines of code then run it
Write a few more lines and run it
Write a few more lines and run it
Allows finding many problems and fix them as you go

Called debugging – find bugs or errors in program
2-32
Comments




Make the code understandable
Not all combination of instructions are immediately
obvious
Explains purpose of segment of program to human
reader
Helps
o
o

You read program later
Others when determining what program does
Alice ignores comments when executing program
2-33
Comment Example
Comment
//comment tile
2-34
Tips for Setting Up Initial Scene

Can set initial position of
objects in scene by
o
o
Using mouse mode buttons
from chapter 1
Right clicking object in
object tree and move
mouse of methods
2-35
Moving Object to Center of World
•
•
Use move to method and
choose entire world as
method’s argument
Places object’s center
point at world’s center
point of (0, 0, 0)
o
o
•
This drives hare’s lower
body into ground
Center of gravity of hare is
in abdomen
Useful when lose object in
scene or to set starting
point
2-36
Positioning Character’s Arms
2-37
Tips for Setting Up Initial Scene
•
•
Positioning objects a specified distance
apart
Ex: position teapot’s center point ½
meter to right of plate’s center point
o
o
o
Move teapot object so its center point is same
as plate’s center point
Set pointOfView in properties of both objects
world’s center point of (0, 0, 0)
Use move method by right clicking on teapot
then method then choose direction of right
then ½ meter under amount
2-38
Moving Camera To An Object
•
Right-click the object
and choose Camera
get a good look at
this
•
Camera moves to a
position so the object
is in plain view
2-39
Homework


Read chapter 2
Do tutorials
o
o
o
o
o



Tutorials
Tutorials
Tutorials
Tutorials
Tutorials
2-1,
2-2,
2-3,
2-4,
2-5,
p 62
p. 67
p. 82
p. 89
p. 98
Answer questions in handout
Do lab assignments after handing in answered
questions
Due one week after assigned with 1 week grace
1-40
Download