CSC931 Autumn 2011

CSC931 Autumn 2011

Alice Practical 4: Loops

Aims

·  To learn how to use loop constructs to produce iterative (repetitive) behaviour. /
© thehathorlegacy.com

Remember to register your practical attendance in the usual way!

Further Documentation

You may find it useful to refer to the notes from the fourth lecture on Alice. You can find these online at http://www.cs.stir.ac.uk/courses/CSC931/

Ice Show

Today you are going to work with a signature Alice creation: the ice skater. Using loop constructs, you will make her perform an ice show. This is based on Programming Project 3.5 from Lewis and dePasquale.

·  Setting up the ice skater and her environment is quite fiddly so we have done it for you. Navigate to Groups on Wide/CSC931/Alice/ and copy the file IceShow.a2w into the folder where you keep your Computing Science work.

·  Explore the objects in the Object Tree. Look at the properties of the IceSkater. She has been given a number of useful preset poses. Notice also that the objects that make up the environment have been collected into a single group. Groups are a useful way of keeping the object tree uncluttered when there are lots of similar objects.

·  Add some commands to world.my first method to get the skater to skate forward 10 metres while simultaneously turning right 1 full revolution.

·  Make the skater take on a more suitable pose (say, skating on the right leg) before she skates. Play your world to try it out.

·  It would be more realistic for the skater to get into her pose simultaneously while skating off, instead of before. Make her do this.

·  Now the problem is that she is too slow at getting into her pose – by the time she finally achieves the pose she has already skated in a full circle. To fix this, adjust the duration of each action so that she gets into the pose quickly (say, in 0.5 seconds) while taking longer (say 2 seconds) to do the skating. (To see the duration parameter, click on more… in each method call.)

·  Next, the skater is going to do a figure of eight. After completing the first circle, make her switch to the other leg and skate in a circle in the opposite direction to the first circle (ie, left instead of right). To make the ending look tidier, make the skater return to her initial pose after she has finished skating.

·  Now, introduce a Loop to cause the skater to do 10 figures of eight before returning to her initial pose. If you’re not sure how to do this, ask a demonstrator for help.

Can you see the index variable which counts how many times the code in the loop has been run? (If you can’t find this variable you may need to look at the complicated version of the loop code.) In the next step we are going to use this variable to vary the skater’s figures of eight. As she skates, her figures of eight will get larger and larger, with the size and duration determined by the loop index.

·  Inside your loop body, change the distance moved in each half of the figure of eight so that it is some multiple of index (say, index * 2). Similarly, change the duration to be index seconds. Play your world.

·  Does your skater seem to do a funny little shuffle at the start of her routine? Why do you think this is? What is the value of index during the first pass through the Loop? What is the distance that the skater travels during her first figure of eight? Find a way to modify your program so as to eliminate this shuffle. (There is more than one solution for this.)

·  Another problem: is your skater skating off the screen? You can control the camera so as to keep the skater in view. Try this now. Use the camera.constrain to face method to make the camera face the skater. This will need to be done together with the skating actions and for the same duration. Experiment until you get it right.

·  Finally, see if you can modify your program so that there is a 50% chance that the skater will pause to do a pirouette in between both halves of the figure of eight.

Something extra

Try adding a second skater to your ice show and create a routine for the two of them to do together. Have fun!

Department of Computing Science & Mathematics, University of Stirling Page 1 of 2