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

  1. Start Alice
  2. Click Cancel on the pop up screen
  3. Under the File tab, select New world
  4. Select the grass template and click Open
  5. Click Add Objects
  6. In the Sports gallery select the toyBall1, add the Ball to the scene, Select Done
  7. In the Object Tree click on toyBall1 (upper left)
  8. 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.
  9. In the Toolbar click Play.
  10. Notice that the Ball moves to the left and back on the screen. Close the screen.
  11. 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.
  12. Repeat the above step to add a second toyBall1 move forward statement to the editor.
  13. 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.
  14. Play the scene.
  15. Notice the jerky movement of the Ball.

B. - Moving a ball with style

  1. 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.
  2. Select style = begin gently for statement 1 and style = end gently for statement 2. Click Play and notice the smooth overall movement.
  3. 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

  1. 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".
  2. 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.
  3. 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).
  4. SAVE YOUR WORK!


Part 2 - Primitive Method Exploration / Class-Level Custom Methods

A. Custom Methods, Methods of the Monkey

  1. Under the File tab, select New world. Save the new world as Lab3-2.
  2. Select the grass template and click Open
  3. Click Add Objects
  4. In the Sports gallery select the toyBall1, add the Ball to the scene.
  5. 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.
  6. Select Done
  7. Examine the Monkey Class-level Custom methods: In the Object Tree click Monkey
  8. 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

Steps
  1. In the Object Tree click Monkey
  2. In the Details panel drag and drop Monkey turn into the Method editor.
  3. In the pop-up window select right > 1 revolution.
  4. Select the Monkey say method, select other, and type in the string I think I'll play with that ball.
  5. Select the monkey turn to face method and select toyBall1
  6. Select the move to method and select toyBall1.
  7. Press Play.
  8. Notice the monkey is now IN the ball. Close the play screen
  9. Delete the last statement.
  10. Select move toward > 1/4 meter > toyBall1
  11. 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.
  12. 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
  1. In the Object Tree expand the monkey object (click the + sign next to monkey)
  2. Select the leftLeg object
  3. Drag the leftLeg turn method to the editor, select turn > forward .25 revolutions
  4. In tree, expand leftLeg object, select lowerLeg object
  5. Drag the lowerLeg turn method to the editor, select turn > forward .25 revolutions
  6. Select leftLeg Object, drag the leftLeg turn method to the editor, select turn > backward .25 revolutions
  7. Select lowerLeg object, drag the lowerLeg turn method to the editor, select turn > backward .5 revolutions
  8. 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.
  9. 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.
  10. 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).
  11. Drag the leftLeg turn forward and the lowerLeg turn forward statements into the Do together block.
  12. With a 2nd Do together block repeat the same steps for the turn backward statements.
  13. Add a statement to return the lower leg to a standing position after the kick.
  14. Press play to verify your code.

D. Own your on - The ball flies

Hints: More hints:

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. We are going to create 3 new world methods.
  1. In the Object tree select world.
  2. In details panel select create new method.
  3. Enter monkeyTurns and click okay.
  4. Repeat step 2 to create the monkeyKicks and ballFlies methods.
  5. In the editor you should now have 4 tabs with the world.my first method, world.monkeyTurns, world.monkeyKicks, and world.ballFlies methods.
  6. Select my first method.
  7. 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.
  8. Select the world.monkeyTurns tab in the editor.
  9. Select the first clipboard and drag it into the editor. The First statement should appear in the window. Repeat for the other statements.
  10. 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.
  11. Repeat this process for the statements that make up the monkey kicking action and the ball fling action.
  12. 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.
  13. From the Tree select world. From Details drag the monkeyTurns method into the editor.
  14. Press play.
  15. From the Tree select world. From Details drag the monkeyKicks method into the editor.
  16. Press play.
  17. From the Tree select world. From Details drag the ballFlies method into the editor.
  18. 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.



Part 4 - Putting it all together exercise -The Monkey Scores

Scene:

Assignment

Upload your work from Part 4 to WebCT.


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