05_31 Android Studio(飞机大战-碰撞,爆炸,音效)

本文介绍了在Android Studio中开发飞机大战游戏时如何实现碰撞检测、爆炸效果的绘制以及音效的添加。尽管在最终验收时遇到了未完全解决的问题,如爆炸效果未实现,但通过这次项目,作者对Java的理解加深,特别是方法重载和调用方面的知识,同时掌握了写博客和使用Xmind等工具来整理思维和学习的新技能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MySurfaceView

package com.example.shinelon.helloworld;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;;
import android.graphics.RectF;
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;//绘制图形
    private Boolean a = true;//标志位
    public static int height;
    public static int width;
    private MyPlan plan;
    //Vector是线程安全的,ArrayList是非线性安全的
    private Vector<Bullet> bulletVector = new Vector<>();//玩家子弹数组
    private Vector<Boom> boomVector = new Vector<>();//Boss子弹数组
    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);//设置屏幕常亮
        setFocusableInTouchMode(true);//设置触摸模式
    }


    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        new Thread(this).start();//启动子线程
        height = getHeight();
        width = getWidth();

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

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

    @Override
    public void run() {
        Paint paint = new Paint();
        BackGround backGround = new BackGround(BitmapFactory.decodeResource(getResources(), R.mipmap.bk));
        plan = new MyPlan(BitmapFactory.decodeResource(getResources(), R.mipmap.myplane), BitmapFactory.decodeResource(getResources(), R.mipmap.hp));
        BossPlan bossPlan = new BossPlan(BitmapFactory.decodeResource(getResources(), R.mipmap.bossplane));

        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值