DOC PREVIEW
Duke CPS 004 - Lecture

This preview shows page 1-2-20-21 out of 21 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 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 21 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 21 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 21 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CompSci 4Chap 6 Tips & TechniquesOct 19, 2006Prof. Susan RodgerNote: Lectures given by Joe Farfel and Tiffany ChenAnnouncements• Read Chapter 7, Sec 1 for next time – Reading Quiz• Don’t use copy to copy an object!– We will learn why later– Instead, import the object twice from the class folder• Lecture on Chap 6, Tips and Techniques– Random numbers and random motion– We will also learn about variablesRandom 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 true– Random value selected from 1, 2, 3, or 4 - not 5!Random 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?Local Variable - in a method•A local variable in a method – Stores a value– Has an initial value– Its value can be changed (set)– Its value can be used only in one instance of this method• To create a local 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 local 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 down• Use print to print out the value of the variableClass Variables• Use “create new variable” under properties to create a class variable for an object• This “class variable” will maintain the value throughout the running of the world unless you reset itRandom 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• Event handlers• Random values•Variables• NO


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

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

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?