⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fishes.java~8~

📁 利用Java 开发的一个手机版的潜艇游戏。游戏实现的重点就是屏幕的绘制滚动技术和碰撞检测技术
💻 JAVA~8~
字号:
package mysubmarine;

import java.util.Vector;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.Image;

/**
 * <p>Title: 一个简单的潜艇游戏</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: Star Group</p>
 *
 * @author not attributable
 * @version 1.0
 */
public class Fishes {
    public final static int nFishTypes = 7; //鱼 种类
    private final static int nFishFrames = 3; //鱼 图片分割帧数

//出现秒跳的频率
    public final static int MAX_FRAME_CHANGED_TICK = 2000 /
            MySubmarineCanvas.MILLIS_PER_TICK;

    /** 游戏域中每个子动画运动域中 鱼群 出现的密度
     * <code>FISH_FACTOR</code> 的注释
     */
    private final static int FISH_FACTOR = 2;
    private final Vector fishes = new Vector();
    private final LayerManager layerManager;

    private int x;
    private int y;
    private int w;
    private int h;
    private int vx;
    private int vy;
    private int xBound;
    private int yBound;
    private int wBound;
    private int hBound;

    private int tickSinceLastChangeX = 0;
    private int tickSinceLastChangeY = 0;

    private int tickCount = 0; //当前鱼类的秒触发次数

    /**
     * @param layerManager       图层管理调用器
     * @param fishTypeId         鱼类型ID
     *
     * @param x                  类图层横坐标
     * @param y                  类图层纵坐标
     * @param w                  类图层宽度                 初始化为WORLD_WIDTH / 4
     * @param h                  类图层高度                 初始化为WORLD_HEIGHT / 4
     * @param vx                 类图层移动x轴速度           初始化为randomVelocity(TILE_WIDTH / 4)
     * @param vy                 类图层移动y轴速度           初始化为randomVelocity(TILE_HEIGHT / 4)
     * @param xBound             类图层运动区域 -- 起点x坐标  初始化为0
     * @param yBound             类图层运动区域 -- 起点y坐标  初始化为0
     * @param wBound             类图层运动区域 -- 终点x坐标  初始化为WORLD_WIDTH
     * @param hBound             类图层运动区域 -- 终点y坐标  初始化为WORLD_HEIGHT
     * @param layerManager       图层管理调用器
     */

    public Fishes(LayerManager layerManager, int fishTypeId, int x, int y,
                        int w, int h, int vx, int vy,
                        int xBound, int yBound, int wBound, int hBound) {
              this.layerManager = layerManager;

              this.x = x;
              this.y = y;
              this.w = w;
              this.h = h;
              this.vx = vx;
              this.vy = vy;
              this.xBound = xBound;
              this.yBound = yBound;
              this.wBound = wBound;
              this.hBound = hBound;

              //初始化鱼种类(确定鱼的图片)
              String filename = fishFilename(fishTypeId);

              try {
                  Image image = Image.createImage(filename);

                  //得到鱼图片每帧的大小, 并根据面积及面积鱼类密度计算鱼集合的数量大小
                  int frameWidth = image.getWidth() / nFishFrames;
                  int frameHeight = image.getHeight();
                  int n = (w * h) / (FISH_FACTOR * (frameWidth * frameHeight));

                  //生成每种鱼的活动子域(运动域)里面, 有几条鱼(几个Sprite图层)
                  for (int i = 0; i < n; i++) {
                      int frameChangedTick = 8;

                      //鱼类数组添加一条"新鱼", 另外图层也添加一条新鱼
                      MoveableSprite fish = new MoveableSprite(image, frameWidth,
                              frameHeight,
                              frameChangedTick,
                              x + SubMIDlet.createRandom(w),
                              y + SubMIDlet.createRandom(h),
                              vx, vy,
                              xBound, yBound, wBound, hBound,
                              layerManager);

                      //fishes只是一个普通的Vector数组
                      //用来存储该类鱼集合中有几条鱼
                      fishes.addElement(fish);
                      layerManager.append(fish);
                  }

              } catch (Exception e) {
                  e.printStackTrace();
              }

          }

    /**
     * fishFilename
     *
     * @param fishTypeId int
     * @return String
     */
    private static String fishFilename(int fishTypeId) {
        return "/res/fishType" + fishTypeId + ".png";
    }

    /** 根据输入参数速率, 随即赋予一个方向, 产生一个随机速度
     * @param factor
     * @return
     */
    public static int randomVelocity(int factor) {
        //产生方向 -1, 0 或者是 1
        int v = ResObj.createRandom(3) - 1;
        return (v * ResObj.createRandom(factor));
    }

    /**
     * 鱼类集合 秒触发 的动画动作
     */
    public void tick() {
        this.tickCount++;

        //鱼群集合 运动
        this.movePosition();

        for (int i = 0; i < fishes.size(); i++) {
            //每条鱼独自运动(切换到下一个画面帧, 再移动位置)
            MoveableSprite fish = (MoveableSprite) fishes.elementAt(i);
            fish.tick(tickCount);
        }
    }

    /**
     * 没有传入参数的movePosition, 表明没有从外界接受移动指令
     * 而是自身自动移动
     */
    protected void movePosition() {
        this.tickSinceLastChangeX++;
        this.tickSinceLastChangeY++;

        //偶尔改变运行速度
        if ((this.tickSinceLastChangeX > 20) &&
            (ResObj.createRandom(20) == 0)) {
            //初试 速率 在1/4单元速度以内
            vx = randomVelocity(MySubmarineCanvas.TILE_WIDTH / 4);
            this.tickSinceLastChangeX = 0;
        }
        if ((this.tickSinceLastChangeY > 20) &&
            (ResObj.createRandom(20) == 0)) {
            vy = randomVelocity(MySubmarineCanvas.TILE_HEIGHT / 4);
            this.tickSinceLastChangeY = 0;
        }

        //当位置将要超出边界时, 速度反向, 并重新开始随机速度计算
        if (((vx < 0) && (x + vx < xBound)) ||
            ((vx > 0) && (x + w + vx > (xBound + wBound)))) {
            vx = -vx;
            this.tickSinceLastChangeX = 0;
        }
        if (((vy < 0) && (y + vy < yBound)) ||
            ((vy > 0) && (y + h + vy > (yBound + hBound)))) {
            if (ResObj.createRandom(2) == 0) {
                vy = -vy;
            } else {
                vy = 0;
            }
            this.tickSinceLastChangeY = 0;
        }

        //改变位置
        //首先, 是此类鱼的运动域FishCollection以 randomVelocity(Tile_Wide / 4) 的速度改变一次位置
        //接着, 在是此类中每一条鱼(MoveableSprite)以速率 1 或者 0 改变一次速度
        x = x + vx;
        y = y + vy;
        for (int i = 0; i < fishes.size(); i++) {
            MoveableSprite fish = (MoveableSprite) fishes.elementAt(i);
            fish.setBound(x, y, w, h);
            fish.movePosition();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -