//package BallGame;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
/**
* <strong>FileName</strong>: Ball.java
* <HR>
* <strong>Description</strong>: Ball object attribute description.
* <P><HR>
* @author
* <em>
* This Game is developed by <B>Fansy</B>.<BR>
* F.S.Studio 1999~2005
* </em>
* @version 0.3.2
*/
public class Ball implements Runnable
{
private int signalx; //控制小球运动方向的信号量
private int signaly;
private int dx,dy; //小球移动的偏移量
private Bar bar;
private BallCanvas canvas; //绘图画布
private Block block; //砖块
private int Speed; //小球的移动速度
private Sprite ballsp; //小球精灵
private boolean isBallRun;
public Sprite createBallSprite()
{
Image ballimage=null;
try{ballimage=Image.createImage("/ballimage.png");} //装载小球图像
catch(Exception e){}
ballsp=new Sprite(ballimage); //创建小球精灵
ballsp.defineReferencePixel(2,2); //修改默认参考点
ballsp.defineCollisionRectangle(2,2,1,1); //设定小球的碰撞区域为中心的一个像素
reset();
return ballsp;
}
public void reset()
{
signalx=2; //控制小球运动方向的信号量
signaly=0;
dx=0; //小球横向偏移量
dy=0; //小球纵向偏移量
isBallRun=false;
ballsp.setPosition(120,257); //设定小球初始位置
setSpeed(10); //设定小球初始速度
}
public Ball(BallCanvas c,Bar b,Block bk)
{
canvas=c;
bar=b;
block=bk;
}
public void run() //小球运动触发事件
{
int tempx=signalx;
int tempy=signaly;
while(true)
{
if(tempx==0) //根据信号量移动小球
{
tempx=signalx;
ballsp.move(dx,0);
}
else tempx--;
if(tempy==0)
{
tempy=signaly;
ballsp.move(0,dy);
}
else tempy--;
if(ballsp.collidesWith(canvas.background,false)) //小球与边界的碰撞检测
{
if((dx<0&&ballsp.getRefPixelX()<=10)||(dx>0&&ballsp.getRefPixelX()>=230)) dx=dx*-1;
if(ballsp.getRefPixelY()<=10) dy=1;
}
if(ballsp.collidesWith(block.blocktd,false)) //小球与砖块的碰撞检测
{
block.setBlock((ballsp.getRefPixelX()-40)/20,(ballsp.getRefPixelY()-30)/10);
if((dx>0&&block.getBlockX()*20+40==ballsp.getRefPixelX())||(dx<0&&block.getBlockX()*20+20+40-1==ballsp.getRefPixelX()))
{
dx=dx*-1;
}
else if((dy>0&&block.getBlockY()*10+30==ballsp.getRefPixelY())||(dy<0&&block.getBlockY()*10+10+30-1==ballsp.getRefPixelY()))
{
dy=dy*-1;
}
// System.out.println("DEBUG_BALL_REF "+"x:"+ballsp.getRefPixelX()+"y:"+ballsp.getRefPixelY());
// System.out.println("DEBUG_BLOCK_INDEX "+"x:"+block.blockx+"y:"+block.blocky);
block.ClearBlock(); //清除碰撞到的砖块
canvas.addScore(20); //加分
canvas.isLevelClear(); //判断是否过关
}
if(ballsp.collidesWith(bar.barsp,false)) //小球与挡板的碰撞判断
{
int dd=ballsp.getRefPixelX()-bar.getBarRefPixelX();
dy=-1;
if(dd>0) //向右反弹
{
dx=1;
}
else //向左反弹
{
dx=-1;
dd=dd*-1;
}
dd=dd/5;
if(dd<=2) //设定小球运动角度
{
signalx=2-dd;
signaly=0;
}
else
{
signalx=0;
signaly=dd-2;
}
tempx=signalx;
tempy=signaly;
}
if(ballsp.getRefPixelY()>270)
{
new GameOver().showGameOver();
}
canvas.drawScreen();
System.gc();
try{Thread.sleep(Speed);}
catch(Exception e){}
}
}
public void start()
{
Thread t=new Thread(this);
t.start();
}
public void setSpeed(int sp)
{
Speed = sp;
}
public void setBallPosition(int x,int y)
{
ballsp.setPosition(x,y);
}
public boolean isBallRun()
{
return isBallRun;
}
public void setBallRun()
{
isBallRun=true;
dx=1;
dy=-1;
}
public void BallReady()
{
new Timer().schedule(new runBallTask(this),3000);
}
}
class runBallTask extends TimerTask
{
private Ball ball;
public runBallTask(Ball b)
{
ball=b;
}
public void run()
{
ball.setBallRun();
}
}