如何构建自己的游戏框架并且制作游戏(二)(附源码)
原文地址:http://www.cnblogs.com/feifei1010/archive/2012/08/24/2653768.html
?
现在我们进行第二篇教学,有了框架我们可以自由地在屏幕上绘制我们想要的东西了。背景是用的BackGround组件,人物和子弹,还有精灵都是用的Sprite精灵组件
?
GameActivity类,游戏的主Activity类,在这里继承基类,只需要将界面替换为GameView就可以了。
?package
com.mocn.airBottle;
?import
android.R;
import
android.os.Bundle;
?import
com.mocn.framework.BaseActivity;
import
com.mocn.framework.BaseView;
?/**
?
* 游戏的Activity类
?
*
?
* @author Administrator
?
*
?
*/
public
class
PlaneGameActivity
extends
BaseActivity {
?????????
public
BaseView baseView;
// 引用BaseView
?????????
public
GameView gameView;
// 引用GameView
?????????
@Override
????????
public
void
onCreate(Bundle savedInstanceState) {
????????????????
super
.onCreate(savedInstanceState);
????????????????
baseView =
new
GameView(
this
);
// 得到baseView对象
????????????????
setContentView(baseView);;
// 设置显示界面为baseView
????????
}
}
GameView类,游戏的主界面绘制类,在这里我们要绘制飞机和敌军,判断检测子弹和敌军的碰撞事件
?package
com.mocn.airBottle;
?import
android.content.Context;
import
android.graphics.Canvas;
import
android.graphics.Paint;
import
android.view.MotionEvent;
import
android.view.View;
import
android.view.View.OnTouchListener;
?import
com.mocn.framework.BackGroundLayer;
import
com.mocn.framework.BaseView;
import
com.mocn.framework.Utils;
?public
class
GameView
extends
BaseView
implements
OnTouchListener {
????????
BackGroundLayer backLayer;
// 背景组件
????????
Plane plane;
// 飞机类
????????
public
boolean
pressPlane =
false
;
?????????
public
GameView(Context context) {
????????????????
super
(context);
????????????????
setOnTouchListener(
this
);
????????????????
// 绘制背景
????????????????
backLayer =
new
BackGroundLayer(Utils.getBitmap(
"game/bg.png"
),
800
,
????????????????????????????????
480
);
????????????????
backLayer.setPosition(
0
,
0
);
?????????????????
// 绘制飞机
????????????????
plane =
new
Plane(Utils.getBitmap(
"game/plane.png"
),
150
,
179
);
????????????????
plane.setPosition(
40
,
300
);
?????????
}
?????????
@Override
????????
public
void
drawSurfaceView(Canvas canvas, Paint paint) {
????????????????
super
.drawSurfaceView(canvas, paint);
????????????????
GameData.bulletsAndEnemy();
// 判断子弹和敌军的碰撞
????????????????
GameData.createEnemy();
// 创建敌军
????????
}
?????????
/**
?????????
* 触摸事件执行的方法
?????????
*/
????????
@Override
????????
public
boolean
onTouch(View v, MotionEvent event) {
????????????????
if
(event.getAction() == MotionEvent.ACTION_DOWN) {
// 按下执行的事件
????????????????????????
// 按下点中飞机执行的方法
????????????????????????
if
(Utils.inRect(plane.x - plane.w /
2
, plane.y - plane.h /
2
,
????????????????????????????????????????
plane.w, plane.h, event.getX(), event.getY())) {
????????????????????????????????
pressPlane =
true
;
// 将飞机可移动设置为true
????????????????????????
}
????????????????
}
else
if
(event.getAction() == MotionEvent.ACTION_MOVE) {
// 拖动执行的事件
????????????????????????
if
(pressPlane) {
????????????????????????????????
plane.setPosition(event.getX(), event.getY());
// 将飞机的坐标设置为拖动时的坐标
????????????????????????
}
????????????????
}
else
if
(event.getAction() == MotionEvent.ACTION_UP) {
// 释放按键时执行的方法
????????????????????????
pressPlane =
false
;
// 将飞机可移动设置为false
????????????????
}
????????????????
return
true
;
????????
}
?}
Plane,飞机类,在飞机类中我们给它顺便装上子弹
?package
com.mocn.airBottle;
?import
android.graphics.Bitmap;
import
android.graphics.Canvas;
import
android.graphics.Paint;
?import
com.mocn.framework.LayerManager;
import
com.mocn.framework.Sprite;
import
com.mocn.framework.Utils;
?/**
?
* 飞机类
?
*
?
* @author Administrator
?
*
?
*/
public
class
Plane
extends
Sprite {
????????
public
int
timeSpan =
1000
;
// 飞机的发子弹间隔时间
????????
public
long
updateTime =
0
;
// 飞机的更新时间
?????????
public
Plane(Bitmap bitmap,
int
w,
int
h) {
????????????????
super
(bitmap, w, h,
true
);
????????
}
?????????
@Override
????????
public
void
drawSelf(Canvas canvas, Paint paint) {
????????????????
super
.drawSelf(canvas, paint);
????????????????
// 给飞机装上子弹
????????????????
if
(System.currentTimeMillis() > updateTime) {
????????????????????????
// 创建子弹
????????????????????????
Bullet b =
new
Bullet(Utils.getBitmap(
"game/buttle.png"
),
50
,
50
);
????????????????????????
b.setPosition(x + w /
2
, y);
// 设置子弹的位置
????????????????????????
b.setAction(
"bb"
);
// 设置子弹的动作
????????????????????????
LayerManager.insert(b,
this
);
// 在飞机层的前面插入子弹层
????????????????????????
GameData.bullets.add(b);
// 添加一颗子弹
????????????????????????
updateTime = System.currentTimeMillis() + timeSpan;
????????????????
}
????????
}
?}
Buttle类,子弹类,在这个类中我们要设置子弹的动作和路线
?
?package
com.mocn.airBottle;
?import
android.graphics.Bitmap;
import
android.graphics.Canvas;
import
android.graphics.Paint;
?import
com.mocn.framework.LayerManager;
import
com.mocn.framework.Sprite;
?/**
?
* 子弹类,继承精灵类
?
*
?
* @author Administrator
?
*
?
*/
public
class
Bullet
extends
Sprite {
????????
public
int
speed =
10
;
// 子弹的速度
?????????
public
Bullet(Bitmap bitmap,
int
w,
int
h) {
????????????????
super
(bitmap, w, h,
false
);
????????????????
addAction(
"bb"
,
new
int
[] {
0
,
1
,
2
,
3
,
4
,
5
},
new
int
[] {
50
,
50
,
50
,
????????????????????????????????
50
,
50
,
50
});
????????
}
?????????
@Override
????????
public
void
drawSelf(Canvas canvas, Paint paint) {
????????????????
super
.drawSelf(canvas, paint);
????????????????
x += speed;
// 让子弹移动
????????????????
// 当子弹超出屏幕移除子弹
????????????????
if
(x >=
800
) {
????????????????????????
LayerManager.deleteLayer(
this
);
????????????????????????
GameData.bullets.remove(
this
);
????????????????
}
?????????
}
?}
?
Enemy类,敌军类,在这个类中我们要给它设置动作和路线
?package
com.mocn.airBottle;
?import
android.graphics.Bitmap;
import
android.graphics.Canvas;
import
android.graphics.Paint;
?import
com.mocn.framework.Sprite;
?/**
?
* 敌军的绘制类
?
*
?
* @author Administrator
?
*
?
*/
public
class
Enemy
extends
Sprite {
?????????
public
Enemy(Bitmap bitmap,
int
w,
int
h) {
????????????????
super
(bitmap, w, h,
true
);
????????????????
addAction(
"left"
,
new
int
[] {
0
,
1
,
2
,
3
,
4
},
new
int
[] {
50
,
50
,
50
,
????????????????????????????????
50
,
50
});
// 设置动作集合
????????
}
?????????
@Override
????????
public
void
drawSelf(Canvas canvas, Paint paint) {
????????????????
super
.drawSelf(canvas, paint);
????????????????
x -=
2
;
// 敌军往左移动
????????
}
?}
最后一个,GameData类,游戏数据类,在这里我们有创建敌军的方法和子弹和敌军的碰撞处理
?package
com.mocn.airBottle;
?import
java.util.Vector;
?import
com.mocn.framework.LayerManager;
import
com.mocn.framework.Utils;
?/**
?
* 游戏数据类,用于控制敌军的出现和子弹与敌军的碰撞
?
*
?
* @author Administrator
?
*
?
*/
public
class
GameData {
????????
public
static
Vector<Bullet> bullets =
new
Vector<Bullet>();
????????
public
static
Vector<Enemy> enemys =
new
Vector<Enemy>();
????????
private
static
long
updateTime;
// 设置更新时间
?????????
/**
?????????
* 创建敌军的方法
?????????
*/
????????
public
static
void
createEnemy() {
????????????????
if
(System.currentTimeMillis() < updateTime)
????????????????????????
return
;
????????????????
updateTime = System.currentTimeMillis() +
3000
;
// 设置更新时间,3秒出一只
????????????????
// 创建敌军
????????????????
Enemy e =
new
Enemy(Utils.getBitmap(
"game/enemy.png"
),
72
,
69
);
????????????????
e.setPosition(
800
, (
float
) Math.random() *
480
);
// 设置敌军的位置,x坐标固定,y坐标随机
????????????????
e.setAction(
"left"
);
// 设置动作
????????????????
enemys.add(e);
// 在Vector中添加一个敌军
????????
}
?????????
/**
?????????
* 子弹和敌军碰撞后的处理方法
?????????
*/
????????
public
static
void
bulletsAndEnemy() {
????????????????
// 遍历子弹和敌军
????????????????
for
(
int
i = bullets.size() -
1
; i >=
0
; i--) {
????????????????????????
Bullet b = bullets.elementAt(i);
????????????????????????
for
(
int
j = enemys.size() -
1
; j >=
0
; j--) {
????????????????????????????????
Enemy e = enemys.elementAt(j);
????????????????????????????????
// 调用工具类中的碰撞检测方法
????????????????????????????????
boolean
t = Utils.colliseWidth(b.x - b.w /
2
, b.y - b.h /
2
,
????????????????????????????????????????????????
b.w, b.h, e.x - e.w /
2
, e.y - e.h /
2
, e.w, e.h);
????????????????????????????????
if
(t) {
????????????????????????????????????????
bullets.remove(b);
// 移除Vector中的子弹
????????????????????????????????????????
enemys.remove(e);
// 移除Vector中的敌军
????????????????????????????????????????
LayerManager.deleteLayer(b);
// 移除组件中的子弹
????????????????????????????????????????
LayerManager.deleteLayer(e);
// 移除组件中的敌军
????????????????????????????????
}
????????????????????????
}
????????????????
}
????????
}
}
至此,整个游戏教学完成。
这是一个简单的游戏,但是代码量却不少,特别是框架部分,但是这个框架可以说是很基础的,但却囊括了游戏的很多方面,如果以后有机会去游戏公司,框架部分会更复杂,现在的辛苦只是为了以后的轻松而准备,我相信,如果一个人可以克服困难,看懂它的话,那他离真正的游戏大师也差不多远了,真正的机遇留个有准备的人!
源码下载:
?http://files.cnblogs.com/feifei1010/AirBottle2.zip