Android飞机大战
简述
android游戏继承surfaceView开发,实现Callback接口,Callback接口作用是回调。
实现Callback接口后,要实现三个抽象方法:surfaceCreated(),surfaceDestroyed(),surfaceChanged()
然后再surfaceCreated()方法里面实例化对象,surfaceDestroyed()里面结束。
创建一个渲染线程,RenderThread,获取画笔,在surfaceView里面去绘图。
飞机与子弹对象继承一个我自己定义的Sprite类,便于在surfaceView里面绘图。
MainActivity里面的setContentView(参数),参数里面不用XML页面,换成自己定义的surfaceView。
监听触屏事件的方法onTouchEvent(MotionEvent event)把获得的触屏事件传递给mysurfaceView的dealTouchEvent()方法。
部分核心代码
MainActivity:
public class MainActivity extends Activity {
private MySurfaceView mySurfaceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mySurfaceView = new MySurfaceView(this);
setContentView(mySurfaceView);
}
public boolean onTouchEvent(MotionEvent event){
mySurfaceView.dealTouchEvent(event);
return super.onTouchEvent(event);
}
}surfaceView
public class MySurfaceView extends SurfaceView implements Callback
surfaceCreated()方法
public void surfaceCreated(SurfaceHolder holder) {
Bitmap img = BitmapFactory.decodeResource(getResources(),R.drawable.mainplane);
mainPlane = new MainPlane(img,new Point(330,900));
bp = new BossPlane(BitmapFactory.decodeResource(getResources(),
R.drawable.boss),new Point());
sp = new CopyOnWriteArrayList<SecPlane>();
bs = new CopyOnWriteArrayList<Bullet>();
isRun = true;
renderThread.start();
System.out.println("开始");
new Thread(){
public void run() {
while(true){//每秒发射10枚子弹(自动发射)
bullet = mainPlane.createBullet(getResources());
bs.add(bullet);
try {
sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}
};
}.start();
new Thread(){
public void run() {
try {
sleep(5000);
} catch (Exception e1) {
e1.printStackTrace();
}
while(true){//自动出现敌机
SecPlane secPlane = new SecPlane(BitmapFactory.decodeResource(
getResources(), R.drawable.secplane),
new Point((int)(Math.random()*600),0));
sp.add(secPlane);
try{
sleep(300);
}catch(Exception e){
e.printStackTrace();
}
}
};
}.start();
}RenderThread渲染线程
class RenderThread extends Thread{
Canvas lockCanvas = null;
private Paint paint = new Paint();
@Override
public void run() {
System.out.println(holder);
while(isRun){
try{
lockCanvas = holder.lockCanvas();
lockCanvas.drawRect(0,0,getWidth(),getHeight(),paint);
mainPlane.drawMySelf(lockCanvas);
bp.drawMySelf(lockCanvas);MainPlane----->玩家控制的飞机:
public class MainPlane extends Sprite{
public int speed = 10;
public MainPlane(Bitmap img, Point currentPoint) {
super(img, currentPoint);
}
public Bullet createBullet(Resources res){
Point point = new Point(currentPoint.x + 66,currentPoint.y);
Bullet bullet = new Bullet(BitmapFactory.decodeResource(res, R.drawable.bullet),point);
return bullet;
}
public void move(int x,int y){
currentPoint.x = x - 84;
currentPoint.y = y - 216;
}
}Sprite类
public class Sprite {
protected Bitmap img;
protected Point currentPoint;
public Sprite(Bitmap img,Point currentPoint){
this.img = img;
if(currentPoint == null)
currentPoint = new Point(0,0);
this.currentPoint = currentPoint;
}
public void drawMySelf(Canvas canvas){
if(img != null && canvas != null){
canvas.drawBitmap(img,currentPoint.x,currentPoint.y,null);
}
}
public Point getCurrentPoint(){
return currentPoint;
}
}上述是部分核心代码
图示游戏
虚拟机上截gif图很卡,手机上运行比市场上的app还流畅
本文介绍了如何使用Android的SurfaceView开发飞机大战游戏,通过实现Callback接口来管理绘图的生命周期,包括surfaceCreated()、surfaceDestroyed()和surfaceChanged()方法。游戏内创建渲染线程,并自定义飞机和子弹对象,继承Sprite类进行绘制。MainActivity直接使用SurfaceView作为内容视图,通过onTouchEvent()捕获触屏事件并传递给SurfaceView处理。

823

被折叠的 条评论
为什么被折叠?



