飞机大战游戏详解

整体思路:

1.我们先把需要制作飞机大战的图片和音效拷贝到里面。

2.我们要创建一个    MySurfaceView类;再创建画布类Canvas的对象canvas和画笔类Paint对象paint并用private修饰。

3.在MySurfaceView类中初始化surfaceHolder;再添加回调函数surfaceHolder = this.getHolder();接着我们初始化画笔。

4.在MysurfaceView类中添加一个子线程,在run()方法中写一个draw()方法并用快捷键实现。再在draw()方法中先将画布加锁,再判断画布是否为空如果不是则将画布显示在模拟器上。

5.开始创作游戏内容。先创建BackGround类将画布背景换成我们需要的背景图案 ,我们先创建一个Bullet类,通过构造方法和switch语句实现飞机和Boss的子弹,再创建Myplane类,通过if和for循环做出自己飞机的坐标,再用noCollision做出无敌状态和无敌时间和血量,再创造Bossplane类,用logic编写出疯狂模式,大部分和Mplane类相似,再创建Boom类用clipRect剪切图片,实现爆炸效果,再创建一个GameSoundPool类将音效放入,最后在MysurfaceView类中进行调用。编写时注意 坐标x,y。(绘制运用封装,继承,多态,方法重载,接口等)。

如何实现循环滚动背景图片:(代码)

绘制图形画布,初始化操作,启动子线程,添加回调事件监听,锁定和解锁画布实现,用logic方法将第二章图片移到第一张图片上面实现循环。


背景循环:
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback,Runnable{

    private SurfaceHolder surfaceHolder;
    private Canvas canvas;//绘制图形的画布
    private  boolean isDrawing = true;//标志位
    public static int height;
    public MySurfaceView(Context context) {
        super(context);
        init();
    }
    /*
    初始化操作
     */
    private void init(){
        surfaceHolder = getHolder();
        surfaceHolder.addCallback(this);//添加回调事件监听
        setFocusable(true);
        setKeepScreenOn(true);
        
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        new Thread(this).start();//启动子线程
        height = getHeight();//把getHeight(获取height的方法)的返回值赋给height

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        isDrawing = false;

    }

    @Override
    public void run() {

        BackGround backGround = new BackGround(BitmapFactory.decodeResource(getResources(),R.mipmap.bk));
        while (isDrawing) {
            canvas = surfaceHolder.lockCanvas();//锁定(选定)画布
            canvas.drawColor(Color.WHITE);
            backGround.draw(canvas);

            surfaceHolder.unlockCanvasAndPost(canvas);//解锁画布,显示到屏幕上

    }
    }
}





import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

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){
        logic();
        Paint paint = new Paint();
        canvas.drawBitmap(bitmap,0,y1,paint);
        canvas.drawBitmap(bitmap,0,y2,paint);
    }

    public void logic() {
        y1+=10;
        y2+=10;
        if (y1>=MySurfaceView.height){
            y1=y2-bitmap.getHeight();//移动到第二张图片的顶部
        }
        if (y2>=MySurfaceView.height){
            y2=y1-bitmap.getHeight();
        }
    }
}

如何绘制飞机:

创建Myplane类,运用runnable接口实现run方法。

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback,Runnable{

    private SurfaceHolder surfaceHolder;
    private Canvas canvas;//绘制图形的画布
    private  boolean isDrawing = true;//标志位
    public static int height;
    public static int width;

    public MySurfaceView(Context context) {
        super(context);
        init();
    }
    /*
    初始化操作
     */
    private void init(){
        surfaceHolder = getHolder();
        surfaceHolder.addCallback(this);//添加回调事件监听
        setFocusable(true);
        setKeepScreenOn(true);

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        new Thread(this).start();//启动子线程
        height = getHeight();//把getHeight(获取height的方法)的返回值赋给height
        width = getWidth();

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        isDrawing = false;

    }

    @Override
    public void run() {
        Paint paint = new Paint();
        BackGround backGround = new BackGround(BitmapFactory.decodeResource(getResources(),R.mipmap.bk));
        Myplane plane = new Myplane(BitmapFactory.decodeResource(getResources(),R.mipmap.myplane));

        while (isDrawing) {

            try {
                canvas = surfaceHolder.lockCanvas();//锁定(选定)画布
                canvas.drawColor(Color.WHITE);
                backGround.draw(canvas,paint);
                plane.draw(canvas,paint);

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (canvas!=null){
                    surfaceHolder.unlockCanvasAndPost(canvas);//解锁画布,显示到屏幕上
                }
            }

        }
    }
}





import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

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+=10;
        y2+=10;
        if (y1>=MySurfaceView.height){
            y1=y2-bitmap.getHeight();//移动到第二张图片的顶部
        }
        if (y2>=MySurfaceView.height){
            y2=y1-bitmap.getHeight();
        }
    }
}




import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

public class Myplane {
    private Bitmap bitmap;
    private int x,y;

    public Myplane(Bitmap bitmap){
        this.bitmap = bitmap;
        x = MySurfaceView.width/2-bitmap.getWidth()/2;
        y = MySurfaceView.height-bitmap.getHeight();
    }
    public void draw(Canvas canvas,Paint paint){
        canvas.drawBitmap(bitmap,x,y,paint);
    }
}
如何绘制子弹:

用run方法添加子弹,通过while语句和for循环绘制和移除子弹,if语句控制子弹速度。

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.util.Vector;

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback,Runnable {

    private SurfaceHolder surfaceHolder;
    private Canvas canvas;//绘制图形的画布
    private boolean isDrawing = true;//标志位
    public static int height;
    public static int width;
    private Myplane plane;
    private int count;
    //Vector是线程安全的,ArrayList是非线程安全的
    private Vector<MyBullet> mybulletVector = new Vector<>();
    private Vector<BossBullet> bossbulletVector = new Vector<>();

    public MySurfaceView(Context context) {
        super(context);
        init();
    }

    /*
    初始化操作
     */
    private void init() {
        surfaceHolder = getHolder();
        surfaceHolder.addCallback(this);//添加回调事件监听
        setFocusable(true);
        setKeepScreenOn(true);

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        new Thread(this).start();//启动子线程
        height = getHeight();//把getHeight(获取height的方法)的返回值赋给height
        width = getWidth();

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        isDrawing = false;

    }

    @Override
    public void run() {

        Paint paint = new Paint();
        BackGround backGround = new BackGround(BitmapFactory.decodeResource(getResources(), R.mipmap.bk));
        plane = new Myplane(BitmapFactory.decodeResource(getResources(), R.mipmap.myplane));
        BossPlane bossPlane = new BossPlane(BitmapFactory.decodeResource(getResources(),R.mipmap.bossplane));

        while (isDrawing) {
            count++;

            try {
                canvas = surfaceHolder.lockCanvas();//锁定(选定)画布
                canvas.drawColor(Color.WHITE);

                backGround.draw(canvas, paint);
                plane.draw(canvas, paint);
                bossPlane.draw(canvas,paint);

                if(count%20==0){
                    MyBullet myBullet = new MyBullet(BitmapFactory.decodeResource(getResources(),R.mipmap.mybullet),plane.getX(),plane.getY());
                    MyBullet myBullet1 = new MyBullet(BitmapFactory.decodeResource(getResources(),R.mipmap.mybullet),plane.getX()+plane.getWidth()*4/5,plane.getY());
                    BossBullet bossBullet = new BossBullet(BitmapFactory.decodeResource(getResources(),R.mipmap.bossbullet),bossPlane.getX()+bossPlane.getFrameW()/2,bossPlane.getY()+bossPlane.getFrameH());

                    mybulletVector.add(myBullet);
                    mybulletVector.add(myBullet1);
                    bossbulletVector.add(bossBullet);
                }

                for (int i=0;i<mybulletVector.size();i++) {
                    if (mybulletVector.elementAt(i).isDead()) {
                        mybulletVector.remove(i);
                    }
                }
                for (int i = 0; i < mybulletVector.size(); i++) {
                        mybulletVector.elementAt(i).draw(canvas, paint);
                }
                for (int i=0;i<bossbulletVector.size();i++) {
                    if (bossbulletVector.elementAt(i).isDead()) {
                        bossbulletVector.remove(i);
                    }
                }
                for (int i = 0; i < bossbulletVector.size(); i++) {
                    bossbulletVector.elementAt(i).draw(canvas, paint);
                }


            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (canvas != null) {
                    surfaceHolder.unlockCanvasAndPost(canvas);//解锁画布,显示到屏幕上
                }
            }

        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        plane.touchEvent(event);
        return true;//永远监听屏幕触摸事件
    }
}







import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
public class Myplane {
    private Bitmap bitmap;
    private int x,y;
    private int width,height;

    public Myplane(Bitmap bitmap){
        this.bitmap = bitmap;
        x = MySurfaceView.width/2-bitmap.getWidth()/2;
        y = MySurfaceView.height-bitmap.getHeight();
        width = bitmap.getWidth();
        height = bitmap.getHeight();
    }
    public void draw(Canvas canvas,Paint paint){
        canvas.drawBitmap(bitmap,x,y,paint);
    }
    public void touchEvent(MotionEvent event){
        if (event.getAction()==MotionEvent.ACTION_MOVE){
            float ex = (int) event.getX();
            float ey = (int) 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;
                }
            }
        }
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getWidth() {
        return width;
    }
}







import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

public class BossPlane {
    private Bitmap bitmap;
    private int x,y;
    private int frameW,frameH;
    private int speed=5;
    private int crazySpeed=50;
    private int count;//计数器
    private int time=300;//疯狂模式间隔
    private boolean isCrazy;

    public BossPlane(Bitmap bitmap) {
        this.bitmap = bitmap;
        this.frameW = bitmap.getWidth()/10;
        this.frameH = bitmap.getHeight();
        x=MySurfaceView.width/2-frameH/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-frameH){
                speed = -speed;
            }
            if (x<0){
                speed = -speed;
            }
        }
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getFrameW() {
        return frameW;
    }

    public int getFrameH() {
        return frameH;
    }
}





我的子弹

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

public class MyBullet {
    private Bitmap bitmap;
    private int x, y;
    private int speed = 10;
    private boolean isDead;

    public MyBullet(Bitmap bitmap, int x, int y) {
        this.bitmap = bitmap;
        this.x = x;
        this.y = y;

    }

    public void draw(Canvas canvas, Paint paint) {
        canvas.drawBitmap(bitmap, x, y, paint);
        logic();
    }

    public void logic() {

        y -= speed;
        if (y < 0) {
            isDead = true;
        }
    }

    public boolean isDead() {
        return isDead;
    }
}






Boos的子弹

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

public class BossBullet {
    private Bitmap bitmap;
    private int x, y;
    private int speed = 20;
    private boolean isDead;

    public BossBullet(Bitmap bitmap, int x, int y) {
        this.bitmap = bitmap;
        this.x = x;
        this.y = y;

    }

    public void draw(Canvas canvas, Paint paint) {
        canvas.drawBitmap(bitmap, x, y, paint);
        logic();
    }

    public void logic() {

        y+= speed;
        if (y < 0) {
            isDead = true;
        }
    }

    public boolean isDead() {
        return isDead;
    }
}

如何判断碰撞:

用if和noCollision方法添加飞机碰撞——无敌状态和非无敌状态,
添加type,用switch语句分别写出玩家子弹和Boss子弹造成的碰撞

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.util.Vector;



public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback,Runnable {

    private SurfaceHolder surfaceHolder;
    private Canvas canvas;//绘制图形的画布
    private boolean isDrawing = true;//标志位
    public static int height;
    public static int width;
    private Myplane plane;
    private Vector<Bullet>bulletVector = new Vector <>();
    private Vector<Bullet>bossBulletVector = new Vector <>();
    private int count;

    public MySurfaceView(Context context) {
        super(context);
        init();
    }

    /*
    初始化操作
     */
    private void init() {
        surfaceHolder = getHolder();
        surfaceHolder.addCallback(this);//添加回调事件监听
        setFocusable(true);
        setKeepScreenOn(true);

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        new Thread(this).start();//启动子线程
        height = getHeight();//把getHeight(获取height的方法)的返回值赋给height
        width = getWidth();

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        isDrawing = false;

    }

    @Override
    public void run() {
        Paint paint = new Paint();
        BackGround backGround = new BackGround(BitmapFactory.decodeResource(getResources(),R. mipmap.bk));
        plane = new Myplane(BitmapFactory.decodeResource(getResources(),R. mipmap.myplane),BitmapFactory.decodeResource(getResources(),R. mipmap.myhp));
        BossPlane boosPlane = new BossPlane(BitmapFactory.decodeResource(getResources(),R. mipmap.bossplane));

        while (isDrawing) {
            count++;

            try {
                canvas = surfaceHolder.lockCanvas();//锁定(选定)画布
                canvas.drawColor(Color.WHITE);

                backGround.draw(canvas, paint);
                plane.draw(canvas, paint);
                boosPlane.draw(canvas,paint);


                if(count%20==0){
                    Bullet bullet = new Bullet(BitmapFactory.decodeResource(getResources(),R. mipmap.mybullet),plane.getX(),plane.getY(),0);
                    Bullet bullet1 = new Bullet(BitmapFactory.decodeResource(getResources(),R. mipmap.mybullet),plane.getX()+plane.getWidth()/5*4,plane.getY(),0);
                    bulletVector.add(bullet);
                    bulletVector.add(bullet1);
                }


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

                    if (bulletVector.elementAt(i).isDead()) {
                        bulletVector.remove(i);

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

                if(count%40==0){
                    Bullet bullet = new Bullet(BitmapFactory.decodeResource(getResources(),R. mipmap.bossbullet),boosPlane.getX()+boosPlane.getFrameW()/2,boosPlane.getY()+boosPlane.getFrameH(),1);
                    bossBulletVector.add(bullet);

                }

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

                    if (bossBulletVector.elementAt(i).isDead()) {
                        bossBulletVector.remove(i);

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

                    bossBulletVector.elementAt(i).draw(canvas,paint);
                    plane.isCollision(bossBulletVector.elementAt(i));
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (canvas != null) {
                    surfaceHolder.unlockCanvasAndPost(canvas);//解锁画布,显示到屏幕上
                }
            }

        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        plane.touchEvent(event);
        return true;//永远监听屏幕触摸事件
    }
}







import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

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+=10;
        y2+=10;
        if (y1>=MySurfaceView.height){
            y1=y2-bitmap.getHeight();//移动到第二张图片的顶部
        }
        if (y2>=MySurfaceView.height){
            y2=y1-bitmap.getHeight();
        }
    }
}






import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
public class Myplane {
    private Bitmap bitmap;
    private Bitmap bitmapHp;
    private int x,y;
    private int width,height;
    private boolean noCollision;
    private int noCollisionCount;

    private int hp = 3;

    public Myplane(Bitmap bitmap, Bitmap bitmapHp){
        this.bitmap = bitmap;
        this.bitmapHp = bitmapHp;
        x = MySurfaceView.width/2-bitmap.getWidth()/2;
        y = MySurfaceView.height-bitmap.getHeight();
        width = bitmap.getWidth();
        height = bitmap.getHeight();
    }
    public void draw(Canvas canvas,Paint paint){
        if (noCollision){
            noCollisionCount++;
            if (noCollisionCount%10==0){
                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 = (int) event.getX();
            float ey = (int) 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;
                }
            }
        }
    }

    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){
                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;
    }
}







import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

public class BossPlane {
    private Bitmap bitmap;
    private int x,y;
    private int frameW,frameH;
    private int speed=5;
    private int crazySpeed=50;
    private int count;//计数器
    private int time=300;//疯狂模式间隔
    private boolean isCrazy;

    public BossPlane(Bitmap bitmap) {
        this.bitmap = bitmap;
        this.frameW = bitmap.getWidth()/10;
        this.frameH = bitmap.getHeight();
        x=MySurfaceView.width/2-frameH/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-frameH){
                speed = -speed;
            }
            if (x<0){
                speed = -speed;
            }
        }
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getFrameW() {
        return frameW;
    }

    public int getFrameH() {
        return frameH;
    }
}







import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

public class Bullet {
    private Bitmap bitmap;
    private int x, y;
    private int speed = 10;
    private boolean isDead;
    private int type;

    public Bullet(Bitmap bitmap, int x, int y,int type) {
        this.bitmap = bitmap;
        this.x = x;
        this.y = y;
        this.type = type;

    }

    public void draw(Canvas canvas, Paint paint) {
        canvas.drawBitmap(bitmap, x, y, paint);
        logic();
    }

    public void logic() {

        switch (type){
            //玩家子弹
            case 0:
                y -= speed;
                if (y < 0) {
                    isDead = true;
                }
                break;
            //Boss子弹
            case 1:
                y += speed+5;
                if (y < 0) {
                    isDead = true;
                }
                break;
            default:

                break;
        }
    }

    public boolean isDead() {
        return isDead;
    }


    public int getX(){
        return x;
    }

    public int getY() {
        return y;
    }
}




如何绘制爆炸效果:

创建Boom类,插入爆炸图片,用clipRect剪切图片。

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.util.Vector;



public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback,Runnable {

    private SurfaceHolder surfaceHolder;
    private Canvas canvas;//绘制图形的画布
    private boolean isDrawing = true;//标志位
    public static int height;
    public static int width;
    private Myplane plane;
    private Vector<Bullet>bulletVector = new Vector <>();
    private Vector<Bullet>bossBulletVector = new Vector <>();
    private Vector<Boom>boomVector = new Vector <>();
    private int count;



    public MySurfaceView(Context context) {
        super(context);
        init();
    }

    /*
    初始化操作
     */
    private void init() {
        surfaceHolder = getHolder();
        surfaceHolder.addCallback(this);//添加回调事件监听
        setFocusable(true);
        setKeepScreenOn(true);

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        new Thread(this).start();//启动子线程
        height = getHeight();//把getHeight(获取height的方法)的返回值赋给height
        width = getWidth();

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        isDrawing = false;

    }

    @Override
    public void run() {
        Paint paint = new Paint();
        BackGround backGround = new BackGround(BitmapFactory.decodeResource(getResources(),R. mipmap.bk));
        plane = new Myplane(BitmapFactory.decodeResource(getResources(),R. mipmap.myplane),BitmapFactory.decodeResource(getResources(),R. mipmap.myhp));
        BossPlane bossPlane = new BossPlane(BitmapFactory.decodeResource(getResources(),R. mipmap.bossplane));

        while (isDrawing) {
            count++;

            try {
                canvas = surfaceHolder.lockCanvas();//锁定(选定)画布
                canvas.drawColor(Color.WHITE);

                backGround.draw(canvas, paint);
                plane.draw(canvas, paint);
               
                bossPlane.draw(canvas,paint);


                if(count%20==0){
                    Bullet bullet = new Bullet(BitmapFactory.decodeResource(getResources(),R. mipmap.mybullet),plane.getX(),plane.getY(),0);
                    Bullet bullet1 = new Bullet(BitmapFactory.decodeResource(getResources(),R. mipmap.mybullet),plane.getX()+plane.getWidth()/5*4,plane.getY(),0);
                    bulletVector.add(bullet);
                    bulletVector.add(bullet1);
                }

                //移除玩家子弹
                for (int i = 0;i<bulletVector.size();i++) {

                    if (bulletVector.elementAt(i).isDead()) {
                        bulletVector.remove(i);

                    }
                }
                //绘制玩家子弹
                for (int i = 0;i<bulletVector.size();i++){
                    bulletVector.elementAt(i).draw(canvas,paint);
                    if (bossPlane.isCollision(bulletVector.elementAt(i))){
                        Boom boom = new Boom(BitmapFactory.decodeResource(getResources(),R.mipmap.boom),bossPlane.getX()+bossPlane.getFrameW()/4,bossPlane.getY(),7);
                        boomVector.add(boom);
                    }
                }
                for (int i=0;i<boomVector.size();i++){
                    if (boomVector.elementAt(i).isEnd()){
                        boomVector.remove(i);
                    }else {
                        boomVector.elementAt(i).draw(canvas,paint);
                    }
                }

                if(count%40==0){
                    Bullet bullet = new Bullet(BitmapFactory.decodeResource(getResources(),R. mipmap.bossbullet),bossPlane.getX()+bossPlane.getFrameW()/2,bossPlane.getY()+bossPlane.getFrameH(),1);
                    bossBulletVector.add(bullet);

                }
                //移除Boss子弹
                for (int i = 0;i < bossBulletVector.size(); i++) {

                    if (bossBulletVector.elementAt(i).isDead()) {
                        bossBulletVector.remove(i);

                    }
                }
                //绘制Boss子弹
                for (int i = 0;i < bossBulletVector.size(); i++){

                    bossBulletVector.elementAt(i).draw(canvas,paint);
                    plane.isCollision(bossBulletVector.elementAt(i));
                }
                plane.isCollision(bossPlane);


            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (canvas != null) {
                    surfaceHolder.unlockCanvasAndPost(canvas);//解锁画布,显示到屏幕上
                }
            }

        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        plane.touchEvent(event);
        return true;//永远监听屏幕触摸事件
    }
}






import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

public class Boom {
    private Bitmap bitmap;
    private int x,y;
    private int totalFrame;
    private int currentFrame;//当前显示的第几幅画面
    private int frameH,frameW;
    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;
        frameH = bitmap.getHeight();
        frameW = bitmap.getWidth()/totalFrame;
    }
    public void draw(Canvas canvas, Paint paint){
        canvas.save();
        canvas.clipRect(x,y,x+frameW,y+frameH);
        canvas.drawBitmap(bitmap,x-currentFrame*frameW,y,paint);
        canvas.restore();
        logic();
    }
    public void logic(){
        if (currentFrame<totalFrame){
            currentFrame++;
        }else {
            isEnd = true;
        }
    }

    public boolean isEnd() {
        return isEnd;
    }
}






import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

public class BossPlane {
    private Bitmap bitmap;
    private int x,y;
    private int frameW,frameH;
    private int speed=5;
    private int crazySpeed=50;
    private int count;//计数器
    private int time=300;//疯狂模式间隔
    private boolean isCrazy;
    private int bossHp=30;

    public BossPlane(Bitmap bitmap) {
        this.bitmap = bitmap;
        this.frameW = bitmap.getWidth()/10;
        this.frameH = bitmap.getHeight();
        x=MySurfaceView.width/2-frameH/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-frameH){
                speed = -speed;
            }
            if (x<0){
                speed = -speed;
            }
        }
    }
    public boolean isCollision(Bullet bullet){
        if (bullet.getX()>x&&bullet.getX()+bullet.getBitmap().getWidth()<x+frameW&&bullet.getY()>y&&bullet.getY()<y+frameH){
            bossHp--;
            bullet.setDead(true);//设置子弹状态,碰撞后修改子弹状态为dead,从数组中移除
            return true;
        }
        return false;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getFrameW() {
        return frameW;
    }

    public int getFrameH() {
        return frameH;
    }
}






import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

public class Bullet {
    private Bitmap bitmap;
    private int x, y;
    private int speed = 10;
    private boolean isDead;
    private int type;

    public Bullet(Bitmap bitmap, int x, int y,int type) {
        this.bitmap = bitmap;
        this.x = x;
        this.y = y;
        this.type = type;

    }

    public void draw(Canvas canvas, Paint paint) {
        canvas.drawBitmap(bitmap, x, y, paint);
        logic();
    }

    public void logic() {

        switch (type){
            //玩家子弹
            case 0:
                y -= speed;
                if (y < 0) {
                    isDead = true;
                }
                break;
            //Boss子弹
            case 1:
                y += speed+5;
                if (y < 0) {
                    isDead = true;
                }
                break;
            default:

                break;
        }
    }

    public boolean isDead() {
        return isDead;
    }


    public int getX(){
        return x;
    }

    public int getY() {
        return y;
    }

    public void setDead(boolean dead) {
        isDead = dead;
    }

    public Bitmap getBitmap() {
        return bitmap;
    }
}





import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

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+=10;
        y2+=10;
        if (y1>=MySurfaceView.height){
            y1=y2-bitmap.getHeight();//移动到第二张图片的顶部
        }
        if (y2>=MySurfaceView.height){
            y2=y1-bitmap.getHeight();
        }
    }
}






import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
public class Myplane {
    private Bitmap bitmap;
    private Bitmap bitmapHp;
    private int x, y;
    private int width, height;
    private boolean noCollision;
    private int noCollisionCount;

    private int hp = 3;

    public Myplane(Bitmap bitmap, Bitmap bitmapHp) {
        this.bitmap = bitmap;
        this.bitmapHp = bitmapHp;
        x = MySurfaceView.width / 2 - bitmap.getWidth() / 2;
        y = MySurfaceView.height - bitmap.getHeight();
        width = bitmap.getWidth();
        height = bitmap.getHeight();
    }

    public void draw(Canvas canvas, Paint paint) {
        if (noCollision) {
            noCollisionCount++;
            if (noCollisionCount % 10 == 0) {
                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 = (int) event.getX();
            float ey = (int) 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;
                }
            }
        }
    }

    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) {
                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()) {
                    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<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;
    }
}

如何添加音效;

创建GrameRoundPool类,用switch语句和soundPool.load方法进行添加

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.util.Vector;



public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback,Runnable {

    private SurfaceHolder surfaceHolder;
    private Canvas canvas;//绘制图形的画布
    private boolean isDrawing = true;//标志位
    public static int height;
    public static int width;
    private MyPlane plane;
    private Vector<Bullet>bulletVector = new Vector <>();
    private Vector<Bullet>bossBulletVector = new Vector <>();
    private Vector<Boom>boomVector = new Vector <>();
    private int count;
    public GameSoundPool gameSoundPool;



    public MySurfaceView(Context context) {
        super(context);
        gameSoundPool = new GameSoundPool(context);
        init();
    }

    /*
    初始化操作
     */
    private void init() {
        surfaceHolder = getHolder();
        surfaceHolder.addCallback(this);//添加回调事件监听
        setFocusable(true);
        setKeepScreenOn(true);

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        new Thread(this).start();//启动子线程
        height = getHeight();//把getHeight(获取height的方法)的返回值赋给height
        width = getWidth();

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        isDrawing = false;

    }

    @Override
    public void run() {
        Paint paint = new Paint();
        BackGround backGround = new BackGround(BitmapFactory.decodeResource(getResources(),R. mipmap.bk));
        plane = new Myplane(BitmapFactory.decodeResource(getResources(),R. mipmap.myplane),BitmapFactory.decodeResource(getResources(),R. mipmap.myhp));
        BossPlane bossPlane = new BossPlane(BitmapFactory.decodeResource(getResources(),R. mipmap.bossplane));

        while (isDrawing) {
            count++;

            try {
                canvas = surfaceHolder.lockCanvas();//锁定(选定)画布
                canvas.drawColor(Color.WHITE);

                backGround.draw(canvas, paint);
                plane.draw(canvas, paint);

                bossPlane.draw(canvas,paint);


                if(count%20==0){
                    gameSoundPool.playSound(1);
                    Bullet bullet = new Bullet(BitmapFactory.decodeResource(getResources(),R. mipmap.mybullet),plane.getX(),plane.getY(),0);
                    Bullet bullet1 = new Bullet(BitmapFactory.decodeResource(getResources(),R. mipmap.mybullet),plane.getX()+plane.getWidth()/5*4,plane.getY(),0);
                    bulletVector.add(bullet);
                    bulletVector.add(bullet1);
                }

                //移除玩家子弹
                for (int i = 0;i<bulletVector.size();i++) {

                    if (bulletVector.elementAt(i).isDead()) {
                        bulletVector.remove(i);

                    }
                }
                //绘制玩家子弹
                for (int i = 0;i<bulletVector.size();i++){

                    bulletVector.elementAt(i).draw(canvas,paint);
                    if (bossPlane.isCollision(bulletVector.elementAt(i))){
                        gameSoundPool.playSound(2);
                        Boom boom = new Boom(BitmapFactory.decodeResource(getResources(),R.mipmap.boom),bossPlane.getX()+bossPlane.getFrameW()/4,bossPlane.getY(),7);
                        boomVector.add(boom);
                    }
                }
                for (int i=0;i<boomVector.size();i++){
                    if (boomVector.elementAt(i).isEnd()){
                        boomVector.remove(i);
                    }else {
                        boomVector.elementAt(i).draw(canvas,paint);
                    }
                }

                if(count%40==0){
                    Bullet bullet = new Bullet(BitmapFactory.decodeResource(getResources(),R. mipmap.bossbullet),bossPlane.getX()+bossPlane.getFrameW()/2,bossPlane.getY()+bossPlane.getFrameH(),1);
                    bossBulletVector.add(bullet);

                }
                //移除Boss子弹
                for (int i = 0;i < bossBulletVector.size(); i++) {

                    if (bossBulletVector.elementAt(i).isDead()) {
                        bossBulletVector.remove(i);

                    }
                }
                //绘制Boss子弹
                for (int i = 0;i < bossBulletVector.size(); i++){

                    bossBulletVector.elementAt(i).draw(canvas,paint);
                    plane.isCollision(bossBulletVector.elementAt(i));
                }
                plane.isCollision(bossPlane);


            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (canvas != null) {
                    surfaceHolder.unlockCanvasAndPost(canvas);//解锁画布,显示到屏幕上
                }
            }

        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        plane.touchEvent(event);
        return true;//永远监听屏幕触摸事件
    }
}





import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

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,1,0,1.0f);
                break;
            case 2:
                soundPool.play(s2,1,1,1,0,1.0f);
                break;
        }


    }
}




import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

public class Boom {
    private Bitmap bitmap;
    private int x,y;
    private int totalFrame;
    private int currentFrame;//当前显示的第几幅画面
    private int frameH,frameW;
    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;
        frameH = bitmap.getHeight();
        frameW = bitmap.getWidth()/totalFrame;
    }
    public void draw(Canvas canvas, Paint paint){
        canvas.save();
        canvas.clipRect(x,y,x+frameW,y+frameH);
        canvas.drawBitmap(bitmap,x-currentFrame*frameW,y,paint);
        canvas.restore();
        logic();
    }
    public void logic(){
        if (currentFrame<totalFrame){
            currentFrame++;
        }else {
            isEnd = true;
        }
    }

    public boolean isEnd() {
        return isEnd;
    }
}






import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

public class BossPlane {
    private Bitmap bitmap;
    private int x,y;
    private int frameW,frameH;
    private int speed=5;
    private int crazySpeed=50;
    private int count;//计数器
    private int time=300;//疯狂模式间隔
    private boolean isCrazy;
    private int bossHp=30;

    public BossPlane(Bitmap bitmap) {
        this.bitmap = bitmap;
        this.frameW = bitmap.getWidth()/10;
        this.frameH = bitmap.getHeight();
        x=MySurfaceView.width/2-frameH/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-frameH){
                speed = -speed;
            }
            if (x<0){
                speed = -speed;
            }
        }
    }
    public boolean isCollision(Bullet bullet){
        if (bullet.getX()>x&&bullet.getX()+bullet.getBitmap().getWidth()<x+frameW&&bullet.getY()>y&&bullet.getY()<y+frameH){
            bossHp--;
            bullet.setDead(true);//设置子弹状态,碰撞后修改子弹状态为dead,从数组中移除
            return true;
        }
        return false;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getFrameW() {
        return frameW;
    }

    public int getFrameH() {
        return frameH;
    }
}






import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

public class Bullet {
    private Bitmap bitmap;
    private int x, y;
    private int speed = 10;
    private boolean isDead;
    private int type;

    public Bullet(Bitmap bitmap, int x, int y,int type) {
        this.bitmap = bitmap;
        this.x = x;
        this.y = y;
        this.type = type;

    }

    public void draw(Canvas canvas, Paint paint) {
        canvas.drawBitmap(bitmap, x, y, paint);
        logic();
    }

    public void logic() {

        switch (type){
            //玩家子弹
            case 0:
                y -= speed;
                if (y < 0) {
                    isDead = true;
                }
                break;
            //Boss子弹
            case 1:
                y += speed+5;
                if (y < 0) {
                    isDead = true;
                }
                break;
            default:

                break;
        }
    }

    public boolean isDead() {
        return isDead;
    }


    public int getX(){
        return x;
    }

    public int getY() {
        return y;
    }

    public void setDead(boolean dead) {
        isDead = dead;
    }

    public Bitmap getBitmap() {
        return bitmap;
    }
}






import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

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+=10;
        y2+=10;
        if (y1>=MySurfaceView.height){
            y1=y2-bitmap.getHeight();//移动到第二张图片的顶部
        }
        if (y2>=MySurfaceView.height){
            y2=y1-bitmap.getHeight();
        }
    }
}






import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
public class Myplane {
    private Bitmap bitmap;
    private Bitmap bitmapHp;
    private int x, y;
    private int width, height;
    private boolean noCollision;
    private int noCollisionCount;

    private int hp = 3;

    public Myplane(Bitmap bitmap, Bitmap bitmapHp) {
        this.bitmap = bitmap;
        this.bitmapHp = bitmapHp;
        x = MySurfaceView.width / 2 - bitmap.getWidth() / 2;
        y = MySurfaceView.height - bitmap.getHeight();
        width = bitmap.getWidth();
        height = bitmap.getHeight();
    }

    public void draw(Canvas canvas, Paint paint) {
        if (noCollision) {
            noCollisionCount++;
            if (noCollisionCount % 10 == 0) {
                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 = (int) event.getX();
            float ey = (int) 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;
                }
            }
        }
    }

    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) {
                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()) {
                    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<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;
    }
}

我的收获和感悟:

经过一个月的实训课,我做出了我人生的第一个游戏,从中没少老师和同学的帮助,能做出来我感到很高兴,但从中也发现了很多不足之处,在今后我还需更加努力,争取创作更多优越的项目,勇于创新。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值