CompSci 94 - Classwork 12 - Oct, 10, 2013
10 pts
Today's classwork goes along with Chapter 6, Tips and Techniques.
You will be using random values and variables.
Create one world with three animals, one animal that can change
colors noticeably.
(For example, if you change the color of the chicken, it is noticeable.
If you try to change the color of the bunny, you don't see the change.)
You might want to consider using cat, chicken and bunny.
You may use this or substitute other
animals or creatures (just make sure that one of them can change colors by
trying it out).
You should write four event handlers. Some may need random values.
For some you may need to create a local variable or a class variable. Think
about which one is the correct one.
- If you have a random value, you should assign it to a variable and
print
it out so you can see its value when you use it!
- One of your objects should move up a random amount
when clicked on and when clicked on a second time moves back down the same
amount). For example, consider using the bunny. If you click on the bunny, it
moves up some nonzero random amount. If you click on it a second time, it
moves down the same amount, ending up back on the ground. Repeat.
(BIG Hint:
You need a class variable to save the random value
because your event handler
is called more than once and both calls need the same value. Also
note that you need an if statement in your event handler to determine
if you need to have the bunny go up or go down.
Here is what the class variable is being used for.
If your class variable has value zero then the bunny is on the ground, and you
need to set the variable to a random number and have the bunny move up that
amount. If the class variable is not zero, then the bunny should move down
and you should reset the class variable back to zero to indicate the bunny
is on the ground. )
- One of your objects should have its head resize to twice as large if
you click on it. If you click on it again, it resizes back to normal size.
This Repeats. A third click makes its head resize to twice as large, etc.
How do you keep track of whether or not it has been resized?
- For one animal (chicken is good to use because it CAN change colors,
not all objects in Alice will change colors), if you click on the ground
have the animal change to one of 5 random colors, one of which is "No
Color".
HINT:
Generate one of five random integers 1
to 5. If the number is 1, you could change the object's color to red,
if 2, to yellow, etc.
- If you click on the third object, have it resize to one of four larger
values. If you click on it a second time, then have it resize back down
to its original size. Repeat. How do you keep track of how much to resize
it back down and whether or not it needs to get larger or smaller?
Hint: Class variable.
If you finish early, feel free to embelish your animations.