Download presentation
Presentation is loading. Please wait.
Published byJuliet Daniel Modified over 9 years ago
1
Events Chapter 7 Part 2
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
3
BDE Begin- what is executed when key is pressed During – what happens while key is kept down o If key is released quickly During will not execute End – what executes when the key is released
4
Example While Key is Pressed BDE Tutorial 7-2 o Jump Jet moves up when space key is hit o Jump Jet goes forward if key is held down o Jump Jet moves down when space key is released Notice nothing is in world.animation
5
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
6
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
7
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
8
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 running o While key is pressed o When condition is true
9
IslandRescue Events
10
IslandRescue Alice Methods
11
Random Number Number that is unpredictable o Uniformly distributed over range of numbers o Not possible to predict where next number will be Programs random numbers are used in o Security for web applications o Encryption for satellite transmissions o Gaming programs o Scientific simulations
12
Built-in Random Function Alice provides World-level built-in functions for generating random numbers In World object choose functions o Then random
13
Random Number Function Returns a floating point value o Fraction between 0 and 1 The integer only option o True: only integers are use o False: floating point numbers are used Change range of values by using the minimum and maximum parameters
14
Demo Penguin moves forward random distance Concepts illustrated o Range of values can be changed with minimum and maximum parameters o Integer only option
15
Random Penguin Moves
16
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
17
Six Possible Directions in 3D Movement in six directions are possible o forward, backward, left, right, up, down Eliminate backward because goldfish don’t swim backward To simplify code, we can use negative numbers o Positive numbers move goldfish to left and up o Negative numbers move goldfish right and down
18
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
19
Goldfish Alice Program
20
Demo Concepts illustrated o 3D random movement accomplished by three simultaneous move instructions o 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
21
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 message o 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
22
Output of Print Statement Output is sent to bottom of world when running Output of print statements
23
Homework Read chapter 6 sections 2 - 4 Answer questions in handout Due one week after assigned with 1 week grace
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.