DOC PREVIEW
Duke CPS 004 - Lecture

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 6 Tips & TechniquesMar 24, 2005Prof. Susan RodgerNote: thanks to Wanda Dann and Steve Cooper for slide ideasAnnouncements• Read Chapter 7, Sec 1 for next time• Survey from March 3• More disk space?• Don’t use copy to copy an objectWhat we will do today• Lecture on Chap 6, Tips and Techniques– Random numbers and random motion• ClassworkRandom Numbers• Random numbers are used in certain kinds of computer programs•Examples– Security for web applications– Encryption for satellite transmissions– Gaming programs• We will look at examples of using random numbers in animationsBuilt-in functions• Alice provides built-in functions for generating random numbersExample• Move chicken forward a random amount• The random number function returns a fractional value between 0 and 1Demo: A range of values• Can specify a different range of values by specifying a minimum and maximumvalue• In this example, the random number will be a fractional value between 1 and 5Demo: Integers (whole numbers)• To generate a random integer value– Select integerOnly from the more option and make it trueRandom Hopping• Rabbit hops (moves up) a random amount• Rabbit comes back down to the ground, the same random amount• What happens? How do we fix it?Variable - in a method• A variable in a method – Stores a value– Has an initial value– Its value can be changed (set)– Its value can be used only in this method• To create a variable in a method– Click on create variable– Give an initial value• To use a variable’s value– Drag the variable into placeExample – create a variable• distance – will store distance bunny is to move upSetting a Variable’s value• Drag variable down and select value•ResultUse Variable’s value - Demo• Drag and drop distance into places where you want to use its valueSet Variable to Random Value - Demo• Distance is set a random value• Same value is then used to move up and downNote: Do Not use other types of variables yet!Random Motion• In some animations, we want an object to move to a random location. We call this random motion.• For example, a goldfish swimming in a random motion.Six Possible Directions• Six move directions are possible– Forward, backward, left, right, up, down• We will eliminate backward, fish do not swim backward• To simplify code, take advantage of negative numbers– This instruction moves the goldfish rightStoryboard• Only three move instructions needed– Up (move down if negative)– Left (move right if negative)– Forward (no backward motion)• Two parameters (min, max) to restrict motion of fish to nearby locationfish.randomMotionParameters: min, maxDo togetherfish move up (or down) random distancefish move left (or right) random distancefish move forward random amountrandomMotion• Minimum distance for move forward is 0Demo• To call randomMotion method, specify min and max valuesDemo• Repeating the random fish motion over and over again…. (more on this next chapter)• Change world.my first methodClasswork today –Where are methods and functions called?1. Setup all the objects (Do not use copy!)2. For 3D text score1 and score21. AddOne – update the score2. Do not need ball or goals for this3. Each goal – IsInGoal – return true or false4. ToyBall – startInMiddleClasswork (cont)5. World method – moveAndCheckScore1. Move ball a small amount2. Check if ball is in goal1, if so update score1 and send ball to middle of field3. Check if ball is in goal2, if so update score2 and send ball to middle of field6. Events – move ball around7. Winner – add 2 3D texts that say “Winning!”1. In 5, check if there is a winning score and updateNew Classwork• Write world method RandomMove– Loop 100 times• Move ball randomly a little in 2 directions• If ball is in goal, then update score, move ball to middle and check winning message• Add an event – if type R, then


View Full Document

Duke CPS 004 - Lecture

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

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 Lecture
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 Lecture 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 Lecture 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?