飞机大战-SmallPlane和MiddlePlane两种敌机的实现

本文详细介绍了如何在游戏开发中实现小敌机和中等敌机,包括敌机的图片加载、屏幕初始化、位置随机生成、速度设定、碰撞检测、攻击响应、生命值管理和动画效果等关键功能。

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


自己的敌机和子弹都实现了,那我们开始添加敌机,总共有三种机型,小敌机,中等敌机和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);
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值