Events Chapter 7 Part 2. While a Key is Pressed Event Specialized event An event occurs when you...

Preview:

Citation preview

EventsChapter 7 Part 2

While a Key is Pressed Event Specialized event An event occurs when you press a key and

continues until you take your finger off of the key

Choose “When a Key is Typed”, right click, choose “change to”

Event is called BDE

BDE Begin- what is executed when key is

pressed During – what happens while key is kept

downo If key is released quickly During will not execute

End – what executes when the key is released

Example While Key is Pressed BDE Tutorial 7-2

o Jump Jet moves up when space key is hito Jump Jet goes forward if key is held downo Jump Jet moves down when space key is released

Notice nothing is in world.animation

Mouse Movement Let mouse move object

o Can move any object with a click and drag Let mouse move the camera

o Moves the camera location with click and drag Let mouse move orient the camera

o Rotates orientation with click and drag When Mouse is clicked on an object

o Same thing happens if mouse is clicked briefly or long

When World is Running Will do method while world is active

o Continuously runs When World Starts only executes when world starts While is like an infinite loop since world is always

running Uses BDE

When Condition Becomes True Does method when condition is true

Useful when object is moving and you want to see if it is close to another object

Example: Have person fall when 10 meters from drop zone

Choose “While something is true”, right click, choose change to

Example Events in Game See IslandRescue Demonstrates

o How to show user instructions at beginning of game

When mouse is clicked on anything instructions disappear

o While the world is runningo While key is pressedo When condition is true

IslandRescue Events

IslandRescue Alice Methods

Random Number

Number that is unpredictable o Uniformly distributed over range of numberso Not possible to predict where next number will

be Programs random numbers are used in

o Security for web applicationso Encryption for satellite transmissionso Gaming programso Scientific simulations

Built-in Random Function

Alice provides World-level built-in functions for generating random numbers

In World object choose functions o Then random

Random Number Function Returns a floating point value

o Fraction between 0 and 1 The integer only option

o True: only integers are useo False: floating point numbers are used

Change range of values by using the minimum and maximum parameters

Demo Penguin moves forward random

distance

Concepts illustratedo Range of values can be changed with

minimum and maximum parameterso Integer only option

Random Penguin Moves

Random 3D Motion Penguin moved in only one direction In random 3D motion

o Object moves to random location in 3D Forward, backward, left and right, up and down

o Ex: goldfish below swims in random motion, fish can move in any direction except backward

Six Possible Directions in 3D

Movement in six directions are possibleo forward, backward, left, right, up, down

Eliminate backward because goldfish don’t swim backward

To simplify code, we can use negative numberso Positive numbers move goldfish to left and upo Negative numbers move goldfish right and

down

Algorithm Only three move instructions are needed

o up (will move down if random number is negative)o left (will move right if random number is negative)o forward (no backward motion)

Two parameters (min, max) define the fish’s range of motion to look like swimming

randomMotion

Parameters: min, max

Do together fish moves up a random distance fish moves left a random distance fish moves forward a random distance

Goldfish Alice Program

Demo

Concepts illustratedo 3D random movement accomplished by

three simultaneous move instructionso Minimum distance of move forward

instruction is 0 Goldfish always moves forward never bacward

o To call randomMotion method, min and max values are sent as arguments to parameters

Debugging With Print

o Print statement allows you to See what values variables are during program

execution Print a text message to identify where program is

o Print statement is a diagnostic messageo Drag print from bottom of Method Editor

Causes a menu to appear asking if you want to Print a text message Print an object or variable under expression

Output of Print Statement

Output is sent to bottom of world when running

Output of print statements

Homework Read chapter 6 sections 2 - 4 Answer questions in handout Due one week after assigned with 1 week

grace

Recommended