Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS320n –Visual Programming Interactive Programs Mike Scott (Slides 5-1)

Similar presentations


Presentation on theme: "CS320n –Visual Programming Interactive Programs Mike Scott (Slides 5-1)"— Presentation transcript:

1 CS320n –Visual Programming Interactive Programs Mike Scott (Slides 5-1)

2 Visual ProgrammingInteractive Programs2 What We Will Do Today Learn about interactive programs Learn how to add events to Alice programs to make them interactive

3 Visual ProgrammingInteractive Programs3 Control of flow Control of flow – how the sequence of actions in a program is controlled –what actions happen first, second, third In movie style programs as shown in chapters 1-4 the sequence of actions is determined by the programmer –create a story board design –implement the program methods to carry out the designed sequence –completely predictable

4 Visual ProgrammingInteractive Programs4 Interactive Animations In interactive programs, the sequence of actions is determined at runtime when the user provides input –clicks the mouse –presses a key on the keyboard Other sources of input are possible –depending on your computer system

5 Visual ProgrammingInteractive Programs5 Interactive games In a video game where the user is guiding a spaceship, the sequence of actions… –depends on what direction the user guides the ship –how fast the user presses the controls. Each time the program runs, user input may cause a different sequence of actions from previous executions of the program. In essence, control of flow is now “in the hands of the user.”

6 Visual ProgrammingInteractive Programs6 Events Each time the user provides some sort of input, we say an event is generated. – An event is “something that happens” –Magically the operating system passes information about the event to our program Interactive Program from appendix What happens when space pressed and method is not done?

7 Visual ProgrammingInteractive Programs7 event handlers An event may – Trigger a response, or – Move objects into positions that create some condition (e.g., a collision) that triggers a response. A method is called to carry out the response. We call this kind of method an event handler. When an event is linked to an event handler, a behavior is created.

8 Visual ProgrammingInteractive Programs8 How does this affect your program? We want to be able to write interactive programs –they can be more interesting than animations The approach will be the same as before, but the difference is that we must now be concerned with behaviors – input from the user -> (events) – how objects respond to an event -> (event handler methods)

9 Visual ProgrammingInteractive Programs9 Example Build an air show flight simulator. In an air show, the pilot uses biplane controls to perform acrobatic stunts.

10 Visual ProgrammingInteractive Programs10 Problem The whole idea in a flight simulator is to allow the user to control the flight path. The problem is: how do we write our program code to provide a guidance system that allows the user to be the pilot?

11 Visual ProgrammingInteractive Programs11 Solution Use keyboard input – Up-arrow key to move the biplane forward – Spacebar to make the biplane do a barrel turn (Note: other sets of keys could be used, we just arbitrarily picked a couple of keys on the keyboard.) Write event handler methods that respond to each key press

12 Visual ProgrammingInteractive Programs12 Storyboards Since two keys are used, two events are possible – so two storyboards are needed: Each storyboard outlines an event handler that responds to a particular event. Event: Spacebar press Response: Do together roll biplane a full revolution play biplane engine sound Event:: Up Arrow key press Response: Do together move biplane forward play biplane engine sound

13 Visual ProgrammingInteractive Programs13 Demo A demonstration of building the biplane acrobatic simulation – flyForward – barrel

14 Visual ProgrammingInteractive Programs14 biplane.flyForward Coordinate duration of move and play sound instructions by matching the duration of the move to the duration of the sound

15 Visual ProgrammingInteractive Programs15 Events Editor - Linking Each event handler method must be linked to an event click “create new event” button in event window a template is created

16 Visual ProgrammingInteractive Programs16 Events Editor - Linking Select which key triggers the event Select which method in the program should be called when the event occurs

17 Visual ProgrammingInteractive Programs17 Testing Test event handler methods as they are developed. Write a method and test it, write a method and test it, and so on… (this is a technique called incremental development).

18 Visual ProgrammingInteractive Programs18 Adding More to Flight Simulator flyRight flyLeft loop alternate means of control


Download ppt "CS320n –Visual Programming Interactive Programs Mike Scott (Slides 5-1)"

Similar presentations


Ads by Google