DOC PREVIEW
Duke CPS 004 - Chap 7 Sec 1

This preview shows page 1-2-22-23 out of 23 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CompSci 4Chap 7 Sec 1Oct 23, 2007Prof. Susan RodgerAnnouncements• Read Chapter 7, Sec 2, Reading quiz due• Registration time coming up – CompSci 6– CompSci 4 prepares you to take CompSci 6– CompSci 6 need to know - Objects, methods, conditionals (if), repetition (loop), list or arrays (we will do)– Will review these topics in CompSci 6 with Java• Assignment 5 due today• Assignment 6 out, Due Nov. 7• Today– Definite loops (Chap 7.1)– More on variables (Timers/counters)Repetition• In many kind of animations, especially simulation and games, some actions happen again and again–Example• Games where targets randomly appear and are caught or shot down, then appear elsewhere• Actions are made to happen again and again by running an instruction or method more than onceExample• Bunny sneaks into garden and wants to eat broccoli. Bunny needs to hop several times over to broccoliBunny.hop• Makes bunny hop up and down, making a sound and traveling .8 meters total• See code in book• How do we get bunny to hop many times over to the broccoli?One solution• What is the problem with this solution?Counted Loop• A counted loop is an alternative way to write repetitive code• Repeats instructions a counted number of timesDemo - Code to hop 6 times• The loop instruction executes a definite number of times, specified by a count• Using a loop instruction– Saves time– Is convenient, easy to change the count– Can use a function in place of the count (must return a number)Infinity times….• If “infinity times” is selected for a loop, loop will run until the program is shut downExample• What happens if we make the other bunny hop up and down infinity times?How do we fix this?• How do we get both bunnies to move, one infinitely and one definitely?• NOTE: Be Very Careful when using infinite loop! If something goes forever, it doesn’t stop!More Complicated Loops• It is possible to place a loop within another loop statement, this is nested loops• Example in book: double ferris wheelDemo -Ferris Wheel nested loopsReview: What is a Variable?• Property that can be changed using setDrag isShowing tile into editor and select new value falseProblem• Given a cow that can randomly appear and disappear.• Want to add a score to count the number of times user clicks on the cow.• User gets specified amount of time to click (timer).• Cow stops moving when time is up.• User wins if a target number of clicks is achieved in the specified time. Cow tells user if they won or not.Solution• Add a new 3D text object– will keep track of times cow clicked on– type in “score,” as the name of the 3D text object– Then change its text value to 0Add Mutable Variables• What does Score need to keep track of?– current value– final value– increment value• Add three mutable variablesMutable Variables Added• Three variables added• Write method to initialize them• Call to initializeIncrement Counter• Add an event to increment score's valuewhen mouse is clicked on cow• Need to write a method to increase the score value – both variable and text displaying score must be changedFirst, change score.value• Drag value over and set to score.value• Use math to increase by incrementSecond, change the text value• Each 3D text has a text value• Drag this field and set•A world built-in function can be used to display the number value as a stringAlmost Done…• Add another 3D text to just say the word score– I named it scoreText– Then changed text value to “score”• Add code to repeat until target is reached• Cow appears at endClassWork• Start with ClickACow.a2w• Add a Score• Add a Timer – similar to score– Start at high value (say 20)– Count down instead• Game is over when Timer runs down– If Target score is reached – you


View Full Document

Duke CPS 004 - Chap 7 Sec 1

Documents in this Course
Lecture

Lecture

18 pages

Chapter 7

Chapter 7

18 pages

Chapter 9

Chapter 9

15 pages

Java 1

Java 1

24 pages

Java 3

Java 3

11 pages

Lecture

Lecture

10 pages

Chapter 4

Chapter 4

28 pages

Chap 2

Chap 2

16 pages

Graphics

Graphics

20 pages

Lecture

Lecture

12 pages

HTML

HTML

16 pages

Java 1

Java 1

6 pages

Chapter 4

Chapter 4

16 pages

The Plan

The Plan

25 pages

Lecture

Lecture

16 pages

Chapter 6

Chapter 6

21 pages

Lecture

Lecture

18 pages

Lecture

Lecture

23 pages

Lecture

Lecture

16 pages

Lecture

Lecture

19 pages

Lecture

Lecture

12 pages

Lecture

Lecture

5 pages

Lecture

Lecture

26 pages

Lecture

Lecture

16 pages

Chapter 7

Chapter 7

23 pages

Lecture

Lecture

21 pages

Lecture

Lecture

4 pages

Lecture

Lecture

4 pages

Lecture

Lecture

8 pages

Lecture

Lecture

4 pages

Lecture

Lecture

10 pages

Chapter 4

Chapter 4

32 pages

Java

Java

4 pages

CompSci 4

CompSci 4

18 pages

Lecture

Lecture

26 pages

CompSci 4

CompSci 4

12 pages

HTML

HTML

17 pages

Lecture

Lecture

16 pages

Chapter 5

Chapter 5

22 pages

Lecture

Lecture

4 pages

Chapter 4

Chapter 4

10 pages

Chapter 2

Chapter 2

15 pages

Chapter 8

Chapter 8

14 pages

Lecture

Lecture

15 pages

Load more
Download Chap 7 Sec 1
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chap 7 Sec 1 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chap 7 Sec 1 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?