Presentation on theme: "PIIT Computer Science Summer Camp - Alice July 11, 2012 Brenda Parker Computer Science Department MTSU."— Presentation transcript:
PIIT Computer Science Summer Camp - Alice July 11, 2012 Brenda Parker Computer Science Department MTSU
April 26, 2015Brenda Parker - Alice Workshop2 Welcome Review?? Demonstrations?
Today’s Topics Event Programming –Event –Event handler Interactive Programming Billboards Games April 26, 20153Brenda Parker - Alice Workshop
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 26, 20154Brenda Parker - Alice Workshop
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 26, 2015Brenda Parker - Alice Workshop5
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 26, 2015Brenda Parker - Alice Workshop6
April 26, 2015Brenda Parker - Alice Workshop7 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 26, 2015Brenda Parker - Alice Workshop8 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
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 26, 2015Brenda Parker - Alice Workshop9
Fish Example 1.Open Alice and create the original scene. April 26, 2015Brenda Parker - Alice Workshop10
Fish Example 2. Create the code to allow the fish to swim (forever). April 26, 2015Brenda Parker - Alice Workshop11
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 26, 2015Brenda Parker - Alice Workshop12
Fish Example April 26, 2015Brenda Parker - Alice Workshop13
Fish Example 4. Create the Event a. In the Events Area, click on “create new event” b. Click on “when key is typed” April 26, 2015Brenda Parker - Alice Workshop14
Fish Example c. Replace “any key” with “Space” d. Replace “do nothing” with “jumpOut” April 26, 2015Brenda Parker - Alice Workshop15
Fish Example 5. Play the video. April 26, 2015Brenda Parker - Alice Workshop16
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 26, 2015Brenda Parker - Alice Workshop17
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 26, 201520Brenda Parker - Alice Workshop
Practice XI Create Instructions 1.Open Word 2.Type in instructions 3.Format instructions 4.Copy instructions 5.Open Paint 6.Paste instructions 7.Save your picture as jump.jpg April 26, 2015Brenda Parker - Alice Workshop21
Displaying Game Instructions 3. In Alice, File Make Billboard select your instructions picture. 4. Position your billboard in your Alice world so that it is displayed appropriately. 5. Make all other objects invisible (properties is showing=false) April 26, 2015 23 Brenda Parker - Alice Workshop
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 26, 201525Brenda Parker - Alice Workshop
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?
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 26, 201528Brenda Parker - Alice Workshop
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
Practice VI Test 1.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 26, 201532Brenda Parker - Alice Workshop
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 structure expressions variable name 4.Add code to the loop
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!
April 26, 2015Brenda Parker - Alice Workshop42 Class Demonstrations Volunteers????
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 26, 201543Brenda Parker - Alice Workshop
User Input Example – Ask user to input a name Create a new variable – name, string Drag variable to editor window and set value expression name Drag world function “ask user for a string” and place on top of “name” other ”What is your name?” April 26, 201544Brenda Parker - Alice Workshop
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.Create a new variable 2.Select Object 3.Choose make a List 4.Add items to the list.