Android植物大战僵尸教程学习总结(四)拓展

本文深入探讨了游戏开发领域的创新技术,包括AI音视频处理、音视频直播流媒体、图像处理AR特效等,同时展示了如何在游戏开发中综合应用这些技术,以提升用户体验和游戏性能。

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

顺着昨天的思路,自己又补充了一些奇怪的东西

(1)射子弹

(2)阳光点数

(3)种子冷却

(4)加入声音特效

(5)僵尸生命及死亡过程


顺着昨天的思路,子弹肯定也是继承BaseModel并且需要传raceWayIndex进去。然后遍历每条跑道,来判断到底僵尸碰撞的是植物还是子弹。实际操作的时候,出现了一些问题,会闪屏。(SurfaceView不是自带双缓冲的嘛尴尬)然后,没办法,就又加了一层缓冲(不闪了,但是子弹会突然卡一下)。理了半天思路,没辙,感觉写的没错。最后,改了子弹的图层,成了下面这样,还是实现了效果。

子弹类,跟之前的差不多,但是传进并且传出了raceWayIndex(用于之后的判断)

public class Bullet extends BaseModel {
	private int locationX;
	private int locationY;
	private boolean isAlive;
	private int raceWayIndex;
	private int xSpeed=20;

	public Bullet(int locationX,int locationY, int raceWayIndex) {
		this.locationX=locationX;
		this.locationY=locationY;
		this.raceWayIndex=raceWayIndex;
		isAlive=true;
	}
	public int getLocationX() {
		return locationX;
	}

	public void setLocationX(int locationX) {
		this.locationX = locationX;
	}

	public int getLocationY() {
		return locationY;
	}

	public void setLocationY(int locationY) {
		this.locationY = locationY;
	}

	public boolean isAlive() {
		return isAlive;
	}

	public void setAlive(boolean isAlive) {
		this.isAlive = isAlive;
	}

	@Override
	public void drawself(Canvas canvas, Paint paint) {
		if (isAlive) {
			canvas.drawBitmap(Config.bullet, locationX, locationY, paint);
			locationX+=xSpeed;
			if (locationX>Config.DeviceWidth) {
				isAlive=false;
			}
		}
	}

	public int getRaceWayIndex() {
		return raceWayIndex;
	}
	@Override
	public int getModelWidth() {
		return Config.bullet.getWidth();
	}
}
至于,子弹嘛,创建方式跟之前向日葵产生阳光的类似,不再赘述(记得判断定时)

GameView新加的方法

public void giveBirth2Bullet(int locationX, int locationY, int raceWayIndex) {
		synchronized (surfaceHolder) {
			gamelayout3.add(new Bullet(locationX
					+ Config.peaFrames[0].getWidth(), locationY, raceWayIndex));
		}
	}
然后在checkCollision里每个case里添加一个foreach结构

for (BaseModel model : gamelayout3) {
					if (Math.abs((model.getLocationX() + model.getModelWidth() / 2)
							- (zombie.getLocationX() + zombie.getModelWidth() / 2)) < (model
							.getModelWidth() + zombie.getModelWidth()) / 3) {
						if (model.getRaceWayIndex() == 0) {
							soundPoolHit.play(1, 1, 1, 0, 0, 1);
							model.setAlive(false);
							zombie.setLifePoint(zombie.getLifePoint() - 10);
							if (zombie.getLifePoint() == 0) {
								soundPoolDeath.play(1, 1, 1, 0, 0, 1);
							}
						}
					}
				}

蓝色背景的那块我们之后分析


接下来,写阳光点数。这边还是很简单的,定义一个config.sunMount

用drawText方法完成绘制(这文字是画上去的,醉了、、、、)

然后在三处加代码(seed里是否可以拖出Emplaceplant,成功放置plant后sunMount减少,sun消失在某处时收集阳光)

这边就不给代码了,没什么难点


然后是种子的冷却,这个还是有点意思的。在原图像上加一层透明蒙版来造成视觉的假象

主要用到paint.setAlpha方法(用来设置画笔透明度的)

seedFlower里的

public void drawself(Canvas canvas, Paint paint) {
		if (isAlive) {
			canvas.drawBitmap(Config.seed_flower, locationX, locationY, paint);
			paint.setAlpha(100);
			canvas.drawRect(locationX, locationY, locationX
					+ Config.seed_flower.getWidth(), locationY
					+ Config.coolTimeDistanceFlower, paint);
			if (Config.coolTimeDistanceFlower > 0) {<	//蒙版的高度(随时间变短)
			Config.coolTimeDistanceFlower -= coolTimeSpeed;//再装填速度
			}
			paint.setAlpha(255);
		}
记得在成功种下植物后,给蒙版高度初始化

好了,开始添加声音特效

Android里背景音乐用MediaPlayer来添加

不过,这边我就省事,跳过了这块

因为要使用SoundPool类(主要用于一些简单的音效,如击打时发出的声音)

声音文件统一放在res/raw文件夹下

这篇讲的简单易懂

http://blog.youkuaiyun.com/pku_android/article/details/7625868

soundPoolTouch = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
		soundPoolTouch.load(mContext, R.raw.error, 5);//初始化并且赋值


之前那块蓝色的


soundPoolHit.play(1, 1, 1, 0, 0, 1);

就是播放了

最后呢,就是给僵尸生命了。死亡的动画就不多说了,生成一个类,播放完将isAlive设成false即可

这边addZombie的时候,给个int作为lifePoint。使用set,get方法

在每次碰撞时减,当为0时算僵尸死亡(就是之前的蓝色背景的那块)

一块完整的改良后的checkCollision(case只给了一层)

public void checkCollision(Zombie zombie, int raceWay) {
		synchronized (surfaceHolder) {
			int i = 10;
			switch (raceWay) {
			case 0:
				for (BaseModel model : gameLayout4plant0) {
					if (Math.abs((model.getLocationX() + model.getModelWidth() / 2)
							- (zombie.getLocationX() + zombie.getModelWidth() / 2)) < (model
							.getModelWidth() + zombie.getModelWidth()) / 3) {
						if (model instanceof Plant) {
							model.setAlive(false);
						}
					}
				}
				for (BaseModel model : gamelayout3) {
					if (Math.abs((model.getLocationX() + model.getModelWidth() / 2)
							- (zombie.getLocationX() + zombie.getModelWidth() / 2)) < (model
							.getModelWidth() + zombie.getModelWidth()) / 3) {
						if (model.getRaceWayIndex() == 0) {
							soundPoolHit.play(1, 1, 1, 0, 0, 1);
							model.setAlive(false);
							zombie.setLifePoint(zombie.getLifePoint() - 10);
							if (zombie.getLifePoint() == 0) {
								soundPoolDeath.play(1, 1, 1, 0, 0, 1);
							}
						}
					}
				}
				break;

黄色为关键代码

当然了,我是直接在Zombie类里判定lifePoint==0就让isAlive为false的

此外,最好在带有onTouch的方法里加个防抖。方法就是定时产生阳光的那种,防止多次非主观意图的点击。

暂时就想到补充这么多,主要是没图,不然还可以加点有意思的。

源代码如下:(是改良后的)

http://pan.baidu.com/s/1dDIm11n

早上起来又加了点内容,僵尸吃菜的过程(并给植物生命)

思路跟昨天是一样的,加一个僵尸Attack(实体),碰到植物zombie死亡,zombieAttack出现,吃完了重新生成zombie

代码就不贴了,直接上链接

http://pan.baidu.com/s/1nt7gKR3






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值