📄 bombermap.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.Vector;
/**
* File: BomberMap.java
* Copyright: Copyright (c) 2001
* @author Sammy Leong
* @version 1.0
*/
/**
* This class draws the map and handles things like bonuses and bombs.
*/
public class BomberMap extends JPanel {
/** frame object */
private BomberMain main = null;
/** game over flag */
private boolean gameOver = false;
/** background color */
private Color backgroundColor = null;
/** the map grid array */
public int[][] grid = null;
/** fire grid */
public boolean[][] fireGrid = null;
/** bomb grid */
public BomberBomb[][] bombGrid = null;
/** bonus grid */
public BomberBonus[][] bonusGrid = null;
/** bombs */
private Vector bombs = null;
/** bonuses */
private Vector bonuses = null;
/**
* Bomb info class
*/
private class Bomb {
public Bomb(int x, int y) {
r = (x >> BomberMain.shiftCount);
c = (y >> BomberMain.shiftCount);
}
public int r = 0;
public int c = 0;
}
/**
* Bonus info class
*/
private class Bonus {
public Bonus(int x, int y) {
r = (x >> BomberMain.shiftCount);
c = (y >> BomberMain.shiftCount);
}
public int r = 0;
public int c = 0;
}
/** image handles for the map images */
private static Image[][] mapImages = null;
/** bomb images */
public static Image[] bombImages = null;
/** fire images */
public static Image[][] fireImages = null;
/** fire brick images */
public static Image[][] fireBrickImages = null;
/** bonus images */
public static Image[][] bonusImages = null;
/** fire type enumerations */
public static final int FIRE_CENTER = 0;
public static final int FIRE_VERTICAL = 1;
public static final int FIRE_HORIZONTAL = 2;
public static final int FIRE_NORTH = 3;
public static final int FIRE_SOUTH = 4;
public static final int FIRE_EAST = 5;
public static final int FIRE_WEST = 6;
public static final int FIRE_BRICK = 7;
/** grid slot type enumerations */
public static final int BONUS_FIRE = -4;
public static final int BONUS_BOMB = -3;
public static final int NOTHING = -1;
public static final int WALL = 0;
public static final int BRICK = 1;
public static final int BOMB = 3;
/** random level generator */
private static BomberRandInt levelRand = null;
/** random bonus generator */
private static BomberRandInt bonusRand = null;
/** current level */
public static int level = 0;
/** rendering hints */
private static Object hints = null;
static {
/** if java runtime is Java 2 */
if (Main.J2) {
/** create the rendering hints for better graphics output */
RenderingHints h = null;
h = new RenderingHints(null);
h.put(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
h.put(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
h.put(RenderingHints.KEY_ALPHA_INTERPOLATION,
RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
h.put(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
h.put(RenderingHints.KEY_COLOR_RENDERING,
RenderingHints.VALUE_COLOR_RENDER_QUALITY);
hints = (RenderingHints)h;
}
/** create the level random generator */
levelRand = new BomberRandInt(0, 100);
/** create the bonus random generator */
bonusRand = new BomberRandInt(0, 7);
/** creat the image objects array */
mapImages = new Image[3][3];
/** create the bomb objects array */
bombImages = new Image[2];
/** create the fire objects array */
fireImages = new Image[8][8];
/** create the fire brick objects array */
fireBrickImages = new Image[3][8];
/** create the bonus image objects array */
bonusImages = new Image[2][2];
try {
String[] strs = new String[3];
/** load the map images */
for (int i = 0; i < 2; i++) {
strs[0] = BomberMain.RP + "Images/BomberWalls/" + (i + 1);
strs[1] = BomberMain.RP + "Images/BomberBricks/" + (i + 1);
strs[2] = BomberMain.RP + "Images/BomberFloors/" + (i + 1);
for (int j = 0; j < 3; j++) {
if (i == 0) strs[j] += ".jpg";
else strs[j] += ".gif";
}
mapImages[i][0] = Toolkit.getDefaultToolkit().getImage(
new File(strs[0]).getCanonicalPath());
mapImages[i][1] = Toolkit.getDefaultToolkit().getImage(
new File(strs[1]).getCanonicalPath());
if (i == 0) mapImages[i][2] = null;
else
mapImages[i][2] = Toolkit.getDefaultToolkit().getImage(
new File(strs[2]).getCanonicalPath());
}
String str = null;
/** load the bomb images */
for (int i = 0; i < 2; i++) {
str = BomberMain.RP + "Images/BomberBombs/" + (i + 1) + ".gif";
bombImages[i] = Toolkit.getDefaultToolkit().getImage(
new File(str).getCanonicalPath());
}
/** load the fire images */
for (int t = 0; t < 7; t++) for (int i = 0; i < 8; i++)
{
str = BomberMain.RP + "Images/BomberFires/";
if (t == FIRE_CENTER) str += "C";
else if (t == FIRE_VERTICAL) str += "V";
else if (t == FIRE_NORTH) str += "N";
else if (t == FIRE_HORIZONTAL) str += "H";
else if (t == FIRE_EAST) str += "E";
else if (t == FIRE_WEST) str += "W";
else if (t == FIRE_SOUTH) str += "S";
if (t == FIRE_BRICK) fireImages[t][i] = null;
else {
str += (i + 1) + ".gif";
fireImages[t][i] = Toolkit.getDefaultToolkit().getImage(
new File(str).getCanonicalPath());
}
}
int f = 0;
/** load the fire brick images */
for (int i = 0; i < 2; i++) for (f = 0; f < 8; f++)
{
str = BomberMain.RP + "Images/BomberFireBricks/" +
(i + 1) + (f + 1) + ".gif";
fireBrickImages[i][f] = Toolkit.getDefaultToolkit().getImage(
new File(str).getCanonicalPath());
}
/** load the bonus image sprites */
for (int i = 0; i < 2; i++) for (f = 0; f < 2; f++)
{
str = BomberMain.RP + "Images/BomberBonuses/" +
(i == 0 ? "F" : "B") + (f + 1) + ".gif";
bonusImages[i][f] = Toolkit.getDefaultToolkit().getImage(
new File(str).getCanonicalPath());
}
}
catch (Exception e) { new ErrorDialog(e); }
}
public BomberMap(BomberMain main) {
this.main = main;
/** generator random level */
level = levelRand.draw() % 2;
MediaTracker tracker = new MediaTracker(this);
/** prepare the images */
try
{
int counter = 0;
/** load the map images */
for (int i = 0; i < 2; i++) for (int j = 0; j < 3; j++) {
if (mapImages[i][j] != null)
{ tracker.addImage(mapImages[i][j], counter++); }
}
/** load the bomb images */
for (int i = 0; i < 2; i++)
tracker.addImage(bombImages[i], counter++);
/** load the fire brick images */
for (int i = 0; i < 8; i++)
fireImages[FIRE_BRICK][i] = fireBrickImages[level][i];
/** load the fire images */
for (int i = 0; i < 8; i++) for (int j = 0; j < 8; j++)
tracker.addImage(fireImages[i][j], counter++);
/** wait for images to finish loading */
tracker.waitForAll();
} catch (Exception e) { new ErrorDialog(e); }
bombs = new Vector();
bonuses = new Vector();
/** create the fire grid */
fireGrid = new boolean[17][17];
/** create the bomb grid */
bombGrid = new BomberBomb[17][17];
/** create the bonus grid */
bonusGrid = new BomberBonus[17][17];
/** create the map grid */
grid = new int[17][17];
/** fill the map with walls by alternating r by c */
for (int r = 0; r < 17; r++) for (int c = 0; c < 17; c++) {
/** if it's the edge */
if (r == 0 || c == 0 || r == 16 || c == 16) grid[r][c] = WALL;
else if ( (r & 1) == 0 && (c & 1) == 0 ) grid[r][c] = WALL;
else grid[r][c] = NOTHING;
fireGrid[r][c] = false;
bombGrid[r][c] = null;
bonusGrid[r][c] = null;
}
int x, y;
BomberRandInt ri = new BomberRandInt(1, 15);
/** generate random bricks */
for (int i = 0; i < 192 * 2; i++)
{
x = ri.draw();
y = ri.draw();
if (grid [x][y] == NOTHING)
grid [x][y] = BRICK;
}
/** clear corners so players can stand there */
grid [ 1][ 1] = grid [ 2][ 1] = grid [ 1][ 2] =
grid [ 1][15] = grid [ 2][15] = grid [ 1][14] =
grid [15][ 1] = grid [14][ 1] = grid [15][ 2] =
grid [15][15] = grid [15][14] = grid [14][15] = NOTHING;
/** create background color */
backgroundColor = new Color(52, 108, 108);
/** set panel size */
setPreferredSize(new Dimension(17 << BomberMain.shiftCount,
17 << BomberMain.shiftCount));
/** double buffer on */
setDoubleBuffered(true);
setBounds(0, 0, 17 << main.shiftCount, 17 << main.shiftCount);
setOpaque(false);
/** add the map to the bottom layer */
main.getLayeredPane().add(this, 1000);
}
/**
* Sets game over flag on
*/
public void setGameOver() {
gameOver = true;
paintImmediately(0, 0,
17 << BomberMain.shiftCount, 17 << BomberMain.shiftCount);
}
/**
* Creates a bonus.
* @param x x-coordinate
* @param y y-coordinate
* @param owner owner
*/
public synchronized void createBonus(int x, int y) {
int _x = (x >> BomberMain.shiftCount) << BomberMain.shiftCount;
int _y = (y >> BomberMain.shiftCount) << BomberMain.shiftCount;
int type = bonusRand.draw();
/** create bonus : 0 = fire; 1 = bomb */
if (type == 0 || type == 1) {
bonusGrid[_x >> BomberMain.shiftCount][_y >> BomberMain.shiftCount] =
new BomberBonus(this, _x, _y, type);
bonuses.addElement(new Bonus(_x, _y));
}
}
/**
* Removes a bonus.
* @param x x-coordinate
* @param y y-coordinate
*/
public synchronized void removeBonus(int x, int y) {
int i = 0, k = bonuses.size();
int r = (x >> BomberMain.shiftCount);
int c = (y >> BomberMain.shiftCount);
Bonus b = null;
while (i < k) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -