?? enemy.java
字號(hào):
package com.mot.j2me.midlets.jbombman;
import java.util.*;
public class Enemy extends Thread {
private Board oBoard;
private BoardView oBoardView;
public Player oPlayer;
private final int iMoveTime = 500;
private Random random;
private int iNumbers;
private int[][] arrPositions;
private volatile boolean stopThread = false;
public Enemy( Board board, BoardView boardview, Player player, int numbers ) {
oBoard = board;
oBoardView = boardview;
oPlayer = player;
iNumbers = numbers;
arrPositions = new int[iNumbers][2];
random = new Random();
init();
}
public void init() {
int x, y;
for( int i=0; i<iNumbers; ) {
x = Math.abs( random.nextInt() ) % oBoard.iCols;
y = Math.abs( random.nextInt() ) % oBoard.iRows;
if( oBoard.chBoard[x][y] != 'N' )
continue;
oBoard.chBoard[x][y] = 'E';
arrPositions[i][0] = x;
arrPositions[i][1] = y;
i++;
}
}
public void stopThread() {
stopThread = true;
}
public void run() {
while (!stopThread) {
try {
sleep(iMoveTime);
for( int i=0; i<iNumbers; i++ )
if( arrPositions[i][0] != -1 )
move( i );
}
catch (InterruptedException ie) {
}
}
}
private void move( int i ) {
int x, y;
switch( random.nextInt() % 4 ) {
case 0://Left
x = arrPositions[i][0] - 1;
y = arrPositions[i][1];
if( canGo( x, y ) ) {
System.out.println( "Enemy " + i + " Left: x=" + x + ", y=" + y );
oBoard.chBoard[x+1][y] = 'N';
oBoard.chBoard[x][y] = 'E';
arrPositions[i][0] = x;
oBoardView.repaintCells( x, y, 2, 1);
}
break;
case 1://Right
x = arrPositions[i][0] + 1;
y = arrPositions[i][1];
if( canGo( x, y ) ) {
System.out.println( "Enemy " + i + " Right: x=" + x + ", y=" + y );
oBoard.chBoard[x-1][y] = 'N';
oBoard.chBoard[x][y] = 'E';
arrPositions[i][0] = x;
oBoardView.repaintCells( x-1, y, 2, 1);
}
break;
case 2://Down
x = arrPositions[i][0];
y = arrPositions[i][1] - 1;
if( canGo( x, y ) ) {
System.out.println( "Enemy " + i + " Down: x=" + x + ", y=" + y );
oBoard.chBoard[x][y+1] = 'N';
oBoard.chBoard[x][y] = 'E';
arrPositions[i][1] = y;
oBoardView.repaintCells( x, y, 1, 2);
}
break;
case 3://Up
x = arrPositions[i][0];
y = arrPositions[i][1] + 1;
if( canGo( x, y ) ) {
System.out.println( "Enemy " + i + " Up: x=" + x + ", y=" + y );
oBoard.chBoard[x][y-1] = 'N';
oBoard.chBoard[x][y] = 'E';
arrPositions[i][1] = y;
oBoardView.repaintCells( x, y-1, 1, 2);
}
break;
}//switch
}
private boolean canGo( int x, int y ) {
if( oBoard.isElement( 'N', x, y ) )
return true;
if( oBoard.isElement( 'P', x, y ) )
oPlayer.die();
return false;
}
public void die( int x, int y ) {
System.out.println( "Bomb die: x=" + x + ", y=" + y );
int i;
for( i=0; i<iNumbers; i++ )
if( (arrPositions[i][0] == x)&&(arrPositions[i][1] == y) ) {
arrPositions[i][0] = -1;
break;
}
for( i=0; i<iNumbers; i++ )
if( arrPositions[i][0] != -1 )
break;
if( i == iNumbers ) {
oBoard.isWin = true;
oBoardView.repaint();
}
}
public void dieAll() {
for( int i=0; i<iNumbers; i++ )
arrPositions[i][0] = -1;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -