![Page 1: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/1.jpg)
CompSci 94
Chap 7 Sec 2
Oct 22, 2013
Prof. Susan Rodger
1
![Page 2: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/2.jpg)
Announcements
• No reading or reading quiz for Thursday
• Assignment 5
– Storyboard due today
– World and web page due Thursday!
• Today
– Lecture on Chap 7 Sec 2 and Tips and Tech.
• While loop – indefinite loop
• Event Loops
2
![Page 3: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/3.jpg)
Last time -Loop – definite number• What happens when this code runs?
3
![Page 4: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/4.jpg)
Repetition• Sometimes don’t know exactly how many times
a set of instructions are repeated.
• Stopping is based on a condition
• Example:
– Game of Chess, how many moves until win
– Stop: when markers are in check mate position
• Indefinite Repetition
– Where number of repetitions not known in advance
– Use while statement4
![Page 5: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/5.jpg)
While statement
• While some condition is true
– execute instructions
condition
true?
execute instructions
no
yes
exit
loop
5
![Page 6: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/6.jpg)
Example
• Common feature in popular “action films”
is a chase scene
• Example: hungry shark chasing fleeing
goldfish
– Repeat: fish swim away from shark, and shark
swim toward fish
– Shark swim distance a little more than fish
swim distance
– Eventually, shark will catch up with fish and
eat fish
6
![Page 7: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/7.jpg)
Storyboard
World.chase
While goldfish more than .5 meters from shark
Do in order
shark point at goldfish
Do together
shark swim (toward goldfish)
goldfish flee (away from shark)
shark eat goldfish
shark.Swim, shark.eat and goldfish.flee in book 7
![Page 8: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/8.jpg)
World.chase
8
![Page 9: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/9.jpg)
Shark will catch goldfish
• How do you know the shark will eventually
catch the goldfish?
• How do you know the Shark will eventually
catch up, and the loop will end
9
![Page 10: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/10.jpg)
General “Rule of Thumb”
• As a general rule, a While loop should be
written so the loop will eventually end
– Requires statements inside the loop change the
conditions of the world such that condition for
While eventually becomes false
• If While loop never ends
– Infinite while loop
10
![Page 11: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/11.jpg)
Practice – From Bunny eats Broccoli
• Replace with while loop, bunny hops until
close to broccoli (< 1 meter)
11
![Page 12: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/12.jpg)
Using While with Events• Create new event “while something is true”
• Result
We call this
a "BDE"
12
![Page 13: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/13.jpg)
BDE – Begin During End
• Event – actions occur at different times
• When the event is first true
– Begin action
• While the event is still true
– During action repeats
• When the event condition is false
– End action occurs
13
![Page 14: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/14.jpg)
Example – Penguins meeting
• While 2 penguins are > 3 meters apart
– Turn to face each other (Begin)
– Move towards each other repeatedly (During)
– Bow to each other (End)
14
![Page 15: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/15.jpg)
Penguins Event Code
15
![Page 16: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/16.jpg)
Penguin Begin and During parts
16
![Page 17: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/17.jpg)
Penguin End Part
17
![Page 18: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/18.jpg)
How do we get this event to
happen again?
• Add an event – When you press b, a
penguin moves backwards ten meters.
• Add an event – When you press c, a camera
moves back ten meters.
18
![Page 19: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/19.jpg)
Another Type of Event – When
something is true
• When you want ONE
thing to happen when
something is true
• Not listed as a new event
• Must create event “while
something is true”
• Then “change to” “when
something becomes
true”19
![Page 20: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/20.jpg)
Add in “mama Penguin”
(penguin3 resize by 2)
• Control her with arrow keys event
20
![Page 21: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/21.jpg)
Penguin3 “take” penquin
• What happens when we move penguin3
over to penguin and then steer penguin3
away?
21
![Page 22: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/22.jpg)
How do we turn off the move
over to penguin2?
• Everytime Penguin3 carries away penguin,
penguin wants to move over to penguin2
22
![Page 23: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/23.jpg)
??
23
![Page 24: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/24.jpg)
??
24
![Page 25: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/25.jpg)
Need another condition
• Can’t turn off the BDE, it kicks in
everytime the condition is true
• Must make the BDE condition stronger –
AND another condition
25
![Page 26: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/26.jpg)
What is the difference between?
• While something is true (BDE)
• When something is true
• Add in one more event:
• Can the “when” event happen again if it
becomes true again?
26
![Page 27: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/27.jpg)
What is the difference between?
• While loop that is not an event
• While something is true (BDE, event)
• When do you use one vs the other?
• How do they work?
27
![Page 28: CompSci 94 Chap 7 Sec 2 Oct 22, 2013db.cs.duke.edu/courses/fall13/cps004/lects/cps94-131022/CompSci… · Storyboard World.chase While goldfish more than .5 meters from shark Do in](https://reader034.vdocuments.us/reader034/viewer/2022051510/5fec38dc4c0e5863c94f6dae/html5/thumbnails/28.jpg)
Classwork
• World 1 - Modify game from last time with while loop
• World 2 - Start a new world with snow background
– Add a penguin and a fish resting on the ice
– Create a BDE that moves the penguin over to the fish and
eats it (fish disappears)
– More with “when event”
28