和之前的差不多哦,直接上代码。
1.BigPlane
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 BigPlane extends GameObject {
private Bitmap bigPlane;图片
private int i;
public BigPlane(Resources res) {
super(res);
initBitmap();
this.score = 5000;//销毁敌机的分数
}
@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 = 100;//血量
currentBlood = bloodVloume;
Random ran = new Random();
this.speed = ran.nextInt(3) + 5;//敌机速率
object_x = screen_width / 2 - object_width / 2;//x坐标
}
@Override
public void initBitmap() {//敌机的初始化
bigPlane = BitmapFactory.decodeResource(res, R.drawable.big1);
object_width = bigPlane.getWidth();
object_height = bigPlane.getHeight() / 5;
}
@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(bigPlane, 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(bigPlane, object_x, object_y - y, paint);
canvas.restore();
currentFrome++;
if (currentFrome >= 5) {
currentFrome = 0;
isAlive = false;
isExplosion = false;
}
}
}
}
@Override
public void move() {//移动方法,自己随便设置的,大家发挥想象啰,具体的逻辑是从上面出来移动到屏幕的中间然后左右移动
if ((object_y + speed) < (screen_height / 2 - object_height / 2)) {
object_y += speed;
} else {
object_y = screen_height / 2 - object_height / 2;
}
if (object_x == screen_width - object_width) {
i = 1;
}
else if (object_x == 0) {
i = 0;
}
switch (i) {
case 0:
if (object_y == screen_height / 2 - object_height / 2) {
object_x += speed;
if (object_x >= screen_width - object_width) {
object_x = screen_width - object_width;
}
}
break;
case 1:
if (object_y == screen_height / 2 - object_height / 2) {
object_x -= speed;
if (object_x <= 0) {
object_x = 0;
}
}
break;
default:
break;
}
}
@Override
public void release() {
if (!bigPlane.isRecycled()) {
bigPlane.recycle();
}
}
@Override
public void attacked(int harm) {//击中敌机时传进来子弹的伤害,并减少血量
currentBlood -= harm;
if (currentBlood <= 0) {
isExplosion = true;
}
}
@Override
public boolean isCollide(GameObject obj) {
return super.isCollide(obj);
}
public float get_x(){//为什么写这两个方法,因为敌机子弹位置与敌机有关
return object_x+object_width/2;
}
public float get_y(){
return object_y+object_height;
}
}
2.BossBullet
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 BossBullet extends GameObject {
private Bitmap bullet;
public BossBullet(Resources res) {
super(res);
initBitmap();
}
@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) {
isAlive = true;
Random ran = new Random();
this.speed = ran.nextInt(3) + 3;
object_x = m - object_width / 2;
object_y = n;
}
@Override
public void initBitmap() {
bullet = BitmapFactory.decodeResource(res, R.drawable.bigbullet);
object_width = bullet.getWidth();
object_height = bullet.getHeight();
}
@Override
public void myDraw(Canvas canvas) {
if (isAlive) {
if (!isExplosion) {
canvas.drawBitmap(bullet, object_x, object_y, paint);
move();
} else {
isAlive = false;
isExplosion = false;
}
}
}
@Override
public void move() {
if (object_y < screen_height) {
object_y += speed;
} else {
isAlive = false;
}
}
@Override
public void release() {
if (!bullet.isRecycled()) {
bullet.recycle();
}
}
@Override
public boolean isCollide(GameObject obj) {
return super.isCollide(obj);
}
}