Android studio 飞机大战项目思路和代码

整体思路

先背景

绘制玩家飞机,玩家飞机随触摸点移动

绘制boss飞机,自动移动

绘制子弹,分别从玩家飞机和boss飞机发射

绘制爆装效果,由子弹触发

绘制血量,玩家血量为0触发死亡画面,boss血量为零触发通过

绘制开始界面,点击开机进入游戏



1.如何绘制滚动的背景图片

第一张图y座标往下递减实现滚动,

第二张图的底部座标等于第一张图的顶部座标,当第一张图片的顶部座标大于屏幕,令其底部座标等于第二张图的顶部座标实现循环滚动。

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


2.如何绘制飞机

 用canvas调用drawBitmap

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+height){
                noCollision = true;
                hp--;
                return true;
            }
        }
        return false;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getWidth() {
        return width;
    }
}


3.如何绘制子弹

 用canvas调用drawBitmap

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;
               if (y < 0) {
                   isDead = true;
               }
               break;
       }
    }

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

    public int getY() {
        return y;
    }
}

4.如何判断碰撞

子弹的左右x座标在飞机左右x座标里面

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

5.如何绘制爆装效果

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



6.如何添加音效

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


    }
}


定义变量时常常用到封装。

需要用某个类的功能但又想扩展一些功能,可以继承那个类。

public class MySurfaceView extends SurfaceView 

实现接口

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback,Runnable 

我的收获与感悟

首先是对java有了更深的理解,但主要学到了不少道理,觉得很受用。尤其学习方法,

最后感受,真应该先跟着老师走,优先老师的任务,老师要求的先全部完成。

为什么?不是说自己学哪里不对,有不懂的自己去查去看毫无问题,但先理解老师要求理解的,因为一旦和老师脱节,老师的之后布置的任务就很难完成,因为任务是老师教的内容的实践。考核也会很烦,因为考的也是老师让我们理解掌握的,结果往往就是为了任务和考核花大量时间精力补老师讲的内容。

Java的内容很多,任何一门学科的全部内容都很多,先完成老师的要求,再去扩展。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章