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.

Part 1 Basic Loop and infinite loop
Part 2 While Loop
Part 3 Build a timer
Part 4 Fixing the Monkey kicks

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.

B. Infinite Loop - The clock pendulum

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.

  1. Replace the infinite loop with a while loop.
  2. Create a Boolean variable to be used in the loop; set the initial value to true.
  3. 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

Create a timer object
  1. 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.
  2. 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).
  3. 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.
  4. 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:


Last modified: 8/19/09
By: David O'Gwynn