Lab 4 Broomball alternative lab: 4.9.1 or 4.9.2

advertisement
Lab 4 Advanced: Broomball
Objective: To gain experience using conditionals.
The Scenario. We would like you to write a program that emulates a broom that can sweep an
object around the screen. The broom will be represented by a small ball with diameter 10 that appears
when the mouse is pressed and disappears when the mouse is released. While the mouse is depressed,
the broom (i.e. ball) is dragged along by the mouse (the upperleft corner of the broom should always be
at the mouse location). In the image below, the broom is partly hidden underneath the cursor arrow.
The “dirt” you are sweeping will be represented by a filled square (30 pixels to a side) shown on
the screen. If the broom bumps into the square while being dragged (as shown in the image above), then
the square will move the same distance (and in the same direction) as the broom, as it would if you had a
real broom.
Notice that we are asking you to have the broom push the square, rather than have the mouse drag
the square. Thus if the mouse is depressed when it is not over the square, but then is dragged toward the
square, the square should be pushed ahead of the mouse only when the circle that represents the broom
is moved into a position where it would overlap with the square. Once this happens, the square will be
pushed in the same direction and for the same distance as the mouse was just moved. As a result, when
the mouse (and the circle) stop moving, the mouse and the circle will still not be on top of the square. You
can use the method overlap to determine if the broom has been moved over the square.
As you write your program, you should not worry about the possibility that the mouse is pointing
at the square when it is first depressed. If you do this, we expect that the code you write will result in
behavior that looks more like dragging than pushing if someone does try depressing the mouse while
pointing at the square. That is, in this case, the circular broom will appear in a position overlapping the
square and the two shapes will move around the screen together as you drag the mouse.
One warning: If you move the broom diagonally while pushing the square, the square will have a
tendency to slide off of the broom. This is because movements parallel to the side of the square will not
affect the square (and the computer is so fast that some of your diagonal moves will end up parallel to the
square). This is a “feature” not a “bug” (this is what happens with real brooms), so don’t worry about it as
long as the square generally is moved along with the broom.
Summary:
1) The program begins( ) by creating a box (30 pixels) and a “broom” (oval, 10 pixels diameter) and
hides the broom.
2) onMousePress(), the broom is shown and moved to the cursor position, also the cursor position
is saved in lastPoint
3) onMouseDrag(), the broom again moves to the current position, and the code for “dragging” the
box that we developed in lecture is performed, but only if the broom overlaps the box.
4) onMouseRelease(), the broom is hidden once again
5) One Point Extra Credit: When you complete the above, modify your program to create a second
box, 100 pixels or so large, that represents a “goal”. If the broom succeeds in pushing the box into
the goal, a message such as “GOAL!” is displayed, and the box is reset to a new random position on
the screen. Another Point? Count/show how many times the broom contacts box…HARD!!!
Lab 4 Alternates: Do one from the following
Download