📄 anemy.java
字号:
import java.util.Random;
import javax.microedition.lcdui.*;
public class Anemy {
Random rd = new Random();
int row, column;//新行列位置
int oldRow = 0, oldColumn = 0;//旧行列位置
int pixRow, pixColumn;//行列像素值
int step;
int life = 1; //船的生命值
int shLevel;//船的等级
int score;//船的分数
int map[][] = new int[11][11];
int mainDir; //主船本次的行走方向
int oldMainDir = 4;//主船上一步的行走方向
int beatDir; //海怪的行走方向
boolean beatDirChange = false;
boolean isDead = false;//是否已死
boolean willDead = false;//将要死的标记
boolean direction = false;
int shDirection;//船行的方向
int mainRow = 0, mainColumn = 0;//玩家船的行 列值
Image shCurrPic = null;//船移动后要显示的图片
Image[] imageShip = new Image[10];
Image[] imageGenShip = new Image[10];
Image[] imageBeat = new Image[10];
public Anemy(int oldColumn, int oldRow, int level) {
try {
this.oldRow = oldRow; //坐标XY
this.oldColumn = oldColumn;
pixRow = this.oldRow * 16;
pixColumn = this.oldColumn * 16;
// 高级敌船图片初始化
imageShip[1] = Image.createImage("/picres/ship3_ld_18x18.png");
imageShip[2] = Image.createImage("/picres/ship3_down_18x18.png");
imageShip[3] = Image.createImage("/picres/ship3_rd_18x18.png");
imageShip[4] = Image.createImage("/picres/ship3_left_18x18.png");
imageShip[6] = Image.createImage("/picres/ship3_right_18x18.png");
imageShip[7] = Image.createImage("/picres/ship3_lu_18x18.png");
imageShip[8] = Image.createImage("/picres/ship3_up_18x18.png");
imageShip[9] = Image.createImage("/picres/ship3_ru_18x18.png");
// 普通敌船图片初始化
imageGenShip[1] = Image.createImage("/picres/ship2_ld_18x18.png");
imageGenShip[2] = Image.createImage("/picres/ship2_down_18x18.png");
imageGenShip[3] = Image.createImage("/picres/ship2_rd_18x18.png");
imageGenShip[4] = Image.createImage("/picres/ship2_left_18x18.png");
imageGenShip[6] = Image
.createImage("/picres/ship2_right_18x18.png");
imageGenShip[7] = Image.createImage("/picres/ship2_lu_18x18.png");
imageGenShip[8] = Image.createImage("/picres/ship2_up_18x18.png");
imageGenShip[9] = Image.createImage("/picres/ship2_ru_18x18.png");
// 海怪图片初始化
imageBeat[1] = Image.createImage("/picres/beat_ld_18x18.png");
imageBeat[2] = Image.createImage("/picres/beat_down_18x18.png");
imageBeat[3] = Image.createImage("/picres/beat_rd_18x18.png");
imageBeat[4] = Image.createImage("/picres/beat_left_18x18.png");
imageBeat[6] = Image.createImage("/picres/beat_right_18x18.png");
imageBeat[7] = Image.createImage("/picres/beat_lu_18x18.png");
imageBeat[8] = Image.createImage("/picres/beat_up_18x18.png");
imageBeat[9] = Image.createImage("/picres/beat_ru_18x18.png");
shLevel = level;
switch (shLevel) {
case 5:
shCurrPic = imageBeat[4];
break;
case 6:
shCurrPic = imageGenShip[4];
break;
case 7:
shCurrPic = imageShip[1];
break;
}
isDead = false;
willDead = false;
switch (shLevel) {
case 6:
score = 30;
break;
case 7:
score = 50;
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Anemy() {
}
public void getMainCoo(int mainColumn, int mainRow, int map[][], int mainDir) {
this.mainRow = mainRow;
this.mainColumn = mainColumn;
this.map = map;
this.mainDir = mainDir;
}
public void nextStep() {
switch (shLevel) {
case 5:
Beat();
break;
case 6:
General();
break;
case 7:
Advanced();
break;
}
}
protected void Advanced() {
//如果敌船与玩家在同一列,则列数不变,行数向玩家的位置靠拢
try {
if (oldRow == mainRow) {
if (oldColumn < mainColumn) {
AdvancedRight();
if (!direction)
AdvancedRightUp();
if (!direction)
AdvancedRightDown();
if (!direction)
AdvancedUp();
if (!direction)
AdvancedDown();
if (!direction)
AdvancedLeft();
if (!direction)
AdvancedLeftUp();
if (!direction)
AdvancedLeftDown();
} else if (oldColumn > mainColumn) {
AdvancedLeft();
if (!direction)
AdvancedLeftUp();
if (!direction)
AdvancedLeftDown();
if (!direction)
AdvancedUp();
if (!direction)
AdvancedDown();
if (!direction)
AdvancedRight();
if (!direction)
AdvancedRightUp();
if (!direction)
AdvancedRightDown();
}
}
//如果敌船与玩家在同一列,则行数不变,列数向玩家的位置靠拢
else if (oldColumn == mainColumn) {
if (oldRow < mainRow) {
AdvancedDown();
if (!direction)
AdvancedRightDown();
if (!direction)
AdvancedLeftDown();
if (!direction)
AdvancedLeft();
if (!direction)
AdvancedRight();
if (!direction)
AdvancedRightUp();
if (!direction)
AdvancedLeftUp();
if (!direction)
AdvancedUp();
} else if (oldRow > mainRow) {
AdvancedUp();
if (!direction)
AdvancedRightUp();
if (!direction)
AdvancedLeftUp();
if (!direction)
AdvancedLeft();
if (!direction)
AdvancedRight();
if (!direction)
AdvancedLeftDown();
if (!direction)
AdvancedRightDown();
if (!direction)
AdvancedDown();
}
}
//如果敌船与玩家不在同一行且不在同一列,行、列数向玩家的位置靠拢
if ((oldRow != mainRow) && (oldColumn != mainColumn)) {
if ((oldRow < mainRow) && (oldColumn < mainColumn)) {
AdvancedRightDown();
if (!direction)
AdvancedRight();
if (!direction)
AdvancedDown();
if (!direction)
AdvancedRightUp();
if (!direction)
AdvancedLeftDown();
if (!direction)
AdvancedUp();
if (!direction)
AdvancedLeft();
if (!direction)
AdvancedLeftUp();
}
if ((oldRow < mainRow) && (oldColumn > mainColumn)) {
AdvancedLeftDown();
if (!direction)
AdvancedLeft();
if (!direction)
AdvancedDown();
if (!direction)
AdvancedLeftUp();
if (!direction)
AdvancedRightDown();
if (!direction)
AdvancedUp();
if (!direction)
AdvancedRight();
if (!direction)
AdvancedRightUp();
}
if ((oldRow > mainRow) && (oldColumn < mainColumn)) {
AdvancedRightUp();
if (!direction)
AdvancedRight();
if (!direction)
AdvancedUp();
if (!direction)
AdvancedLeftUp();
if (!direction)
AdvancedRightDown();
if (!direction)
AdvancedLeft();
if (!direction)
AdvancedDown();
if (!direction)
AdvancedLeftDown();
}
if ((oldRow > mainRow) && (oldColumn > mainColumn)) {
AdvancedLeftUp();
if (!direction)
AdvancedUp();
if (!direction)
AdvancedLeft();
if (!direction)
AdvancedRightUp();
if (!direction)
AdvancedLeftDown();
if (!direction)
AdvancedDown();
if (!direction)
AdvancedRight();
if (!direction)
AdvancedRightDown();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected void AdvancedRight() {
if (oldRow >= 0
&& oldRow < 11
&& oldColumn + 1 >= 0
&& oldColumn + 1 < 11
&& map[oldRow][oldColumn + 1] != Map.ISLAND
&& map[oldRow][oldColumn + 1] != Map.TREE
&& map[oldRow][oldColumn + 1] != Map.BONE
&& map[oldRow][oldColumn + 1] != Map.SWIRL
&& !TestBomb(mainRow, mainColumn, oldRow, oldColumn + 1,
mainDir)) {
shDirection = 6;
row = oldRow;
column = oldColumn + 1;
shCurrPic = imageShip[6];
direction = true;
} else
direction = false;
}
protected void AdvancedRightUp() {
if (oldRow - 1 >= 0
&& oldRow - 1 < 11
&& oldColumn + 1 >= 0
&& oldColumn + 1 < 11
&& map[oldRow - 1][oldColumn + 1] != Map.ISLAND
&& map[oldRow - 1][oldColumn + 1] != Map.TREE
&& map[oldRow - 1][oldColumn + 1] != Map.BONE
&& map[oldRow - 1][oldColumn + 1] != Map.SWIRL
&& !TestBomb(mainRow, mainColumn, oldRow - 1, oldColumn + 1,
mainDir)) {
shDirection = 9;
row = oldRow - 1;
column = oldColumn + 1;
shCurrPic = imageShip[9];
direction = true;
} else
direction = false;
}
protected void AdvancedRightDown() {
if (oldRow + 1 >= 0
&& oldRow + 1 < 11
&& oldColumn + 1 >= 0
&& oldColumn + 1 < 11
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -