1. 程式人生 > >05_31 Android Studio (飛機大戰-繪製玩家和Boss飛機,子彈)

05_31 Android Studio (飛機大戰-繪製玩家和Boss飛機,子彈)

MySurfaceView

package com.example.shinelon.mysurfaceview;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
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; public static int height; public static int width; private boolean inDrawing = true
; private BlackGround blackGround; private Myplane myplane; private Bossplane bossplane; private Vector<Bullet> bulletVector = new Vector<>(); private int count; public MySurfaceView(Context context) { super(context); init(); } private void
init() { surfaceHolder = getHolder(); surfaceHolder.addCallback(this);//添加回調事件 setFocusable(true);//設定可聚焦 setKeepScreenOn(true);//保持螢幕常亮 setFocusableInTouchMode(true);//設定觸控模式 } @Override public void surfaceCreated(SurfaceHolder holder) { height = getHeight(); width = getWidth(); new Thread(this).start();//啟動子執行緒 } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { } @Override public void run() { Paint paint = new Paint(); blackGround = new BlackGround(BitmapFactory.decodeResource(getResources(), R.mipmap.bk)); myplane = new Myplane((BitmapFactory.decodeResource(getResources(), R.mipmap.myplane))); bossplane = new Bossplane(BitmapFactory.decodeResource(getResources(),R.mipmap.bossplane)); while (inDrawing) { count++; try { canvas = surfaceHolder.lockCanvas(); canvas.drawColor(Color.WHITE); blackGround.draw(canvas, paint); myplane.draw(canvas, paint); bossplane.draw(canvas,paint); if(count % 50 ==0){ Bullet bullet = new Bullet(BitmapFactory.decodeResource(getResources(),R.mipmap.mybullet),myplane.getX()+100,myplane.getY(),0); bulletVector.add(bullet);//將子彈新增到bulletVector陣列中 } for(int i=0;i<bulletVector.size();i++){ bulletVector.elementAt(i).draw(canvas,paint);//將子彈打印出來 } for(int i =0;i<bulletVector.size();i++){ if(bulletVector.elementAt(i).isDead()){ bulletVector.remove(i);//移除無效子彈 } } //測試,有效子彈的數量 //Log.e("MySurfaceView",".................."+bulletVector.size()); if(count % 20 ==0){ Bullet bullet = new Bullet(BitmapFactory.decodeResource(getResources(),R.mipmap.bossbullet),bossplane.getX(),bossplane.getY(),1); bulletVector.add(bullet);//將子彈新增到bulletVector陣列中 } for(int i=0;i<bulletVector.size();i++){ bulletVector.elementAt(i).draw(canvas,paint);//將子彈打印出來 } for(int i =0;i<bulletVector.size();i++){ if(bulletVector.elementAt(i).isDead()){ bulletVector.remove(i);//移除無效子彈 } } } catch (Exception e) { e.printStackTrace(); } finally { if (canvas != null) { surfaceHolder.unlockCanvasAndPost(canvas); } } } } }

Bossplane敵機

package com.example.shinelon.mysurfaceview;

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 = 4;
    private int crazySpeed =50;
    private int count;//計數器
    private int time = 50;//瘋狂時間間隔
    private boolean isCrazy;


    public Bossplane(Bitmap bitmap) {
        this.bitmap = bitmap;
        this.frameW = bitmap.getWidth()/10;
        this.frameH = bitmap.getHeight();
    }

    public void draw(Canvas canvas, Paint paint){
        canvas.save();//先儲存
        canvas.clipRect(x,y,x+frameW,y+frameH);//對Boss飛機進行裁剪
        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 (y > MySurfaceView.height - frameH) {
                crazySpeed = -crazySpeed;
            }

            if (count % time == 0) {
                isCrazy = true;
            }
            x = x + speed;
            if (x > MySurfaceView.width - frameW) {
                speed = -speed;
            }
            if (x < 0) {
                speed = -speed;
            }
        }
    }



    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public Bitmap getBitmap() {
        return bitmap;
    }

    public int getFrameW() {
        return frameW;
    }

    public int getFrameH() {
        return frameH;
    }
}

Bullet子彈

package com.example.shinelon.mysurfaceview;

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

public class Bullet {
    private Bitmap bitmap;
    private int x,y;
    private boolean isDead;
    private int speed = 20;
    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 int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public Bitmap getBitmap() {
        return bitmap;
    }

Myplane玩家飛機

package com.example.shinelon.mysurfaceview;

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 height, width;//
    private boolean noCollision;
    private int noCollisionCount;

    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 int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getHeight() {
        return height;
    }

    public int getWidth() {
        return width;
    }
}