📄 submarine.java~52~
字号:
package mysubmarine;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.*;
/**
* <p>Title: 一个简单的潜艇游戏</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: Star Group</p>
*
* @author not attributable
* @version 1.0
*/
public class Submarine extends MySprite implements Collidion {
private boolean bLive = false; //生命状态
private int troop = 0; //敌我识别标志
private int levelState = 0; //自身以及子弹速度状态
private int nDirectFlag = 1; //当前图形方向. 0为左, 1为右
boolean bHurt = false;
private int keyState = 0;
private int hpLife = 15; //玩家生命值HP
private int xStep = 15;
private int yStep = 15;
private int oldX;
private int oldY;
public Submarine(MySubmarineCanvas subCanvas, Image image,
int xPos, int yPos, LayerManager layerManager) {
super(image);
this.subCanvas = subCanvas;
this.setPosition(xPos, yPos);
this.layManager = layerManager;
this.bLive = true;
//设定敌我识别
this.troop = MySubmarineCanvas.TROOP_PLAYER;
this.levelState = 3;
this.nDirectFlag = 1;
}
public void movePosition(int keyState) {
this.keyState = keyState;
//判断走向, 执行移动
x = getX();
y = getY();
oldX = x;
oldY = y;
w = getWidth();
h = getHeight();
if(subCanvas.gameState == subCanvas.GAME_RUN){
if((keyState & GameCanvas.DOWN_PRESSED)!= 0){//向下
y = y+yStep;
} else if((keyState & GameCanvas.UP_PRESSED)!= 0){//向上
y = y-yStep;
} else if((keyState & GameCanvas.LEFT_PRESSED)!= 0){//左移
if(this.nDirectFlag == 1){
setTransform(Sprite.TRANS_MIRROR);
this.nDirectFlag = 0;
}else {
x = x-xStep;
}
} else if((keyState & GameCanvas.RIGHT_PRESSED)!= 0){//右移
if (this.nDirectFlag == 0) {
setTransform(Sprite.TRANS_NONE);
this.nDirectFlag = 1;
} else {
x = x + xStep;
}
}
}
//确保图形在游戏区域
if (x > MySubmarineCanvas.WORLD_WIDTH - w) {
x = MySubmarineCanvas.WORLD_WIDTH - w;
}
if (x < 0) {
x = 0;
}
if (y > MySubmarineCanvas.WORLD_HEIGHT - h) {
y = MySubmarineCanvas.WORLD_HEIGHT - h;
}
if (y < 0) {
y = 0;
}
this.setPosition(x, y);
//处理碰撞事件
checkCollidion();
//当受到伤害时
if (bHurt) {
this.hurt();
}
//重新设置图层显示区域
subCanvas.adjustViewWindow(x, y, getWidth(), getHeight());
}
public void fire() {
if (subCanvas.torpedoVector.size() <= 10) {
//开火
Image image = ResObj.createImage("/res/tinfish3.png");
if (this.nDirectFlag == 0) {
//鱼雷方向向左
Torpedo terpedo = new Torpedo(subCanvas, image, x, y + h / 2,
this.troop, this.levelState,
this.nDirectFlag);
//添加鱼雷到数组以及图层上
this.layManager.insert(terpedo, 0);
subCanvas.torpedoVector.addElement(terpedo);
} else {
//鱼雷方向向左
Torpedo tinfish = new Torpedo(subCanvas, image, x + w,
y + h / 2,
this.troop, this.levelState,
this.nDirectFlag);
//添加鱼雷到数组以及图层上
layManager.insert(tinfish, 0);
subCanvas.torpedoVector.addElement(tinfish);
}
image = null;
}
}
private void hurt() {
this.hpLife--;
if (hpLife <= 0) {
bLive = false;
}
}
public void checkCollidion() {
Sprite collideable = null;
this.bHurt = false;
for (int j = 0; j < subCanvas.enemyVector.size(); j++) {
collideable = (Sprite) subCanvas.enemyVector.elementAt(j);
if (collidesWith(collideable, true)) {
this.bHurt = true;
}
}
}
public boolean getLifeState() {
return bLive;
}
public int getTroopState() {
return troop;
}
/** 获取玩家生命值
* @return 返回 hpLife。
*/
public int getHpLife() {
return hpLife;
}
/**
* @param hpLife 要设置的 hpLife。
*/
public void setHpLife(int hpLife) {
this.hpLife = hpLife;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -