?? player.java
字號:
package com.mot.j2me.midlets.jbombman;
import java.util.*;
public class Player {
private Board oBoard;
private BoardView oBoardView;
private Enemy oEnemy;
private Bomb oBomb;
private Random random;
private int iX, iY;
public Player( Board board, BoardView boardview ) {
oBoard = board;
oBoardView = boardview;
random = new Random();
init();
}
public void init() {
while( true ) {
iX = Math.abs( random.nextInt() ) % oBoard.iCols;
iY = Math.abs( random.nextInt() ) % oBoard.iRows;
if( oBoard.chBoard[iX][iY] != 'N' )
continue;
if( oBoard.near( 'E', iX, iY ) )
continue;
if( !oBoard.near2( 'N', iX, iY ) )
continue;
break;
}
oBoard.chBoard[iX][iY] = 'P';
}
public void setEnemy( Enemy enemy ) {
oEnemy = enemy;
}
public void clearBomb() {
oBomb = null;
System.out.println( "delete Bomb" );
}
private boolean canGo( int x, int y ) {
if( oBoard.isElement( 'N', x, y ) )
return true;
if( oBoard.isElement( 'E', x, y ) )
die();
return false;
}
public void fire() {
oBomb = new Bomb( oBoard, oBoardView, this, oEnemy, iX, iY );
oBomb.start();
System.out.println( "new Bomb: x=" + iX + ", y=" + iY );
}
public void left() {
if( !canGo( iX-1, iY ) )
return;
if( oBoard.chBoard[iX][iY] == 'U' )
oBoard.chBoard[iX][iY] = 'B';
else //Should be 'P'
oBoard.chBoard[iX][iY] = 'N';
iX--;
oBoard.chBoard[iX][iY] = 'P';
oBoardView.repaintCells( iX, iY, 2, 1 );
}
public void right() {
if( !canGo( iX+1, iY ) )
return;
if( oBoard.chBoard[iX][iY] == 'U' )
oBoard.chBoard[iX][iY] = 'B';
else //Should be 'P'
oBoard.chBoard[iX][iY] = 'N';
iX++;
oBoard.chBoard[iX][iY] = 'P';
oBoardView.repaintCells( iX-1, iY, 2, 1 );
}
public void up() {
if( !canGo( iX, iY-1 ) )
return;
if( oBoard.chBoard[iX][iY] == 'U' )
oBoard.chBoard[iX][iY] = 'B';
else //Should be 'P'
oBoard.chBoard[iX][iY] = 'N';
iY--;
oBoard.chBoard[iX][iY] = 'P';
oBoardView.repaintCells( iX, iY, 1, 2 );
}
public void down() {
if( !canGo( iX, iY+1 ) )
return;
if( oBoard.chBoard[iX][iY] == 'U' )
oBoard.chBoard[iX][iY] = 'B';
else //Should be 'P'
oBoard.chBoard[iX][iY] = 'N';
iY++;
oBoard.chBoard[iX][iY] = 'P';
oBoardView.repaintCells( iX, iY-1, 1, 2 );
}
public void die() {
System.out.println( "Player die: x=" + iX + ", y=" + iY );
oEnemy.dieAll();
oBoard.isGameOver = true;
oBoardView.repaint();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -