飞机大战游戏详解

1、整体实现思路

(1)在绘制飞机、绘制导弹、爆炸效果、背景图片等利用了canvas的drawBitmap方法 ;

(2)监听屏幕,飞机跟踪等运用了onTouchEvent的方法;

(3)在背景音乐,爆炸音效等等方面,用到了SoundPool

(4)


2、如何绘制循环滚动的背景图片

    private void logic() {
        y1 += 5;
        y2 += 5;
        if (y1 >= MySurfaceView.height) {
            y1 = y2 - bitmap.getHeight();
        } else if (y2 >= MySurfaceView.height) {
            y2 = y1 - bitmap.getHeight();
        }
    }
public void draw(Canvas canvas,Paint paint) {

        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(bitmap, 0, y1, paint);
        canvas.drawBitmap(bitmap, 0, y2, paint);
        logic();
    }
 public void run() {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.bk);



这部分代码完成了背景循环滚动速率及循环

3、如何绘制飞机

在飞机的绘制中,利用了canvas的drawBitmap方法 

以x,y为轴,监听屏幕,,飞机随着手指移动。

飞机的长宽高的设定(x,y)

 this.bitmap1 = bitmap1;
        this.bitmaphp = bitmap2;
        x = MySurfaceView.width/2-bitmap1.getWidth()/2;
        y = MySurfaceView.heigth-bitmap1.getHeight();
        width = bitmap1.getWidth();
        heigth = bitmap1.getHeight();
    }

还要记得用代码裁剪飞机


 public void  myPlane (Canvas canvas,Paint paint){
        if (noCollsion){
            count++;
            if (count%8==0){
                canvas.drawBitmap(bitmap1,x,y,paint);
            }
            if (count==100){
                noCollsion=false;
                count=0;
            }
        }else {
            canvas.drawBitmap(bitmap1,x,y,paint);
        }
        for (int i =0;i<hp;i++){
            canvas.drawBitmap(bitmaphp,i*bitmaphp.getWidth(),MySurfaceView.heigth-bitmaphp.getHeight(),paint);
        }
    }

4、如何绘制子弹

绘制Boss的子弹

  for(int i=0;i<boosBulletVector.size();i++){
                            boosBulletVector.elementAt(i).draw(canvas,paint);
                            plane.isCollision(boosBulletVector.elementAt(i));
                        }

//移除飞机的子弹

 
                        for(int i=0;i<bulletVector.size();i++){
                            if(bulletVector.elementAt(i).isOut()){
                                bulletVector.remove(i);
                            }

                        }

                      

//绘制飞机的子弹

 
                        for(int i=0;i<bulletVector.size();i++){

                            bulletVector.elementAt(i).draw(canvas,paint);
                            if(bossPlane.isCollison(bulletVector.elementAt(i))){
                            }
}


  public void draw(Canvas canvas, Paint paint){
        canvas.drawBitmap(bitmap,x,y,paint);
        ff();
    }
    private void ff(){
        switch (teyp){
            case 0:
                y-=speed;
                if (y<-300){
                    isDead=true;
                }
                break;
            case 1:
                y+=(speed/2);
                if (y>MySurfaceView.heigth){
                    isDead=true;
                }


//我的小飞机的子弹

 Bitmap bitmap3 = BitmapFactory.decodeResource(getResources(),R.mipmap.mybullet);

//敌机敌机!!!!BOSS的子弹

Bitmap bitmap4 = BitmapFactory.decodeResource(getResources(),R.mipmap.bossbullet);

5、如何判断碰撞(子弹与飞机碰撞,飞机与飞机碰撞)


判断范围

  private void ff(){
        switch (teyp){
            case 0:
                y-=speed;
                if (y<-300){
                    isDead=true;
                }
                break;
            case 1:
                y+=(speed/2);
                if (y>MySurfaceView.heigth){
                    isDead=true;
                }
                break;
        }
    }


6、如何绘制爆炸效果

爆炸效果利用了canvasdrawBitmap 方法

都要用到ArrayList数组 ,


 public void draw(Canvas canvas, Paint paint){
        canvas.save();
        canvas.clipRect(x,y,x+frameW,y+frameH);
        canvas.drawBitmap(bitmap,x,y,paint);
        canvas.restore();
        ff();
 public void logic(){
        if (currentFrame<totalFrame){
            currentFrame++;
        }else {
            isEnd = true;
        }
    }


7、如何添加音效

首先要定义建造一个GameSoundPool类

其次定义一个SoundPool对象

用电脑自带的的播放器用soundPool.play播放

public class GameSoundpool {
    private SoundPool soundPool;
    private int s1;
    private int s2;
    public GameSoundpool(Context context) {
        this.soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC,0);
        s1 = soundPool.load(context, R.raw.shoot,1);
        s2 = soundPool.load(context, R.raw.explosion2,1);
    }
    public void playSound(int s){
        switch (s){
            case 1:
                soundPool.play(s1,1,1,0,1,1.0f);
                break;
            case 2:
                soundPool.play(s2,1,1,0,1,1.0f);
                break;
        }


    }
}


8、哪些地方用封装,继承,多态、方法重载、接口等

封装:处处都有,无处不在!

借口:SurfaceHolder.Callback,与Runnable 

继承:MySurfaceView类中有用到哦!!

多态:。。。。。。(没找到用到的地方)

方法重载:。。。。。(没找到用到的地方)

9、我的收获与感悟

作为我的第一个安卓项目,我对他有爱有恨。整整一周,我对他花了太多心思,思考做不出来的

过程是痛苦的,紧张的。但是他也让我补足了之前不懂的部分知识,虽然苦,但我觉得,是值得的!!

这次做的代码,还有不足之处,所以我不能放弃,安卓开发的知识是身高的,难度极高的,即使这样,

我还是应该有信心去完成一个又一个项目




猜你喜欢

转载自blog.csdn.net/nb157052/article/details/80525124