【Java开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了Java开源代码-Bomb.java的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
package com.mot.j2me.midlets.jbombman;
//游戏中的炸弹类
public class Bomb extends Thread {
private Board oBoard;
private BoardView oBoardView;
private Player oPlayer;
private Enemy oEnemy;
//定义炸弹爆炸前等待的时间
private final int iExplodingTime = 4000;
//定义爆炸效果显示的时间
private final int iDisapearTime = 1000;
private int iX, iY;
private volatile boolean stopThread = false;
//炸弹类的构造方法
public Bomb( Board board, BoardView boardview, Player player, Enemy enemy,
int x, int y ) {
oBoard = board;
oBoardView = boardview;
oPlayer = player;
oEnemy = enemy;
iX = x;
iY = y;
oBoard.chBoard[iX][iY] = 'U';
}
public void stopThread() {
stopThread = true;
}
//根据给定的单元格判断炸弹爆炸后的结果的方法
private void explode( int x, int y ) {
//如果给定的单元格中是游戏主角或者是游戏主角正在放置炸弹,则游戏主角死掉
if( oBoard.isElement( 'P', x, y )||oBoard.isElement( 'U', x, y ) )
oPlayer.die();
//如果给定的单元格中是敌人,则敌人死掉
else if ( oBoard.isElement( 'E', x, y ) )
oEnemy.die( x, y );
//只要给定的单元格不是岩石和炸弹爆炸的元素,则给定的单元格中设置炸弹爆炸元素
if ( oBoard.isElement( 'N', x, y )||oBoard.isElement( 'W', x, y )
||oBoard.isElement( 'P', x, y )||oBoard.isElement( 'E', x, y )
||oBoard.isElement( 'U', x, y )||oBoard.isElement( 'B', x, y ) )
oBoard.chBoard[x][y] = 'X';
}
//清空给定的单元格中的元素
private void clear( int x, int y ) {
//如果当前给定的单元格中是炸弹爆炸元素,则为该单元格设置为空
if( oBoard.isElement( 'X', x, y ) )
oBoard.setElement( 'N', x, y );
}
//定义线程的方法体,在该方法中将定义炸弹爆炸的效果
public void run() {
while (!stopThread) {
try {
//放置炸弹后,显示一段时间
sleep(iExplodingTime);
System.out.println( "Explode" );
//设定给定单元格及其周围四个单元格在炸弹爆炸后的结果
explode( iX, iY );
explode( iX-1, iY );
explode( iX+1, iY );
explode( iX, iY-1 );
explode( iX, iY+1 );
//重新绘制棋盘中设定的某个区域
oBoardView.repaintCells(iX-1, iY-1, 3, 3);
//炸弹爆炸后结果显示一段时间
sleep(iDisapearTime);
System.out.println( "Clear" );
//根据炸弹爆炸后给定单元格及其周围四个单元格的元素设置情况进行清除
oBoard.chBoard[iX][iY] = 'N';
clear( iX-1, iY );
clear( iX+1, iY );
clear( iX, iY-1 );
clear( iX, iY+1 );
//重新绘制棋盘中设定的某个区域
oBoardView.repaintCells(iX-1, iY-1, 3, 3);
//线程停止
stopThread = true;
//清除游戏主角与该炸弹的关联
oPlayer.clearBomb();
}
catch (InterruptedException ie) {
}
}
}
}