CS105L Introduction to Game Programming Using Alice (Lab)
Fall 2009
Lab 5
Chapter 4, Part 2: Repetition Structures
Covers material from second part of Chapter 4 in class text;
Starting Out with Alice.
Note: The previous labs had detailed instructions. The labs will
begin to have less step-by-step instructions requiring you to
think on your own and to use the basic concepts of Alice
programming that you have learned in past labs. Remember to save
your work for later reuse.
Part 1 - Basic Loop and infinite loop - The Clock
A. Basic Loop - Spinning clock hands
Do the clock tutorial; 4-3 page 173 in the book with changes
below.
-
Use the cuckoo clock. Use the
Search command to search the
gallery if you cannot find it.
-
Omit Step 1. You will create the world from scratch.
B. Infinite Loop - The clock pendulum
-
The cuckoo clock has a pendulum.
Create an infinite loop (using
the Loop structure) to cause the
pendulum to swing as the hands spin.
-
Create an infinite loop that rolls the pendulum left, then
right in a swinging motion.
-
Create a Do together block.
-
Place the original Clock-Hands code and the Pendulum code in
the Do together block.
-
Play the scene
-
Notice that the pendulum continues to swing after the hands are
done.
Part 2 - While Loop - Control the pendulum
A while loop is a pretest loop. The while tests a condition, if
the condition is true then the loop executes else the loop
terminates and the code continues at the line after the loop
block. Like the if/else structure, the while uses a condition
that will result in a Boolean value of true or false. Modify the
above code so that the pendulum stops when the hands finish.
-
Replace the infinite loop with a
while loop.
-
Create a Boolean variable to be used in the loop; set the
initial value to true.
-
Modify the Clock-Hands block such that the Boolean variable is
set to false at the completion of
the spin-hands loop
Part 3 - Build a timer
-
In class you saw an example of a timer. Now you will build a
timer for use in your labs.
-
This timer will keep track of the time remaining in some context
such as a game.
-
Modify the Clock program so that the spin-hands loop has a
duration of 1 second per iteration (a pass through the loop).
Add a timer display in the lower left-hand corner that counts
down from 12 seconds. Start the timer when you start spinning
the clock hands.
Create a timer object
-
First, add a 3D text object to the
world. Any string of digits could be used. We arbitrarily chose
"0.0". Rename the object to timer.
-
Select the timer object and then create a new class variable
named timeLeft.
-
The variable is declared to be a Number type.
-
The value is 0 (the game hasn't begun, as yet).
-
At the start of the game, the
timer's timeLeft variable must be
initialized to the amount of time allowed for playing the
game. (This example uses seconds, but minutes would work just as
well.) Create a timer.initialize
method to set the start time (in this case 12). This method sets
the value of timer.timeLeft. This
method will need a parameter to pass in the value.
-
Create a timer.countDown method
that causes the timer display to count down to 0. Note that
timeLeft will have to converted to a string (a built in world
function) before it can be displayed as text.
Below is the pseudo code for the timer:
countDown
Do in order
While timeLeft is greater than 0
Do in order
-
update the 3D text to show the remaining number of seconds
-
decrease timeLeft by 1
update the 3D text to display 0 seconds
The world.myfirstmethod will use a Do
together block start the clock and the countdown timer.
Part 4 - ASSIGNMENT Fixing The
Monkey Kicks
You have created a monkey kicking a toyBall in past labs. Modify
the code such that the ball moves away when the monkey's foot hits
the ball. Use a while loop to control the swing of the monkey's
foot. Have the monkey's foot stop swinging forward when the ball
is struck.
Hints:
-
Refer to figures 4-40, 4-41, 4-42 and the associated text on
page 178 of the book. Recreate the tennis code in a new world to
understand how it works.
-
In the monkey world set the distance of the monkey to the ball
so that the monkey's foot goes through (into) the ball. You
have a past lab where you used the distance-to function to have
the monkey close to the ball.
-
The reaction between the ball and the monkey's foot should be
the same as the tennis racquet and the tennis ball.
-
Remember to use the monkey's foot in the distance calculation
and not the entire monkey.