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
  • Math
  • Using equations
Part 4
  • Strings
  • Concatenation

Part 1 - Variables

A. Creating a new local variable, The Penguin Jumps

  1. From the animals gallery, add a penguin to the scene
  2. Select Done
  3. In the object tree select penguin
  4. Notice the Class-Level methods for the penguin located above the create new method box; wing_flap, jumping, and others
  5. Add (drag and drop) the jumping height method to the method editor, select a default value of 2
  6. Play scene
  7. 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
  8. THIS IS A LOCAL VARIABLE. What is the variable local to?
  9. It is local to the world.my first method method.
  10. Select and drag the penguinJumpHeight variable into the penguin.jumping statement, place it over the height = 1 section of the statement
  11. Notice that the height = 1 section becomes outlined in yellow indicating that the variable may be placed there.
  12. Play the scene. Notice that the scene remains the same as before.
  13. Mimic the previous steps to add penguin.jump method, create the penguinJumpTimes variable and add the new variable to the jump statement
  14. Play the scene

B. Parameter variable, The Penguin Jumps Higher

  1. In the object tree select the penguin
  2. 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.
  3. 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?
  4. 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.
  5. 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.
  6. In the penguin.jump method select the create new parameter box. Enter height as the name.
  7. Drag the height parameter to the height field of the jumping method call.
  8. Select the world.my first method tab at the top of the editor
  9. Notice that a height = 1 field has been added to the penguin.jump method call
  10. Drag the penguinJumpHeight variable to the height field of the penguin.jump method.
  11. Play the scene

C. Class variable, Too Many Penguins

  1. From the gallery add a second penguin to the scene, select Done
  2. Add the penguin2.jumping height = 3 and penguin2jump times=3 methods to the editor
  3. Create a penguin2JumpHeight variable = 3 and add the variable to the method
  4. 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.
  5. In the object tree select penguin
  6. Select the properties tab in the detail window
  7. Select create new variable, enter height as the name
  8. Select create new variable enter times as the name
  9. THESE ARE CLASS VARIABLES for the penguin object
  10. Select the my first world method in the editor
  11. Drag the height class variable into the height fields of the penguin.jumping and penguin.jump methods
  12. Drag the times class variable into the times field of the penguin.jump method
  13. Delete the penguinJumpHeight, penguinJumpTimes, and penguin2JumpHeight local variables
  14. Repeat the previous three steps for penguin2 and its methods
  15. Play the scene
  16. 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

  1. Select world in the object tree
  2. Select properties tab in the details window
  3. Select create new variable with the name height and a value of 5
  4. THIS IS A WORLD VARIABLE (sometimes called global)
  5. 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
  6. Edit the penguin2.jump method. In the object tree select penguin2. Select the edit button beside the jump method in the penguin2 details window.
  7. Drag the world.height variable to the height field of the penguin2.jumping method.
  8. Play the scene
  9. 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

  1. Select the my first method tab
  2. 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
  3. Select the world.height set statement just created to a point above the Do together block as indicated by the bright green line.
  4. 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.
  5. Play the scene.
  6. The set value can also be used for properties.
  7. Add a statement to have penguin ask if he should disappear
  8. Select the properties tab in the penguin's detail window.
  9. Drag the isShowing property to the editor and set its value to true
  10. THIS IS A BOOLEEN VARIABLE TYPE
  11. Add the world.ask user for yes or no function to the true field of the isShowing set statement.
  12. Drag the world.not a function to the world.ask user for yes or no field.
  13. Play the scene.
  14. Why did the penguin disappear when Yes was selected?
  15. SAVE YOUR WORK!

Part 2 - Functions

A. World primitive functions, A personalized hello

  1. Open a new world. Under the File tab, select New world. Save the new world as Lab4-2a.
  2. Add a penguin to the scene, select Done.
  3. 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
  4. Select penguin
  5. Drag the penguin say method set to What is your name
  6. create inputName variable
  7. Drag inputName to editor set value to default string
  8. Select world function tab
  9. 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.
  10. Select okay until the pop-up box goes away
  11. Select penguin methods tab
  12. Drag penguin say into editor set value to hello
  13. Drag penguin say into editor set value to inputName
  14. Play scene
  15. SAVE YOUR WORK, you will reuse the file below.

Class functions

  1. Save the current world as Lab4-2b.
  2. Add a comment explaining the next block of code
  3. Add the penguin move up method to the program set value to 2
  4. Create a number variable hgt and a string variable height
  5. Add a statement to set height to the penguins distance above the ground.
  6. Drag height to the editor set value to default string
  7. Select world functions tab
  8. Drag what as a string to the default string field in the set height statement
  9. Drag hgt to the what field of the above statement
  10. Add a statement to have the penguin say I am
  11. Add a statement to have the penguin say his height above the ground.
  12. Drag the penguin say method to the editor set the value to default string
  13. Drag the height variable to the default string field
  14. Add a statement for the penguin to say meters above the ground
  15. Play the scene
  16. Add a penguin move statement set value to 1 meter. Select the penguin functions tab
  17. Drag the penguin distance to function to the 1 meter field of the penguin move statement. Set value to ground object
  18. Play scene
  19. Why is the penguin halfway merged with the ground? Hint: remember object center points. Where is the center point of the penguin?
  20. SAVE YOUR WORK!

Part 3 - Math

A. Using equations, Save the Penguin

  1. 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.
  2. Disable or delete all statements above the class comment added in the last section
  3. Add a comment to the bottom of the code explaining that the next block of code is math function related
  4. Calculate the distance the Penguin needs to move up to be back on the ground
  5. Add a statement to move the penguin up 0.5 meters.
  6. 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
  7. Select the penguin functions tab
  8. Drag the penguin's height function to the 1 field in the penguin move statement
  9. Play the scene
  10. 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?
  11. SAVE YOUR WORK!

B. On your own, Touch the ball

  1. Open a new world. Save as Lab4-3b.
  2. 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.
  3. 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.

  1. Open the lab from part 2a above - Lab4-2a
  2. In the functions tab of the world object find the string function a joined with b.
  3. Drag this function to the penguin say hello statement. Place the function over the hello field. Select expression > inputName
  4. Delete the penguin say inputName statement
  5. Play the scene
  6. Note the text runs together. How would you fix this? There are two ways.
  7. 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.
  8. Second way: Use the drop down arrow beside hello to select other and enter hello followed by a single space.



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