470 likes | 486 Views
PIIT Computer Science Summer Camp - Alice. July 11, 2012 Brenda Parker Computer Science Department MTSU. Welcome. Review?? Demonstrations?. Today’s Topics. Event Programming Event Event handler Interactive Programming Billboards Games. Event-Driven Programming.
E N D
PIIT Computer Science Summer Camp - Alice July 11, 2012 Brenda Parker Computer Science Department MTSU
Welcome • Review?? • Demonstrations? Brenda Parker - Alice Workshop
Today’s Topics • Event Programming • Event • Event handler • Interactive Programming • Billboards • Games Brenda 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. Brenda 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 Brenda Parker - Alice Workshop
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? Brenda Parker - Alice Workshop
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 Brenda Parker - Alice Workshop
Event Programming • Click “create new event” in the Events Editor Window • Choose desired event • Create method to handle the event • Play and save video Brenda Parker - Alice Workshop
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 Brenda Parker - Alice Workshop
Fish Example • Open Alice and create the original scene. Brenda Parker - Alice Workshop
Fish Example 2. Create the code to allow the fish to swim (forever). Brenda Parker - Alice Workshop
Fish Example 3. Create a world method to tell the fish to jump out of the water. • Select the world object • Choose “create new method” • Name the method (jumpOut) • Write the method Brenda Parker - Alice Workshop
Fish Example Brenda Parker - Alice Workshop
Fish Example 4. Create the Event a. In the Events Area, click on “create new event” b. Click on “when key is typed” Brenda Parker - Alice Workshop
Fish Example c. Replace “any key” with “Space” d. Replace “do nothing” with “jumpOut” Brenda Parker - Alice Workshop
Fish Example 5. Play the video. Brenda Parker - Alice Workshop
Practice X Choose from any one of the following to create “interactive” programming. Save the file! • 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. • 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. • Create your own scene which allows user input of some type. Brenda Parker - Alice Workshop
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). Brenda Parker - Alice Workshop
Practice XICreate Instructions • Open Word • Type in instructions • Format instructions • Copy instructions • Open Paint • Paste instructions • Save your picture as jump.jpg Brenda Parker - Alice Workshop
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) Brenda Parker - Alice Workshop
Practice XII • Use Microsoft Word to create instructions for your Practice X program. • Copy the instructions. (Control + C) • Open Microsoft Paint • Paste the instructions (Control+P) • Save your instructions (instructions.jpg) Brenda 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 • We have events (user presses space bar and user types a 1) • We have event handlers. • We have a Billboard to display instruction • Now, let’s put it all together! Brenda Parker - Alice Workshop
Putting it All Together When the World starts: • Make everything invisible except the billboard (instructions). • When the user presses a 1, the world.myfirstmethod should begin execution. • The world.myfirstmethod should make the billboard invisible and everything else should be visible.
Practice XIII Choose 1 of the following: • 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. • Page AL 224, Three Dancing Penguins • Page AL 226, Simple Flight Simulator
Practice VI Test • What is the difference between “interactive” and “passive” animations? • 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? Brenda 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 • Click on “create new variable” • Name the variable and give a value • Bring in loop structureexpressionsvariable name • Add code to the loop
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: • Create an Alice scene which will compute and display the average of two test grades. (Alice should ask for data from the user) • Create an Alice scene which computes and displays amount of pay (hours worked * pay rate) • Create an Alice scene which asks for your name and then prints a welcome message that includes your name. • Create an Alice scene of your own but you must use variables!
Class Demonstrations Volunteers???? Brenda Parker - Alice Workshop
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. Brenda 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 valueexpressionname • Drag world function “ask user for a string” and place on top of “name”other”What is your name?” Brenda 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 • Create a new variable • Select Object • Choose make a List • Add items to the list.