Java Reference
In-Depth Information
if (step == 2) {
g.setColor(Color.RED);
g.fillOval(drawX - 15, drawY - 15, 30, 30);
}
if (step == 3) {
g.setColor(Color.RED);
g.fillOval(drawX - 15, drawY - 15, 20, 20);
}
if (step == 4) {
g.setColor(Color.RED);
g.fillOval(drawX - 15, drawY - 15, 10, 10);
done = true;
}
} else {
// if we are done, erase the remaining circle
g.setColor(Color.WHITE);
g.fillOval(drawX + 1, drawY + 1, 30, 30);
}
step++;
}
// This is how we let the playing field
// know whether this target is done
public boolean isDone() {
return done;
}
// Did this target get hit?
public void pointInTarget(int x, int y) {
if (x > drawX -15 && x < drawX + 15 && y > drawY - 15 && y < drawY + 15) {
// A hit! So add 1 to the sore and indicate that this target is done
TargetClick.score++;
done = true;
}
// no need to do anything if not a hit
}
}
The Target class contains the logic for drawing a target and for figuring out whether a target has
been clicked. As a simplification, I determined whether the click was within a square that contains the
target spot. For a more certain method, you could check the color of the pixel the player clicked on and
award a hit if it is not white. My simplification makes the game a bit easier.
TargetClick is about as simple as a game gets. Let's move on to a more complex game.
The Shooting Gallery Game
As the title indicates, Shooting Gallery is a very simple shooting gallery game. I created it mostly to show
the concept of controlling multiple items with a single timer, to show similar objects moving at different
speeds, and to show one simple way to resolve collision detection (which means detecting when one
Search WWH ::




Custom Search