Alice 2.0 Introduction to Event-Driven Programming Mr. Planck.

12
Alice 2.0 Introduction to Event- Driven Programming Mr. Planck
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    223
  • download

    0

Transcript of Alice 2.0 Introduction to Event-Driven Programming Mr. Planck.

Page 1: Alice 2.0 Introduction to Event-Driven Programming Mr. Planck.

Alice 2.0

Introduction to Event-Driven Programming

Mr. Planck

Page 2: Alice 2.0 Introduction to Event-Driven Programming Mr. Planck.

Show Respect

• Value yourself. Be honest and ethical, and practice strong moral values.

• Treat all members of the school community and all visitors with politeness and respect.

• Honor the ideas and opinions of others.

• Offer to help.

• Be responsible with property and belongings.

• FOLLOW THE LEADER!

Page 3: Alice 2.0 Introduction to Event-Driven Programming Mr. Planck.

• The driver of a car is responsible for deciding what operations the car will perform. Without a driver the car will just “wait” for the driver to tell it what to do next. Looking at this picture what are some of the things a driver can “tell” a car to “do”?

Page 4: Alice 2.0 Introduction to Event-Driven Programming Mr. Planck.

Driving a Car

“Tell” (trigger) “wait’ (listen) “do” (handle)

Press Accelerator Increase speed

Press Brake Pedal Decrease speed

Turn steering wheel right Turn front wheels right

Move stick shift to “D” Enable car to move forward

Move stick shift to “R” Enable car to move backward

Turn radio volume clockwise Increase radio volume

Page 5: Alice 2.0 Introduction to Event-Driven Programming Mr. Planck.

Using a Word Processor

• What about a computer program like Microsoft Word?

Even Trigger (“tell”) Event Listener (“wait) Event Handler (“do”)

Press “B” Type the letter “b” in the doc

Press the left mouse button Set the typing cursor in the doc

Alt + F Open the File menu

• The event trigger is typically caused by a computer user manipulating some hardware (keyboard, mouse, scanner, etc.) but can be anything (date/time, email address, etc.)

Page 6: Alice 2.0 Introduction to Event-Driven Programming Mr. Planck.

What is Event-driven programming?• In conventional programming, the sequence of operations for an

application is determined by a central controlling program (e.g., a main procedure). In event-driven programming, the sequence of operations for an application is determined by the user’s interaction with the application’s interface (forms, menus, buttons, etc.).

• To create an event-driven application, the programmer creates small programs—called event handlers—and associates them with specific events raised by specific objects.

• Most, if not all, GUI systems are designed to be event driven, meaning that the main flow of your program is not sequential from beginning to end. If you've never done GUI programming, this is one of the trickiest paradigm shifts.

• The Hollywood Principle: "Don't call us; we'll call you." ... You implement the event handlers, you get registered. You get called when the time is right (event trigger). This requires a distinctly different way of thinking to that which is taught in introductory programming where the student dictates the flow of control.

Page 7: Alice 2.0 Introduction to Event-Driven Programming Mr. Planck.

Alice Interface

Object Tree

World Window

Events Area

Editor Area

Details Area

Page 8: Alice 2.0 Introduction to Event-Driven Programming Mr. Planck.

Alice Scene Editor

Layout Tools

Gallery Navigation

Page 9: Alice 2.0 Introduction to Event-Driven Programming Mr. Planck.

Event-driven programming

• The use of a GUI on a computer system requires event-driven programming. An event occurs whenever an event listener detects an event trigger and responds by running a method called an event handler. An event listener is a combination of hardware and software that repeatedly checks the computer system for the event trigger.

• An event trigger can be any activity or condition selected by the programmer, such as someone pressing the Enter key or a bank account balance going below zero.

• An event handler is a method that is activated when the trigger occurs. Almost any method can serve as an event handler.

Page 10: Alice 2.0 Introduction to Event-Driven Programming Mr. Planck.

Extended Activity

• Add three more events to your current ballerina world (keep the ones we created together, just add these additional ones):1. Add an event trigger based on the up arrow key

on the keyboard that will move the pinkBallerina up ½ meter

2. Add an event trigger based on the down arrow key on the keyboard that will move the pinkBallerina down ½ meter

3. Add and event trigger based on clicking the mouse on the pinkBallerina that will make her say “Ouch!”

Page 11: Alice 2.0 Introduction to Event-Driven Programming Mr. Planck.

Extended Activity

• Once these new events are working properly print your work using the menu File->Export code for printing

• A dialog box will appear, you must enter your name for the Author’s name: and the press the Export Code button (you will have many more directions to export than seen in the example below).

Page 12: Alice 2.0 Introduction to Event-Driven Programming Mr. Planck.

Extended Activity

• Find the newly exported html file with your Alice code on the desktop and print it to hand in at the end of class.

• Download Alice at http://www.alice.org/ to use on your home computer