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