《飞机大战》安卓游戏开发源码(二)
本文章属于原创性文章,珍惜他人劳动成果,转载请注明出处:http://www.pm-road.com/index.php/2014/11/06/157/
前言:最 近闲来无事,而且也是因为刚接触安卓不久,所以萌生了以后在开发web 项目的过程中同样开发安卓手游,因前一段时间项目需要独立完成了一款功能型手机app ,该App比较简单,全是按钮操作,也就是Activity之间的相互传值与过渡,所以对安卓的基本知识有了一定了解,在我的印象当中,感觉安卓手游是比 较困难的,但是有困难就要克服,所以有了开发游戏的念头。
接着上一篇文章,框架搭建出来之后,我们把代码写一下:
以下是:框架:
按照思路先把Root代码写出来:
package com.pm_road.fireair.bean;
/**
*
* @ClassName: Root
* @Description: 所有实体类的父类
* @author liPeng
* @date 2014-11-5 上午10:20:19
*
*/
public class Root {
protected float x;// 位置x
protected float y;// 位置y
protected int srcPic;// 图标指向
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public int getSrcPic() {
return srcPic;
}
public void setSrcPic(int srcPic) {
this.srcPic = srcPic;
}
}
然后把飞机的实体类代码写出来:
package com.pm_road.fireair.bean;
/**
*
* @ClassName: Plane
* @Description: 飞机
* @author lipeng
* @date 2014-11-5 上午10:03:50
*
*/
public class Plane extends Root {
protected int life;// 生命
protected boolean shot;// 是否发射
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
public boolean isShot() {
return shot;
}
public void setShot(boolean shot) {
this.shot = shot;
}
}
接下来是你控制的飞机(实体类里没有任何代码,主要原因是继承的原因,写这样一个实体类,主要原因是用于区别敌机):
package com.pm_road.fireair.bean;
/**
* 控制的战机
* @ClassName: YourPlane
* @Description: 你的战机,继承飞机
* @author lipeng
* @date 2014-11-5 上午10:21:02
*
*/
public class YourPlane extends Plane{
}
敌机实体类代码:(里面也没有任何代码,主要原因是继承了plane,区别你控制的飞机)
package com.pm_road.fireair.bean;
/**
*
* @ClassName: EnemyPlane
* @Description: 敌机
* @author lipeng
* @date 2014-11-5 上午10:22:57
*
*/
public class EnemyPlane extends Plane{
}
飞机的子弹实体类:
package com.pm_road.fireair.bean;
/**
*
* @ClassName: Bullet
* @Description: 子弹
* @author liPeng
* @date 2014-11-5 上午10:24:48
*
*/
public class Bullet extends Root{
private boolean fly;//是否飞行
public boolean isFly() {
return fly;
}
public void setFly(boolean fly) {
this.fly = fly;
}
}
实体类写完之后:我们开始写相应的线程代码:首先是PlaneRunnable 线程,而在此线程中:飞机会发射子弹,在发射子弹的时候,会再起一个子弹线程BulletRunnable
package com.pm_road.fireair.thread;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.util.Log;
import android.view.SurfaceHolder;
import android.widget.ImageButton;
import com.pm_road.fireair.bean.Bullet;
import com.pm_road.fireair.bean.Plane;
import com.pm_road.fireair.common.CommonUtil;
/**
* 飞机线程
*
* @ClassName: PlaneThread
* @Description: 飞机线程
* @author lipeng
* @date 2014-11-5 上午10:27:40
*
*/
public class PlaneRunnable implements Runnable {
private Plane plane;// 此线程中的飞机
private Context context;// 上下文
private SurfaceHolder holder;
private ImageButton planeImg;// 飞机的图片
private int planeHalfWidth;//飞机宽度一半,美化射击效果
/**
* 构造方法
*
* @param context
*/
public PlaneRunnable(Context context, SurfaceHolder holder) {
super();
this.context = context;
this.holder = holder;
}
@Override
public void run() {
Canvas canvas = null;
while (plane.isShot()) {
canvas = holder.lockCanvas();
canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
Log.i(CommonUtil.THREAD_TAG, plane.getX()+”:”+plane.getY());
canvas.drawText(CommonUtil.BULLET, plane.getX() + planeHalfWidth, plane.getY(), paint);
holder.unlockCanvasAndPost(canvas);
//发射一个子弹
shotOneBullet();
Log.i(CommonUtil.THREAD_TAG, “plane射击状态为true”);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Log.i(CommonUtil.THREAD_TAG, “plane停止射击”);
}
/**
* 发射一枚子弹
*/
private void shotOneBullet(){
Bullet bullet = new Bullet();
bullet.setX(plane.getX()+planeHalfWidth);
bullet.setY(plane.getY());
bullet.setFly(true);//飞行中
BulletRunnable bulletRunnable = new BulletRunnable(context,holder);
//子弹装进线程
bulletRunnable.setBullet(bullet);
//子弹开始飞行
new Thread(bulletRunnable).start();
}
public Plane getPlane() {
return plane;
}
public void setPlane(Plane plane) {
this.plane = plane;
}
public ImageButton getPlaneImg() {
return planeImg;
}
public void setPlaneImg(ImageButton planeImg) {
this.planeImg = planeImg;
planeHalfWidth = planeImg.getWidth() / 2;
}
}
下面是BulletRunnable 的代码:,为了美化一个效果,所以当子弹没有碰到任何东西,飞出屏幕边界的时候,在屏幕边缘要进行爆炸,爆炸线程:BoomRunnable
package com.pm_road.fireair.thread;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.util.Log;
import android.view.SurfaceHolder;
import com.pm_road.fireair.bean.Bullet;
import com.pm_road.fireair.common.CommonUtil;
/**
* 子弹线程
*
* @ClassName: BulletRunnable
* @Description: 子弹线程
* @author lipeng
* @date 2014-11-5 上午10:33:19
*
*/
public class BulletRunnable implements Runnable {
private Bullet bullet;//当前的子弹
private Context context;//上下文
private SurfaceHolder holder;
public BulletRunnable(Context context,SurfaceHolder holder) {
super();
this.context = context;
this.holder = holder;
}
@Override
public void run() {
Canvas canvas = null;
while(bullet.isFly()){
canvas = holder.lockCanvas();
canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
canvas.drawText(CommonUtil.BULLET, bullet.getX(), bullet.getY()-70, paint);
holder.unlockCanvasAndPost(canvas);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
bullet.setY(bullet.getY() -70);
if(bullet.getY() <= 0){
//如果当前线程的Y超过了屏幕,则将该线程回收
bullet.setFly(false);
//爆炸效果
boom();
break;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i(CommonUtil.THREAD_TAG, “子弹飞行中”);
}
Log.i(CommonUtil.THREAD_TAG, “子弹线程结束”);
}
/**
* 爆炸效果
*/
private void boom(){
//声明爆炸效果
BoomRunnable boomRunnable = new BoomRunnable(context, holder);
boomRunnable.setBullet(bullet);
//爆炸
new Thread(boomRunnable).start();
}
public Bullet getBullet() {
return bullet;
}
public void setBullet(Bullet bullet) {
this.bullet = bullet;
}
}
以下是爆炸线程:BoomRunnable,该爆炸线程暂时没有调用图片,只是用几根线勾勒出一个简单效果 ,后期继续增加:
package com.pm_road.fireair.thread;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.view.SurfaceHolder;
import com.pm_road.fireair.bean.Bullet;
/**
* 爆炸线程
*
* @ClassName: BoomRunnable
* @Description: 爆炸线程
* @author lipeng
* @date 2014-11-5 下午02:43:39
*
*/
public class BoomRunnable implements Runnable {
private Bullet bullet;// 当前的子弹
private Context context;// 上下文
private SurfaceHolder holder;
public BoomRunnable(Context context,SurfaceHolder holder) {
super();
this.context = context ;
this.holder = holder;
}
@Override
public void run() {
Canvas canvas = holder.lockCanvas();
canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
//碰撞效果
canvas.drawLine(bullet.getX()-10, 2, bullet.getX()-15, 10, paint);
canvas.drawLine(bullet.getX()-15, 10, bullet.getX()-10, 12, paint);
canvas.drawLine(bullet.getX()-10, 12, bullet.getX()-13, 20, paint);
canvas.drawLine(bullet.getX()-13, 20, bullet.getX(), 12, paint);
canvas.drawLine(bullet.getX(), 12, bullet.getX()+13, 20, paint);
canvas.drawLine(bullet.getX()+13, 20, bullet.getX()+10, 12, paint);
canvas.drawLine(bullet.getX()+10, 12, bullet.getX()+15, 10, paint);
canvas.drawLine(bullet.getX()+15, 10, bullet.getX()+10, 2, paint);
holder.unlockCanvasAndPost(canvas);
}
public Bullet getBullet() {
return bullet;
}
public void setBullet(Bullet bullet) {
this.bullet = bullet;
}
}
写完实体类和线程之后,就应该把他和安卓的xml 进行结合。结合代码:http://www.pm-road.com/index.php/2014/11/06/161/
本文章属于原创性文章,珍惜他人劳动成果,转载请注明出处:http://www.pm-road.com/index.php/2014/11/06/157/