CompSci 4Chap 6 Tips & TechniquesOct 20, 2005Prof. Susan RodgerNote: thanks to Wanda Dann and Steve Cooper for slide ideasAnnouncements• Read Chapter 7, Sec 1 for next time• Don’t use copy to copy an object– We will learn why later– Instead, import the object twice from the class folderWhat we will do today• Lecture on Chap 6, Tips and Techniques– Random numbers and random motion– We will also learn about variables• 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 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?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 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