PIIT Computer Science Summer Camp - Alice July 11, 2012 Brenda Parker Computer Science Department MTSU Welcome • Review?? • Demonstrations? April 13, 2015 Brenda Parker - Alice Workshop 2 Today’s Topics • Event Programming – Event – Event handler • Interactive Programming • Billboards • Games April 13, 2015 Brenda Parker - Alice Workshop 3 Event-Driven Programming Event – special condition that occurs while a program is running (event trigger) Event Handler – method that executes when an event has occurred. April 13, 2015 Brenda Parker - Alice Workshop 4 Sample Events • The user presses the left mouse button • The user presses a key on the keyboard • The user presses the space bar on the keyboard April 13, 2015 Brenda Parker - Alice Workshop 5 Event Handlers We must create “event handlers” – we must create methods to tell the computer what to do if an event occurs! What do we do if the user presses the spacebar? April 13, 2015 Brenda Parker - Alice Workshop 6 Events and Event Handlers use Interactive Programming • Interactive – User participates while the video is playing – When the user clicks mouse or presses a key, an event occurs and must be handled by a given method April 13, 2015 Brenda Parker - Alice Workshop 7 Event Programming 1. Click “create new event” in the Events Editor Window 2. Choose desired event 3. Create method to handle the event 4. Play and save video April 13, 2015 Brenda Parker - Alice Workshop 8 Simple Example of Event and Event Handlers Suppose that we wish to create a video which contains a fish swimming in the ocean by an island. The fish jumps out of the water when the user presses the space bar. EVENT: user presses the space bar EVENT HANDLER: a method indicating what to do when the space bar is pressed April 13, 2015 Brenda Parker - Alice Workshop 9 Fish Example 1. Open Alice and create the original scene. April 13, 2015 Brenda Parker - Alice Workshop 10 Fish Example 2. Create the code to allow the fish to swim (forever). April 13, 2015 Brenda Parker - Alice Workshop 11 Fish Example 3. Create a world method to tell the fish to jump out of the water. a.Select the world object b.Choose “create new method” c.Name the method (jumpOut) d.Write the method April 13, 2015 Brenda Parker - Alice Workshop 12 Fish Example April 13, 2015 Brenda Parker - Alice Workshop 13 Fish Example 4. Create the Event a. In the Events Area, click on “create new event” b. Click on “when key is typed” April 13, 2015 Brenda Parker - Alice Workshop 14 Fish Example c. Replace “any key” with “Space” d. Replace “do nothing” with “jumpOut” April 13, 2015 Brenda Parker - Alice Workshop 15 Fish Example 5. Play the video. April 13, 2015 Brenda Parker - Alice Workshop 16 Practice X Choose from any one of the following to create “interactive” programming. Save the file! 1. Create a boat scene in which the boat moves to the right when the right arrow key is pressed and moves to the left when the left arrow key is pressed. 2. Create a scene with 2 penguins (Sam and Joe). When the user presses “J” on the keyboard, Joe jumps or flaps his wings. When the user presses “S”, Sam jumps and flaps his wings. 3. Create your own scene which allows user input of some type. April 13, 2015 Brenda Parker - Alice Workshop 17 User Input If a program (video) requires interactive input, the user should be given instructions on what to input. Example: In the “fishJump.a2w” video, the user should be told to press the space bar to make the fish jump. User Input The user should be given a set of instructions to read when the world first starts. These instructions can be displayed in Alice with a “billboard” A billboard is a picture file that is added to an Alice world. Displaying User Instructions 1. Use Microsoft Word to create the instructions. 2. Use Copy and Paste to place the image into Microsoft Paintbrush or Paint and save the file as a picture (jpg or gif). April 13, 2015 Brenda Parker - Alice Workshop 20 1. 2. 3. 4. 5. 6. 7. Practice XI Create Instructions Open Word Type in instructions Format instructions Copy instructions Open Paint Paste instructions Save your picture as jump.jpg April 13, 2015 Brenda Parker - Alice Workshop 21 Instructions created in Microsoft Word Displaying Game Instructions 3. In Alice, FileMake Billboardselect your instructions picture. 4. Position your billboard in your Alice world so that it is displayed appropriately. 5. Make all other objects invisible (propertiesis showing=false) April 13, 2015 Brenda Parker - Alice Workshop 23 Billboard is visible! Practice XII 1. Use Microsoft Word to create instructions for your Practice X program. 2. Copy the instructions. (Control + C) 3. Open Microsoft Paint 4. Paste the instructions (Control+P) 5. Save your instructions (instructions.jpg) April 13, 2015 Brenda Parker - Alice Workshop 25 We now have 2 Events • If user presses the space bar, our program will execute the jumpOut method • What do we do if the user presses a 1? Events Summary 1. We have events (user presses space bar and user types a 1) 2. We have event handlers. 3. We have a Billboard to display instruction 4. Now, let’s put it all together! April 13, 2015 Brenda Parker - Alice Workshop 28 Putting it All Together When the World starts: 1. Make everything invisible except the billboard (instructions). 2. When the user presses a 1, the world.myfirstmethod should begin execution. 3. The world.myfirstmethod should make the billboard invisible and everything else should be visible. Practice XIII Choose 1 of the following: 1. Add instructions for your Practice XI program. Make sure that your billboard “is showing=true” but that it changes to “is showing=false” when the user presses a particular key. 2. Page AL 224, Three Dancing Penguins 3. Page AL 226, Simple Flight Simulator 1. Practice VI Test What is the difference between “interactive” and “passive” animations? 2. Interactive animations are created in Alice creating a/an ___________. 3. _________________ run __________________ when certain things happen like a key is pressed or the mouse is clicked. on an object. 4. Show the specific instruction that should be used for each of the following scenarios. a. We wish to move a penguin when a number is typed on the keyboard b. We wish to move a penguin when the mouse is click on the penguin c. We wish to allow the mouse to move the penguin d. We wish the arrow keys to move the penguin. e. Allow the mouse to control the camera location 5. If we want a custom method called “jump” to be executed each time the mouse is clicked on a penguin, what must we do first? April 13, 2015 Brenda Parker - Alice Workshop 32 Review of Variables A variable is a name for a storage location in the computer’s memory. In Algebra: x=21 (x is called a variable) In computer science, a variable is a name for a location in memory. Variables • In a programming language, when working with variables: – You should create “good” variable names (names should be meaningful – use the camelCase naming convention – camelCase means that multi-word names begin with a lowercase letter and following words are in uppercase – Example: timesToJump,yourAge,yourFirstName Variables • You should specify the type: number, object, string, Boolean (true or false) • You should give variables a beginning value • Example: timesToJump (number), yourFirstName(string) Creating and using a variable in Alice Example 1: Let’s create a world with a bunny. Your program should have a variable called numberHops, set it as a number and give it the value 5. Then, create a Alice file that will allow the bunny to jump 5 times using the variable numberJumps Using Variables 1. Click on “create new variable” 2. Name the variable and give a value 3. Bring in loop structureexpressionsvariable name 4. Add code to the loop Using a Variable Creating and using a variable in Alice Example 2: Same problem. However, this time let’s ask the user how many times the bunny should jump. Practice XIII Choose one of the following: 1) Create an Alice scene which will compute and display the average of two test grades. (Alice should ask for data from the user) 2) Create an Alice scene which computes and displays amount of pay (hours worked * pay rate) 3) Create an Alice scene which asks for your name and then prints a welcome message that includes your name. 4) Create an Alice scene of your own but you must use variables! Class Demonstrations Volunteers???? April 13, 2015 Brenda Parker - Alice Workshop 42 Manipulating the Camera • Create world methods – Scene1 – Scene2 • Add Objects • Click “more controls” • Move camera and drop dummy at camera for each scene • At the beginning of each scene, place “camera set point of view to ….” (dummy1, dummy2, etc. April 13, 2015 Brenda Parker - Alice Workshop 43 User Input Example – Ask user to input a name • Create a new variable – name, string • Drag variable to editor window and set valueexpressionname • Drag world function “ask user for a string” and place on top of “name”other”What is your name?” April 13, 2015 Brenda Parker - Alice Workshop 44 Using Random Numbers Example: Create a world with Alice. Alice should ask the user for a number between 1 and 10. The user will get three choices to guess the number. Using Lists Suppose we wish to work with several similar objects. (7 penguins, for example) We should use a programming construct called “list” or “array”. To create a list of 7 penguins 1. 2. 3. 4. Create a new variable Select Object Choose make a List Add items to the list.