Android Studio 飞机大战

1.整体实现思路

实现飞机大战的简单思路:

①要先建MySurfaceView这个类,在这个类中绘制画布,有画布的高度和宽度,还有画笔,然后在这个建好的画布上开始继续“作画”画布利用了canvas实现画笔利用paint实现

②然后将飞机大战的部分闯关背景“画”上画布,实现绘图操作,利用多线程的方法,调用runable方法并用stater启动子线程。记得解锁画布,显示到屏幕上。

③选择背景图片,建立BackGround类,利用两张相同背景图片利用if循环语句实现背景图片的移动。并在MySurfaceView中调用。

④建立Myplane和BossPlane类,利用if语句for循环onTouchEventisCollisionnoisCollisionnoisCollisioncount等实现飞机的飞行移动,并可以实现屏幕触摸,还创建hp,来实现飞机血量的减少。还要利用isCrazytimecount实现boss飞机的疯狂模式时间。并在MySurfaceView中调用。

并在MySurfaceView中调用。

⑥创建Boom类,利用clipRect裁剪图片以及totalFrame来实现爆炸现象currenFrame用来显示当前显示的第几幅画。最后记得结束爆炸。并在MySurfaceView中调用。

⑦添加结束游戏输赢的图片,利用SoundPool,switch语句实现,利用RectF完成图片的大小与屏幕吻合,还有调用GAEM_STATE和switch在MySurfaceView中调用实现图片的运用。


2.如何实现循环滚动的背景图片

利用两张相同背景图片,画好背景图片的坐标即大小,利用if语句判断实现两张图片的循环滚动。

package com.example.jinxin.myapplication;  
  
import android.graphics.Bitmap;  
import android.graphics.Canvas;  
import android.graphics.Paint;  
  
/** 
 * Created by lenovo64 
 */  
  
public class BackGround {  
    private int y1;  
    private  int y2;  
    private  Bitmap bitmap ;  
    public BackGround( Bitmap bitmap){  
        this.bitmap = bitmap;  
        y1 = 0;  
        y2 = y1 - bitmap.getHeight();  
  
    }  
  
  
  
    public void draw(Canvas canvas,Paint paint){  
        logic();  
        canvas.drawBitmap(bitmap,0,y1,paint);  
        canvas.drawBitmap(bitmap,0,y2,paint);  
  
    }  
  
        public void logic(){  
            y1+=5;  
            y2+=5;  
            if (y1>MySurfaceView.height){  
                y1 = y2 - bitmap.getHeight();//移动到第二张图片的顶部  
  
            }  
            if (y2>MySurfaceView.height){  
                y2 = y1 - bitmap.getHeight();  
  
            }  
  
        }  
  
}  

3.如何绘制飞机

建立Myplane和BossPlane类,利用if语句和for循环和onTouchEvent和isCollision和noisCollision和noisCollisioncount等实现飞机的飞行移动,并可以实现屏幕触摸,还创建hp,来实现飞机血量的减少。还要利用isCrazy,time,count实现boss飞机的疯狂模式时间。并在MySurfaceView中调用。

MyPlane:

package com.example.jinxin.myapplication;  
  
import android.graphics.Bitmap;  
import android.graphics.Canvas;  
import android.graphics.Paint;  
import android.graphics.YuvImage;  
import android.util.Log;  
import android.view.MotionEvent;  
  
public class MyPlane {  
  
    private Bitmap bitmap;  
    private int x, y;  
    private static int width, height;  
    private int fx;  
  
    private boolean noCollision;  
    private int noCollisionCount;//碰撞计数器  
    private Bitmap bitmapHp;  
    private int hp = 3;  
  
  
    public MyPlane(Bitmap bitmap, Bitmap bitmapHp) {  
        this.bitmap = bitmap;  
        x = MySurfaceView.width / 2 - bitmap.getWidth() / 2;  
        y = MySurfaceView.height - bitmap.getHeight();  
        width = bitmap.getWidth();  
        height = bitmap.getHeight();  
        this.bitmapHp = bitmapHp;  
  
  
    }  
  
  
    public void draw(Canvas canvas, Paint paint) {  
        if (hp<=0){  
            MySurfaceView.GAME_STATE =3;  
  
        }  
  
        if (noCollision) {  
            noCollisionCount++;  
            if (noCollisionCount % 10 == 0) {  
                Log.e("&&&&&", "****");  
                canvas.drawBitmap(bitmap, x, y, paint);//飞机闪烁  
            }  
            if (noCollisionCount > 100) {//无敌时间  
                noCollision = false;  
                noCollisionCount = 0;  
            }  
  
        } else {  
            //非无敌状态  
            canvas.drawBitmap(bitmap, x, y, paint);  
  
        }  
  
        for (int i = 0; i < hp; i++) {  
            canvas.drawBitmap(bitmapHp, i * bitmapHp.getWidth(), MySurfaceView.height - bitmapHp.getHeight(), paint);  
  
        }  
  
    }  
  
  
    public void touchEvent(MotionEvent event) {  
        if (event.getAction() == MotionEvent.ACTION_MOVE) {  
            float ex = event.getX();  
            float ey = event.getY();  
            if (ex > x && ex < x + width && ey > y && ey < y + height) {  
                x = (int) ex - width / 2;  
                y = (int) ey - height / 2;  
                if (y < 0) {  
                    y = 0;  
                }  
                if (y + height > MySurfaceView.height) {  
                    y = MySurfaceView.height - height;  
                }  
            }  
            if (x < 0) {  
                x = 0;  
            }  
            if (x + width > MySurfaceView.width) {  
                y = MySurfaceView.width - width;  
            }  
  
        }  
  
    }  
  
    public boolean isCollision(Bullet bullet) {  
        if (noCollision) {  
            return false;  
        } else {  
            if (bullet.getX() > x && bullet.getX() < x + width && bullet.getY() > y && bullet.getY() < y + height) {  
                Log.e("***", "------------------");  
                noCollision = true;  
                if (hp > 0) {  
                    hp--;  
                }  
                return true;  
            }  
  
  
        }  
        return false;  
    }  
  
    public boolean isCollision(BossPlane bossPlane) {  
        if (noCollision) {  
            return false;  
        } else {  
            if (bossPlane.getY() + bossPlane.getFrameH() > y && bossPlane.getY() + bossPlane.getFrameH() < y + height) {  
  
                if (x < bossPlane.getX() && x + width > bossPlane.getX()) {  
                    Log.e("AAAAAAAAAa", "isCollision: ...................................");  
                    noCollision = true;  
                    if (hp > 0) {  
                        hp--;  
                    }  
                    return true;  
  
                }  
                if (x > bossPlane.getX() && x + width < bossPlane.getX() + bossPlane.getFrameW()) {  
                    noCollision = true;  
                    if (hp > 0) {  
                        hp--;  
                    }  
                    return true;  
  
                }  
                if (x > bossPlane.getX() && x + width > bossPlane.getX() + bossPlane.getFrameW()) {  
                    noCollision = true;  
                    if (hp > 0) {  
                        hp--;  
                    }  
                    return true;  
                }  
            }  
  
        }  
        return false;  
    }  
  
  
    public int getX() {  
        return x;  
    }  
  
    public int getY() {  
        return y;  
    }  
  
  
    public int getWidth() {  
        return width;  
  
  
    }  
  
}  

BossPlane:

package com.example.jinxin.myapplication;  
  
import android.graphics.Bitmap;  
import android.graphics.Canvas;  
import android.graphics.Paint;  
import android.util.Log;  
  
public class BossPlane {  
    private Bitmap bitmap;  
    private int x, y;  
    private int frameW, frameH;  
    private int speed = 10;  
    private int count;//计数器  
    private int time = 100;//疯狂模式间隔时间  
    private boolean isCrazy;  
    private int crazySpeed = 50;  
  
  
    private int bossHp = 10;  
  
  
    public BossPlane(Bitmap bitmap) {  
        this.bitmap = bitmap;  
        this.frameW = bitmap.getWidth() / 10;  
        this.frameH = bitmap.getHeight();  
        x = MySurfaceView.width / 2 - frameW / 2;  
  
  
    }  
  
  
    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();  
        logic();  
    }  
  
    public void logic() {  
        count++;  
        //疯狂模式  
        if (isCrazy) {  
            y = y + crazySpeed;  
            crazySpeed--;  
            if (y == 0) {  
                isCrazy = false;  
                crazySpeed = 50;  
  
            }  
  
        } else {  
            if (count % time == 0) {  
                isCrazy = true;  
  
            }  
            x = x + speed;  
            if (x > MySurfaceView.width - frameW) {  
                speed = -speed;  
  
            }  
            if (x < 0) {  
                speed = -speed;  
  
            }  
        }  
  
    }  
  
  
  
  
  
  
  

   public boolean isCollision(Bullet bullet) {  
        if (bullet.getX()>x&&bullet.getX()+bullet.getBitmap().getWidth()y&&bullet.getY()

4.如何绘制子弹

创建子弹Bullet的类,利用get set方法,以及if语句实现子弹的移动速度和击中飞机范围。最后要移除子弹,减少内存负荷。

  1. package com.example.jinxin.myapplication;  
  2.   
  3. import android.graphics.Bitmap;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Paint;  
  6.   
  7. public class Bullet {  
  8.   
  9.         private Bitmap bitmap;  
  10.         private int x,y;  
  11.         private int speed =10;  
  12.         public boolean isDead;  
  13.         private int type;  
  14.   
  15.   
  16.         public Bullet(Bitmap bitmap,int x,int y,int type){  
  17.             this.bitmap = bitmap;  
  18.             this.x = x;  
  19.             this.y = y;  
  20.             this.type = type;  
  21.   
  22.   
  23.   
  24.         }  
  25.         public void draw(Canvas canvas, Paint paint){  
  26.             canvas.drawBitmap(bitmap,x,y,paint);  
  27.             logic();  
  28.   
  29.   
  30.         }  
  31.         public void logic(){  
  32.             switch (type){  
  33.                 case 0:  
  34.                         y-=speed;  
  35.                         if (y<0){  
  36.                             isDead = true;  
  37.                         }  
  38.                     break;  
  39.   
  40.                 case 1:  
  41.                     y+=speed+5;  
  42.                     if (y>MySurfaceView.height){  
  43.                         isDead = true;  
  44.                     }  
  45.   
  46.                     break;  
  47.   
  48.             }  
  49.   
  50.         }  
  51.   
  52.   
  53.     public int getX() {  
  54.         return x;  
  55.     }  
  56.   
  57.     public int getY() {  
  58.         return y;  
  59.     }  
  60.   
  61.     public boolean isDead() {  
  62.         return isDead;  
  63.   
  64.   
  65.     }  
  66.   
  67.     public void setDead(boolean dead) {  
  68.         isDead = dead;  
  69.     }  
  70.   
  71.     public Bitmap getBitmap() {  
  72.         return bitmap;  
  73.     }  
  74. }  

5.如何判断碰

飞机与子弹
public boolean isCollision(Bullet bullet) {  
        if (noCollision) {  
            return false;  
        } else {  
            if (bullet.getX() > x && bullet.getX() < x + width && bullet.getY() > y && bullet.getY() < y + height) {  
                Log.e("***", "------------------");  
                noCollision = true;  
                if (hp > 0) {  
                    hp--;  
                }  
                return true;  
            }  
  
  
        }  
        return false;  
    }  

飞机与飞机的

public boolean isCollision(BossPlane bossPlane) {  
        if (noCollision) {  
            return false;  
        } else {  
            if (bossPlane.getY() + bossPlane.getFrameH() > y && bossPlane.getY() + bossPlane.getFrameH() < y + height) {  
  
                if (x < bossPlane.getX() && x + width > bossPlane.getX()) {  
                    Log.e("AAAAAAAAAa", "isCollision: ...................................");  
                    noCollision = true;  
                    if (hp > 0) {  
                        hp--;  
                    }  
                    return true;  
  
                }  
                if (x > bossPlane.getX() && x + width < bossPlane.getX() + bossPlane.getFrameW()) {  
                    noCollision = true;  
                    if (hp > 0) {  
                        hp--;  
                    }  
                    return true;  
  
                }  
                if (x > bossPlane.getX() && x + width > bossPlane.getX() + bossPlane.getFrameW()) {  
                    noCollision = true;  
                    if (hp > 0) {  
                        hp--;  
                    }  
                    return true;  
                }  
            }  
  
        }  
        return false;  
    }  

6.如何绘制爆炸效果:

创建Boom类,利用clipRect裁剪图片以及totalFrame来实现爆炸现象,currenFrame用来显示当前显示的第几幅画。最后记得结束爆炸。

package com.example.jinxin.myapplication;  
  
import android.graphics.Bitmap;  
import android.graphics.Canvas;  
import android.graphics.Paint;  
  
public class Boom {  
    private int totalFrame;  
    private Bitmap bitmap;  
    private int x,y;  
    private int currenFrame;//当前显示的第几幅画面  
    private int frameW;  
    private int frameH;  
    private boolean isEnd;  
  
  
  
    public Boom(Bitmap bitmap, int x, int y,int totalFrame) {  
        this.bitmap = bitmap;  
        this.x = x;  
        this.y = y;  
        this.totalFrame = totalFrame;  
        frameW = bitmap.getWidth()/totalFrame;  
        frameH = bitmap.getHeight();  
    }  
  
  
    public void draw(Canvas canvas, Paint paint){  
        canvas.save();  
        canvas.clipRect(x,y,x+frameW,y+frameH);  
        canvas.drawBitmap(bitmap,x-currenFrame*frameW,y,paint);  
        canvas.restore();  
        logic();  
  
  
  
    }  
    public void logic(){  
        if (currenFrame

7.如何添加音效:

利用MediaPlayer来实现

[java]  view plain  copy
  1. MediaPlayer mediaPlayer = null;  
  2.         mediaPlayer = MediaPlayer.create(getContext(),R.raw.bgm_zhuxuanlv);  
  3.         mediaPlayer.setLooping(true);  
  4.         mediaPlayer.start();  
  5.         Paint paint = new Paint();  

8.哪些地方用到封装,继续,多态,方法的重载,接口等

运用到封装的地方为(代码):

封装就是把对象的信息和内部逻辑的结构隐藏起来。在MySurfaceView,Bullet,MyPlane,BossPlane,Boom,GameSounPool几个类中都用到了,

MySurfaceView

[java]  view plain  copy
  1. public static int height;  
  2.    private SurfaceHolder surfaceHolder;  
  3.    private Canvas canvas;//绘制图形的画布  
  4.    private boolean isDrawing = true;//标志位  
  5.    public static int width;  
  6.    private MyPlane plane;  
  7.    //Vector是线程安全的,ArrayList是非线程安全的  
  8.    private Vector bulletVector = new Vector<>();  
  9.    private Vector bossbulletVector = new Vector<>();  
  10.    private VectorboomVector = new Vector<>();  
  11.    private int count;  
  12.    private BossPlane bossPlane;  
  13.    private GameSoundPool gameSoundPool;  
  14.    public static int GAME_STATE = 4;  
  15.    private MediaPlayer mediaPlayer;  
BossPlane
[java]  view plain  copy
  1. private Bitmap bitmap;  
  2.     private int x, y;  
  3.     private int frameW, frameH;  
  4.     private int speed = 10;  
  5.     private int count;//计数器  
  6.     private int time = 100;//疯狂模式间隔时间  
  7.     private boolean isCrazy;  
  8.     private int crazySpeed = 50;  
MyPlane
[java]  view plain  copy
  1. private Bitmap bitmap;  
  2.    private int x, y;  
  3.    private static int width, height;  
  4.    private int fx;  
  5.   
  6.    private boolean noCollision;  
  7.    private int noCollisionCount;//碰撞计数器  
  8.    private Bitmap bitmapHp;  
  9.    private int hp = 3;  
Bullet
[java]  view plain  copy
  1. private Bitmap bitmap;  
  2.       private int x,y;  
  3.       private int speed =10;  
  4.       public boolean isDead;  
  5.       private int type;  
GameSounPool
[java]  view plain  copy
  1. private SoundPool soundPool;  
  2.     private int s1;  
  3.     private int s2;  

Boom

[java]  view plain  copy
  1. private int totalFrame;  
  2.     private Bitmap bitmap;  
  3.     private int x,y;  
  4.     private int currenFrame;//当前显示的第几幅画面  
  5.     private int frameW;  
  6.     private int frameH;  
  7.     private boolean isEnd;  


方法的重载:两个或多个方法名一样,参数列表不一样。(代码)

MySurfaceView

[java]  view plain  copy
  1. @Override  
  2.    public void surfaceCreated(SurfaceHolder holder) {  
  3.        new Thread(this).start();//启动子线程  
  4.        height = getHeight();  
  5.        width = getWidth();  
  6.    }  
  7.   
  8.    @Override  
  9.    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {  
  10.   
  11.    }  
  12.   
  13.    @Override  
  14.    public void surfaceDestroyed(SurfaceHolder holder) {  
  15.        isDrawing = false;  
  16.    }  

BossPlane

[java]  view plain  copy
  1. public BossPlane(Bitmap bitmap) {  
  2.         this.bitmap = bitmap;  
  3.         this.frameW = bitmap.getWidth() / 10;  
  4.         this.frameH = bitmap.getHeight();  
  5.         x = MySurfaceView.width / 2 - frameW / 2;  
  6.   
  7.   
  8.     }  
  9.   
  10.   
  11.   
  12.   
  13.   
  14. public boolean isCollision(Bullet bullet) {  
  15.         if (bullet.getX()>x&&bullet.getX()+bullet.getBitmap().getWidth()y&&bullet.getY()
  16.             bossHp--;  
  17.   
  18.             bullet.setDead(true);//设置子弹状态,碰撞后修改子弹状态为dead,从数组中移除  
  19.             if (bossHp<0){  
  20.                 MySurfaceView.GAME_STATE =2;  
  21.             }  
  22.             return true;  
  23.   
  24.   
  25.         }  
  26.   
  27.         return false;  
  28.       
[java]  view plain  copy
  1. public void draw(Canvas canvas, Paint paint) {  
  2.        canvas.save();  
  3.        canvas.clipRect(x, y, x + frameW, y + frameH);  
  4.        canvas.drawBitmap(bitmap, x, y, paint);  
  5.        canvas.restore();  
  6.        logic();  
  7.    }  


MyPlane

[java]  view plain  copy
  1. public void draw(Canvas canvas, Paint paint) {  
  2.         if (hp<=0){  
  3.             MySurfaceView.GAME_STATE =3;  
  4.   
  5.         }  
  6.   
  7.         if (noCollision) {  
  8.             noCollisionCount++;  
  9.             if (noCollisionCount % 10 == 0) {  
  10.                 Log.e("&&&&&""****");  
  11.                 canvas.drawBitmap(bitmap, x, y, paint);//飞机闪烁  
  12.             }  
  13.             if (noCollisionCount > 100) {//无敌时间  
  14.                 noCollision = false;  
  15.                 noCollisionCount = 0;  
  16.             }  
  17.   
  18.         } else {  
  19.             //非无敌状态  
  20.             canvas.drawBitmap(bitmap, x, y, paint);  
  21.   
  22.         }  
  23.   
  24.         for (int i = 0; i < hp; i++) {  
  25.             canvas.drawBitmap(bitmapHp, i * bitmapHp.getWidth(), MySurfaceView.height - bitmapHp.getHeight(), paint);  
  26.   
  27.         }  
  28.   
  29.     }  
  30.   
  31.   
  32.     public void touchEvent(MotionEvent event) {  
  33.         if (event.getAction() == MotionEvent.ACTION_MOVE) {  
  34.             float ex = event.getX();  
  35.             float ey = event.getY();  
  36.             if (ex > x && ex < x + width && ey > y && ey < y + height) {  
  37.                 x = (int) ex - width / 2;  
  38.                 y = (int) ey - height / 2;  
  39.                 if (y < 0) {  
  40.                     y = 0;  
  41.                 }  
  42.                 if (y + height > MySurfaceView.height) {  
  43.                     y = MySurfaceView.height - height;  
  44.                 }  
  45.             }  
  46.             if (x < 0) {  
  47.                 x = 0;  
  48.             }  
  49.             if (x + width > MySurfaceView.width) {  
  50.                 y = MySurfaceView.width - width;  
  51.             }  
  52.   
  53.         }  
  54.   
  55.     }  
  56.   
  57.     public boolean isCollision(Bullet bullet) {  
  58.         if (noCollision) {  
  59.             return false;  
  60.         } else {  
  61.             if (bullet.getX() > x && bullet.getX() < x + width && bullet.getY() > y && bullet.getY() < y + height) {  
  62.                 Log.e("***""------------------");  
  63.                 noCollision = true;  
  64.                 if (hp > 0) {  
  65.                     hp--;  
  66.                 }  
  67.                 return true;  
  68.             }  
  69.   
  70.   
  71.         }  
  72.         return false;  
  73.     }  
  74.   
  75.     public boolean isCollision(BossPlane bossPlane) {  
  76.         if (noCollision) {  
  77.             return false;  
  78.         } else {  
  79.             if (bossPlane.getY() + bossPlane.getFrameH() > y && bossPlane.getY() + bossPlane.getFrameH() < y + height) {  
  80.   
  81.                 if (x < bossPlane.getX() && x + width > bossPlane.getX()) {  
  82.                     Log.e("AAAAAAAAAa""isCollision: ...................................");  
  83.                     noCollision = true;  
  84.                     if (hp > 0) {  
  85.                         hp--;  
  86.                     }  
  87.                     return true;  
  88.   
  89.                 }  
  90.                 if (x > bossPlane.getX() && x + width < bossPlane.getX() + bossPlane.getFrameW()) {  
  91.                     noCollision = true;  
  92.                     if (hp > 0) {  
  93.                         hp--;  
  94.                     }  
  95.                     return true;  
  96.   
  97.                 }  
  98.                 if (x > bossPlane.getX() && x + width > bossPlane.getX() + bossPlane.getFrameW()) {  
  99.                     noCollision = true;  
  100.                     if (hp > 0) {  
  101.                         hp--;  
  102.                     }  
  103.                     return true;  
  104.                 }  
  105.             }  
  106.   
  107.         }  
  108.         return false;  
  109.     }  

Boom

[java]  view plain  copy
  1. public void draw(Canvas canvas, Paint paint){  
  2.        canvas.save();  
  3.        canvas.clipRect(x,y,x+frameW,y+frameH);  
  4.        canvas.drawBitmap(bitmap,x-currenFrame*frameW,y,paint);  
  5.        canvas.restore();  
  6.        logic();  
  7.   
  8.   
  9.   
  10.    }  
  11.    public void logic(){  
  12.        if (currenFrame
  13.            currenFrame++;  
  14.        }else {  
  15.            isEnd = true;  
  16.        }  
  17.    }  
  18.    public boolean isEnd(){  
  19.        return isEnd;  
  20.    }  



Bullet

[java]  view plain  copy
  1. public void draw(Canvas canvas, Paint paint){  
  2.            canvas.drawBitmap(bitmap,x,y,paint);  
  3.            logic();  
  4.   
  5.   
  6.        }  
  7.        public void logic(){  
  8.            switch (type){  
  9.                case 0:  
  10.                        y-=speed;  
  11.                        if (y<0){  
  12.                            isDead = true;  
  13.                        }  
  14.                    break;  
  15.   
  16.                case 1:  
  17.                    y+=speed+5;  
  18.                    if (y>MySurfaceView.height){  
  19.                        isDead = true;  
  20.                    }  
  21.   
  22.                    break;  
  23.   
  24.            }  
  25.   
  26.        }  


多态:两个或多个属于不同类的对象,对于同一个消息(方法调用)做出不同响应的方式。
在飞机大战中,MySurView里的private修饰的bitmap,height ,width,count等在bossplane,myplane,bullet,boom等中,都做出了不同的反应,比如高度,MySurfaceView中定义好了高度,在bossplane中的高度和myplane中的高度是不同的,都做出了各自的响应,hight在bossplane是指boosplane的高度,在myplane中指myplane的的高度,以此类推.。


接口:接口是一种特殊的抽象类,特点是不被interface修饰,接口也有抽象类,但不被abstr修饰,实现接口需要用implements。

代码:

[java]  view plain  copy
  1. public class MySurfaceView extends SurfaceView "color:#cc0000;">implements SurfaceHolder.Callback,Runnable {  
  2.   
  3.   
  4.     public static int height;  
  5.     private SurfaceHolder surfaceHolder;  
  6.     private Canvas canvas;//绘制图形的画布  
  7.     private boolean isDrawing = true;//标志位  
  8.     public static int width;  
  9.     private MyPlane plane;  
  10.     //Vector是线程安全的,ArrayList是非线程安全的  
  11.     private Vector bulletVector = new Vector<>();  
  12.     private Vector bossbulletVector = new Vector<>();  
  13.     private VectorboomVector = new Vector<>();  
  14.     private int count;  
  15.     private BossPlane bossPlane;  
  16.     private GameSoundPool gameSoundPool;  
  17.     public static int GAME_STATE = 4;  
  18.     private MediaPlayer mediaPlayer;  
  19.   
  20.   
  21.     public MySurfaceView(Context context) {  
  22.         super(context);  
  23.         gameSoundPool = new GameSoundPool(getContext());  
  24.         init();  
  25.   
  26.     }  
  27.   
  28.     /** 
  29.      * 初始化操作 
  30.      */  
  31.   
  32.     private void init() {  
  33.         surfaceHolder = getHolder();  
  34.         surfaceHolder.addCallback(this);//添加回调事件监听  
  35.         setFocusable(true);//设置可聚焦  
  36.         setKeepScreenOn(true);//设置屏幕常亮  
  37.         setFocusableInTouchMode(true);//设置触摸模式  
  38.   
  39.   
  40.     }  
  41.   
  42.     @Override  
  43.     public void surfaceCreated(SurfaceHolder holder) {  
  44.         new Thread(this).start();//启动子线程  
  45.         height = getHeight();  
  46.         width = getWidth();  
  47.     }  
  48.   
  49.     @Override  
  50.     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {  
  51.   
  52.     }  
  53.   
  54.     @Override  
  55.     public void surfaceDestroyed(SurfaceHolder holder) {  
  56.         isDrawing = false;  
  57.     }  
  58.   
  59.     /** 
  60.      * 实现绘图操作 
  61.      */  
  62.   
  63.     @Override  
  64.     public void run() {  
  65.         MediaPlayer mediaPlayer = null;  
  66.         mediaPlayer = MediaPlayer.create(getContext(),R.raw.bgm_zhuxuanlv);  
  67.         mediaPlayer.setLooping(true);  
  68.         mediaPlayer.start();  
  69.         Paint paint = new Paint();  
  70.   
  71.   
  72.   
  73.   
  74. //        BackGround1 backGround1= new BackGround1(BitmapFactory.decodeResource(getResources(), mipmap.mainmenu));  
  75.         BackGround backGround = new BackGround(BitmapFactory.decodeResource(getResources(), mipmap.bk));  
  76.         plane = new MyPlane(BitmapFactory.decodeResource(getResources(), mipmap.myplane),BitmapFactory.decodeResource(getResources(), mipmap.myhp));  
  77.         BossPlane bossPlane = new BossPlane(BitmapFactory.decodeResource(getResources(), mipmap.bossplane));  
  78.   
  79.         while (isDrawing) {  
  80.             count++;  
  81.             try {  
  82.                 canvas = surfaceHolder.lockCanvas();//锁定(选定)画布  
  83.                 canvas.drawColor(Color.WHITE);  
  84.                 switch (GAME_STATE){  
  85.   
  86.                     case 1:  
  87.                         backGround.draw(canvas, paint);  
  88.                         plane.draw(canvas, paint);  
  89.                         bossPlane.draw(canvas, paint);  
  90.   
  91.                         if(count%30==0){  
  92.                             Bullet bullet = new Bullet(BitmapFactory.decodeResource(getResources(), mipmap.mybullet), plane.getX() , plane.getY(),0);  
  93.                             Bullet bullet1 = new Bullet(BitmapFactory.decodeResource(getResources(), mipmap.mybullet), plane.getX()+ plane.getWidth(), plane.getY(),0);  
  94.                             bulletVector.add(bullet);  
  95.                             bulletVector.add(bullet1);  
  96.                             gameSoundPool.playSound(1);  
  97.   
  98.   
  99.                         }  
  100.   
  101.   
  102.   
  103.   
  104.                         //移除消失的子弹  
  105.                         for (int i = 0; i < bulletVector.size(); i++) {  
  106.   
  107.                             if (bulletVector.elementAt(i).isDead()) {  
  108.                                 bulletVector.remove(i);  
  109.   
  110.                             }  
  111.                         }  
  112.                         //绘制玩家子弹  
  113.                         for (int i = 0; i < bulletVector.size(); i++){  
  114.   
  115.                             bulletVector.elementAt(i).draw(canvas, paint);  
  116.                             if (bossPlane.isCollision(bulletVector.elementAt(i))){  
  117.                                 gameSoundPool.playSound(2);  
  118.                                 Boom boom = new Boom(BitmapFactory.decodeResource(getResources(),R.mipmap.boom),bossPlane.getX(),bossPlane.getY(),7);  
  119.                                 boomVector.add(boom);  
  120.                             }  
  121.                         }  
  122.   
  123.   
  124.   
  125.   
  126.                         for (int i =0;i
  127.                             if (boomVector.elementAt(i).isEnd()){  
  128.                                 boomVector.remove(i);  
  129.                             }else {  
  130.   
  131.                                 boomVector.elementAt(i).draw(canvas,paint);  
  132.                                 //Log.e("..","....");  
  133.                             }  
  134.                         }  
  135.   
  136.   
  137.                         if(count%30==0){  
  138.                             Bullet bullet = new Bullet(BitmapFactory.decodeResource(getResources(), mipmap.bossbullet), bossPlane.getX() , bossPlane.getY()+bossPlane.getFrameH(),1);  
  139.                             Bullet bullet1 = new Bullet(BitmapFactory.decodeResource(getResources(), mipmap.bossbullet), bossPlane.getX()+ bossPlane.getFrameW(), bossPlane.getY()+bossPlane.getFrameH(),1);  
  140.                             bossbulletVector.add(bullet);  
  141.                             bossbulletVector.add(bullet1);  
  142.   
  143.                         }  
  144.                         //移除boss的子弹  
  145.                         for (int i = 0; i < bossbulletVector.size(); i++) {  
  146.                             if (bossbulletVector.elementAt(i).isDead()) {  
  147.                                 bossbulletVector.remove(i);  
  148.   
  149.                             }  
  150.                         }  
  151.                         //绘制boss子弹  
  152.                         for (int i = 0; i < bossbulletVector.size(); i++) {  
  153.                             bossbulletVector.elementAt(i).draw(canvas, paint);  
  154.                             plane.isCollision(bossbulletVector.elementAt(i));  
  155.   
  156.   
  157.                         }  
  158.   
  159.                         plane.isCollision(bossPlane);//一定不要忘了调用这个方法  
  160.                         break;  
  161.                     case 2://RectF矩形  
  162.                         RectF rectF0 = new RectF(0,0,getWidth(),getHeight());  
  163.                         canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), mipmap.gamewin),null,rectF0,paint);  
  164.                         break;  
  165.                     case 3:  
  166.                         RectF rectF = new RectF(0,0,getWidth(),getHeight());  
  167.                         canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), mipmap.gamelost),null,rectF,paint);  
  168.                         break;  
  169.                     case 4:  
  170.                         RectF rectF1 = new RectF(0,0,getWidth(),getHeight());  
  171.                         canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), mipmap.mainmenu),null,rectF1,paint);  
  172.                         RectF rectF2 = new RectF(0,getHeight()*2/5,getWidth(),getHeight()*3/5);  
  173.                         canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), mipmap.logo),null,rectF2,paint);  
  174.                         if(count>20){  
  175.                             MySurfaceView.GAME_STATE = 1;  
  176.                         }  
  177.                         break;  
  178.   
  179.   
  180.                 }  
  181.   
  182.   
  183.   
  184.   
  185.                 } catch(Exception e){  
  186.             e.printStackTrace();  
  187.         } finally{  
  188.             if (canvas != null) {  
  189.                 surfaceHolder.unlockCanvasAndPost(canvas);//解锁画布,显示到屏幕上  
  190.             }  
  191.   
  192.         }  
  193.           
  194.     }  
  195.   
  196. }  
  197.   
  198.     @Override  
  199.     public boolean onTouchEvent(MotionEvent event) {  
  200. //        if (event.getAction()==MotionEvent.ACTION_MOVE){  
  201. //            Log.e("MySurfaceView","onTouchEvent:"+event.getX());  
  202. //        }else if (event.getAction() == MotionEvent.ACTION_UP){  
  203. //            Log.d("MySurfaceView","手指按起");  
  204. //        }else if (event.getAction() == MotionEvent.ACTION_DOWN){  
  205. //            Log.d("MySurfaceView","手指按下");  
  206.   
  207.         plane.touchEvent(event);  
  208.             return true;//永远监听屏幕触摸事件  
  209.   
  210.     }  
  211.   
  212. }  



继承:继承是从已有的类中派生出新类,新的类能吸收已有类的数据属性行为并能扩展新的能力。

代码:

[java]  view plain  copy
  1. public "color:#ff0000;">class MySurfaceView "color:#ff0000;">extends SurfaceView implements SurfaceHolder.Callback,Runnable {  
  2.   
  3.   
  4.     public static int height;  
  5.     private SurfaceHolder surfaceHolder;  
  6.     private Canvas canvas;//绘制图形的画布  
  7.     private boolean isDrawing = true;//标志位  
  8.     public static int width;  
  9.     private MyPlane plane;  
  10.     //Vector是线程安全的,ArrayList是非线程安全的  
  11.     private Vector bulletVector = new Vector<>();  
  12.     private Vector bossbulletVector = new Vector<>();  
  13.     private VectorboomVector = new Vector<>();  
  14.     private int count;  
  15.     private BossPlane bossPlane;  
  16.     private GameSoundPool gameSoundPool;  
  17.     public static int GAME_STATE = 4;  
  18.     private MediaPlayer mediaPlayer;  
  19.   
  20.   
  21.     public MySurfaceView(Context context) {  
  22.         super(context);  
  23.         gameSoundPool = new GameSoundPool(getContext());  
  24.         init();  
  25.   
  26.     }  
  27.   
  28.     /** 
  29.      * 初始化操作 
  30.      */  
  31.   
  32.     private void init() {  
  33.         surfaceHolder = getHolder();  
  34.         surfaceHolder.addCallback(this);//添加回调事件监听  
  35.         setFocusable(true);//设置可聚焦  
  36.         setKeepScreenOn(true);//设置屏幕常亮  
  37.         setFocusableInTouchMode(true);//设置触摸模式  
  38.   
  39.   
  40.     }  
  41.   
  42.     @Override  
  43.     public void surfaceCreated(SurfaceHolder holder) {  
  44.         new Thread(this).start();//启动子线程  
  45.         height = getHeight();  
  46.         width = getWidth();  
  47.     }  
  48.   
  49.     @Override  
  50.     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {  
  51.   
  52.     }  
  53.   
  54.     @Override  
  55.     public void surfaceDestroyed(SurfaceHolder holder) {  
  56.         isDrawing = false;  
  57.     }  
  58.   
  59.     /** 
  60.      * 实现绘图操作 
  61.      */  
  62.   
  63.     @Override  
  64.     public void run() {  
  65.         MediaPlayer mediaPlayer = null;  
  66.         mediaPlayer = MediaPlayer.create(getContext(),R.raw.bgm_zhuxuanlv);  
  67.         mediaPlayer.setLooping(true);  
  68.         mediaPlayer.start();  
  69.         Paint paint = new Paint();  
  70.   
  71.   
  72.   
  73.   
  74. //        BackGround1 backGround1= new BackGround1(BitmapFactory.decodeResource(getResources(), mipmap.mainmenu));  
  75.         BackGround backGround = new BackGround(BitmapFactory.decodeResource(getResources(), mipmap.bk));  
  76.         plane = new MyPlane(BitmapFactory.decodeResource(getResources(), mipmap.myplane),BitmapFactory.decodeResource(getResources(), mipmap.myhp));  
  77.         BossPlane bossPlane = new BossPlane(BitmapFactory.decodeResource(getResources(), mipmap.bossplane));  
  78.   
  79.         while (isDrawing) {  
  80.             count++;  
  81.             try {  
  82.                 canvas = surfaceHolder.lockCanvas();//锁定(选定)画布  
  83.                 canvas.drawColor(Color.WHITE);  
  84.                 switch (GAME_STATE){  
  85.   
  86.                     case 1:  
  87.                         backGround.draw(canvas, paint);  
  88.                         plane.draw(canvas, paint);  
  89.                         bossPlane.draw(canvas, paint);  
  90.   
  91.                         if(count%30==0){  
  92.                             Bullet bullet = new Bullet(BitmapFactory.decodeResource(getResources(), mipmap.mybullet), plane.getX() , plane.getY(),0);  
  93.                             Bullet bullet1 = new Bullet(BitmapFactory.decodeResource(getResources(), mipmap.mybullet), plane.getX()+ plane.getWidth(), plane.getY(),0);  
  94.                             bulletVector.add(bullet);  
  95.                             bulletVector.add(bullet1);  
  96.                             gameSoundPool.playSound(1);  
  97.   
  98.   
  99.                         }  
  100.   
  101.   
  102.   
  103.   
  104.                         //移除消失的子弹  
  105.                         for (int i = 0; i < bulletVector.size(); i++) {  
  106.   
  107.                             if (bulletVector.elementAt(i).isDead()) {  
  108.                                 bulletVector.remove(i);  
  109.   
  110.                             }  
  111.                         }  
  112.                         //绘制玩家子弹  
  113.                         for (int i = 0; i < bulletVector.size(); i++){  
  114.   
  115.                             bulletVector.elementAt(i).draw(canvas, paint);  
  116.                             if (bossPlane.isCollision(bulletVector.elementAt(i))){  
  117.                                 gameSoundPool.playSound(2);  
  118.                                 Boom boom = new Boom(BitmapFactory.decodeResource(getResources(),R.mipmap.boom),bossPlane.getX(),bossPlane.getY(),7);  
  119.                                 boomVector.add(boom);  
  120.                             }  
  121.                         }  
  122.   
  123.   
  124.   
  125.   
  126.                         for (int i =0;i
  127.                             if (boomVector.elementAt(i).isEnd()){  
  128.                                 boomVector.remove(i);  
  129.                             }else {  
  130.   
  131.                                 boomVector.elementAt(i).draw(canvas,paint);  
  132.                                 //Log.e("..","....");  
  133.                             }  
  134.                         }  
  135.   
  136.   
  137.                         if(count%30==0){  
  138.                             Bullet bullet = new Bullet(BitmapFactory.decodeResource(getResources(), mipmap.bossbullet), bossPlane.getX() , bossPlane.getY()+bossPlane.getFrameH(),1);  
  139.                             Bullet bullet1 = new Bullet(BitmapFactory.decodeResource(getResources(), mipmap.bossbullet), bossPlane.getX()+ bossPlane.getFrameW(), bossPlane.getY()+bossPlane.getFrameH(),1);  
  140.                             bossbulletVector.add(bullet);  
  141.                             bossbulletVector.add(bullet1);  
  142.   
  143.                         }  
  144.                         //移除boss的子弹  
  145.                         for (int i = 0; i < bossbulletVector.size(); i++) {  
  146.                             if (bossbulletVector.elementAt(i).isDead()) {  
  147.                                 bossbulletVector.remove(i);  
  148.   
  149.                             }  
  150.                         }  
  151.                         //绘制boss子弹  
  152.                         for (int i = 0; i < bossbulletVector.size(); i++) {  
  153.                             bossbulletVector.elementAt(i).draw(canvas, paint);  
  154.                             plane.isCollision(bossbulletVector.elementAt(i));  
  155.   
  156.   
  157.                         }  
  158.   
  159.                         plane.isCollision(bossPlane);//一定不要忘了调用这个方法  
  160.                         break;  
  161.                     case 2://RectF矩形  
  162.                         RectF rectF0 = new RectF(0,0,getWidth(),getHeight());  
  163.                         canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), mipmap.gamewin),null,rectF0,paint);  
  164.                         break;  
  165.                     case 3:  
  166.                         RectF rectF = new RectF(0,0,getWidth(),getHeight());  
  167.                         canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), mipmap.gamelost),null,rectF,paint);  
  168.                         break;  
  169.                     case 4:  
  170.                         RectF rectF1 = new RectF(0,0,getWidth(),getHeight());  
  171.                         canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), mipmap.mainmenu),null,rectF1,paint);  
  172.                         RectF rectF2 = new RectF(0,getHeight()*2/5,getWidth(),getHeight()*3/5);  
  173.                         canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), mipmap.logo),null,rectF2,paint);  
  174.                         if(count>20){  
  175.                             MySurfaceView.GAME_STATE = 1;  
  176.                         }  
  177.                         break;  
  178.   
  179.   
  180.                 }  
  181.   
  182.   
  183.   
  184.   
  185.                 } catch(Exception e){  
  186.             e.printStackTrace();  
  187.         } finally{  
  188.             if (canvas != null) {  
  189.                 surfaceHolder.unlockCanvasAndPost(canvas);//解锁画布,显示到屏幕上  
  190.             }  
  191.   
  192.         }  
  193.           
  194.     }  
  195.   
  196. }  
  197.   
  198.     @Override  
  199.     public boolean onTouchEvent(MotionEvent event) {  
  200. //        if (event.getAction()==MotionEvent.ACTION_MOVE){  
  201. //            Log.e("MySurfaceView","onTouchEvent:"+event.getX());  
  202. //        }else if (event.getAction() == MotionEvent.ACTION_UP){  
  203. //            Log.d("MySurfaceView","手指按起");  
  204. //        }else if (event.getAction() == MotionEvent.ACTION_DOWN){  
  205. //            Log.d("MySurfaceView","手指按下");  
  206.   
  207.         plane.touchEvent(event);  
  208.             return true;//永远监听屏幕触摸事件  
  209.   
  210.     }  
  211.   
  212. }  


感悟

代码是不会骗人的,但是如果不仔细的话一个括号或一个字母错了会让整段代码垮掉

不报错代码不一定是对的,但是报错的一定是错的

打代码是一个很复杂,但也是一个很简单的事情

每个符号都有不同的定义,就像是一个又一个的拼图一些是学到的另一些是自己摸索的利用代码可以画画可以播音

可以游戏,不同的组合,演绎不同的精彩

时间很快,一下一个月过去了

我重新找到了代码的乐趣,就像发现了新大陆一样


你可能感兴趣的:(Android Studio 飞机大战)