📄 subcanvas.java~42~
字号:
Image fire = null;
for (int j = 0; j < this.enemyVector.size(); j++) {
EnemySub enemySub = (EnemySub) enemyVector.elementAt(j);
//当生命周期结束
if (!enemySub.getLifeState()) {
fire = ResObj.createImage("/res/fire.png");
enemySub.setImage(fire, fire.getWidth(),
fire.getHeight());
enemyVector.removeElementAt(j);
//消灭一艘敌人潜艇, 同时更新监听数组
SpriteChanged spriteChanged = new SpriteChanged(enemySub,
layerManager);
spriteChanged.start();
spriteChanged = null;
enemySub = null;
this.nCurrentEnemy--;
this.nTotalEnemy--;
} else {
enemySub.tick();
}
}
fire = null;
}
/**
* 触发玩家船只
*/
private void tickShip() {
if (!ship.getLifeState()) {
nState = GAME_OVER;
}
}
/**
* 触发鱼雷
*/
protected void tickTorpedo() {
//触发玩家发射的鱼雷
//如果某个鱼雷元素已经结束生命周期, 则置null, 并从数组中删除
for (int j = 0; j < this.torpedoVector.size(); j++) {
Torpedo torpedo = (Torpedo) torpedoVector.elementAt(j);
//当生命周期结束
if (!torpedo.getLifeState()) {
torpedoVector.removeElementAt(j);
this.layerManager.remove(torpedo);
torpedo = null;
} else {
torpedo.tick();
}
}
//触发敌方潜艇发射的鱼雷
for (int j = 0; j < this.enemyTorpedoVector.size(); j++) {
Torpedo torpedo = (Torpedo) enemyTorpedoVector.elementAt(j);
//当生命周期结束
if (!torpedo.getLifeState()) {
enemyTorpedoVector.removeElementAt(j);
this.layerManager.remove(torpedo);
torpedo = null;
} else {
torpedo.tick();
}
}
}
/**
* 清楚游戏中的数据信息
*/
private void clear() {
this.nLevel = 4;
this.nTotalEnemy = this.nLevel * 10;
this.nCurrentEnemy = 0;
nTriggerCount = 0;
nDensity = nLevel - 1;
this.nCurrentEnemyLimit = this.nLevel * 2;
}
/**重新设置图层显示区域
* @param xShip 玩家船只的x位置
* @param width 玩家船只的宽度
*/
public void adjustViewWindow(int xShip, int width) {
if (this.bUserViewWindow) {
xPosOfView = xShip + (width / 2) - (nViewWidth / 2);
if (xPosOfView < 0) {
xPosOfView = 0;
}
if (xPosOfView > (WORLD_WIDTH - nViewWidth)) {
xPosOfView = WORLD_WIDTH - nViewWidth;
}
layerManager.setViewWindow(xPosOfView, yPosofView,
nViewWidth, nViewHeight);
}
}
/**
* 画布重画事件,替代Canvas中的paint()事件, 根据不同的游戏状态(nState)画出游戏画面
*/
public synchronized void paintCanvas(Graphics g) {
switch (nState) {
case GAME_INIT: //游戏第一次启动
g.setColor(255, 255, 255);
g.fillRect(0, 0, mainWidth, mainHeight);
//重新绘制图层
if (this.layerManager != null) {
this.layerManager = null;
this.layerManager = new LayerManager();
if (bUserViewWindow) {
this.layerManager.setViewWindow(xPosOfView, yPosofView,
nViewWidth, nViewHeight);
}
}
//重新绘制玩家潜艇
if (ship != null) {
ship = null;
ship = new Ship(this, ResObj.createImage("/res/ship.png"),
getWidth() / 3, 0, layerManager);
}
//创建背景图层
this.createSeaBottom();
this.layerManager.append(ship);
ship.setHpLife(this.nLevel * 10);
this.createSeaBackground();
nState = GAME_RUN;
break;
case GAME_RUN: //游戏处于运行状态
g.setColor(255, 255, 255);
g.fillRect(0, 0, mainWidth, mainHeight);
g.setColor(255, 0, 0);
float fLifePercent = 100 * ship.getHpLife() / (nLevel * 10);
StringBuffer str = new StringBuffer("Life:" + fLifePercent + "%");
g.drawString(str.toString(), 0, 0, Graphics.LEFT | Graphics.TOP);
if (rt.freeMemory() < (rt.totalMemory() * 4 / 5)) {
rt.gc();
}
break;
case GAME_NEXTROUND: //下一轮游戏
//更新数据
ship.setHpLife(nLevel * 10);
ship.setPosition(mainWidth / 3, 0);
if (this.nLevel >= 4) {
nState = GAME_OVER;
} else {
this.nLevel++;
controller.handleEvent(UIController.EventID.EVENT_NEXTROUND);
//目前游戏只设计了四级关卡
this.nTotalEnemy = this.nLevel * 10;
this.nCurrentEnemy = 0;
nTriggerCount = 0;
this.nCurrentEnemyLimit = this.nLevel * 2;
//暂时删除layerManager中所有文件,清空鱼雷与敌人潜艇数据
//为更新图层做准备
this.torpedoVector.removeAllElements();
this.enemyVector.removeAllElements();
//更新海底图层数据
this.nDensity = (nDensity + 1) % 4;
this.hideNotify();
nState = GAME_INIT;
}
break;
case GAME_OVER: //游戏结束
threadAlive = false;
g.setColor(0, 0, 0);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(255, 0, 0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.drawString("Game Over", getWidth() / 2, getHeight() / 2,
Graphics.BASELINE | Graphics.HCENTER);
this.flushGraphics();
try {
thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
this.layerManager = null;
this.removeCommand(this.pauseCommand);
this.addCommand(startCommand);
controller.handleEvent(UIController.EventID.EVENT_EXIT);
return;
default:
break;
}
//在缓冲区重画
this.layerManager.paint(g, 0, 0);
this.flushGraphics();
}
/**
* 创建海水背景
*/
private void createSeaBackground() {
if (this.nDensity >= 4) {
nDensity = 0;
}
//创建海水背景图
Image image = ResObj.createImage("/res/layer" +
this.nDensity + ".png");
layerSeaback = new TiledLayer(WIDTH_IN_TILES, HEIGHT_IN_TILES,
image, TILE_WIDTH, TILE_HEIGHT);
for (int row = 0; row < HEIGHT_IN_TILES; row++) {
for (int column = 0; column < WIDTH_IN_TILES; column++) {
layerSeaback.setCell(column, row,
ResObj.createRandom(
NUM_DENSITY_LAYER_TILES) +
1);
}
}
layerSeaback.setPosition(0, SHIP_HEIGHT);
layerManager.append(layerSeaback);
image = null;
}
/**
* 创建海底背景
*/
private void createSeaBottom() {
Image bottomTitles = ResObj.createImage("/res/bottom.png");
//将图片bottomTitles切成指定大小(TILE_WIDTH, TILE_HEIGHT)
//创建一个指定维数(1, WIDTH_IN_TILES)的背景数组
TiledLayer layer = new TiledLayer(WIDTH_IN_TILES, 1,
bottomTitles, TILE_WIDTH, TILE_HEIGHT);
for (int column = 0; column < WIDTH_IN_TILES; column++) {
//将海底图层数组中的每个小格用原始图片的第i块来填充
int i = ResObj.createRandom(NUM_DENSITY_LAYER_TILES) + 1;
layer.setCell(column, 0, i);
}
layer.setPosition(0, mainHeight - bottomTitles.getHeight());
layerManager.append(layer);
bottomTitles = null;
}
public Ship getShip() {
return this.ship;
}
public void setActive() {
this.showNotify();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -