自己的敌机和子弹都实现了,那我们开始添加敌机,总共有三种机型,小敌机,中等敌机和boss敌机,先添加小敌机和中等敌机。
1.SmallPlane
package com.example.qgns;
import java.util.Random;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
public class SmallPlane extends GameObject {
private Bitmap smallPlane;//小敌机图片
public SmallPlane(Resources res) {
super(res);
initBitmap();
this.score = 100;//击中此敌机得到的分数
}
@Override
public void initScreen(float screen_width, float screen_height) {
super.initScreen(screen_width, screen_height);
}
@Override
public void initial(int i, float m, float n, int j) {
super.initial(i, m, n, j);
bloodVloume = 1;//敌机血量
currentBlood = bloodVloume;//当前血量等于总血量
Random ran = new Random();
object_x = ran.nextInt((int) (screen_width - object_width));//使敌机x坐标随机出现,y坐标已经在父类中定义了哦
this.speed = ran.nextInt(6) + 3* j;//敌机速率
}
@Override
public void initBitmap() {//初始化敌机
smallPlane = BitmapFactory.decodeResource(res, R.drawable.small1);
object_width = smallPlane.getWidth();
object_height = smallPlane.getHeight() / 3;//因为图片包含里几个敌机,所以高度为图片的1/3
}
@Override
public void myDraw(Canvas canvas) {//绘制敌机
if (isAlive) {
if (!isExplosion) {
canvas.save();
canvas.clipRect(object_x, object_y, object_x + object_width,
object_y + object_height);
canvas.drawBitmap(smallPlane, object_x, object_y, paint);
canvas.restore();
move();
} else {
float y = currentFrome * object_height;//剪切图动画,改变敌机的y坐标
canvas.save();
canvas.clipRect(object_x, object_y, object_x + object_width,
object_y + object_height);
canvas.drawBitmap(smallPlane, object_x, object_y - y, paint);
canvas.restore();
currentFrome++;
if (currentFrome >= 3) {
currentFrome=0;
isAlive = false;//注意碰撞之后改变他们的标志位
isExplosion = false;
}
}
}
}
@Override
public void move() {//移动方法
object_y += speed;
if (object_y > screen_height) {
isAlive = false;//走出屏幕外不在绘制
}
}
@Override
public void release() {
if (!smallPlane.isRecycled()) {
smallPlane.recycle();
}
}
@Override
public void attacked(int harm) {//被子弹击中减少血量
currentBlood -= harm;
if (currentBlood <= 0) {
isExplosion = true;//血量为0时爆炸
}
}
@Override
public boolean isCollide(GameObject obj) {
return super.isCollide(obj);
}
}
2.MiddlePlane
package com.example.qgns;
import java.util.Random;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
public class MiddlePlane extends GameObject {
private Bitmap middlePlane;
public MiddlePlane(Resources res) {
super(res);
initBitmap();
this.score=1500;
}
@Override
public void initScreen(float screen_width, float screen_height) {
super.initScreen(screen_width, screen_height);
}
@Override
public void initial(int i, float m, float n, int j) {
super.initial(i, m, n, j);
bloodVloume = 15;
currentBlood = bloodVloume;
Random ran = new Random();
object_x = ran.nextInt((int) (screen_width - object_width));
object_y=-object_height*(i*8+1);
this.speed = ran.nextInt(2) + 3 * j;
}
@Override
public void initBitmap() {
middlePlane = BitmapFactory.decodeResource(res, R.drawable.middle1);
object_width = middlePlane.getWidth();
object_height = middlePlane.getHeight() / 4;
}
@Override
public void myDraw(Canvas canvas) {
if (isAlive) {
if (!isExplosion) {
canvas.save();
canvas.clipRect(object_x, object_y, object_x + object_width,
object_y + object_height);
canvas.drawBitmap(middlePlane, object_x, object_y, paint);
canvas.restore();
move();
} else {
float y = currentFrome * object_height;
canvas.save();
canvas.clipRect(object_x, object_y, object_x + object_width,
object_y + object_height);
canvas.drawBitmap(middlePlane, object_x, object_y-y, paint);
canvas.restore();
currentFrome++;
if(currentFrome>=4){
currentFrome=0;
isAlive=false;
isExplosion=false;
}
}
}
}
@Override
public void move() {
object_y += speed;
if (object_y > screen_height) {
isAlive = false;
}
}
@Override
public void release() {
if (!middlePlane.isRecycled()) {
middlePlane.recycle();
}
}
@Override
public void attacked(int harm) {
currentBlood-=harm;
if(currentBlood<=0){
isExplosion=true;
}
}
@Override
public boolean isCollide(GameObject obj) {
return super.isCollide(obj);
}
}