📄 tilecanvas.java
字号:
imgMainPic = imgShip[7];
oldKeyCode = tmpKeyCode;
for (int imove = 0; imove < 16; imove = imove + 4) {
y -= 4;
x -= 4;
repaint();
Thread.sleep(50);
}
eat(map[personRow - 1][personColumn - 1], personRow - 1,
personColumn - 1);
if (map[personRow - 1][personColumn - 1] == Map.SWIRL) {
swirl();
} else {
personRow = personRow - 1;
personColumn = personColumn - 1;
}
changeEnemy = true;
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected void walkUpRight() {
try {
if ((personRow - 1 >= 0) && (personColumn + 1 < ROWCOL)
&& (map[personRow - 1][personColumn + 1] != Map.TREE)
&& (map[personRow - 1][personColumn + 1] != Map.ISLAND)
&& (map[personRow - 1][personColumn + 1] != Map.ENEMY1)
&& (map[personRow - 1][personColumn + 1] != Map.ENEMY2)
&& (map[personRow - 1][personColumn + 1] != Map.BEAT)
&& (map[personRow - 1][personColumn + 1] != Map.BONE)) { //没有超出边界或障碍
imgMainPic = imgShip[9];
oldKeyCode = tmpKeyCode;
for (int imove = 0; imove < 16; imove = imove + 4) {
y -= 4;
x += 4;
repaint();
Thread.sleep(50);
}
eat(map[personRow - 1][personColumn + 1], personRow - 1,
personColumn + 1);
if (map[personRow - 1][personColumn + 1] == Map.SWIRL) {
swirl();
} else {
personRow = personRow - 1;
personColumn = personColumn + 1;
}
changeEnemy = true;
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected void walkLeft() {
try {
if ((personColumn - 1 >= 0)
&& (map[personRow][personColumn - 1] != Map.TREE)
&& (map[personRow][personColumn - 1] != Map.ISLAND)
&& (map[personRow][personColumn - 1] != Map.ENEMY1)
&& (map[personRow][personColumn - 1] != Map.ENEMY2)
&& (map[personRow][personColumn - 1] != Map.BEAT)
&& (map[personRow][personColumn - 1] != Map.BONE)) { //没有超出边界或障碍
imgMainPic = imgShip[4];
oldKeyCode = tmpKeyCode;
for (int imove = 0; imove < 16; imove = imove + 4) {
x -= 4;
repaint();
Thread.sleep(50);
}
eat(map[personRow][personColumn - 1], personRow,
personColumn - 1);
if ((map[personRow][personColumn - 1] == Map.SWIRL)) {
swirl();
} else
personColumn = personColumn - 1;
changeEnemy = true;
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected void walkRight() {
try {
if ((personColumn + 1 < ROWCOL)
&& (map[personRow][personColumn + 1] != Map.TREE)
&& (map[personRow][personColumn + 1] != Map.ISLAND)
&& (map[personRow][personColumn + 1] != Map.ENEMY1)
&& (map[personRow][personColumn + 1] != Map.ENEMY2)
&& (map[personRow][personColumn + 1] != Map.BEAT)
&& (map[personRow][personColumn + 1] != Map.BONE)) { //没有超出边界或障碍
imgMainPic = imgShip[6];
oldKeyCode = tmpKeyCode;
for (int imove = 0; imove < 16; imove = imove + 4) {
x += 4;
repaint();
Thread.sleep(50);
}
eat(map[personRow][personColumn + 1], personRow,
personColumn + 1);
if ((map[personRow][personColumn + 1] == Map.SWIRL)) {
swirl();
} else
personColumn = personColumn + 1;
changeEnemy = true;
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected void walkStop() {
try {
repaint();
Thread.sleep(50);
}
catch (Exception e) {
e.printStackTrace();
}
}
protected void drawFlame(Graphics g, int nBomb, int xFlame, int yFlame) {
try {
bg.drawImage(imgFlame, LMARGE + xFlame, TMARGE + yFlame,
Graphics.TOP | Graphics.LEFT);
if (nBomb == 1) {
bFlame1 = false;
}
if (nBomb == 2) {
bFlame2 = false;
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected void drawWater(Graphics g, int nBomb, int xWater, int yWater) {
try {
bg.drawImage(imgWater, LMARGE + xWater, TMARGE + yWater,
Graphics.TOP | Graphics.LEFT);
if (nBomb == 1) {
bWater1 = false;
}
if (nBomb == 2) {
bWater2 = false;
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected void eat(int what, int nextRow, int nextColumn) {
switch (what) {
case Map.HELPMAN:
map[nextRow][nextColumn] = -1;
repaint();
score += 50;
break;
case Map.BOTTLE:
map[nextRow][nextColumn] = -1;
repaint();
score += 30;
break;
}
}
protected void life() {
life = life - 1;
if (life >= 0) {
isDead = false;
iswillDead = false;
randPosition();
} else {
isRun = false;
rcav = new Rcav(BCai, display);//记录仓库
rcav.matches("xxx", score);
SoundEffects.getInstance().stopBackgroundSound();
bGameOver = true;
repaint();
delay(4000);
BCai.fd = true;
BCai.PT = 1;
BCai.menuY = 59;
display.setCurrent(BCai);
}
}
protected void EnemyNext() {
for (int i = 0; i < nEnemy; i++) {
if (!anemy[i].isDead) {
anemy[i].getMainCoo(personColumn, personRow, map, mainDir);
anemy[i].nextStep();
}
}
}
protected void EnemyActionTest() {
for (int i = 0; i < nEnemy; i++) {
if (!anemy[i].isDead) {
map[anemy[i].oldRow][anemy[i].oldColumn] = -1;
if (map[anemy[i].row][anemy[i].column] == Map.ISLAND
|| (map[anemy[i].row][anemy[i].column] == Map.TREE)
|| (map[anemy[i].row][anemy[i].column] == Map.BONE)) {
anemy[i].willDead = true;
} else if (map[anemy[i].row][anemy[i].column] == Map.SHIP) {
if (anemy[i].shLevel != Map.BEAT) {
anemy[i].willDead = true;
iswillDead = true;
} else {
iswillDead = true;
}
} else if (map[anemy[i].row][anemy[i].column] == Map.BOTTLE
|| (map[anemy[i].row][anemy[i].column] == Map.HELPMAN)) {
map[anemy[i].row][anemy[i].column] = -1;
} else {
for (int j = i + 1; j < nEnemy; j++)
if (anemy[j].willDead != true) {
if ((anemy[i].row == anemy[j].row)
&& (anemy[i].column == anemy[j].column)) {
anemy[i].willDead = true;
anemy[j].willDead = true;
}
}
}
}
}
}
protected void EnemyMove() {
EnemyNext();
EnemyActionTest();
try {
//显示敌人移动的过程,每次只移动4个素,四次可移动到指定位置
for (int imove = 0; imove < 16; imove = imove + 4) {
for (int i = 0; i < nEnemy; i++) {
if (!anemy[i].isDead) {
anemy[i].move();
}
}
repaint();
Thread.sleep(50);
}
} catch (Exception e) {
e.printStackTrace();
}
// 如果敌人移动到指定位置,而且该位置已经有另一敌船或是岛则画出死亡图片
for (int i = 0; i < nEnemy; i++) {
anemy[i].oldRow = anemy[i].row;
anemy[i].oldColumn = anemy[i].column;
if (!anemy[i].isDead) {
if (anemy[i].willDead) {
if ((map[anemy[i].row][anemy[i].column] != Map.ISLAND)
&& (map[anemy[i].row][anemy[i].column] != Map.TREE)
&& (map[anemy[i].row][anemy[i].column] != Map.BONE))
map[anemy[i].row][anemy[i].column] = Map.BONE;
anemy[i].isDead = true;
Score(anemy[i]);
} else {
map[anemy[i].row][anemy[i].column] = anemy[i].shLevel;
}
}
}
for (int i = 0; i < nEnemy; i++) {
if (anemy[i].shLevel == Map.BEAT) {
map[anemy[i].row][anemy[i].column] = anemy[i].shLevel;
anemy[i].isDead = false;
anemy[i].willDead = false;
}
}
changeEnemy = false;
addLife();
}
protected void swirl() {
map[personRow][personColumn] = -1; //恢复主船原来位置标记
for (int i = 1; i < 10; i++) {
if (i == 5)
continue;
imgMainPic = imgShip[i];
repaint();
delay(30);
}
randPosition();
}
public void randPosition() {
int position = 0;
int randPosition = 1;
if (personRow <= 5 && personColumn <= 5)
position = 1;
else if (personRow <= 5 && personColumn >= 5)
position = 2;
else if (personRow >= 5 && personColumn <= 5)
position = 3;
else if (personRow >= 5 && personColumn >= 5)
position = 4;
personRow = getRandomInt(0, 10);
personColumn = getRandomInt(0, 10);
if (personRow <= 5 && personColumn <= 5)
randPosition = 1;
else if (personRow <= 5 && personColumn >= 5)
randPosition = 2;
else if (personRow >= 5 && personColumn <= 5)
randPosition = 3;
else if (personRow >= 5 && personColumn >= 5)
randPosition = 4;
while (map[personRow][personColumn] == Map.ISLAND
|| map[personRow][personColumn] == Map.TREE
|| map[personRow][personColumn] == Map.ENEMY1
|| map[personRow][personColumn] == Map.BONE
|| map[personRow][personColumn] == Map.BOTTLE
|| map[personRow][personColumn] == Map.HELPMAN
|| map[personRow][personColumn] == Map.ENEMY2
|| map[personRow][personColumn] == Map.SWIRL
|| map[personRow][personColumn] == Map.BEAT
|| position == randPosition) {
personRow = getRandomInt(0, 10);
personColumn = getRandomInt(0, 10);
if (personRow <= 5 && personColumn <= 5)
randPosition = 1;
else if (personRow <= 5 && personColumn >= 5)
randPosition = 2;
else if (personRow >= 5 && personColumn <= 5)
randPosition = 3;
else if (personRow >= 5 && personColumn >= 5)
randPosition = 4;
}
oldPersonRow = personRow;
oldPersonColumn = personColumn;
y = 16 * personRow;
x = 16 * personColumn;
imgMainPic = imgShip[4];
mainDir = 4;
oldKeyCode = 52;
}
protected void addLife() {
if (score >= addLife) {
life += 1;
addLife += 500;
}
}
protected void Score(Anemy anemy) {
score += anemy.score;
}
protected int getRandomInt(int min, int max) {
return Math.abs(rd.nextInt()) % (max - min + 1) + min;
}
protected void delay(int time) {
try {
Thread.sleep(time);
} catch (Exception e) {
e.printStackTrace();
}
}
public void drawMap(Graphics g) {
// 画地图格
for (i = 0; i < ROWCOL; i++) {
for (j = 0; j < ROWCOL; j++) {
g.drawImage(imgBg, LMARGE + 16 * i, TMARGE + 16 * j,
Graphics.TOP | Graphics.LEFT);
}
}
//画边框
g.setColor(0x0061ad);
g.drawRect(LMARGE - 1, TMARGE - 1, 176, 176);
for (i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
switch (map[i][j]) {
case Map.BONE:
g.drawImage(imgBone, LMARGE + 16 * j, TMARGE + 16 * i,
Graphics.TOP | Graphics.LEFT);
break;
case Map.ISLAND:
g.drawImage(imgIsland, LMARGE + 16 * j, TMARGE + 16 * i,
Graphics.TOP | Graphics.LEFT);
break;
case Map.TREE:
g.drawImage(imgTree, LMARGE + 16 * j, TMARGE + 16 * i,
Graphics.TOP | Graphics.LEFT);
break;
case Map.BOTTLE:
g.drawImage(imgBottle, LMARGE + 16 * j, TMARGE + 16 * i,
Graphics.TOP | Graphics.LEFT);
break;
case Map.HELPMAN:
g.drawImage(imgHelpMan, LMARGE + 16 * j, TMARGE + 16 * i
+ 15, Graphics.BOTTOM | Graphics.LEFT);
break;
case Map.SWIRL:
switch (swirlNum) {
case 0:
g.drawImage(imgSwirl[0], LMARGE + 16 * j, TMARGE + 16
* i, Graphics.TOP | Graphics.LEFT);
break;
case 1:
g.drawImage(imgSwirl[1], LMARGE + 16 * j, TMARGE + 16
* i, Graphics.TOP | Graphics.LEFT);
break;
case 2:
g.drawImage(imgSwirl[2], LMARGE + 16 * j, TMARGE + 16
* i, Graphics.TOP | Graphics.LEFT);
break;
}
swirlNum++;
if (swirlNum >= 3)
swirlNum = 0;
break;
}
}
}
}
protected void Sound() {
SoundEffects.getInstance().startBackgroundSound();//背景声音
}
protected void StopSound() {
SoundEffects.getInstance().stopBackgroundSound();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -