Java Reference
In-Depth Information
I try to not restrict the user's actions unless I have a good, user-friendly reason to do so. So I would
not normally have removed the ability to resize the window. Again, though, I thought it would make a
good example, and it simplified the programming a bit. Restricting the user's actions to make your own
life easier is poor practice though (it's a form of intellectual laziness) - don't do it unless you're doing it
just to show how it's done.
One last thing to note about the ShootingGallery class (and the game) is that it has no start or stop
control. It just runs continuously without end. I left out a number of common features that you might
expect in a game so that you can implement them yourself. I've added a list of those ideas near the end
of the chapter.
Listing 12-6. The ShootingGalleryPanel class
package com.bryantcs.examples.videogames;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ShootingGalleryPanel extends JPanel implements ActionListener{
private static final long serialVersionUID = 1L;
// The cursor, controlled by the player
private ShootingGalleryShooter shooter;
// The sprites that march down the game area
private ShootingGalleryTargetSprites sprites;
// The width of the widest target
public final static int TARGET_SPACE = 50;
// A tick counter to control the animation
private int ticks;
// Three rows of targets
static ShootingGalleryTargetRow row1, row2, row3;
// The timer, to control the game loop
private Timer timer = new Timer(20, this);
// The constructor
public ShootingGalleryPanel() {
Search WWH ::




Custom Search