Java 7 for Absolute Beginners

(nextflipdebug5) #1
CHAPTER 12 ■ VIDEO GAMES

// Set up the sprites
sprites = new ShootingGalleryTargetSprites();
sprites.init();


// Set a horizontal layout, because we'll
// add three vertical rows to the left side
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));


// Set up and add the left-most row (the smallest and most valuable targets)
row1 = new ShootingGalleryTargetRow(sprites.getSpriteBySize(25), 50);
row1.setPreferredSize(new Dimension(TARGET_SPACE + 10, getHeight()));
add(row1);


// Set up and add the middle row (slower and worth less)
row2 = new ShootingGalleryTargetRow(sprites.getSpriteBySize(40), 25);
row2.setPreferredSize(new Dimension(TARGET_SPACE + 10, getHeight()));
add(row2);


// Set up and add the right-most row (slowest and least valuable targets)
row3 = new ShootingGalleryTargetRow(sprites.getSpriteBySize(50), 10);
row3.setPreferredSize(new Dimension(TARGET_SPACE + 10, getHeight()));
add(row3);


// Figure out how far to indent the player's cursor and add it
int shooterOffset = (3 * (TARGET_SPACE + 10));
int gameWidth = ShootingGallery.gameDimension.width;


// Set up and add the player's cursor
shooter = new ShootingGalleryShooter(gameWidth - shooterOffset);
shooter.setPreferredSize(new Dimension(gameWidth - shooterOffset, getHeight()));
add(shooter);


// Start the timer
timer.start();
}


// Let the parent component paint itself
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
}


// The game loop
@Override
public void actionPerformed(ActionEvent e) {


// increment the tick counter
ticks++;

Free download pdf