Lab 5: Animation
Download and unzip lab5.zip, which contains all
the files you will need for this assignment. Open the
file Lab5.java in DrJava. All the code you write for this
assignment should be added to this file.
Be sure to test your code after completing each step.
- Insert code to make a new drawing window appear. Draw a small
ball (or other entity) in the bottom left corner of the
window.
- Modify your program so that it shows the ball move to the right,
from the bottom left to the bottom right corner of the window. Use a
variable to store the x-coordinate of the ball. Use a loop to
repeatedly draw the ball at that x-coordinate, pause for a short
amount of time, and increase the x-coordinate by some amount. Feel
free to fine tune these values until the animation feels
right.
- Modify your program so that the ball moves diagonally up and to
the right, by decreasing the ball's y-coordinate as you increase the
ball's x-coordinate.
- At the beginning of the program, prompt the user to enter the
x-velocity and then the y-velocity. Remember these values. Modify
your loop so that, whenever you adjust the x-coordinate of the ball,
you increase it by the x-velocity the user entered (instead of using
the amount you chose earlier). Likewise, increase the y-coordinate by
the y-velocity entered.
- In the real world, what goes up must come down. Modify your
program so that the ball is gradually pulled down by gravity. This is
easier than it sounds. Inside the loop, add a fixed amount to the
y-velocity. (Do you see why this will work?) The ball should now
follow a parabolic path. Feel free to fine tune your values until it
looks right.
- Draw a target somewhere on the right side of the window.
- If the ball passes near the target, display a message. Something
like "You win!" or "You hit the target! Now you'll have to pay for a
new one!"
- Modify your program so that it places the target at a random
height.
- Some Challenging Suggestions:Modify your program so that
the user can keep shooting at the target until you hit it. Then
change the height of the target and let the user continue shooting
that one. Keep track of the number of times the user hits the target.
Modify the program so that the user enters the angle and velocity of
their shot, instead of entering the x-velocity and
y-velocity.