CS320n%20 - PowerPoint PPT Presentation

About This Presentation
Title:

CS320n%20

Description:

if car stays on road display next section of road. if car hits another car, crash. if car goes too far off road, crash. if pass checkpoints, more time ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 29
Provided by: sco1
Category:
Tags: cs320n

less

Transcript and Presenter's Notes

Title: CS320n%20


1
CS320n Visual Programming
  • Execution Control with If / Else and Boolean
    Functions (Slides 6-2-1)

Thanks to Wanda Dann, Steve Cooper, and Susan
Rodger for slide ideas.
2
What We Will Do Today
  • Learn about
  • using if / else statements to control the flow of
    a program
  • using boolean functions

3
Adding More Complexity to Worlds
  • Initially programs were not interactive
  • program ran and user watched
  • Then learned how to make programs interactive
  • add events to program
  • when event occurs an event handler method is
    called
  • adds variability to programs
  • not always the same thing happening

4
Event Driven Programming
  • a whole philosophy of programming called event
    driven programming
  • flow of program controlled by external events as
    opposed to things internal to the program
  • very important in user interfaces
  • the parts of programs that interact with real
    users
  • think about using a computer
  • the operating system ( Windows XP, Mac OS X, etc.
    ) has a large component that is event driven
  • responding to your actions. (Move mouse, click,
    type)

5
Internal Decision Making
  • To build more complex programs and animations
    external decision making is needed in the form of
    events
  • But also need internal decision making
  • important in many types of programs
  • good examples are simulations and games

6
Examples of Decisions
  • A race car simulation
  • driver provides input from steering wheel, gas
    pedal, and brake
  • if car stays on road display next section of road
  • if car hits another car, crash
  • if car goes too far off road, crash
  • if pass checkpoints, more time

7
Logical Expressions
  • Program must make decision based on current
    conditions
  • on road? time left? intersected another car?
  • Conditions are checked in a logical expression
    (also called a boolean expression)
  • the logical expression evaluates to true or false
  • car on road -gt true
  • car passed finish line -gt false
  • car intersected any other cars -gt false

8
If/Else
  • In Alice, a logical expression is used as the
    condition in an If/Else control structure .
  • Decisions (using If/Else) are used in
  • functions
  • methods

IF
ELSE
9
Example Boolean Question
  • Suppose you are building a simulation system used
    to train flight controllers.
  • One of the tasks of a flight controller is to be
    alert for possible collisions in the flight
    space.

10
Problem Description
  • two aircraft
  • biplane and helicopter
  • as biplane moves towards helicopter make sure
    they do not collide
  • if they are too close, they need to adjust
    altitude (height) or course (direction of travel)

11
Problem Description
  • two factors in determining whether two aircraft
    are in danger of collision
  • total distance between them
  • center point to center point
  • vertical distance between them
  • differences in height
  • both distances must be greater than some safe
    distance otherwise aircraft are too close

red line total distance green line vertical
distance
a method or a function? class level or world
level?
12
tooCloseByTotalDistance
isTooCloseByTotalDistance Parameters
aircraft1, aircraft2, minDistance return true if
the distance between aircraft1 andaircraft2 is
less than minDistance else return false
  • Whether the distance between aircraft1 and
    aircraft2 is less than minDistance is an example
  • of a boolean expression.
  • It is either true of false
  • return result of expression

13
Steps in building function
  • Select the world object and click on the
    functions tab
  • select new function

14
Steps in building function
  • type in name of function
  • pick Boolean for the return type (function will
    return true or false)
  • add parameters for 2 objects (the aircraft) and a
    number (min distance)

15
Steps in building function
  • function returns true
  • want to return if distance between aircraft is
    less than minDistance
  • select World functions
  • pick a lt b and drag to replace true
  • example of a relational operator

16
Steps in building function
  • replace true
  • fill in a and b with dummy values (like 1)

17
Steps in building function
  • would like to drag aircraft1 parameter and call
    distance to function
  • cant ?
  • select biplane and the distance to function
  • drag that to replace 1

18
Steps in building function
  • replace dummy objects with parameters aircraft1
    and aircraft2
  • replace second 1 with minDistance

19
Completed Method
20
Using if / else
  • in World.my first method
  • check to see if the aircraft are too close
  • IF they are then avoid a collision
  • hard code motion OR
  • create a method to avoid the collision
  • to use if / else in program drag if /else from
    bottom

21
Using if / else
  • if statement tests a boolean expression
  • can be a relation operator
  • OR a function that returns a boolean
  • OR a boolean value (true / false)

22
Completing the if / else
  • replace parameters with biplane and helicopter
  • call method avoid collision if the condition is
    true
  • nothing to do if they are not to close

23
Completing avoid collision
  • assume if aircraft are too close one moves up and
    one moves down to increase the vertical distance
    between them
  • elevation separation
  • does it matter which moves up and which moves
    down?

24
using if / else is avoid collision
  • Move the aircraft that is above the other up

25
isTooCloseVerticalDistance
  • check to see if aircraft are in the same strata
  • to find difference in altitude use the built-in
    distance above function
  • dont know which aircraft is above the other
  • to avoid possible negative value, use the
    absolute value of the distance (World level
    function)

26
Adding Motion
  • create a method that moves an aircraft forward
    some distance
  • if the two aircraft are closer than twice the
    distance of the move
  • avoid collision if they are the vertical distance
    between them is too small
  • move aircraft forward twice the distance

27
forward And Check Collision
28
check For Height Collision
Write a Comment
User Comments (0)
About PowerShow.com