<<

Alice

Alice is a programming language that allows creating 3D-. It is designed to teach how to program a computer, but in a much different way than with most other programming languages. First you won’t have to remember special words or syntax. Then you will not be asked to solve obscure problems. Instead you will create short animations, and if we get far enough simple interactive games. To be honest, the animations will not be of professional quality but many students before you have shown that this is a teaching tool that allows them to make full use of their creativity while at the same time learning important programming concepts. You will have some homework assignments in which you need to use the software. If you want to work on these assignments on your own computer you can download Alice from http://alice.org/index.php?page=downloads/download_alice2.2. Choose the alternate downloads with the Alice textbook Worlds. If you do not have Alice on your computer, you can use the Math lab computers to complete your homework.

A few things to know before working with Alice: • You cannot open an Alice file by double-clicking it. You need to open Alice and open the file through Alice. • Alice is a great program but it is not perfect. It tends to freeze sometimes. Save often your programs so you will not lose all your work if this happens to you.

Section 1: Storyboards (PowerPoint presentation: StoryBoardDesign) Programmers write programs to solve problems. The first steps of solving a problem is to make sure we understand well the problem and then to a solution. Once we have the design we can write code, that is write the solution in the special programming language used by the programmer. When programming with Alice these stages will correspond to stages used by professionals in studios. Understanding the problem means having a clear scenario, i.e. a story to tell. The design will be expressed by a series of frames, called storyboards, which show the major steps in the story. The frame contains an image (it can be rather crude) showing the scene at that point of the story and a brief text describing the action. (See the PowerPoint entitled StoryBoardDesign for an example. ) For those of us who feel too artistically challenged for the storyboard, the design can be a textual storyboard. In this case, the actions are described in brief concise sentences. Rather than being written as the scenario in paragraph style, the text is formatted in such a way that the order and the logic of the actions is made very clear. This will make more sense as our design get a little more complex. Here an example of a textual storyboard.

Section 2: Setting up the initial scene Before we can program an animation we need to set up the stage. In Alice this will mean setting up the initial scene. We will choose a template, and add and position characters and objects in this template. The PowerPoint SettingUpWorlds shows the tools used to set up the initial scene.

Section 3: From Storyboards to Code (PowerPoint presentation: StoryBoard to Code) Once we have our design, i.e. our storyboard, and created the initial scene, we are ready to write the code to animate the characters. As shown clearly by the storyboard the story can be decomposed in several actions. Moreover, we can see that some actions are specific to a character and could possible be reused in other situations. For example, in our dragon and princess story the dragon takes off. This action involves only the dragon and it is something we might want to reuse for future dragons. In the PowerPoint StoryboardToCode, we give the details how to make the dragon take off. We write some pieces of code, called methods that make the dragon perform given actions. • flapRightWing: it is used to make the dragon flap its right wing. • flapLeftWing: it is used to make the dragon flap its left wing. • flapWings: its is used to make the dragon flap both wings at the same time. To write it, we call the methods flapRightWing and flapLeftWing. • takeOff: this method allows the dragon to take off. To write it, we call the method flapWings. These methods are methods created for the dragon (they are called class-level methods) because they are specific to the dragon and do not involve any other object or character in the World. When you create a method, you first give a name to the method, and then you move to the editor window the tiles that tell what the method does. For example the method flapLeftWing might look as in Figure 1. This is similar to writing a recipe in a recipe notebook. But in the same way that writing a recipe and looking at it in the notebook does not get you much closer to a nice meal, writing a method definition and seeing it in the editor window won’t animate your characters. The method will “act” only if it is called. For example, in Figure 2 the method takeOff is called in my first method. If we click on Play, every action included in the definition of the method takeoff will be executed, including any call to other methods (here a call to the method flapWing).

Figure 1- flapLeftWing

Figure 2- my first method

Rules to remember: 1. If an action of the storyboard involves only one character (or object) in the World, create a class- level method for this action. 2. If the action is somewhat complex, it is a good idea to decompose it in several methods. 3. A method will not execute (“play”) if it is not called.

Saving a new class Once we have written method to allow the dragon to take off and to fly, we can save if as a new class that can be reused in other word. We need to change its name, for example to flyingDragon and save the class. The file will have the extension .a2c while files that contain Alice worlds have the extension .a2w. A demo on how to save a class and how to import the object in a new world is included at the end of the pdf-PowerPoint presentation StoryboardToCode.

Section 4: Loops, built-in functions and parameters (PowerPoint presentation: FunctionsAndLoops) We now know how to give simple instructions to an object. We have used two constructs: • Do in order: the instructions are executed sequentially, i.e., one after the other in the order they are written. • Do together: the instructions are executed at the same time. Often we want a given instruction or a given set of instructions to be executed multiple times. For example if we know how to make a character take one step, making the character walk would require taking one step repeatedly. If we made a method to make the dragon fly forward one meter, we can repeatedly call that method to make the dragon fly to the princess. A simple way to repeat an instruction is of course to call it several times in a “Do in order” block. See Figure 3. But this is not always convenient (suppose we want to repeat the instruction 100 times!). Alice and all other programming languages provide repetition structures to indicate that an instruction or a set of instructions need to be repeated. The simplest structure in Alice is the Loop. We can replace the code in Figure 3 by the code shown in Figure 4.

Figure 3 Figure 4

The set of instructions that is within the body of the loop (delimited in green) will be repeated the number of times indicated next to the word Loop. A problem that arises often is that we do not know exactly how many times the “loop” must execute. For example, the number of times the dragon must fly one meter depends on how far it is from the princess. Alice can answer the question “how far from the princess is the dragon”? Answers to such questions are given by built-in functions. We have used one tab of the object’s details area of the Alice interface: the method tab. Another tab is the function tab (see figure 5). Functions provide information about an object, such as its size and its position relative to other objects in the world. Functions return values of certain data types. For example the distance functions will return numbers while the function “is larger than” will return a Boolean value, which means a value that is either true or false. Next to the word “Loop”, you are expected to write a number. You may use any function that returns a number instead of an actual number value. See Figure 6.

Figure 5

To write the Loop shown on left, first write it with any number (1, 2, 3…) in place of the function. Then drag in the function and drop it over the number. If you try to use a function that does not return a

Figure 6 number in a place where a number is expected (such as behind Loop), Alice does not allow you to drop the function: the square around the function remains red. Methods with parameters The code shown in figure 6 could be the code for a method called flyToPrincess and it works well. The only disadvantage of this method is that it is very specialized. It can be used only to make the dragon fly to the princess, and just to the given princess, called princess in this world. What if there is another fair lady in distress? Do we have to write another method for it? Just as using loops and functions added flexibility to our code we have another tool that will add a lot of flexibility to our methods. We are going to use a parameter in the method. Instead of saying that the dragon is flying to the princess (and code it that way), we say that the dragon is flying to an object (yet to be determined). This object is the parameter. We give it a name, but it is a place holder name. The actual value of the parameter will be decided when the method is called. Let say here we will say that “who” is the name of the parameter. Then the storyboard for this method becomes:

Method: flyToPerson Parameter: who

Do in order

dragon takes off

dragon turns to face who

Repeat Distance of who in front of dragon number of times

dragon fly

The code in Alice is shown in Figure 7. Then when we call the method we just need to indicate who is who. We could use for the dragon to fly to the princes or to the !

Figure 7

Alice lesson 5: Interactive programs (PowerPoint presentation: InteractivityWithAlice) Until now our programs have been animations. The sequence of actions has been completely controlled by the programmer. This sequence of actions is what is called the control of flow: it controls what happens first, what happens second, what happens next, etc. In interactive programs, actions depend on user input. For example when you play a video game, what happens on the screen depends on what you do with the joy stick (or mouse or keys depending of the type of input device you use). The flow of control is now determined by the user input. The program will react to events. An event is generated when the user provides an input or something else notable in the system happens. In an interactive program, events will usually produce a response. The programmer write methods whose purpose is to respond to particular events: they are called event-handling methods. In Alice the process to create an event and to link an event to a method is simple. You need of course to do some planning first. In an interactive program, your storyboard should list the events you want to program and the corresponding event-handling methods. See example in figure 8.

Figure 8

When you program, you first write the methods as you write any methods you’ve written so far. The name “event-handling methods” refer to their purpose but these methods are no different from any other ones. Then using the event pane of the Alice interface, you create the events and link each event with the appropriate method. Figure 9 shows that the event “mouse is clicked on georgeBeetle” is associated with the method “georgeBeetle.playMusic” and a new event “a key is typed” is in the process of being created and associated with a method.

Figure 9

Section 6: Conditional Statements (PowerPoint presentation: MoreInteractivity.ppt) In the storyboard above we created four methods that do exactly the same thing but for four different characters and four different pieces of music. We have seen earlier that using parameters could make methods more flexible, allowing to use the same methods for different objects when the object are “used” the same way in the method. This is what we can do here. We write a method playMusic with two parameters, one will tell who is playing and the other one which music is playing. The code is shown in figure 10.

Figure 10

Then we’ll write a method that assigns to each of the Beetles the correct music. It will have one parameter to tell who is playing. The storyboard must be: • if the player (the parameter) is GeorgeBeetle then PlayMusic with the GuitarBass piece is called • otherwise, if the player is LennonBeetle then playMusic with the Guitar piece is called • otherwise, if the player is RingoBeetle then playMusic with the Drum piece is called • otherwise, if the player is PaulBeetle then playMusic with the Saxo piece is called

The flow of control depends now on a condition. We are going to use an “If – Else” statement, also called a conditional statement. It has this form: • condition is a Boolean expression, which means an expression that is either true or false, • Statements 1 are executed when the condition is true, • Statements 2 are executed when the condition is false.

The completed code for the method is shown in figure 11.

Figure 11

Exercises 1. Think about an initial scene with at least 4 objects and/or characters and create it with Alice. 2. Set up a scene with three penguins on top of each other. Use the Quad view to make sure that the penguins are really on top of each other. 3. Create a world with two different kinds of animal. For each animal create a method to make it move. The kind of move will depend on the animal. For example, a rabbit would hop but a bird would fly. The richer the details in the movement the better. Before choosing the animals and the “moves” read exercise # 4. 4. Continuation of number 3. Using the methods you wrote in exercise #3, write some code in my first method to implement a brief story. For example: “the rabbit is chasing the turtle, but the mighty turtle escapes.” 5. The 3 most often used categories of functions are: proximity, size, and spatial relation. For each of these three categories give the name of three functions and indicate for each whether it returns a number or a boolean value (true or false). (Important: to answer the question make sure that an object is selected, not the world, when you look at the functions tab). 6. Create a world with kangaroo and a fence as shown below. For each method you have to write in the exercise, choose appropriately the type (i.e., class-level or world-level). a. Write a method hop for the kangaroo that makes the kangaroo hop one meter forward. b. Write a method hopToFence. You must use the method hop from question a., a loop, and a built-in function. Make sure you test your method by calling it in my first method. c. Write a method called hopToObject that has one parameter, the object to which the kangaroo will hop. Call the method in my method to make the kangaroo hop to the fence and hop to the tree (Hint: you’ll have to make the kangaroo turn to face the objects first!)

7. Complete all exercises from presentation InteractivityWithAlice (Pages 12 and 14)