CS105L Introduction to Game Programming Using Alice (Lab)
Fall 2009
Lab 3
Covers material from Chapter 2 in class text - Starting Out
with Alice
| Part 1 |
- Introduction to programming in Alice
- Moving an object on the screen.
|
| Part 2 |
- Primitive method exploration
- Custom methods of the monkey:
- turn
- turn-to-face
- move-to
- move-towards
- do-together
|
| Part 3 |
Breaking an animation into scenes using world methods
|
| Part 4 |
Putting it all together exercise
|
Part 1 - Introduction to Programming in Alice
A. - Moving a ball
- Start Alice
- Click Cancel on the pop up screen
- Under the File tab,
select New world
- Select the grass template and
click Open
- Click Add Objects
- In the Sports gallery select
the toyBall1, add the Ball to the
scene, Select Done
- In the Object Tree click
on toyBall1 (upper left)
- In the Details panel (lower left), drag and
drop toyBall1 move into the
Method editor (world.my first method area). In the pop-up
window select right > 1
meter.
- In the Toolbar
click Play.
- Notice that the Ball moves to the left and back on the
screen. Close the screen.
- We need to change the direction of the movement so that the
Ball moves forward and to the left of the screen. In the
Method editor, find the line starting
with toyBall1 move... Click the
drop down arrow to the right of right and
select forward. Replay the
scene.
- Repeat the above step to add a
second toyBall1 move forward
statement to the editor.
- We only want the Ball to move forward 0.8 meters in the
second statement. Select 1 meter
> and click other. A
number pad will pop-up, select .8
and click okay.
- Play the scene.
- Notice the jerky movement of the Ball.
B. - Moving a ball with style
-
In the first statement, select
the more dropdown arrow. Two of
the options available
are duration
and style.
-
duration controls the speed
of the movement; a shorter time gives a faster movement.
-
style controls how the Ball moves
-
gently will start the
movement gently and accelerate and then decelerate to
a gentle stop (gentle at both ends),
-
abruptly will move at a
constant speed
-
begin gently begins
gentle and ends abrupt
-
end gently begins
abruptly and ends gentle.
-
Try selecting different styles for the first statement and
observe the effect.
-
Select style = begin gently for
statement 1 and style = end
gently for statement 2.
Click Play and notice the smooth
overall movement.
-
SAVE YOUR WORK:
-
Under the File tab select
Save world.
-
In the look in box use the dropdown arrow and find
the Z: drive (this is your
network drive associated with your CS department account.)
-
Use Lab3-1 as the file
name. Click save.
C. On your own - When balls collide
-
Add a second toyBall to the scene
so that the first Ball "hits" the new Ball at the end of its
movement. The new Ball moves away from the "impact".
-
You will need to be careful of your placement of the 2nd Ball
so that it can be hit by the first Ball. The best way to do
this is to use the methods to place the Ball as talked about
in Lab 1. You also need to think of the movement styles of
the 2 Balls.
-
Placement hint:
-
Add the new Ball to the scene.
-
Use the move-to method of the
new Ball to place the Ball over the old Ball.
-
Use the move method to move
the new Ball forward 1.80 meters plus a little more for
the radius of the Balls
(move-to
and move use the center of
the Ball to measure distance).
-
SAVE YOUR WORK!
Part 2 - Primitive Method Exploration / Class-Level Custom Methods
A. Custom Methods, Methods of the Monkey
-
Under the File tab,
select New world. Save the new
world as Lab3-2.
-
Select the grass template and
click Open
-
Click Add Objects
-
In the Sports gallery select the
toyBall1, add the Ball to the
scene.
-
In the Animals gallery select the
class Monkey, add a Monkey to the
scene. Place the Monkey sort of near the Ball and facing away
from the Ball.
-
Select Done
-
Examine the Monkey Class-level Custom methods: In the Object
Tree click Monkey
-
In the Details panel notice the five methods that are listed
above the create new method box.
These are Class-level custom methods for the Monkey class.
Take a few minutes to drag and drop the methods into the
Method editor and press play to
see what they do. (You will need earphones to hear the
screech sound).
B. Primitive methods, The Monkey turns
-
Delete the statements from the editor.
-
Scene:
-
The monkey turns around 360 degrees (1 revolution)
-
The monkey says, "I think I'll play with that ball"
-
Monkey turns to ball, moves closer and kicks the ball.
-
The ball moves forward in an arc.
Steps
-
In the Object Tree click Monkey
-
In the Details panel drag and
drop Monkey turn into the Method
editor.
-
In the pop-up window select right > 1
revolution.
-
Select the Monkey say method,
select other, and type in the
string I think I'll play with that
ball.
-
Select the monkey turn to face
method and select toyBall1
-
Select the move to method and
select toyBall1.
-
Press Play.
-
Notice the monkey is now IN the ball. Close the play screen
-
Delete the last statement.
-
Select move toward > 1/4 meter >
toyBall1
-
In the editor, in the last statement, select the dropdown
arrow next to amount=0.25 meters
and select other. Enter a
distance to move towards the ball. If you do not know the
correct distance try a number, press play to see what it does
and repeat till the monkey is close enough to kick the ball.
-
Save your work. Press play.
C. The Monkey kicks
Monkey kicks the ball. To do this you will have to manipulate
the monkey's leg to perform a smooth kicking motion.
Steps
-
In the Object Tree expand
the monkey object (click the +
sign next to monkey)
-
Select the leftLeg object
-
Drag the leftLeg turn method to
the editor, select turn > forward .25
revolutions
-
In tree, expand leftLeg object,
select lowerLeg object
-
Drag the lowerLeg turn method to
the editor, select turn > forward .25
revolutions
-
Select leftLeg Object, drag the
leftLeg turn method to the
editor, select turn > backward .25
revolutions
-
Select lowerLeg object, drag the
lowerLeg turn method to the
editor, select turn > backward .5
revolutions
-
Press play.
-
Notice that the leg movement is not smooth. We need some
way to move the upper and lower legs at the same time.
-
Across the bottom of the editor are 7 control structures.
These can control the flow of your program. The 2nd from the
left is the Do together
structure. Program statements are performed
sequentially. Do together allows
you to perform two statements at the same time in a block.
The block is treated as a sequential statement by Alice.
-
Drag Do together into the editor
and place it above the
first leftLeg turn statement (a
green line will indicate where the statement will be placed).
-
Drag the leftLeg turn forward and
the lowerLeg turn forward
statements into the Do together
block.
-
With a 2nd Do together block
repeat the same steps for the turn backward statements.
-
Add a statement to return the lower leg to a standing position
after the kick.
-
Press play to verify your code.
D. Own your on - The ball flies
-
After the monkey kicks the ball the ball will fly in an arc
(sort of) away from the monkey. To get the ball to arc you
need to move the ball up and forward at the same time and then
down and forward at the same time.
-
Add statements and control blocks to move the ball forward in
a 1 meter high arc a distance of 2 meters.
-
Press Play.
Hints:
-
Change the styles so that the
up-forward movement is faster than the down.
-
Make sure that the durations inside a Do
together block match. What happens if they don't?
-
Play with changing the up style -
leave the forward style as
before.
-
I ended with the up-forward durations being .5 seconds and the
down-forward being 1 second (the default).
More hints:
-
The current program has the monkey ending his kick (back to
standing) before the ball moves. Change this by moving the
last statement of the Monkey kick (line 12 above) into the
ball's up-forward Do together
block. Try it first with the default duration of 1 second for
the leg movement (the duration for the ball movements should
be .5 seconds).
-
Notice the ball pauses in flight? Why?
-
Change the leg movement duration to .5 seconds.
-
Play and save your work.
-
For fun you can add a monkey
screech when the monkey kicks the ball. Be careful, at
present you have no way of controlling the duration of the
screech statement. You could also do a little more to make
the ball move when the monkey's foot is touching the ball.
This would require more statements and a little
experimentation with the timing.
Part 3 - Breaking an animation into scenes using world methods
Note: These are world methods NOT Class-level custom methods.
What we have now is an animation of a monkey kicking a ball. It
is written as one long series of statements. We can break this
animation down into 3 subparts; monkey turns to face ball,
monkey kicks ball, and the ball flies through the air or
monkeyTurns, monkeyKicks,
and ballFlies. These subparts can
be called modules. We can break our program into modules by
dividing the code into methods to be called by the main world
method. The best way to do this is as you are writing your
program. We did not do this so we will have to break our
program into modules using copy and paste.
We need to setup Alice to better help us with this task.
- Select Edit > Preferences
from the top menu.
- Change the number of clipboards to 5.
- Click okay. You should now
have 5 clipboard icons in the upper right corner of the
screen.
- Save as a new file. Select File >
save world as from the top menu.
- Save the file as Lab3-3.
We are going to create 3 new world methods.
-
In the Object tree select world.
-
In details panel select create new
method.
-
Enter monkeyTurns and
click okay.
-
Repeat step 2 to create
the monkeyKicks
and ballFlies methods.
-
In the editor you should now have 4 tabs with
the world.my first
method, world.monkeyTurns, world.monkeyKicks, and
world.ballFlies methods.
-
Select my first method.
-
Starting at the first statement, drag the statement to the
first clipboard. Drag the next statement to the 2nd
clipboard. Repeat this process for the statements that have
the monkey turn, turn to face the ball, and move to the ball.
-
Select the world.monkeyTurns tab
in the editor.
-
Select the first clipboard and drag it into the editor. The
First statement should appear in the window. Repeat for the
other statements.
-
Select my first method. Delete
the statements you just pasted into
the monkeyTurns method. In the
editor right click on the statement and select delete. Or
drag the statement into the garbage can at the top of the
screen.
-
Repeat this process for the statements that make up the monkey
kicking action and the ball fling action.
-
You're my first method should now be empty. If you
press play nothing will happen.
All of your code still exists, it exists in
methods. A method must be called
for its code to be executed. Let's do this now.
-
From the Tree select world. From
Details drag the monkeyTurns
method into the editor.
-
Press play.
-
From the Tree select world. From
Details drag the monkeyKicks
method into the editor.
-
Press play.
-
From the Tree select world. From
Details drag the ballFlies method
into the editor.
-
Now you have the complete animation in 3 modules.
Press play.
You may ask, "In step 12 you said that a method had to be
called to be executed. What is calling my first method?"
Good question.
If you look in the Events area of the Alice screen (to the right
of the animation window) you will see the
line When world starts do followed
by my first method.
-
Using the dropdown arrow try changing this
to ballFlies.
Press play.
-
You will notice that just the statements in
the ballFlies method are
executed. The monkey should be left standing with his left
leg in an awkward position. Change the Event back
to my first method.
-
SAVE YOUR WORK!
Part 4 - Putting it all together exercise -The Monkey Scores
-
Create a new world saved as Lab3-4.
-
Create an animation of the scene described below.
-
The code should be modularized into appropriate modules and
use good naming conventions for the methods.
-
You can refactor (reuse) some of the code from the previous
exercise.
Scene:
-
Setup:
-
On a grass field a monkey stands near a ball.
-
A few meters from the monkey there is a soccer goal with
the open side towards the monkey.
-
Standing in front of the goal is a man playing goalie. He
is standing in the normal position of a goalie between the
goal and the ball and the monkey.
-
So, you scene should be in this order. The monkey facing
towards goal, ball, a little space, goalie facing towards
monkey, goal with open end towards goalie.
-
Action:
-
The monkey takes 2 steps towards the ball and kicks the ball.
-
The ball flies in an arc towards one side of the goal.
-
The goalie jumps/dives and reaches to block the ball from
getting onto the goal.
-
The ball sores past the goalie's hands.
-
The ball lands in the goal.
-
The goalie lays on his side where he fell in defeat.
-
The monkey screeches a victory screech.
-
More action:
-
The goalie gets up. The goalie picks up the ball, tosses
it back to in front of the monkey. The monkeys kicks
again and this time the goalie blocks the shot by catching
the ball.
-
The monkey is sad.
Assignment
Upload your work from Part 4 to WebCT.