CS105L Introduction to Game Programming Using Alice (Lab)
Fall 2009
Lab 4
Variables, Functions, Math and Strings - WebCT
Covers material from Chapter 3 in class text; Starting Out with
Alice. WebCt test submission exercise.
| Part 1 |
- Variables
- Creating a new variable
- Local, Parameter, Class, and World variables
- Setting values
|
| Part 2 |
- Functions
- World primitive functions
- Class functions
|
| Part 3 |
|
| Part 4 |
|
Part 1 - Variables
A. Creating a new local variable, The Penguin Jumps
- From the animals gallery, add a penguin to the scene
- Select Done
- In the object tree select penguin
-
Notice the Class-Level methods for the penguin located above
the create new method box; wing_flap, jumping, and others
-
Add (drag and drop) the jumping height method to the method
editor, select a default value of 2
- Play scene
-
In the upper right hand corner of the method editor select
create new variable, enter penguinJumpHeight as the name and a
default value of 2
- THIS IS A LOCAL VARIABLE. What is the variable local to?
- It is local to the world.my first method method.
-
Select and drag the penguinJumpHeight variable into the
penguin.jumping statement, place it over the height = 1
section of the statement
-
Notice that the height = 1 section becomes outlined in yellow
indicating that the variable may be placed there.
-
Play the scene. Notice that the scene remains the same as
before.
-
Mimic the previous steps to add penguin.jump method, create
the penguinJumpTimes variable and add the new variable to the
jump statement
-
Play the scene
B. Parameter variable, The Penguin Jumps Higher
-
In the object tree select the penguin
-
In the penguin's details window select the edit button next to
the jump method. The penguin.jump method should be displayed
in the method editor.
-
Notice that the penguin.jump method calls two other penguin
methods; penguin.wing_flap and penguin.jumping. Also note
that the local variables to the my first method created in
part A do not appear in the penguin.jump method. Why is that?
-
The variable times appears in the parameters section at the
top of the method editor. THIS IS A PARAMETER VARIABLE.
Remember that a parameter variable holds an argument that is
passed to a called method. The my first method method calls
the penguin.jump method passing the penguinJumpTimes variable
as an argument. Penguin.jump holds this argument in the times
parameter.
-
Play the scene, notice that the penguin jumps 2 meters on the
first jump and 1 meter on the next two jumps. Looking at the
penguin.jump method in the editor you will see that the height
parameter is set to 1 meter. Let's make this a changeable
value when the jump method is called.
-
In the penguin.jump method select the create new parameter
box. Enter height as the name.
-
Drag the height parameter to the height field of the jumping
method call.
-
Select the world.my first method tab at the top of the editor
-
Notice that a height = 1 field has been added to the
penguin.jump method call
-
Drag the penguinJumpHeight variable to the height field of the
penguin.jump method.
-
Play the scene
C. Class variable, Too Many Penguins
-
From the gallery add a second penguin to the scene, select
Done
-
Add the penguin2.jumping height = 3 and penguin2jump times=3
methods to the editor
-
Create a penguin2JumpHeight variable = 3 and add the variable
to the method
-
NOTE: At this point you should start to realize that the names
of the variables look nearly the same. If we had a troop of
fifty penguins this could get really confusing. The variables
we added are concerned only with a specific object, but we
created them as local method variables. Let's correct this
issue.
-
In the object tree select penguin
-
Select the properties tab in the detail window
-
Select create new variable, enter height as the name
-
Select create new variable enter times as the name
-
THESE ARE CLASS VARIABLES for the penguin object
-
Select the my first world method in the editor
-
Drag the height class variable into the height fields of the
penguin.jumping and penguin.jump methods
-
Drag the times class variable into the times field of the
penguin.jump method
-
Delete the penguinJumpHeight, penguinJumpTimes, and
penguin2JumpHeight local variables
-
Repeat the previous three steps for penguin2 and its methods
-
Play the scene
-
Note that if you select the world or camera objects in the
object tree that the new variables do not appear in the
object's properties. Why is that?
D. World variable, Everybody Jumps
-
Select world in the object tree
-
Select properties tab in the details window
-
Select create new variable with the name height and a value of
5
-
THIS IS A WORLD VARIABLE (sometimes called global)
-
In the my first method create a Do together block containing
the penguin.jump and penguin2.jumping methods. For the values
select expression > penguin.times > penguin.height for penguin
and expression > penguin2.times for penguin2
-
Edit the penguin2.jump method. In the object tree select
penguin2. Select the edit button beside the jump method in the
penguin2 details window.
-
Drag the world.height variable to the height field of the
penguin2.jumping method.
-
Play the scene
-
Notice that the world.height variable can be assigned as an
argument to multiple objects and in different methods. It's
global.
E. Setting values, That Penguin Can't Jump
-
Select the my first method tab
-
In the world's details window select the properties tab and
drag the height variable onto the method editor. Set the
value to 0.1
-
Select the world.height set statement just created to a point
above the Do together block as indicated by the bright green
line.
-
Can you predict how high the two penguins will jump when they
jump together at the end of the scene? Will they jump the
same height? Trace the values from the my first method method
to the penguin.jump and penguin2.jump methods.
-
Play the scene.
-
The set value can also be used for properties.
-
Add a statement to have penguin ask if he should disappear
-
Select the properties tab in the penguin's detail window.
-
Drag the isShowing property to the editor and set its value to
true
-
THIS IS A BOOLEEN VARIABLE TYPE
-
Add the world.ask user for yes or no function to the true
field of the isShowing set statement.
-
Drag the world.not a function to the world.ask user for yes or
no field.
-
Play the scene.
-
Why did the penguin disappear when Yes was selected?
-
SAVE YOUR WORK!
Part 2 - Functions
A. World primitive functions, A personalized hello
-
Open a new world. Under the File tab, select New world. Save the
new world as Lab4-2a.
-
Add a penguin to the scene, select Done.
-
Note that the penguin.jump method is in its original form, the
change made in the previous exercise does not persist into the
new world
-
Select penguin
-
Drag the penguin say method set to What is your name
-
create inputName variable
-
Drag inputName to editor set value to default string
-
Select world function tab
-
Drag ask user for string to the default string field of the
inputName set statement and set the value to What is your name.
You can use the other option to enter a string of your choosing.
-
Select okay until the pop-up box goes away
-
Select penguin methods tab
-
Drag penguin say into editor set value to hello
-
Drag penguin say into editor set value to inputName
-
Play scene
-
SAVE YOUR WORK, you will reuse the file below.
Class functions
-
Save the current world as Lab4-2b.
-
Add a comment explaining the next block of code
-
Add the penguin move up method to the program set value to 2
-
Create a number variable hgt and a string variable height
-
Add a statement to set height to the penguins distance above
the ground.
-
Drag height to the editor set value to default string
-
Select world functions tab
-
Drag what as a string to the
default string field in the set height statement
-
Drag hgt to the what field of the above statement
-
Add a statement to have the penguin say I am
-
Add a statement to have the penguin say his height above the
ground.
-
Drag the penguin say method to the editor set the value to
default string
-
Drag the height variable to the default string field
-
Add a statement for the penguin to say meters above the ground
-
Play the scene
-
Add a penguin move statement set value to 1 meter. Select the
penguin functions tab
-
Drag the penguin distance to function to the 1 meter field of
the penguin move statement. Set value to ground object
-
Play scene
-
Why is the penguin halfway merged with the ground? Hint:
remember object center points. Where is the center point of
the penguin?
-
SAVE YOUR WORK!
Part 3 - Math
A. Using equations, Save the Penguin
-
We left our poor penguin trapped halfway into the ground at the
end of the last section. We need to put him back on his feet
properly.
-
Disable or delete all statements above the class comment added
in the last section
-
Add a comment to the bottom of the code explaining that the
next block of code is math function related
-
Calculate the distance the Penguin needs to move up to be back
on the ground
-
Add a statement to move the penguin up 0.5 meters.
-
Select the drop down arrow next to the 0.5. select math > 0.5
* > 1 this means move a distance equal to .5 * 1 meters
-
Select the penguin functions tab
-
Drag the penguin's height function to the 1 field in the
penguin move statement
-
Play the scene
-
You may notice that the penguin seems to be a little above the
ground. This is a glitch. How would you verify this? How
would you correct this?
-
SAVE YOUR WORK!
B. On your own, Touch the ball
-
Open a new world. Save as Lab4-3b.
-
Scene:
-
A penguin is a few meters or so from the ball.
-
The penguin walks to the ball so that his side is just
touching the ball.
-
There is a penguin walking x method.
-
Hints:
-
See page 133 of the Gaddis book.
-
Distances are measured from the CENTER not the Edge of an
object.
-
Resize the ball to 3 times its normal size so that it is
the same height as the penguin.
C. Extra at home work (not in lab), Fix the monkey
Go back to lab 3 and use the above techniques
to have the monkey's outstretched foot actually touch the ball and not
go into the ball. Hint: The monkey's foot is an object with its own
center point.
Part 4 - Strings
Concatenation
In section 2B of this lab we converted a number to a string
so that the penguin could say the number. The multiple
statements were used to have the penguin say multiple
statements in Part 2. We need to correct this for a
smoother animation. The combining of two or more strings is
called string concatenation.
-
Open the lab from part 2a above - Lab4-2a
-
In the functions tab of the world object find the string
function a joined with b.
-
Drag this function to the penguin say hello statement.
Place the function over the hello field. Select expression >
inputName
-
Delete the penguin say inputName statement
-
Play the scene
-
Note the text runs together. How would you fix this? There
are two ways.
-
First way: Drag the string a joined with b function to the
hello field. Select a value of other and enter a single
space. This makes a “nested” statement; one statement is
nested inside another.
-
Second way: Use the drop down arrow beside hello to select
other and enter hello followed by a single space.