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

📄 fishes.java~4~

📁 利用Java 开发的一个手机版的潜艇游戏。游戏实现的重点就是屏幕的绘制滚动技术和碰撞检测技术
💻 JAVA~4~
字号:
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 "";
    }
}

⌨️ 快捷键说明

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