Title: LEARN TO PROGRAM WITH ALICE
1LEARN TO PROGRAM WITH ALICE
- Xiannong Meng
- Computer Science Department
- Bucknell University
2Lecture Five
- Inheritance and Interactive Programming
3The Example of Ice-Skater
- Demonstrate the example of Ice-Skater
- The move is pretty complicated that it takes
effort to create an object of this kind
4A Question to Ask
- Question
- How do we use the skate method when we have an
ice skater object in a different world? - Answer
- Save out the ice skater object as a new kind of
character model. Then, the new kind of object can
be used in other worlds.
5Demo Creating a character model
Rename object. Save object as a new kind of
character. Characters in Alice are saved as
.a2c rather than .a2w
6Creating a new character
- Give the model a new name (why?).
7Inheritance
- The new ice skater, CleverSkater
- inherits all the properties and methods from the
original IceSkater model (class) - has its own method, CleverSkater.skate
- In other programming languages, the concept of
creating a new class based on a previously
defined class is called inheritance.
8Using CleverSkater
- CleverSkater can be added to a new world, the
same as any other object.
9Benefits of Inheritance
- Inheritance supports
- the reuse of program code -- allows the
programmer to write code once and use it again
later in different programs. - sharing code with others in a team project
10Guidelines
- Character-level methods must meet the following
guidelines - no references to other objects
- no calls to world-level methods
- no sounds played unless the sounds were imported
as part of the character - If these guidelines are not met, adding the saved
character to a new world may cause problems.
11What is wrong with this code?
12Find the problem in this code..
13Problem
- What if you are convinced that you need to write
a character-level method where another object is
involved? - Example
- We want to write a method to make the skater
skate around another object, in this case the
penguin.
14Solution
- One solution is to write a method that accepts
an object parameter, specifying the object. - SkateAroundDemo
15Interactive Programming
16Control of flow
- Control of flow -- how the sequence of actions in
a program is controlled. - What action happens first, what happens next, and
then what happensand so on. - In movie-style programs (chapters 1-4) the
sequence of actions is determined by the
programmer - Creating a storyboard design
- Writing program methods to carry out the designed
sequence
17Interactive Animations
- In interactive programs, the sequence of actions
is determined at runtime when the user provides
input - clicks the mouse
- presses a key on the keyboard
- Other sources of input are possible, depending on
your computer system.
18Interactive games
- In a video game where the user is guiding a
spaceship, the sequence of actions in the game
depends on what direction the user guides the
ship and how fast the user presses the controls. - Each time the program runs, user input may cause
a different sequence of actions from previous
executions of the program. - In essence, control of flow is now in the hands
of the user!
19Events
- Each time the user provides some sort of input,
we say an event is generated. - An event is something that happens
20Event-handlers
- An event may
- Trigger a response, or
- Move objects into positions that create some
condition (e.g., a collision) that triggers a
response. - A method is invoked to carry out the response. We
call this kind of method an event-handler. - When an event is linked to an event-handler, a
behavior is created.
21How does this effect your program?
- The goal is to write your own interactive
programs. - The approach will be the same as before, but the
difference is that we must now be concerned with
behaviors - input from the user (events)
- how objects respond to an event (event-handlers)
22Example
- Build an air show flight simulator. In an air
show, the pilot uses biplane controls to perform
acrobatic stunts.
23Problem
- The whole purpose of a flight simulator is to
allow the user to control the flight path. - The problem is how do we write our program code
to provide a guidance system allowing the user to
be the pilot?
24Solution
- Use keyboard input
- Up-arrow key to move the biplane forward
- Space to make the biplane do a barrel turn
- (Note other sets of keys could be used, we
just arbitrarily picked a couple of keys on the
keyboard.) - Write event-handler methods that respond to each
key press
25Storyboards
- Since two keys are used, two events are possible
so two storyboards are needed - Each storyboard outlines an event-handler
that responds to a particular event.
Event Space press Response Simultaneously
roll biplane a full revolution play
biplane engine sound
Event Up Arrow key press Response
Simultaneously move biplane forward play
biplane engine sound
26Demo
- A demonstration of building the biplane acrobatic
simulation
27Biplane.flyForward method
28Biplane.barrel method
29Events Editor
- Once the event-handler methods are written, each
method must be linked to an event.
30A Greek Tragedy
- Zeus was a powerful god in Greek mythology. When
Zeus was angry, he would shoot a thunderbolt out
of the heavens to strike anyone who got in the
way.
31Mouse-click events
- We want to create an interactive animation that
simulates an ancient Greek tragedy. - The user will choose the philosopher that will be
the next victim of Zeuss anger. When the user
mouse-clicks on one of the philosophers, Zeus
will point at that philosopher and a thunderbolt
will strike the object that was clicked.
32Problem
- In the initial scene, four philosophers
(Euripides, Plato, Socrates, and Homer) are
possible targets for a mouse-click. - The problem is how do we write our program to
aim the thunderbolt at the selected philosopher ?
33One solution
- One possible solution to this problem is to write
four event-handlers, one for each target object.
Event Click on Euripides Responding Method
Zeus turns to point at Euripides Shoot
thunderbolt at Euripides Euripides meets a
tragic ending
Event Click on Socrates Responding Method
Zeus turns to point at Socrates Shoot
thunderbolt at Socrates Socrates meets a
tragic ending
Event Click on Plato Responding Method
Zeus turns to point at Plato Shoot
thunderbolt at Plato Plato meets a tragic
ending
Event Click on Homer Responding Method
Zeus turns to point at Homer Shoot
thunderbolt at Homer Homer meets a tragic
ending
34A Better Solution
- A better solution is to write one event-handler
method and pass in a parameter that identifies
the target object
Event Click on target object Parameter target
object Responding Method Zeus turns to
point at target object Shoot thunderbolt
at target object target object meets a
tragic ending
35Demo Zeus world
36shootBolt code (continued)
37Parameter Passing
- When linking the mouse-click event to the
shootBolt event-handler method, select object
under mouse cursor as the parameter
38move to
- Several statements in the shootBolt method use a
move to instruction - The move to instruction moves an object to a
particular position in the world. In the
example above, the lightening bolt is moved to
the position of the cloud.
39move to with an object parameter
- If an object parameter is used to specify a
location, the process of creating the move to
statement involves three steps - Use default parameter
- Drag in an arbitrary object as the parameter
- Substitute the object parameter name
40Testing
- When parameters are used in interactive
programming, it is especially important to test
run the animation several times, each time
creating different events to be sure all possible
parameter values work as expected. - It is also important to try things that shouldnt
work.
41Demo
- Test run of Zeus world.
- What happens if you click on each philosopher,
one at a time? - What happens if you click on a column in the
scene, instead of a philosopher?
42Exercise of the Day
- You are asked to work on a revised version of
Problem 3 on page 145 of the textbook (Robot
Remote Control). See Web page for details. - Turn in an electronic copy to Alexis and a
printed copy to the instructor on Friday,
November 11th by noon.