如何构建自己的游戏框架并且制作游戏(二)(附源码)

来源:互联网 发布:java短信接口开发实例 编辑:程序博客网 时间:2024/05/16 13:52
现在我们进行第二篇教学,有了框架我们可以自由地在屏幕上绘制我们想要的东西了。背景是用的BackGround组件,人物和子弹,还有精灵都是用的Sprite精灵组件

6 
7 

GameActivity类,游戏的主Activity类,在这里继承基类,只需要将界面替换为GameView就可以了。
  1. package com.mocn.airBottle;

  2. import android.R;
  3. import android.os.Bundle;

  4. import com.mocn.framework.BaseActivity;
  5. import com.mocn.framework.BaseView;

  6. /**
  7. * 游戏的Activity类

  8. * @author Administrator

  9. */
  10. public class PlaneGameActivity extends BaseActivity {

  11.         public BaseView baseView;// 引用BaseView

  12.         public GameView gameView;// 引用GameView

  13.         @Override
  14.         public void onCreate(Bundle savedInstanceState) {
  15.                 super.onCreate(savedInstanceState);
  16.                 baseView = new GameView(this);// 得到baseView对象
  17.                 setContentView(baseView);;// 设置显示界面为baseView
  18.         }
  19. }
复制代码
GameView类,游戏的主界面绘制类,在这里我们要绘制飞机和敌军,判断检测子弹和敌军的碰撞事件
  1. package com.mocn.airBottle;

  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Paint;
  5. import android.view.MotionEvent;
  6. import android.view.View;
  7. import android.view.View.OnTouchListener;

  8. import com.mocn.framework.BackGroundLayer;
  9. import com.mocn.framework.BaseView;
  10. import com.mocn.framework.Utils;

  11. public class GameView extends BaseView implements OnTouchListener {
  12.         BackGroundLayer backLayer;// 背景组件
  13.         Plane plane;// 飞机类
  14.         public boolean pressPlane = false;

  15.         public GameView(Context context) {
  16.                 super(context);
  17.                 setOnTouchListener(this);
  18.                 // 绘制背景
  19.                 backLayer = new BackGroundLayer(Utils.getBitmap("game/bg.png"), 800,
  20.                                 480);
  21.                 backLayer.setPosition(0, 0);

  22.                 // 绘制飞机
  23.                 plane = new Plane(Utils.getBitmap("game/plane.png"), 150, 179);
  24.                 plane.setPosition(40, 300);

  25.         }

  26.         @Override
  27.         public void drawSurfaceView(Canvas canvas, Paint paint) {
  28.                 super.drawSurfaceView(canvas, paint);
  29.                 GameData.bulletsAndEnemy();// 判断子弹和敌军的碰撞
  30.                 GameData.createEnemy();// 创建敌军
  31.         }

  32.         /**
  33.          * 触摸事件执行的方法
  34.          */
  35.         @Override
  36.         public boolean onTouch(View v, MotionEvent event) {
  37.                 if (event.getAction() == MotionEvent.ACTION_DOWN) {// 按下执行的事件
  38.                         // 按下点中飞机执行的方法
  39.                         if (Utils.inRect(plane.x - plane.w / 2, plane.y - plane.h / 2,
  40.                                         plane.w, plane.h, event.getX(), event.getY())) {
  41.                                 pressPlane = true;// 将飞机可移动设置为true
  42.                         }
  43.                 } else if (event.getAction() == MotionEvent.ACTION_MOVE) {// 拖动执行的事件
  44.                         if (pressPlane) {
  45.                                 plane.setPosition(event.getX(), event.getY());// 将飞机的坐标设置为拖动时的坐标
  46.                         }
  47.                 } else if (event.getAction() == MotionEvent.ACTION_UP) {// 释放按键时执行的方法
  48.                         pressPlane = false;// 将飞机可移动设置为false
  49.                 }
  50.                 return true;
  51.         }

  52. }
复制代码
Plane,飞机类,在飞机类中我们给它顺便装上子弹
  1. package com.mocn.airBottle;

  2. import android.graphics.Bitmap;
  3. import android.graphics.Canvas;
  4. import android.graphics.Paint;

  5. import com.mocn.framework.LayerManager;
  6. import com.mocn.framework.Sprite;
  7. import com.mocn.framework.Utils;

  8. /**
  9. * 飞机类

  10. * @author Administrator

  11. */
  12. public class Plane extends Sprite {
  13.         public int timeSpan = 1000;// 飞机的发子弹间隔时间
  14.         public long updateTime = 0;// 飞机的更新时间

  15.         public Plane(Bitmap bitmap, int w, int h) {
  16.                 super(bitmap, w, h, true);
  17.         }

  18.         @Override
  19.         public void drawSelf(Canvas canvas, Paint paint) {
  20.                 super.drawSelf(canvas, paint);
  21.                 // 给飞机装上子弹
  22.                 if (System.currentTimeMillis() > updateTime) {
  23.                         // 创建子弹
  24.                         Bullet b = new Bullet(Utils.getBitmap("game/buttle.png"), 50, 50);
  25.                         b.setPosition(x + w / 2, y);// 设置子弹的位置
  26.                         b.setAction("bb");// 设置子弹的动作
  27.                         LayerManager.insert(b, this);// 在飞机层的前面插入子弹层
  28.                         GameData.bullets.add(b);// 添加一颗子弹
  29.                         updateTime = System.currentTimeMillis() + timeSpan;
  30.                 }
  31.         }

  32. }
复制代码
Buttle类,子弹类,在这个类中我们要设置子弹的动作和路线
  1. package com.mocn.airBottle;

  2. import android.graphics.Bitmap;
  3. import android.graphics.Canvas;
  4. import android.graphics.Paint;

  5. import com.mocn.framework.LayerManager;
  6. import com.mocn.framework.Sprite;

  7. /**
  8. * 子弹类,继承精灵类

  9. * @author Administrator

  10. */
  11. public class Bullet extends Sprite {
  12.         public int speed = 10;// 子弹的速度

  13.         public Bullet(Bitmap bitmap, int w, int h) {
  14.                 super(bitmap, w, h, false);
  15.                 addAction("bb", new int[] { 0, 1, 2, 3, 4, 5 }, new int[] { 50, 50, 50,
  16.                                 50, 50, 50 });
  17.         }

  18.         @Override
  19.         public void drawSelf(Canvas canvas, Paint paint) {
  20.                 super.drawSelf(canvas, paint);
  21.                 x += speed;// 让子弹移动
  22.                 // 当子弹超出屏幕移除子弹
  23.                 if (x >= 800) {
  24.                         LayerManager.deleteLayer(this);
  25.                         GameData.bullets.remove(this);
  26.                 }

  27.         }

  28. }
复制代码
Enemy类,敌军类,在这个类中我们要给它设置动作和路线
  1. package com.mocn.airBottle;

  2. import android.graphics.Bitmap;
  3. import android.graphics.Canvas;
  4. import android.graphics.Paint;

  5. import com.mocn.framework.Sprite;

  6. /**
  7. * 敌军的绘制类

  8. * @author Administrator

  9. */
  10. public class Enemy extends Sprite {

  11.         public Enemy(Bitmap bitmap, int w, int h) {
  12.                 super(bitmap, w, h, true);
  13.                 addAction("left", new int[] { 0, 1, 2, 3, 4 }, new int[] { 50, 50, 50,
  14.                                 50, 50 });// 设置动作集合
  15.         }

  16.         @Override
  17.         public void drawSelf(Canvas canvas, Paint paint) {
  18.                 super.drawSelf(canvas, paint);
  19.                 x -= 2;// 敌军往左移动
  20.         }

  21. }
复制代码
最后一个,GameData类,游戏数据类,在这里我们有创建敌军的方法和子弹和敌军的碰撞处理
  1. package com.mocn.airBottle;

  2. import java.util.Vector;

  3. import com.mocn.framework.LayerManager;
  4. import com.mocn.framework.Utils;

  5. /**
  6. * 游戏数据类,用于控制敌军的出现和子弹与敌军的碰撞

  7. * @author Administrator

  8. */
  9. public class GameData {
  10.         public static Vector<Bullet> bullets = new Vector<Bullet>();
  11.         public static Vector<Enemy> enemys = new Vector<Enemy>();
  12.         private static long updateTime;// 设置更新时间

  13.         /**
  14.          * 创建敌军的方法
  15.          */
  16.         public static void createEnemy() {
  17.                 if (System.currentTimeMillis() < updateTime)
  18.                         return;
  19.                 updateTime = System.currentTimeMillis() + 3000;// 设置更新时间,3秒出一只
  20.                 // 创建敌军
  21.                 Enemy e = new Enemy(Utils.getBitmap("game/enemy.png"), 72, 69);
  22.                 e.setPosition(800, (float) Math.random() * 480);// 设置敌军的位置,x坐标固定,y坐标随机
  23.                 e.setAction("left");// 设置动作
  24.                 enemys.add(e);// 在Vector中添加一个敌军
  25.         }

  26.         /**
  27.          * 子弹和敌军碰撞后的处理方法
  28.          */
  29.         public static void bulletsAndEnemy() {
  30.                 // 遍历子弹和敌军
  31.                 for (int i = bullets.size() - 1; i >= 0; i--) {
  32.                         Bullet b = bullets.elementAt(i);
  33.                         for (int j = enemys.size() - 1; j >= 0; j--) {
  34.                                 Enemy e = enemys.elementAt(j);
  35.                                 // 调用工具类中的碰撞检测方法
  36.                                 boolean t = Utils.colliseWidth(b.x - b.w / 2, b.y - b.h / 2,
  37.                                                 b.w, b.h, e.x - e.w / 2, e.y - e.h / 2, e.w, e.h);
  38.                                 if (t) {
  39.                                         bullets.remove(b);// 移除Vector中的子弹
  40.                                         enemys.remove(e);// 移除Vector中的敌军
  41.                                         LayerManager.deleteLayer(b);// 移除组件中的子弹
  42.                                         LayerManager.deleteLayer(e);// 移除组件中的敌军
  43.                                 }
  44.                         }
  45.                 }
  46.         }
  47. }
复制代码
至此,整个游戏教学完成。

源码下载:

http://download.csdn.net/detail/mocn26169/4503357

原创粉丝点击