41
Programming in Alice Chapter 2

Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

  • Upload
    dangnga

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

Programming in Alice

Chapter 2

Page 2: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

Today’s Agenda

Designing a Program

Writing Methods

Executing Instructions Simultaneously

Comments

Tips for Setting Up an Initial Scene

2-2

Page 3: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

Steps in Creating Programs

Four step process

1. What is problem?

2. Design or plan ahead

3. Implement or write the program

4. Test to see if it works

2-3

Page 4: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

Scenario

Problem statement or story

o Also called requirement specifications

Scenario in Alice consists of

o World scene

o Objects

o Actions

2-4

Page 5: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

Scenario Provides Answers To

What story is to be told?

What objects are needed?

o Primary objects

o Background objects

What actions are to take place?

o Become instructions in program

2-5

Page 6: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

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

Page 7: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

Step 1: What is Problem?

What story will be told?

o A robot’s first encounter with an alien on a distant moon

What objects are used?

o Robot, lunar lander, alien, lunar background

What actions take place?

o Alien peeks out from behind rock

o Robot turns head around & moves toward alien

o Alien hides behind rocks

o Robot sends message back to earth

2-7

Page 8: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

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

Page 9: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

Example Sketch For Scenario

2-9

Page 10: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

Scenario Scene

Not intended to be artistic

o Can use simple circles, squares and lines

o Label color of shapes

Just give an idea of what scene will look like

2-10

Page 11: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

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

Page 12: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

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

Page 13: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

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

Alice takes care of this for you

You drag instruction to method editor

o Semantics – meaning of statements

o Sequence – order of programming instructions

2-13

Page 14: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

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

Page 15: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

Writing Methods

• What is a method?

o Set of instructions

o Create methods by dragging tiles from details pane into the Method Editor

o These tiles are the instructions

o Method Editor not available in Scene Editor mode

2-15

Method Editor

my first method

Page 16: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

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

Amount to move (use predefined values or other (enter value with keyboard))

Direction

Drag method to editor

2-16

Page 17: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

Method with Instruction Tiles

• Methods execute the instructions (tiles) that are dragged into the Method Editor

2-17

Page 18: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

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 below

• What direction to move?

• Possibilities: up, down, left, right, forward and backward

• How far to move is another argument to send move method

• Passing arguments

• A method is called with arguments sent to its parameters

2-18

object

argument argument

editing tagmethod name

Page 19: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

Entering Custom Number

• When other is chosen for amount to move

• A number pad pops up

o Can enter number with keyboard

o Click on number pad

Does not always work

o Click Okay or Cancel when done

o ‘/’ is for fractions instead of manually calculating it

2-19

Page 20: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

Editing Tag

• Holds optional arguments

• Options available depend on the method

• Ex: Duration - amount of time for action to take place

• Default - 1 second

2-20

Page 21: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

Duration Argument

Each instruction takes 1 second to execute by default

Change time with duration option

Increase or decrease duration for realism

o Increase to show movement over long distance and time

o Decrease to speed up action

o Synchronize objects together

Example: Make leg move in sync with lunar robot

o Lunar robot moved 1 sec at time

o Each leg needed to move forward and backward in 1 sec

o Therefore move forward ½ sec and backward ½ sec

2-21

Page 22: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

Style Argument

Specifies how smoothly one movement will blend into next movement

o Gently – begins and ends gradually

o Abruptly – begins and ends instantly

Sometimes needed for smooth transition since gently shows what appears to be an abrupt end and beginning

o Begin gently – begins gradually, ends instantly

o End gently – begins instantly, ends gradually

2-22

Page 23: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

Instructions (Methods)

Methods available to use are:

o Move – forward, backward, up, down, left, right

In meters

o Turn – left, right, forward, backward (revolutions)

o Roll – left, right (revolutions)

o Resize – increase or decrease size

o Say – put thought bubble over object

o Turn to face – select who to turn to face

2-23

Page 24: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

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

2-24

dot notation: hare.move

Page 25: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

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

Page 26: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

Do in Order

By default, instructions are executed in the order they appear in the Method Editor one after the other.

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

Page 27: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

Do Together

• For simultaneous actions, drag“Do together” into the MethodEditor

• Place instructions to be executed simultaneously in “Do together”block

• Make sure the duration for allitems in “Do together” are same

• Otherwise some will finish quicker than others

2-27

2-27

Page 28: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

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 timeas sister penguin does

• If one peguin’s head was turned left and right within one Do together nothing would happen

• Head would turn right and left at exact same time

2-28

Page 29: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

Create Program

Choose in what sequence to execute instructions

o At same time

o Consecutively

Refer to algorithm to find instruction order

2-29

Page 30: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

First Encounter Program

2-30

Page 31: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

Demonstrate Program

Demonstrate creating FirstEncounter

2-31

Page 32: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

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 Write a few lines of code then run it

o Write a few more lines and run it

o Write a few more lines and run it

o Allows finding many problems and fix them as you go

Called debugging – find bugs or errors in program

2-32

Page 33: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

Comments

Make the code understandable

Not all combination of instructions are immediately obvious

Explains purpose of segment of program to human reader

Helps

o You read program later

o Others when determining what program does

Alice ignores comments when executing program

2-33

Page 34: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

Comment Example

2-34

//comment tile

Comment

Page 35: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

Tips for Setting Up Initial Scene

2-35

Can set initial position of objects in scene by

o Using mouse mode buttons from chapter 1

o Right clicking object in object tree and move mouse to methods

o Then move mouse to method/action you would like to perform

o This has immediate effect

When methods are placed in method editor will not execute until Play is clicked on

Page 36: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

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 This drives hare’s lower body into ground

o Center of gravity of hare is in abdomen

Useful when lose object in scene or to set starting point

2-36

Page 37: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

Positioning Character’s Arms

2-37

Page 38: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

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 Move teapot object so its center point is same as plate’s center point

o Set pointOfView in properties of both objects world’s center point of (0, 0, 0)

o Use move method by right clicking on teapot then method then choose direction of rightthen ½ meter under amount

2-38

Page 39: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

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

Page 40: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

Homework

Read chapter 2

Do tutorials

o Tutorials 2-1, p 62

o Tutorials 2-2, p. 67

o Tutorials 2-3, p. 82

o Tutorials 2-4, p. 89

o Tutorials 2-5, p. 98

Do exercises in handout

Due one week after it is assigned

1-40

Page 41: Programming in Alice - Kean University | World Class …amanno/Chapter_02 Programming In Alice...Example Sketch For Scenario 2-9 Scenario Scene Not intended to be artistic o Can use

Lab

Lab exercises require you to demonstrate your program to the professor

When you write code you must explain to professor what each line of code does and why.

Professor will sign lab sheet after you do that

You only get full credit for lab exercise if professor signs lab sheet

If you hand in lab without explaining code then you will lose half credit