23
Events Chapter 7 Part 2

Events Chapter 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

Embed Size (px)

Citation preview

Page 1: Events Chapter 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

EventsChapter 7 Part 2

Page 2: Events Chapter 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

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

Page 3: Events Chapter 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

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

Page 4: Events Chapter 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

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

Page 5: Events Chapter 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

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

Page 6: Events Chapter 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

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

Page 7: Events Chapter 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

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

Page 8: Events Chapter 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

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

Page 9: Events Chapter 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

IslandRescue Events

Page 10: Events Chapter 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

IslandRescue Alice Methods

Page 11: Events Chapter 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

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

Page 12: Events Chapter 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

Built-in Random Function

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

In World object choose functions o Then random

Page 13: Events Chapter 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

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

Page 14: Events Chapter 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

Demo Penguin moves forward random

distance

Concepts illustratedo Range of values can be changed with

minimum and maximum parameterso Integer only option

Page 15: Events Chapter 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

Random Penguin Moves

Page 16: Events Chapter 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

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

Page 17: Events Chapter 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

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

Page 18: Events Chapter 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

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

Page 19: Events Chapter 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

Goldfish Alice Program

Page 20: Events Chapter 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

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

Page 21: Events Chapter 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

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

Page 22: Events Chapter 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

Output of Print Statement

Output is sent to bottom of world when running

Output of print statements

Page 23: Events Chapter 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

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

grace