飞机大战-GameGoods商品的实现


游戏过程中达到一定分数时会有商品下来,这里设置了两种商品,一种是爆炸,另一种是改变子弹,注意爆炸商品得到了之后是将他显示在左下角。


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 GameGoods extends GameObject {
	private Bitmap missile;//爆炸商品
	private Bitmap bullet;//改变子弹商品
	private boolean isAlive1;//标志位
	private boolean isExplosion1;

	private float object_x1;
	private float object_y1;

	private int i;
	private int id1;
	private int id2;

	public GameGoods(Resources res) {//初始化操作
		super(res);
		isAlive = true;
		isAlive1 = true;
		initBitmap();
	}

	@Override
	public void initScreen(float screen_width, float screen_height) {
		super.initScreen(screen_width, screen_height);
		Random ran = new Random();
		i = ran.nextInt(2);
		this.speed = ran.nextInt(2) + 1;//商品速率
		object_x = ran.nextInt((int) (screen_width - object_width));
		object_y = -object_height;  //他们的x,y坐标 
		object_x1 = ran.nextInt((int) (screen_width - object_width));
		object_y1 = -object_height;
	}

	@Override
	public void initBitmap() {
		missile = BitmapFactory.decodeResource(res, R.drawable.missile_goods);
		bullet = BitmapFactory.decodeResource(res, R.drawable.bullet_goods);
		object_width = bullet.getWidth();
		object_height = bullet.getHeight();

	}

	public void myDraw(Canvas canvas, int i) {//通过传入的i确定放哪一种商品
		switch (i) {
		case 0:
			if (isAlive) {
				if (!isExplosion) {
					canvas.drawBitmap(missile, object_x, object_y, paint);
					move();
				} else {
					canvas.drawBitmap(missile, 5, screen_height - object_height
							- 5, paint);
				}
			}
			break;
		case 1:
			if (isAlive1) {
				if (!isExplosion1) {
					canvas.drawBitmap(bullet, object_x1, object_y1, paint);
					move1();
				} else {
					isAlive1=false;
					isExplosion1=false;
				}
			}
			break;
		default:
			break;
		}
	}

	@Override
	public void move() {//简单的移动方法,自己随便设置,出了屏幕之外则不再进行绘制
		if (object_y <= screen_height) {
			if (object_x == screen_width - object_width) {
				i = 1;
			} else if (object_x == 0) {
				i = 0;
			}
			switch (i) {
			case 0:
				object_x += speed;
				if (object_x > screen_width - object_width) {
					object_x = screen_width - object_width;
				}
				object_y += speed;
				break;
			case 1:
				object_x -= speed;
				if (object_x < 0) {
					object_x = 0;
				}
				object_y += speed;
				break;
			default:
				break;
			}

		} else {
			isAlive = false;
		}
	}
	public void move1() {
		if (object_y1 <= screen_height) {
			if (object_x1 == screen_width - object_width) {
				i = 1;
			} else if (object_x1 == 0) {
				i = 0;
			}
			switch (i) {
			case 0:
				object_x1 += speed;
				if (object_x1 > screen_width - object_width) {
					object_x1 = screen_width - object_width;
				}
				object_y1 += speed;
				break;
			case 1:
				object_x1 -= speed;
				if (object_x1 < 0) {
					object_x1 = 0;
				}
				object_y1 += speed;
				break;
			default:
				break;
			}
			
		} else {
			isAlive1 = false;
		}
	}

	public boolean getAlive1() {
		return isAlive1;
	}

	public void setId_1() {
		id1 = 1;
	}

	public int getId_1() {
		return id1;
	}

	public void setId_2() {
		id2 = 2;
	}

	public int getId_2() {
		return id2;
	}

	@Override
	public void release() {//图片回收
		if (!missile.isRecycled()) {
			missile.recycle();
		}
		if (!bullet.isRecycled()) {
			bullet.recycle();
		}
	}

	@Override
	public boolean isCollide(GameObject obj) {//碰撞检测
		return super.isCollide(obj);
	}
	
	public boolean isCollide1(GameObject obj) {
		if (Math.abs((object_x1 + object_width / 2)
				- (obj.object_x + obj.object_width / 2)) < (object_width + obj
				.object_width) / 2
				&& Math.abs((object_y1 + object_height / 2)
						- (obj.object_y + obj.object_height / 2)) < (object_height + obj
						.object_height) / 2) {
			isExplosion1 = true;
			return true;
		}
		return false;
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值