📄 subcanvas.java~35~
字号:
package mysubmarine;
import java.util.Vector;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
/**
* <p>Title: 一个简单的潜艇游戏</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: Star Group</p>
*
* @author wangyaobsz
* @version 1.0
*/
public class SubCanvas extends GameCanvas implements Runnable,
CommandListener {
private UIController controller; //UI控制
private Graphics g; //图形设备描述符
private Thread thread; //主绘线程
private boolean threadAlive = false; //表示当前线程是否活跃
private Command startCommand;
private Command exitCommand;
private Command pauseCommand;
//图层数据
public LayerManager layerManager; //层管理器
private TiledLayer layerSeaback; //海水背景
public final int GAME_INIT = 0; //游戏初始状态
public final int GAME_RUN = 1; //游戏运行状态
public final int GAME_OVER = 4; //游戏结束状态
public final int GAME_PAUSE = 5;
public final int GAME_NEXTROUND = 9; //暂停状态
public static final byte PLAYER = 0; //敌我标识
public static final byte ENEMY = 1;
public static int nLevel = 1; //当前玩家水平
public static int nTotalEnemy = nLevel * 10; //最大敌人数量
public static int nCurrentEnemy = 0; //当前敌人数量
public static int nCurrentEnemyLimit = 0; //当前敌人数量限制
protected int nTriggerCount = 0; //拖延计数器,避免敌人新潜艇同一时刻全部产生
public static int mainWidth; //屏幕宽度
public static int mainHeight; //屏幕高度
public int nState; //游戏状态
public Vector enemyVector = new Vector();
public Vector torpedoVector = new Vector();
public Vector enemyTorpedoVector = new Vector();
public final static int TILE_WIDTH = 10; //背景 单元宽度 10px
public final static int TILE_HEIGHT = 10; //背景 单元高度 10px
private final static int WIDTH_IN_TILES = 45; //游戏域宽度(以单元宽度计算)
private final static int HEIGHT_IN_TILES = 24; //游戏域高度(以单元高度计算)
private final static int NUM_DENSITY_LAYERS = 4; //海面密度(背景图层)
public final static int SHIP_HEIGHT = 40; //船的高度
/** 整个游戏背景宽度(以象素计算 : 宽度单元数 * 宽度单元象素)*/
public final static int WORLD_WIDTH = WIDTH_IN_TILES * TILE_WIDTH;
private final static int NUM_DENSITY_LAYER_TILES = 4; //每一个密度层的TILE单元数
private final static int FRACT_DENSITY_LAYER_ANIMATE = 20;
private int nDensity; //游戏初始海水密度为0
//初始化为不使用窗口区域视野
private boolean bUserViewWindow = false;
private Ship ship = null;
private Runtime rt = null;
//LayerManager的偏移坐标
private int xPosOfView;
private int yPosofView;
private int nViewWidth;
private int nViewHeight;
public SubCanvas(UIController controller) {
super(false);
this.controller = controller;
this.g = getGraphics();
this.layerManager = new LayerManager();
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
//画布构造即建立玩家潜艇
ship = new Ship(this, ResObj.createImage("/res/ship.png"),
mainWidth / 3, 0, layerManager);
//监测运行时,以便及时运行垃圾回收
rt = Runtime.getRuntime();
//初始化游戏状态
this.nState = this.GAME_INIT; //游戏处于demo画面状态
//启动应用程序
threadAlive = true;
thread = new Thread(this);
thread.start();
}
private void jbInit() throws Exception {
//清理数据
this.clear();
mainWidth = getWidth();
mainHeight = getHeight();
//根据显示设备,设置合适的最大区和显示视野
this.xPosOfView = 0;
if (WORLD_WIDTH > mainWidth) {
//现有设备不能容纳所有游戏区域
bUserViewWindow = true;
this.nViewWidth = mainWidth;
} else {
//现有设备可以容纳所有游戏区域
this.nViewWidth = WORLD_WIDTH;
}
this.yPosofView = 0;
this.nViewHeight = mainHeight;
//设定图层显示方式
if (bUserViewWindow) {
this.layerManager.setViewWindow(xPosOfView, yPosofView,
nViewWidth, nViewHeight);
}
setCommandListener(this);
startCommand = new Command("Start", Command.OK, 1);
pauseCommand = new Command("Pause", Command.OK, 1);
exitCommand = new Command("Exit", Command.EXIT, 2);
this.addCommand(pauseCommand);
this.addCommand(exitCommand);
}
public void commandAction(Command command, Displayable displayable) {
if (command == startCommand) {
if (this.nState == GAME_OVER) {
nState = GAME_INIT;
} else {
nState = GAME_RUN;
}
this.removeCommand(this.startCommand);
this.addCommand(pauseCommand);
} else if (command == pauseCommand) {
nState = GAME_PAUSE;
this.removeCommand(this.pauseCommand);
this.addCommand(startCommand);
} else if (command == exitCommand) {
nState = GAME_OVER;
}
}
/**
* 主绘线程的运行函数,每50ms绘制一次屏幕
*/
public void run() {
//利用threadAlive标志驱动线程
while (threadAlive) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (nState == GAME_RUN) {
ship.movePosition(getKeyStates());
}
this.paintCanvas(g);
this.tick();
}
}
/**
* 键盘响应函数,响应玩家的“开火”命令
*/
protected synchronized void keyPressed(int keyCode) {
int action = getGameAction(keyCode);
if (action == Canvas.FIRE && nState == GAME_RUN) {
//玩家潜艇开火
if (ship != null) {
ship.fire();
}
}
}
/**
* 触发器,触发所有物体的运动
*/
public synchronized void tick() {
if (nState != GAME_OVER) {
//执行鱼雷触发
this.tickTorpedo();
if (nState == GAME_RUN) {
//创建并执行敌人潜艇行为
this.tickShip();
this.tickEnemySub();
if (this.nCurrentEnemy == 0 && this.nTotalEnemy == 0) {
nState = GAME_NEXTROUND;
}
}
}
}
/**
* 创建并执行敌人潜艇的运行操作
*/
protected void tickEnemySub() {
//当敌人剩余最大数量大于0,并且敌人当前数量小于并行敌人上限时
//可以添加新的敌人潜艇
if (this.nTotalEnemy >= 0 &&
this.nCurrentEnemy <= this.nCurrentEnemyLimit &&
this.nCurrentEnemy < 10) {
int iLeft = this.nTotalEnemy - this.nCurrentEnemy;
//当剩余敌人量(最大量 - 当前量)大于0的时候
if (iLeft > 0) {
int n = ResObj.createRandom(iLeft) + 1;
Image image = ResObj.createImage("/res/sub.png");
int xPosition = 0;
int yPosition = 20 + SHIP_HEIGHT +
(ResObj.createRandom(5) * mainHeight) / 5;
for (int i = 0; i < n; i++) {
//拖延标识,避免敌人新潜艇同一时刻全部产生
if (nTriggerCount >= 20) {
yPosition = SHIP_HEIGHT + (mainHeight * (i % 5)) / 5;
if (i % 2 == 0) {
xPosition = 0; //从海面位置出现
} else { //从海水中间位置出现
xPosition = WORLD_WIDTH - image.getWidth();
}
//创建一艘敌人潜艇, 同时更新监听数组
EnemySub enemySub = new EnemySub(this, image,
xPosition, yPosition, this.nLevel);
this.nCurrentEnemy++;
layerManager.insert(enemySub, 0);
this.enemyVector.addElement(enemySub);
nTriggerCount = 0;
} else {
nTriggerCount++;
}
}
image = null;
}
}
//对所有已经存在的敌人潜艇进行tick触发
Image fire = null;
for (int j = 0; j < this.enemyVector.size(); j++) {
EnemySub enemySub = (EnemySub) enemyVector.elementAt(j);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -