Android 射击类游戏 (一)

来源:互联网 发布:欧姆龙plc网络通信 编辑:程序博客网 时间:2024/04/26 14:54

以前见过一个C#写的射击类游戏。最近看了点android开发的东东,五一期间将游戏改成了android。

游戏的截图

废话就不多说了,开始设计。

1. 类设计图


上图就是主要牵涉到的一些类 红色是抽象类。看图基本一目了然了。

类里的方法,代码会贴出来(或者相关伪码)

2. 抽象类实现


Element.java

/***   www.91gangting.com*   桦骏岗亭**/
import android.graphics.Canvas;public abstract class Element {// 元素坐标protected int x, y;// 元素是否活着protected boolean live = true;public Element(int x, int y) {super();this.x = x;this.y = y;};/** * 画自己 *  * @param canvas *            画布 */protected abstract void Draw(Canvas canvas);public int getX() {return x;}public int getY() {return y;}public boolean isLive() {return live;}public void setLive(boolean live) {this.live = live;}}

RoAndMi.java

/***  www.91gangting.com*  桦骏岗亭**/import android.graphics.Rect;public abstract class RoAndMi extends Element {protected boolean good; // 好坏public final int WIDTH; // 宽度public final int HEIGHT; // 高度protected int XSPEED; // X坐标速度protected int YSPEED; // Y坐标速度public RoAndMi(int x, int y, int width, int height, int xspeed, int yspeed,boolean good) {super(x, y);this.good = good;this.WIDTH = width;this.HEIGHT = height;this.XSPEED = xspeed;this.YSPEED = yspeed;}/** * 获取角色(子弹)所在矩形 * @return */public Rect GetRect() {return new Rect(x, y, x + WIDTH, y + HEIGHT);}/** * 移动 */protected abstract void Move();public boolean isGood() {return good;}public void setGood(boolean good) {this.good = good;}}

Roles.java

/***  www.91gangting.com*  桦骏岗亭**/public abstract class Roles extends RoAndMi {    protected int life;// 角色生命    protected RolesDirection dir = RolesDirection.STOP;// 角色开始方向    public Roles(int x, int y, int width, int height, int xspeed, int yspeed,            int life, boolean good) {        super(x, y, width, height, xspeed, yspeed, good);        this.life = life;    }    /**     * 角色死亡方法     */    public void Death() {        this.life = 0;        this.live = false;        ThisBomb();// 引发爆    }    /**     * 引爆     */    protected abstract void ThisBomb();    /**     * 定义角色的流血方法     *      * @param i流血量     */    public void Bleeding(int i) {        if (this.live) {            life -= i;        }        if (life <= 0) {            Death();        }    }    /**     * 角色开火     */    protected abstract void Fire();    /**     * 角色移动     */    protected void Move() {        /*         * switch (dir) { case L: x -= XSPEED; break; case LU: x -= XSPEED; y -=         * YSPEED; break; case U: y -= YSPEED; break; case RU: x += XSPEED; y -=         * YSPEED; break; case R: x += XSPEED; break; case RD: x += XSPEED; y +=         * YSPEED; break; case D: y += YSPEED; break; case LD: x -= XSPEED; y +=         * YSPEED; break; case STOP: break; default: break; }         */    }    /**     *      * @param canvas画布     * @param bitmap图片文件     */    protected void Draw(Canvas canvas, Bitmap bitmap) {        canvas.drawBitmap(bitmap, x, y, null);    }    public int getLife() {        return life;    }    public void setLife(int life) {        this.life = life;    }}

Missiles.java

import java.util.HashMap;import android.graphics.Bitmap;import android.graphics.Canvas;import com.johnny.dragon.general.ConstantData;import com.johnny.dragon.general.MissileDirection;public abstract class Missiles extends RoAndMi {// 方向protected MissileDirection dir;// 威力protected int power;public Missiles(Roles role, int width, int height, int xspeed, int yspeed,boolean good, MissileDirection dir, int power) {super((int) (role.getX() + role.WIDTH / 2 - width / 2), (int) (role.getY() + role.HEIGHT / 2 - height / 2), width, height, xspeed,yspeed, good);this.dir = dir;this.power = power;}protected void Move() {switch (dir) {case L:x -= XSPEED;break;case LU:x -= (int) (ConstantData.COT30 * XSPEED);y -= YSPEED;break;case LUU:x -= (int) (ConstantData.COT60 * XSPEED);y -= YSPEED;break;case U:y -= YSPEED;break;case RUU:x += (int) (ConstantData.COT60 * XSPEED);y -= YSPEED;break;case RU:x += (int) (ConstantData.COT30 * XSPEED);y -= YSPEED;break;case R:x += XSPEED;break;case RD:x += (int) (ConstantData.COT30 * XSPEED);y += YSPEED;break;case RDD:x += (int) (ConstantData.COT60 * XSPEED);y += YSPEED;break;case D:y += YSPEED;break;case LDD:x -= (int) (ConstantData.COT30 * XSPEED);y += YSPEED;break;case LD:x -= (int) (ConstantData.COT60 * XSPEED);y += YSPEED;break;}}protected void Draw(Canvas canvas, HashMap<String, Bitmap> bimaps, int x,int y) {switch (dir) {case L:canvas.drawBitmap(bimaps.get("L"), x, y, null);break;case LU:canvas.drawBitmap(bimaps.get("LU"), x, y, null);break;case LUU:canvas.drawBitmap(bimaps.get("LUU"), x, y, null);break;case U:canvas.drawBitmap(bimaps.get("U"), x, y, null);break;case RUU:canvas.drawBitmap(bimaps.get("RUU"), x, y, null);break;case RU:canvas.drawBitmap(bimaps.get("RU"), x, y, null);break;case R:canvas.drawBitmap(bimaps.get("R"), x, y, null);break;case RD:canvas.drawBitmap(bimaps.get("RD"), x, y, null);break;case RDD:// g.DrawImage(images["RDD"], x, y);canvas.drawBitmap(bimaps.get("RDD"), x, y, null);break;case D:canvas.drawBitmap(bimaps.get("D"), x, y, null);break;case LDD:canvas.drawBitmap(bimaps.get("LDD"), x, y, null);break;case LD:canvas.drawBitmap(bimaps.get("LD"), x, y, null);break;}}public int getPower() {return power;}public void setPower(int power) {this.power = power;}}

draw()方法是 画出子弹在各个方向的位图。  比如:LD表示↙;D表示↓。。。


其他相关类

public class ConstantData {public static double COT30 = 1.732;// cot30public static double COT60 = 0.577;// cot60/** * 获取随机范围内的值 * @param start * @param end * @return */public static int getRandom(int start, int end) {if (start > end || start < 0 || end < 0) {return -1;}return (int) (Math.random() * (end - start + 1)) + start;}/** * 随机方向 MissileDirection * @param e1 * @param e2 * @return */public static MissileDirection getMissileDirection(int e1, int e2) {int len = MissileDirection.values().length; // 获取枚举元素个数if (e1 > e2 || e2 > len || e1 < 0) {return MissileDirection.D;}int i = getRandom(e1, e2);return MissileDirection.values()[i]; // 从枚举中随机获取一个值}}

// / <summary>// / 子弹的方向// / </summary>public enum MissileDirection {L, LU, LUU, U, RUU, RU, R, RD, RDD, D, LDD, LD, STOP}

// / <summary>// / 子弹的方向// / </summary>public enum RolesDirection {L, LU, U, RU, R, RD, D, LD, STOP}

抽象类完成了。

就可以创建 主人公Hero类,  Hero的子弹,坏蛋,坏蛋的子弹,主人公坏蛋爆炸的效果等。这些就是具体的类了


	
				
		
原创粉丝点击