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;
    }
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Android Studio 飞机大战是一款基于 Android Studio 开发的飞行射击游戏。玩家需要控制飞机躲避敌机的攻击并射击敌机,最终击败 Boss。这款游戏具有简单易懂的操作和精美的画面,适合各个年龄段的玩家。 ### 回答2: Android Studio飞机大战是一款基于Android平台开发的游戏应用程序。这款游戏使用了Android Studio开发工具,并利用Java语言编写。玩家扮演一名飞行员,在游戏中操控一架战斗机,与敌机展开战斗。 在游戏中,玩家可以使用屏幕上的触摸操作来控制战斗机的移动,同时按下屏幕进行射击。其操作简单直观,适合各个年龄段的玩家。游戏场景设置在空中,玩家需要躲避敌机的攻击,并尽可能多地消灭敌机。玩家可以通过击落敌机来积分,同时还可以获得道具和奖励,提升自己的战斗力。 这款游戏不仅具有良好的游戏性,还有精美的图形和音效。通过Android Studio平台的强大功能,游戏开发者可以设计出精美的游戏场景、真实的音效以及流畅的游戏操作,为玩家带来更好的游戏体验。 在开发过程中,开发者需要使用Android Studio提供的各种工具和资源进行开发。如Android Studio提供了所见即所得的可视化界面设计,开发者可以方便地布局游戏界面,设置按钮、背景等元素。同时,Android Studio还提供了强大的调试功能,开发者可以随时检查游戏代码的执行情况,及时修复bug。 总之,Android Studio飞机大战是一款基于Android平台的游戏应用程序,通过精美的图形和音效,简单直观的操作方式,给玩家带来了极具乐趣的游戏体验。如对游戏开发有兴趣的人员,可以使用Android Studio进行开发,体验游戏开发的魅力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值