ball.javapackage com.tqs;import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;public class ball extends Thread{
int bx;
int by;
int vx=30;
int vy=30;
int ballSize = 10;
Bitmap bmpBall;
gameView father;
boolean isStarted;
boolean isPlaying;
int sleepSpan = 50;
int lastKicker;


public ball(gameView father){
super.setName("##-Ball"); //设置线程名字,用于调试用
this.father = father;
Resources r = father.getContext().getResources(); //获取Resources对象
bmpBall = BitmapFactory.decodeResource(r, R.drawable.ball);//设置图片
isStarted = true; //设置循环变量
isPlaying = true; //
}

public void run(){
while(isStarted){
while(isPlaying){
//移动足球
move();
//碰撞检测
checkCollision();
//休眠一下
try{
Thread.sleep(sleepSpan);
}
catch(Exception e){
e.printStackTrace();
}
}
try{
Thread.sleep(500);
}
catch(Exception e){ e.printStackTrace();
}
}
} private void checkCollision() {
// checkForBorders(); //检查是否出边界
// checkForAIPlayer(); //检查是否碰到AI
// checkForUserPlayer(); //检查是否碰到玩家
// checkIfScoreAGoal(); //检查是否进球啦
} private void move() {
bx+=vx;
by+=vy;
}
public void drawSelf(Canvas canvas){
Bitmap bmp=null;
if(isPlaying){
 bmp = Bitmap.createBitmap(bmpBall, 0, 0, ballSize, ballSize, null,true);
}
else{
bmp = bmpBall; //如果没有在游戏中,则不作旋转变换
}
canvas.drawBitmap(bmp, bx-ballSize/2, by-ballSize/2, null);
}


}
gameView,java

package com.tqs;import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceHolder;
import android.view.SurfaceView;public class gameView extends SurfaceView implements SurfaceHolder.Callback {
PingpongActivity father;
TutorialThread thread;
Bitmap background;
boolean isGameOver; //一局比赛结束
boolean isGameWin; //是否赢了
Bitmap bmpWinBanner; //游戏胜利的图片
Bitmap bmpLoseBanner; //游戏失败的图片

AIThread ait;
ball Ball;

public gameView(Context context,PingpongActivity father) {
super(father); //调用父类构造器
getHolder().addCallback(this);//添加Callback接口
this.father = father;
Ball = new ball(this); //创建足球线程
initPlayerInstance();//初始化双方队员
initGame();//初始化游戏
initBitmap(); //初始化图片资源
// ait = new AIThread(this);//创建AI分析线程
this.thread = new TutorialThread(getHolder(), this);//创建后台刷屏线程
}
private void initPlayerInstance() {
// TODO Auto-generated method stub

}
private void initGame() {
// TODO Auto-generated method stub
Ball.bx=300;
Ball.by=240; }
private void initBitmap() {
background = BitmapFactory.decodeResource(getResources(), R.drawable.back);//开始游戏按钮
bmpWinBanner = BitmapFactory.decodeResource(getResources(), R.drawable.win);
bmpLoseBanner = BitmapFactory.decodeResource(getResources(), R.drawable.lose); }
public void onDraw(Canvas canvas){//自己写的绘制方法
canvas.drawColor(Color.WHITE);//清屏
canvas.drawBitmap(background, 0, 0, null);//绘制图片
Ball.drawSelf(canvas);

if (isGameOver) {// 一场比赛结束,根据胜负,绘制相应图片
canvas.drawBitmap(bmpWinBanner,0 ,200 , null);
} else if (isGameWin) { // 获得胜利
canvas.drawBitmap(bmpLoseBanner,0 , 200, null);
}
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub

} @Override
public void surfaceCreated(SurfaceHolder holder) {
this.thread.setFlag(true);//设置循环标志位
        this.thread.start();//启动线程
} @Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub

}  private class TutorialThread extends Thread{//刷帧线程
private int span = 500;//睡眠的毫秒数 
private SurfaceHolder surfaceHolder;//SurfaceHolder的引用
private gameView gameView;//MenuView的引用
private boolean flag = false;//循环标记位
        public TutorialThread(SurfaceHolder surfaceHolder, gameView gameView) {//构造器
            this.surfaceHolder = surfaceHolder;//得到surfaceHolder引用
            this.gameView = gameView;//得到menuView引用
        }
        public void setFlag(boolean flag) {//设置循环标记位
         this.flag = flag;
        }
public void run() {//重写的run方法
Canvas c;//画布
            while (this.flag) {//循环
                c = null;
                try {
                 // 锁定整个画布,在内存要求比较高的情况下,建议参数不要为null
                    c = this.surfaceHolder.lockCanvas(null);
                    synchronized (this.surfaceHolder) {//同步锁
                     gameView.onDraw(c);//调用绘制方法
                    }
                } finally {//使用finally保证下面代码一定被执行
                    if (c != null) {
                     //更新屏幕显示内容
                        this.surfaceHolder.unlockCanvasAndPost(c);
                    }
                }
                try{
                 Thread.sleep(span);//睡眠指定毫秒数
                }catch(Exception e){//捕获异常
                 e.printStackTrace();//有异常时打印异常堆栈信息
                }
            }
}
}
}