飞机大战总结与完整代码

这个项目用了哪些方法

这个项目用到了JAVA语言中的封装,继承,多态,方法重载,接口等多种思想

继承和接口主要运用在MySurfaceView中,这个类继承了surfaceview里的属性与方法,并且还要实现SurfaceHolder.Callback.runnable的接口,这就体现了子类与父类之间的继承关系,还有接口怎么运用
封装用的的地方比较多,各个类中使用的四个访问权限符,public,private,default,protected,当使用privat修饰的需要用get,set方法来进行调用
方法的重载使用与飞机飞机的碰撞,在飞机类和子弹类中使用,使用同一个方法名但参数不同,来构成方法的重载。

实训的感想与收获

一个月前我们开始了JAVA实训,在此之前,我对JAVA一无所知,一个月的实训很累但同时我也收获了很多,从一开始的eclip到后来的andriod studio,我从不会写代码到会写代码,这是进步很大的,在这一个月的时间里,我学会了JAVA的基本知识,在此过程中我遇到了很多麻烦,在多方的帮助下完成了自己的第一个完整的JAVA代码——图书管理系统,对于我来说我觉得是不可能完成的,但是通过老师和同学们的帮助我还是完成了,之后又经过一个礼拜的学习,我们开始用studio进行制作飞机大战的代码,从一开始最简单的背景图滚动到现在完成简单的游戏运行,这个进步也是很大的,虽然每天抓耳挠腮写代码,写到抓狂,但是从来没有想过放弃,出了N多个问题,N多个BUG但是,每次崩溃之后还是乖乖的一个个解决掉。一个月实训就要结束,收获是巨大。也很感谢老师和同学对于我的鼓励和帮助。

完整的代码

package com.example.administrator.myapplication9;

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

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+=10;
        y2+=10;
        if (y1>=MySurfaceView.height){
            y1=y2-bitmap.getHeight();//移动到第二张图片的顶部
        }
        if (y2>=MySurfaceView.height){
            y2=y1-bitmap.getHeight();
        }
    }
}
public Boom(Bitmap bitmap, int x, int y, int totalFrame) {
        super();
        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-currentFrame*frameW,y,paint);
        canvas.restore();
        logic();

    }
    public void logic(){

        if (currentFrameelse{
            isEnd = true;
        }
    }

    public boolean isEnd() {
        return isEnd;
    }
}
package com.example.administrator.myapplication9;
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=5;//boos飞机的速度
    private int crazySpeed=50;//疯狂速度
    private int count;//计数器
    private int time=500;//疯狂模式间隔
    private boolean isCrazy;

    private int bossHp = 100;

    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()y&&bullet.getY()true);
                if (bossHp<0){
                    MySurfaceView.GAME_STATE=1;

                }
                return true;
            }
            return false;

    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getFrameW() {
        return frameW;
    }

    public int getFrameH() {
        return frameH;
    }

}


package com.example.administrator.myapplication9;
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 Bullet() {

    }

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

    public void logic() {

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

    public boolean isDead() {
        return isDead;
    }
    public Bitmap getBitmap(){
        return bitmap;
    }
    public int getX(){
        return x;
    }

    public int getY() {
        return y;
    }

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

package com.example.administrator.myapplication9;

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

public class GameSoundPool {
    private SoundPool soundPool;
    private int s1;
    private int s2;
   private int s3;

    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);
       // s3 = soundPool.load(context,R.raw.bg_logobg,2);
    }
    public void playSound(int s) {
        switch (s){
            case 1:
               soundPool.play(s1,1,1,1,1,1.0f);
                break;
            case 2:
                soundPool.play(s2,1,1,1,1,1.0f);
                break;
            case 3:
               soundPool.play(s3,1,1,1,1,1.0f);
               break;
        }
    }
    }

package com.example.administrator.myapplication9;
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 =5;

    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 (hp<=0){
            MySurfaceView.GAME_STATE= 2;
        }
        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; ipublic void touchEvent(MotionEvent event){
        if (event.getAction()==MotionEvent.ACTION_MOVE){
            float ex = (int) event.getX();
            float ey = (int) event.getY();
            if (ex>x&&exy&&eyint) 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()y&&bullet.getY()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()if(xbossPlane.getX()){
                    noCollision = true;
                    if (hp>0){
                        hp--;
                    }

                    return true;

                }
                if (x>bossPlane.getX()&&x+widthtrue;
                    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;
    }
}
package com.example.administrator.myapplication9;
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.RectF;
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 VectorbulletVector = new Vector <>();
    private VectorbossBulletVector = new Vector <>();
    private VectorboomVector = new Vector <>();
    private int count;
    private GameSoundPool gameSoundPool;

    public static int GAME_STATE=0;

    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() {
        gameSoundPool.playSound(3);

        Paint paint = new Paint();
        BackGround backGround = new BackGround(BitmapFactory.decodeResource(getResources(), R.mipmap.img_bg_level_3));
        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);
             switch (GAME_STATE){

                 case 0:
                     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(),plane.getY(),0);
                         bulletVector.add(bullet);
                         bulletVector.add(bullet1);
                     }


                     for (int i = 0;iif (bulletVector.elementAt(i).isDead()) {
                             bulletVector.remove(i);

                         }
                     }
                     for (int i = 0;iif (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;iif (boomVector.elementAt(i).isDead()){
                             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);
                         //Bullet bullet1 = new Bullet(BitmapFactory.decodeResource(getResources(), R.mipmap.bossbullet),bossPlane.getX()+bossPlane.getFrameW(),bossPlane.getY()+bossPlane.getFrameH(),1);
                         bossBulletVector.add(bullet);
                         // bossBulletVector.add(bullet1);
                     }

                     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));
                     }
                     plane.isCollision(bossPlane);
                        break;
                 case 1:
                     RectF rectF1 = new RectF(0,0,getWidth(),getHeight());
                     canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.gamewin),null,rectF1,paint);
                     break;
                 case 2:
                     RectF rectF = new RectF(0,0,getWidth(),getHeight());
                     canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.gamelost),null,rectF,paint);
                     break;
             }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (canvas != null) {
                    surfaceHolder.unlockCanvasAndPost(canvas);//解锁画布,显示到屏幕上
                }
            }

        }

    }

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

你可能感兴趣的:(飞机大战总结与完整代码)