飞机大战-GameObject精灵父类的实现

来源:互联网 发布:关于单片机的外文文献 编辑:程序博客网 时间:2024/04/29 02:38


所有的精灵都继承这个父类,精灵共有的属性或者是使用次数多的变量以及一些公共的方法,都在此类声明,提高代码的利用率。


有一个碰撞检测方法,用的是矩形碰撞检测,原理是:一个精灵矩形的中点横坐标减去另一个精灵矩形中点的横坐标他们的绝对值会小于他们矩形长度的一半的和,同理纵

坐标也一样。当然方法也有很多种,不过这样会带来误差,想象一下可以知道,精灵不一定是一个矩形,而我们把它定义成一个矩形,这样会带来误差碰撞,我们可以在方法中

处理一下,(楼主偷懒了)因为宁愿少检测也不带来误差检测。


package com.example.qgns;import android.content.res.Resources;import android.graphics.Canvas;import android.graphics.Paint;public class GameObject {protected int currentFrome;//当前帧protected int score;//分数protected int speed;//速度protected int harm;//伤害protected int bloodVloume//血量;protected int currentBlood;//当前血量protected float object_x;//精灵x坐标protected float object_y;//精灵y坐标protected float object_width;//精灵宽protected float object_height;//精灵高protected float screen_width;//屏宽protected float screen_height;//屏高protected boolean isAlive;//是否存活protected boolean isExplosion;//是否爆炸protected Paint paint;//画笔protected Resources res;//资源public GameObject(Resources res) {this.res = res;paint = new Paint();//实例化画笔}public void initScreen(float screen_width, float screen_height) {//得到屏幕的宽和高this.screen_width = screen_width;this.screen_height = screen_height;}public void initial(int i, float m, float n, int j) {//精灵的初始化操作isAlive = true;object_y = -object_height * (i * 2 + 1);//y坐标随传进来的i而确定}public void initBitmap() {}public void myDraw(Canvas canvas) {}public void move() {}public void release() {}public void attacked(int harm) {}public boolean isCollide(GameObject obj) {//碰撞检测方法if (Math.abs((object_x + object_width / 2)- (obj.object_x + obj.object_width / 2)) < (object_width + obj.object_width) / 2&& Math.abs((object_y + object_height / 2)- (obj.object_y + obj.object_height / 2)) < (object_height + obj.object_height) / 2) {isExplosion = true;//碰撞了将爆炸的标志位改为truereturn true;}return false;}}


0 0
原创粉丝点击