1 / 16

Lecture 12 or President’s Day Holiday

Lecture 12 or President’s Day Holiday. Chapter 7.2 While Loops Finish up our race of the penguins. Hint to getting while loops right. Humans naturally think “until” I’ll keep spending until I run out of money (balance <= 0) I’ll keep dancing until I fall asleep

javier
Download Presentation

Lecture 12 or President’s Day Holiday

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 12 or President’s Day Holiday • Chapter 7.2 While Loops • Finish up our race of the penguins

  2. Hint to getting while loops right • Humans naturally think “until” • I’ll keep spending until I run out of money (balance <= 0) • I’ll keep dancing until I fall asleep • Computers use “while” loops – the opposite • I’ll keep spending while I still have money (balance > 0) • I’ll keep dancing while I am not asleep • While I have a dirty dish, I’ll keep washing dishes

  3. Let’s have a race… • A • Wind up penguin (he just goes) • While loop with “walk and spin” inside it • Jet-pack penguin2 (controlled by <- event) • Moves forward .5 meters • Race to a stop sign (within 2 meters) • Whenever someone gets within 2 meters • Stop looping (going)

  4. When should we keep going?e.g. while loop expression true(P1: wind up, P2: jet pack)

  5. Which while loop header (tile) would you use to control the “going”?

  6. What does the other one do? This would STOP the game (evaluate to false) when • Both penguins must be close to the stop sign • Either penguin is close to the stop sign • Neither penguin is close to the stop sign • I don’t know

  7. Truth Table for OR logical operator

  8. Truth Table for AND logical operator

  9. Let’s look at the code I wrote:

  10. The jet pack penguin (P2) can move forward on a <- event when • Neither penguin is close to the stop sign • The windup penguin is close, but the jet pack penguin isn’t • The jet pack penguin is close, but the windup penguin isn’t • Any time (any possible situation of TT, TF, FT, FF)

  11. Both penguins stop moving when someone “close”To fix this we’d need to create how many of the following? • Method for <- event handler • Method to be called by the windUpAndGo method • If statement in <- event handler • If statement in penguin move method • If statement in windUpAndGo method

  12. Which if statement would you want and why?Allow to move when… C) Both A and B D) Neither A nor B E) I don’t know

  13. Let’s Build This…

  14. 2 meters from the stop sign? That seems far!(3-D object representation trickiness) • Stop Sign center: in middle SIGN • Penguin center: in middle of FEET • I want to control stopping by distance of penguin center from BASE of stop sign! • Use math – again SQRT( c*c – b*b) SQRT (penguin.distanceTo(stopSign) *penguin.distanceTo(stopSign) – stopSign.height * stopSign.height)

More Related