Android_(游戏)打飞机05:处理子弹,击中敌机,添加计分板

(游戏)打飞机01:前言  传送门

(游戏)打飞机02:游戏背景滚动  传送门

(游戏)打飞机03:控制玩家飞机   传送门

(游戏)打飞机04:绘画敌机、添加子弹    传送门

(游戏)打飞机05:处理子弹,击中敌机,添加计分板   传送门

处理子弹,击中敌机,添加计分板效果

package com.example.administrator.myapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by Administrator on 2018/8/9.
 */

public class dafeijiGameView extends SurfaceView implements
        SurfaceHolder.Callback,Runnable,android.view.View.OnTouchListener{

    private Bitmap my;//自己
    private Bitmap baozha;//爆炸
    private Bitmap bg;//背景
    private Bitmap diren;//敌人
    private Bitmap zidan;//子弹
    private Bitmap erjihuancun;//二级缓存
    private WindowManager windowManager;//获得界面长宽高
    private int display_w;  //界面的宽
    private int display_h;  //界面的高
    private ArrayList<GameImage> gameImage = new ArrayList();
    private ArrayList<Zidan> zidans = new ArrayList<Zidan>();

    public dafeijiGameView(Context context) {
        super(context);
        getHolder().addCallback(this);
        //事件注册
        this.setOnTouchListener(this);
    }

    private void init(){
        //加载照片
        my= BitmapFactory.decodeResource(getResources(),R.drawable.my);
        baozha= BitmapFactory.decodeResource(getResources(),R.drawable.baozha);
        bg= BitmapFactory.decodeResource(getResources(),R.drawable.bg);
        diren= BitmapFactory.decodeResource(getResources(),R.drawable.diren);
        zidan= BitmapFactory.decodeResource(getResources(),R.drawable.zidan);

        erjihuancun=Bitmap.createBitmap(display_w,display_h, Bitmap.Config.ARGB_8888);
        gameImage.add(new BeijingImage(bg));    //先加入背景照片
        gameImage.add(new FeijiImage(my));
        gameImage.add(new DijiImage(diren,baozha));

    }

    FeijiImage selectfeiji;
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        //手接近屏幕产生的事件
        if(event.getAction()==MotionEvent.ACTION_DOWN){
            for(GameImage game: gameImage){
                if(game instanceof FeijiImage){

                    FeijiImage feiji = (FeijiImage)game;

                    //判断条件为真时选中飞机
                    if(feiji.getX()<event.getX()&&
                            feiji.getY()<event.getY()&&
                            feiji.getX()+feiji.getWidth()>event.getX() &&
                            feiji.getY()+feiji.getHeigth()>event.getY()){
                        selectfeiji=feiji;

                    }else{
                        selectfeiji=null;
                    }
                    break;
                }
            }
        }else if(event.getAction()==MotionEvent.ACTION_MOVE){
            //移动鼠标
            if( selectfeiji!=null){
                selectfeiji.setX((int)event.getX()-selectfeiji.getWidth()/2);
                selectfeiji.setY((int)event.getY()-selectfeiji.getHeigth()/2);
            }

        }else if(event.getAction()==MotionEvent.ACTION_UP){
            //松开鼠标
            selectfeiji=null;
        }

        return true;
    }

    private interface GameImage{
        public Bitmap getBitmap();
        public int getX();
        public int getY();

    }

    private class Zidan implements GameImage{

        Bitmap zidan;
        private FeijiImage feiji;
        private int x;
        private int y;

        public Zidan(FeijiImage feiji,Bitmap zidan){
            this.feiji=feiji;
            this.zidan=zidan;

            x=(feiji.getX()+feiji.getWidth()/2)-25;//居中位置
            y=feiji.getY()-zidan.getHeight();
        }

        public Bitmap getBitmap() {
            y-=19;
            if(y<=-10){
                zidans.remove(this);
            }
            return zidan;
        }

        @Override
        public int getX() {
            return x;
        }

        @Override
        public int getY() {
            return y;
        }
    }

    private class DijiImage implements GameImage{

        private Bitmap diren = null;

        private List<Bitmap> bitmaps = new ArrayList<Bitmap>();
        private List<Bitmap> baozhas = new ArrayList<Bitmap>();
        private int x;
        private int y;
        private int width;
        private int height;

        public DijiImage(Bitmap diren,Bitmap baozha){
            this.diren=diren;
            bitmaps.add(Bitmap.createBitmap(diren,0,0,diren.getWidth()/4,diren.getHeight()));
            bitmaps.add(Bitmap.createBitmap(diren,(diren.getWidth()/4)*1,0,diren.getWidth()/4,diren.getHeight()));
            bitmaps.add(Bitmap.createBitmap(diren,(diren.getWidth()/4)*2,0,diren.getWidth()/4,diren.getHeight()));
            bitmaps.add(Bitmap.createBitmap(diren,(diren.getWidth()/4)*3,0,diren.getWidth()/4,diren.getHeight()));

            baozhas.add(Bitmap.createBitmap(baozha,0,0,baozha.getWidth()/4,baozha.getHeight()/2));
            baozhas.add(Bitmap.createBitmap(baozha,(baozha.getWidth()/4)*1,0,baozha.getWidth()/4,baozha.getHeight()/2));
            baozhas.add(Bitmap.createBitmap(baozha,(baozha.getWidth()/4)*2,0,baozha.getWidth()/4,baozha.getHeight()/2));
            baozhas.add(Bitmap.createBitmap(baozha,(baozha.getWidth()/4)*3,0,baozha.getWidth()/4,baozha.getHeight()/2));

            baozhas.add(Bitmap.createBitmap(baozha,0,baozha.getHeight()/2,
                    baozha.getWidth()/4,baozha.getHeight()/2));
            baozhas.add(Bitmap.createBitmap(baozha,(baozha.getWidth()/4)*1,baozha.getHeight()/2,
                    baozha.getWidth()/4,baozha.getHeight()/2));
            baozhas.add(Bitmap.createBitmap(baozha,(baozha.getWidth()/4)*2,baozha.getHeight()/2,
                    baozha.getWidth()/4,baozha.getHeight()/2));
            baozhas.add(Bitmap.createBitmap(baozha,(baozha.getWidth()/4)*3,baozha.getHeight()/2,
                    baozha.getWidth()/4,baozha.getHeight()/2));

            width=diren.getWidth()/4;
            height=diren.getHeight();

            y=-diren.getHeight();
            Random ran = new Random();
            x=ran.nextInt(display_w-(diren.getWidth()/4));

        }

        private int index=0;
        private int num =0;
        @Override
        public Bitmap getBitmap() {
            Bitmap bitmap = bitmaps.get(index);
            //控制切换四个飞机的频率
            if(num == 7){
                index++;
                //当爆炸动画运行一次时候删除爆炸效果
                if(index==8 &&state){
                    gameImage.remove(this);
                }

                if(index == bitmaps.size()){
                    index=0;
                }
                num=0;
            }
            y+=dijiyidong;
            num++;

            if(y>display_h){
                gameImage.remove(this);
            }

            return bitmap;
        }

        //判断敌机是否被击中
        private boolean state=false;
        //受到攻击
        public void shoudaogongji(ArrayList<Zidan> zidans){

            if(!state){
            for(GameImage zidan:(List<GameImage>)zidans.clone()){

               if(zidan.getX()>x&&zidan.getY()>y
                       &&zidan.getX()<x+width
                       &&zidan.getY()<y+height){
                        //子弹击中敌机了
                   zidans.remove(zidan);
                   state=true;
                   bitmaps=baozhas;
                   fenshu+=10;
                   break;
                  }
                }
            }
        }

        @Override
        public int getX() {
            return x;
        }

        @Override
        public int getY() {
            return y;
        }

    }

    private class FeijiImage implements GameImage{

        private Bitmap my;
        private int x;
        private int y;
        private int width;
        private int heigth;

        private int getWidth(){
            return width;
        }

        private int getHeigth(){
            return heigth;
        }

        private List<Bitmap> bitmaps = new ArrayList<Bitmap>();

        private  FeijiImage(Bitmap my){
            this.my=my;
            bitmaps.add(Bitmap.createBitmap(my,0,0,my.getWidth()/4,my.getHeight()));
            bitmaps.add(Bitmap.createBitmap(my,(my.getWidth()/4),0,my.getWidth()/4,my.getHeight()));
            bitmaps.add(Bitmap.createBitmap(my,(my.getWidth()/4)*2,0,my.getWidth()/4,my.getHeight()));
            bitmaps.add(Bitmap.createBitmap(my,(my.getWidth()/4)*3,0,my.getWidth()/4,my.getHeight()));
            //得到战机的高和宽
            width=my.getWidth()/4;
            heigth=my.getHeight();
            x=(display_w - my.getWidth()/4)/2;
            y=display_h-my.getHeight()-30;
        }

        private int index=0;
        private int num =0;
        @Override
        public Bitmap getBitmap() {
            Bitmap bitmap = bitmaps.get(index);
            //控制切换四个飞机的频率
            if(num == 7){
                index++;
                if(index == bitmaps.size()){
                    index=0;
                }
                num=0;
            }
            num++;
            return bitmap;
        }

        @Override
        public int getX() {
            return x;
        }

        @Override
        public int getY() {
            return y;
        }

        public void setY(int y){
            this.y=y;
        }

        public void setX(int x){
            this.x=x;
        }

    }

    //负责背景照片的处理
    private class BeijingImage implements GameImage{
        private Bitmap bg;
        private BeijingImage(Bitmap bg){
            this.bg=bg;
            newBitmap = Bitmap.createBitmap(display_w,display_h, Bitmap.Config.ARGB_8888);
        }

        private Bitmap newBitmap = null;
        private int height = 0;

        public Bitmap getBitmap(){
            Paint p = new Paint();
            Canvas canvas = new Canvas(newBitmap);
            canvas.drawBitmap(bg,
                    new Rect(0,0,bg.getWidth(),bg.getHeight()),
                    new Rect(0,height,display_w,display_h+height),p);

            canvas.drawBitmap(bg,
                    new Rect(0,0,bg.getWidth(),bg.getHeight()),
                    new Rect(0,-display_h+height,display_w,height),p);

            height++;
            if(height==display_h){
                height=0;
            }
            return newBitmap;
        }

        public int getX(){
            return 0;
        }

        public int getY(){
            return 0;
        }
    }

    private boolean state = false;
    private SurfaceHolder holder;
    private long fenshu=0;
    private int guanka=1;

    private int chudishu=50;        //出敌机的数字
    private int dijiyidong=5;       //敌机的移动
    private int xiayiguan=50;       //下一关分数

    private int[][] sj={
            {1,50,50,4},
            {2,100,45,4},
            {3,150,40,5},
            {4,200,35,5},
            {5,300,30,6},
            {6,400,30,6},
            {7,500,25,7},
            {8,650,25,7},
            {9,900,20,8},
            {10,1200,20,9}};

    private boolean stopState=false;
    public void stop(){
        stopState=true;
    }

    public void start(){
        stopState=false;
        thread.interrupt(); //叫醒线程
    }

    //绘画中心
    public void run() {
        Paint p1 = new Paint();
        int diren_num=0;//
        int zidan_num=0;
        //绘画分数的笔
        Paint p2=new Paint();
        p2.setColor(Color.WHITE);
        p2.setTextSize(30);
        p2.setDither(true);
        p2.setAntiAlias(true);

        try{
            while(state){
                    while(stopState){
                        try{
                            Thread.sleep(1000000);
                        }catch(Exception e){
                        }
                    }
                if(selectfeiji!=null){
                    if(zidan_num==10){
                        zidans.add(new Zidan(selectfeiji,zidan));
                        zidan_num=0;
                    }
                    zidan_num++;
                }

                Canvas newCanvas = new Canvas(erjihuancun);
                for(GameImage image:(List<GameImage>)gameImage.clone()){
                    if(image instanceof DijiImage){
                        //把子弹告诉敌机
                        ((DijiImage)image).shoudaogongji(zidans);
                    }
                    newCanvas.drawBitmap(image.getBitmap(),image.getX(),image.getY(),p1);
                }

                for(GameImage image:(List<GameImage>)zidans.clone()){
                    newCanvas.drawBitmap(image.getBitmap(),image.getX(),image.getY(),p1);
                }

                //分数
                newCanvas.drawText("分数:"+fenshu,0,30,p2);
                newCanvas.drawText("关卡:"+guanka,0,60,p2);
                newCanvas.drawText("下一关:"+xiayiguan,0,90,p2);

                //升级关卡
                if(sj[guanka-1][1]<=fenshu){

                    chudishu=sj[guanka][2]; //出敌机的数字
                    dijiyidong=sj[guanka][3];
                    fenshu=sj[guanka-1][1]-fenshu;
                    xiayiguan=sj[guanka][1];
                    guanka=sj[guanka][0];
                }

                if(diren_num==150){
                    diren_num=0;
                    gameImage.add(new DijiImage(diren,baozha));
                }
                diren_num++;
                Canvas canvas = holder.lockCanvas();
                canvas.drawBitmap(erjihuancun,0,0,p1);
                holder.unlockCanvasAndPost(canvas);
                Thread.sleep(10);
            }
        }catch(Exception e){
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
    }

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

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        //得到屏幕的宽和高
        display_w=width;
        display_h=height;
        init();
        this.holder=holder;
        state = true;
        thread=new Thread(this);
        thread.start();
    }

    Thread  thread=null;
}

dafeijiGameView.java

package com.example.administrator.myapplication;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;

public class MainActivity extends AppCompatActivity implements DialogInterface.OnClickListener{

    dafeijiGameView view  = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去掉标题
       // requestWindowFeature(Window.FEATURE_NO_TITLE);
          dafeijiGameView view =  new dafeijiGameView(this);
        view = new dafeijiGameView(this);
        setContentView(view);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if(keyCode==KeyEvent.KEYCODE_BACK){
            view.stop();

            AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle("确定是否退出");
            alert.setNeutralButton("退出",this);
            alert.setNegativeButton("继续",this);
            alert.create().show();

            return false;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        if(which==-2){
            view.start();;
        }else{
            android.os.Process.killProcess(android.os.Process.myPid());
        }
    }
}

MainActivity.java

实现过程

处理子弹

当子弹击中敌机时,移除当前子弹

  把子弹告诉敌机

               for(GameImage image:(List<GameImage>)gameImage.clone()){
                    if(image instanceof DijiImage){

                        ((DijiImage)image).shoudaogongji(zidans);
                    }
                    newCanvas.drawBitmap(image.getBitmap(),image.getX(),image.getY(),p1);
                }

当子弹击中敌机时,移除当前子弹

  在制作过程中发现判断子弹击中敌机时,如果不移除子弹。会在子弹进入敌机身体的时候,继续判断子弹击中敌机(造成一颗子弹击中三次敌机)

for(GameImage game: gameImage){
                if(game instanceof FeijiImage){

                    FeijiImage feiji = (FeijiImage)game;

                    //判断条件为真时选中飞机
                    if(feiji.getX()<event.getX()&&
                            feiji.getY()<event.getY()&&
                            feiji.getX()+feiji.getWidth()>event.getX() &&
                            feiji.getY()+feiji.getHeigth()>event.getY()){
                        selectfeiji=feiji;

                    }else{
                        selectfeiji=null;
                    }
                    break;
                }
            }
 public void shoudaogongji(ArrayList<Zidan> zidans){

            for(GameImage zidan:(List<GameImage>)zidans.clone()){

                if(zidan.getX()>x&&zidan.getY()>y
                        &&zidan.getX()<x+width
                        &&zidan.getY()<y+height){
                    //子弹击中敌机了
                    zidans.remove(zidan);
                    break;
                }

            }

        }

package com.example.administrator.myapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by Administrator on 2018/8/9.
 */

public class dafeijiGameView extends SurfaceView implements
        SurfaceHolder.Callback,Runnable,android.view.View.OnTouchListener{

    private Bitmap my;//自己
    private Bitmap baozha;//爆炸
    private Bitmap bg;//背景
    private Bitmap diren;//敌人
    private Bitmap zidan;//子弹
    private Bitmap erjihuancun;//二级缓存
    private WindowManager windowManager;//获得界面长宽高
    private int display_w;  //界面的宽
    private int display_h;  //界面的高
    private ArrayList<GameImage> gameImage = new ArrayList();
    private ArrayList<Zidan> zidans = new ArrayList<Zidan>();

    public dafeijiGameView(Context context) {
        super(context);
        getHolder().addCallback(this);
        //事件注册
        this.setOnTouchListener(this);
    }

    private void init(){
        //加载照片
        my= BitmapFactory.decodeResource(getResources(),R.drawable.my);
        baozha= BitmapFactory.decodeResource(getResources(),R.drawable.baozha);
        bg= BitmapFactory.decodeResource(getResources(),R.drawable.bg);
        diren= BitmapFactory.decodeResource(getResources(),R.drawable.diren);
        zidan= BitmapFactory.decodeResource(getResources(),R.drawable.zidan);

        erjihuancun=Bitmap.createBitmap(display_w,display_h, Bitmap.Config.ARGB_8888);
        gameImage.add(new BeijingImage(bg));    //先加入背景照片
        gameImage.add(new FeijiImage(my));
        gameImage.add(new DijiImage(diren));

    }

    FeijiImage selectfeiji;
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        //手接近屏幕产生的事件
        if(event.getAction()==MotionEvent.ACTION_DOWN){
            for(GameImage game: gameImage){
                if(game instanceof FeijiImage){

                    FeijiImage feiji = (FeijiImage)game;

                    //判断条件为真时选中飞机
                    if(feiji.getX()<event.getX()&&
                            feiji.getY()<event.getY()&&
                            feiji.getX()+feiji.getWidth()>event.getX() &&
                            feiji.getY()+feiji.getHeigth()>event.getY()){
                        selectfeiji=feiji;

                    }else{
                        selectfeiji=null;
                    }
                    break;
                }
            }
        }else if(event.getAction()==MotionEvent.ACTION_MOVE){
            //移动鼠标
            if( selectfeiji!=null){
                selectfeiji.setX((int)event.getX()-selectfeiji.getWidth()/2);
                selectfeiji.setY((int)event.getY()-selectfeiji.getHeigth()/2);
            }

        }else if(event.getAction()==MotionEvent.ACTION_UP){
            //松开鼠标
            selectfeiji=null;
        }

        return true;
    }

    private interface GameImage{
        public Bitmap getBitmap();
        public int getX();
        public int getY();

    }

    private class Zidan implements GameImage{

        Bitmap zidan;
        private FeijiImage feiji;
        private int x;
        private int y;

        public Zidan(FeijiImage feiji,Bitmap zidan){
            this.feiji=feiji;
            this.zidan=zidan;

            x=(feiji.getX()+feiji.getWidth()/2)-25;//居中位置
            y=feiji.getY()-zidan.getHeight();
        }

        public Bitmap getBitmap() {
            y-=19;
            if(y<=-10){
                zidans.remove(this);
            }
            return zidan;
        }

        @Override
        public int getX() {
            return x;
        }

        @Override
        public int getY() {
            return y;
        }
    }

    private class DijiImage implements GameImage{

        private Bitmap diren = null;

        private List<Bitmap> bitmaps = new ArrayList<Bitmap>();
        private int x;
        private int y;
        private int width;
        private int height;

        public DijiImage(Bitmap diren){
            this.diren=diren;
            bitmaps.add(Bitmap.createBitmap(diren,0,0,diren.getWidth()/4,diren.getHeight()));
            bitmaps.add(Bitmap.createBitmap(diren,(diren.getWidth()/4)*1,0,diren.getWidth()/4,diren.getHeight()));
            bitmaps.add(Bitmap.createBitmap(diren,(diren.getWidth()/4)*2,0,diren.getWidth()/4,diren.getHeight()));
            bitmaps.add(Bitmap.createBitmap(diren,(diren.getWidth()/4)*3,0,diren.getWidth()/4,diren.getHeight()));

            width=diren.getWidth()/4;
            height=diren.getHeight();

            y=-diren.getHeight();
            Random ran = new Random();
            x=ran.nextInt(display_w-(diren.getWidth()/4));

        }

        private int index=0;
        private int num =0;
        @Override
        public Bitmap getBitmap() {
            Bitmap bitmap = bitmaps.get(index);
            //控制切换四个飞机的频率
            if(num == 7){
                index++;
                if(index == bitmaps.size()){
                    index=0;
                }
                num=0;
            }
            y+=3;
            num++;

            if(y>display_h){
                gameImage.remove(this);
            }

            return bitmap;
        }

        //受到攻击
        public void shoudaogongji(ArrayList<Zidan> zidans){

            for(GameImage zidan:(List<GameImage>)zidans.clone()){

                if(zidan.getX()>x&&zidan.getY()>y
                        &&zidan.getX()<x+width
                        &&zidan.getY()<y+height){
                    //子弹击中敌机了
                    zidans.remove(zidan);
                    break;
                }

            }

        }

        @Override
        public int getX() {
            return x;
        }

        @Override
        public int getY() {
            return y;
        }
    }

    private class FeijiImage implements GameImage{

        private Bitmap my;
        private int x;
        private int y;
        private int width;
        private int heigth;

        private int getWidth(){
            return width;
        }

        private int getHeigth(){
            return heigth;
        }

        private List<Bitmap> bitmaps = new ArrayList<Bitmap>();

        private  FeijiImage(Bitmap my){
            this.my=my;
            bitmaps.add(Bitmap.createBitmap(my,0,0,my.getWidth()/4,my.getHeight()));
            bitmaps.add(Bitmap.createBitmap(my,(my.getWidth()/4),0,my.getWidth()/4,my.getHeight()));
            bitmaps.add(Bitmap.createBitmap(my,(my.getWidth()/4)*2,0,my.getWidth()/4,my.getHeight()));
            bitmaps.add(Bitmap.createBitmap(my,(my.getWidth()/4)*3,0,my.getWidth()/4,my.getHeight()));
            //得到战机的高和宽
            width=my.getWidth()/4;
            heigth=my.getHeight();
            x=(display_w - my.getWidth()/4)/2;
            y=display_h-my.getHeight()-30;
        }

        private int index=0;
        private int num =0;
        @Override
        public Bitmap getBitmap() {
            Bitmap bitmap = bitmaps.get(index);
            //控制切换四个飞机的频率
            if(num == 7){
                index++;
                if(index == bitmaps.size()){
                    index=0;
                }
                num=0;
            }
            num++;
            return bitmap;
        }

        @Override
        public int getX() {
            return x;
        }

        @Override
        public int getY() {
            return y;
        }

        public void setY(int y){
            this.y=y;
        }

        public void setX(int x){
            this.x=x;
        }

    }

    //负责背景照片的处理
    private class BeijingImage implements GameImage{
        private Bitmap bg;
        private BeijingImage(Bitmap bg){
            this.bg=bg;
            newBitmap = Bitmap.createBitmap(display_w,display_h, Bitmap.Config.ARGB_8888);
        }

        private Bitmap newBitmap = null;
        private int height = 0;

        public Bitmap getBitmap(){
            Paint p = new Paint();
            Canvas canvas = new Canvas(newBitmap);
            canvas.drawBitmap(bg,
                    new Rect(0,0,bg.getWidth(),bg.getHeight()),
                    new Rect(0,height,display_w,display_h+height),p);

            canvas.drawBitmap(bg,
                    new Rect(0,0,bg.getWidth(),bg.getHeight()),
                    new Rect(0,-display_h+height,display_w,height),p);

            height++;
            if(height==display_h){
                height=0;
            }
            return newBitmap;
        }

        public int getX(){
            return 0;
        }

        public int getY(){
            return 0;
        }
    }

    private boolean state = false;
    private SurfaceHolder holder;

    //绘画中心
    public void run() {
        Paint p1 = new Paint();
        int diren_num=0;//
        int zidan_num=0;
        try{
            while(state){
                if(selectfeiji!=null){
                    if(zidan_num==10){
                        zidans.add(new Zidan(selectfeiji,zidan));
                        zidan_num=0;
                    }
                    zidan_num++;
                }

                Canvas newCanvas = new Canvas(erjihuancun);
                for(GameImage image:(List<GameImage>)gameImage.clone()){
                    if(image instanceof DijiImage){
                        //把子弹告诉敌机
                        ((DijiImage)image).shoudaogongji(zidans);
                    }
                    newCanvas.drawBitmap(image.getBitmap(),image.getX(),image.getY(),p1);
                }

                for(GameImage image:(List<GameImage>)zidans.clone()){
                    newCanvas.drawBitmap(image.getBitmap(),image.getX(),image.getY(),p1);
                }

                if(diren_num==150){
                    diren_num=0;
                    gameImage.add(new DijiImage(diren));
                }
                diren_num++;
                Canvas canvas = holder.lockCanvas();
                canvas.drawBitmap(erjihuancun,0,0,p1);
                holder.unlockCanvasAndPost(canvas);
                Thread.sleep(10);
            }
        }catch(Exception e){
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
    }

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

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        //得到屏幕的宽和高
        display_w=width;
        display_h=height;
        init();
        this.holder=holder;
        state = true;
        new Thread(this).start();
    }

}

dafeijiGameView.java

package com.example.administrator.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去掉标题
        // requestWindowFeature(Window.FEATURE_NO_TITLE);
        dafeijiGameView view =  new dafeijiGameView(this);

        setContentView(view);
    }

}

MainActivity.java

绘制爆炸动画

  将爆炸图片分成八分,从左上角第一张爆炸到最后一张

 private List<Bitmap> baozhas = new ArrayList<Bitmap>();

  将八张图片分别加入链表

 baozhas.add(Bitmap.createBitmap(baozha,0,0,baozha.getWidth()/4,baozha.getHeight()/2));
            baozhas.add(Bitmap.createBitmap(baozha,(baozha.getWidth()/4)*1,0,baozha.getWidth()/4,baozha.getHeight()/2));
            baozhas.add(Bitmap.createBitmap(baozha,(baozha.getWidth()/4)*2,0,baozha.getWidth()/4,baozha.getHeight()/2));
            baozhas.add(Bitmap.createBitmap(baozha,(baozha.getWidth()/4)*3,0,baozha.getWidth()/4,baozha.getHeight()/2));

            baozhas.add(Bitmap.createBitmap(baozha,0,baozha.getHeight()/2,
                    baozha.getWidth()/4,baozha.getHeight()/2));
            baozhas.add(Bitmap.createBitmap(baozha,(baozha.getWidth()/4)*1,baozha.getHeight()/2,
                    baozha.getWidth()/4,baozha.getHeight()/2));
            baozhas.add(Bitmap.createBitmap(baozha,(baozha.getWidth()/4)*2,baozha.getHeight()/2,
                    baozha.getWidth()/4,baozha.getHeight()/2));
            baozhas.add(Bitmap.createBitmap(baozha,(baozha.getWidth()/4)*3,baozha.getHeight()/2,
                    baozha.getWidth()/4,baozha.getHeight()/2));

  前面四张图片都在y轴上方,  bitmaps.add(Bitmap.createBitmap())第二个参数分别取爆炸图x轴上的第一张、第二张、第三张、第四张,第三个参数为都为0表示y轴上方的图片

  后面四张图片在y轴下方,同理,第二个参数取x轴方向上的第一张、第二张、第三张、第四张,第三个参数为都为baozha.getHeight()/2表示y轴下方的图片

  当判断子弹击中敌机时,生成爆炸效果  

 //判断敌机是否被击中
        private boolean state=false;
        //受到攻击
        public void shoudaogongji(ArrayList<Zidan> zidans){

            if(!state){
                for(GameImage zidan:(List<GameImage>)zidans.clone()){

                    if(zidan.getX()>x&&zidan.getY()>y
                            &&zidan.getX()<x+width
                            &&zidan.getY()<y+height){
                        //子弹击中敌机了
                        zidans.remove(zidan);
                        state=true;
                        bitmaps=baozhas;
                        break;
                    }
                }
            }
        }

  处理到这时发现爆炸效果会一直循环出现,而我们只需要当爆炸效果出现一次后就消失,还需要的对爆炸效果动画进行处理

 public Bitmap getBitmap() {
            Bitmap bitmap = bitmaps.get(index);
            //控制切换四个飞机的频率
            if(num == 7){
                index++;
                //当爆炸动画运行一次时候删除爆炸效果
                if(index==8 &&state){
                    gameImage.remove(this);
                }

                if(index == bitmaps.size()){
                    index=0;
                }
                num=0;
            }
            y+=dijiyidong;
            num++;

            if(y>display_h){
                gameImage.remove(this);
            }

            return bitmap;
        }

分数和关卡

添加文本

  绘画分数的笔

        Paint p1 = new Paint();
        int diren_num=0;//
        int zidan_num=0;

        Paint p2=new Paint();
        p2.setColor(Color.WHITE);
        p2.setTextSize(30);
        p2.setDither(true);
        p2.setAntiAlias(true);
 private boolean state = false;
    private SurfaceHolder holder;
    private long fenshu=0;
    private int guanka=1;

    private int chudishu=50;        //出敌机的数字
    private int dijiyidong=5;       //敌机的移动
    private int xiayiguan=50;       //下一关分数

    private int[][] sj={
            {1,50,50,4},
            {2,100,45,4},
            {3,150,40,5},
            {4,200,35,5},
            {5,300,30,6},
            {6,400,30,6},
            {7,500,25,7},
            {8,650,25,7},
            {9,900,20,8},
            {10,1200,20,9}};

  判断关卡,当分数达到要求时进行下一关

         //分数
                newCanvas.drawText("分数:"+fenshu,0,30,p2);
                newCanvas.drawText("关卡:"+guanka,0,60,p2);
                newCanvas.drawText("下一关:"+xiayiguan,0,90,p2);

                //升级关卡
                if(sj[guanka-1][1]<=fenshu){

                    chudishu=sj[guanka][2]; //出敌机的数字
                    dijiyidong=sj[guanka][3];
                    fenshu=sj[guanka-1][1]-fenshu;
                    xiayiguan=sj[guanka][1];
                    guanka=sj[guanka][0];
                }

  增加敌机速度在 Bitmap getBitmap()中

            y+=dijiyidong;
            num++;

  添加游戏关卡

    private int[][] sj={
            {1,50,50,4},
            {2,100,45,4},
            {3,150,40,5},
            {4,200,35,5},
            {5,300,30,6},
            {6,400,30,6},
            {7,500,25,7},
            {8,650,25,7},
            {9,900,20,8},
            {10,1200,20,9}};

  第一个参数:关卡

  第二个参数:达到分数进入下一个关卡  

  第三个参数:自己飞机向前飞行频率

  第四个参数:敌机向下飞行速度

原文地址:https://www.cnblogs.com/1138720556Gary/p/9459400.html

时间: 2024-10-06 22:25:04

Android_(游戏)打飞机05:处理子弹,击中敌机,添加计分板的相关文章

Android_(游戏)打飞机02:游戏背景滚动

Android_(游戏)打飞机01:前言 传送门 Android_(游戏)打飞机02:游戏背景滚动 传送门 游戏背景滚动效果 package com.example.administrator.myapplication; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; imp

Android_(游戏)打飞机03:控制玩家飞机

Android_(游戏)打飞机01:前言 传送门 Android_(游戏)打飞机02:游戏背景滚动 传送门 Android_(游戏)打飞机03:控制玩家飞机 传送门 控制玩家飞机效果 package com.example.administrator.myapplication; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import

第四十天 一乐在其中—Android的小游戏打飞机(三)添加敌机

8月9日,晴."江城如画里,山晓望晴空.雨水夹明镜,双桥落彩虹. 人烟寒橘柚,秋色老梧桐." 上篇已经让飞机加载子弹和音效及背景音乐,本篇主要添加敌机. 本篇要用到的几个函数讲解: 1.voidsetTag (int nTag) 设置动作的标记. 2.CCRANDOM_0_1()函数生成的是 [0, 1] 之间的随机数:要生成  [0-100] 之间的数CCRANDOM_0_1 * 100:生成 [1,5]  之间的float 数,就是 CCRANDOM_0_1 * 4 + 1. 3.

第三十九天 一乐在其中—Android的小游戏打飞机(三)飞机打子弹

8月7日,小雨."乳鸦啼散玉屏空,一枕新凉一扇风.睡起秋色无觅处,满阶梧桐月明中." 上篇已经让飞机随手指自由移动,本篇加载子弹和音效及背景音乐. 本篇要用到的几个函数讲解: 一.cocos2d的音效.背景音乐函数如下: 1.SoundEngine.sharedEngine().playSound(Context ctxt, int resId, boolean loop) 用于播放背景音乐. 2.SoundEngine.sharedEngine().playEffect(Contex

第四十一天 一乐在其中—Android的小游戏打飞机(五)添加背景

8月10日,晴."寒蝉凄切,对长亭晚,骤雨初歇." 上篇已经加载了敌机,本篇主要添加背景画面. 本篇要用到的几个函数讲解: 1.addChild(CCNode child, int z):两个参数,第一个要加入场景的对象,第二个是绘制层的顺序,默认参数值为0,表示最高优先层导入,该值越大表示该层在最后加载(在最高一层),一般背景图层是首先加载,其它元素在加载在背景层上面.因为背景在后面加载的话,会覆盖掉前面加载的元素,看不到想要的图层组合效果. 2.schedule(String se

第三十七天 一乐在当中—Android的小游戏打飞机(一)环境搭建

8月3日,晴天."绿树阴浓夏日长.楼台倒影入池塘. 水晶帘动微风起,满架蔷薇一院香. " cocos2d是一个开源的游戏开发框架.利用它能够非常easy的开发2D游戏.眼下最基本的分支是iPhone版本号的,你能够在iPhone上开发2D游戏. 它提供了物理.精灵(sprites).视差卷轴(parallax scrolling),支持iPhone touch和accelerator等非常多功能.如今在App Store上已经有不少用cocos2d开发的游戏. 假设你想在Andriod

第三十七天 一乐在其中—Android的小游戏打飞机(一)

8月3日,晴天."绿树阴浓夏日长,楼台倒影入池塘, 水晶帘动微风起,满架蔷薇一院香." cocos2d是一个开源的游戏开发框架,利用它可以非常容易的开发2D游戏.目前最主要的分支是iPhone版本的,你可以在iPhone上开发2D游戏.它提供了物理,精灵(sprites),视差卷轴(parallax scrolling),支持iPhone touch和accelerator等很多功能.现在在App Store上已经有不少用cocos2d开发的游戏. 如果你想在Andriod上使用Jav

第三十八天 一乐在其中—Android的小游戏打飞机(二)移动飞机

8月6日,阴."桑条无叶土生烟,箫管迎龙水庙前.朱门几处看歌舞,犹恐春阴咽管弦". 上篇已经把飞机放到了屏幕上,怎么让飞机随手指自由移动,是本篇解决的问题. 本篇要用到的几个函数讲解: 一.cocos2d的触摸事件如下: 1.ccTouchesMoved:接受的是手指在屏幕上滑动/停留的信息,即当用户手指在屏幕移动时,执行该方法. 2.ccTouchesBegan:接受的是手指点击屏幕的那一下的信息,即当用户开始触摸屏幕时,执行该方法. 3.ccTouchesEnded: 接受手指离开

用Quick3.3简单开发微信打飞机 (三)----------------------------- 添加爆炸动画和子弹与敌机的碰撞

用Quick3.3简单开发微信打飞机03 添加爆炸动画和子弹与敌机的碰撞 这个就作为最后一篇吧,当然,可以扩展的方面还有很多,比如分数的记录,结算界面还有一些小道具等,就不添加了.这只是当作一个简单的入门,而在这个入门中确实学到了不少东西.而那些没添加的东西呢,其实,就没那么重要了.没有太难的知识点.所以,就没必要纠结太长时间了. 碰撞检测: 碰撞检测重新写了一个计时器,这个计时器是每帧调用的,也就是每秒调用60次.但是,没有用updata. 检测碰撞的函数如下: [plain] view pl