📄 fish.java
字号:
package fishtank;import javax.microedition.lcdui.*;import com.nokia.mid.ui.*;import java.util.Random;public class Fish{ // These NUM values depend on the images actually included // in the MIDLET JAR file. private static final int TYPE_NUM = 3; private static final int FRAME_NUM = 2; private static final Image[][] fishImage; private static final int fishWidth, fishHeight; private static final Random random = new Random(); private final int type; private int frame; private int x, y, z; private int vx, vy; private int ticksSinceLastChangeX = 0; private int ticksSinceLastChangeY = 0; private int ticksSinceLastChangeD = 0; static { fishImage = new Image[TYPE_NUM][FRAME_NUM]; int i = 0; int k = 0; try { for (i = 0; i < TYPE_NUM; i ++) { for (k = 0; k < FRAME_NUM; k ++) { fishImage[i][k] = Image.createImage("/res/fish" + i + k + ".png"); } } } catch (java.io.IOException e) { fishImage[i][k] = null; } // This MIDlet implicitly assumes that all the Images // are the same width and height. fishWidth = fishImage[0][0].getWidth(); fishHeight = fishImage[0][0].getHeight(); } public Fish() { type = rand(TYPE_NUM); frame = rand(FRAME_NUM); x = rand(FishTankCanvas.waterWidth - fishWidth); y = rand(FishTankCanvas.waterHeight - fishHeight); vx = rand(2) * 2 - 1; // -1 or 1 vy = rand(3) - 1; // -1, 0 or 1 z = rand(FishTankCanvas.NUM_PLANES); } public void tick() { ticksSinceLastChangeX++; ticksSinceLastChangeY++; ticksSinceLastChangeD++; // mostly continue as we are, but sometimes randomly change if ((ticksSinceLastChangeX > 20) && (rand(20) == 0)) { vx = rand(2) * 2 - 1; // -1 or 1 ticksSinceLastChangeX = 0; } // if moving would take us off the left or right of the water, // reverse if (((vx < 0) && (x + vx < 0)) || ((vx > 0) && (x + fishWidth + vx > FishTankCanvas.waterWidth))) { vx = -vx; ticksSinceLastChangeX = 0; } // mostly continue as we are, but sometimes randomly change if ((ticksSinceLastChangeY > 20) && (rand(20) == 0)) { vy = rand(3) - 1; // -1, 0 or 1 ticksSinceLastChangeY = 0; } // if moving would take us off the top or bottom of the water, // either reverse or go level if (((vy < 0) && (y + vy < 0)) || ((vy > 0) && (y + fishHeight + vy > FishTankCanvas.waterHeight))) { if (rand(2) == 0) { vy = -vy; } else { vy = 0; } ticksSinceLastChangeY = 0; } // mostly continue as we are, but sometimes randomly change if ((ticksSinceLastChangeD > 10) && (rand(10) == 0)) { z += rand(3) - 1; // -1, 0 or 1 // If moving would take us out of the 'depth' of zs if ((z < 0) || (z == FishTankCanvas.NUM_PLANES)) { if (rand(2) == 0) { z = (z < 0) ? 0 : z - 1; } else { z = (z < 0) ? 1 : z - 2; } } ticksSinceLastChangeD = 0; } x += vx; y += vy; } public void draw(Graphics g) { DirectGraphics dg = DirectUtils.getDirectGraphics(g); Image img = fishImage[type][frame]; if (img != null) { if (vx < 0) { dg.drawImage(img, x, y, (Graphics.LEFT | Graphics.TOP), DirectGraphics.FLIP_HORIZONTAL); } else { dg.drawImage(img, x, y, (Graphics.LEFT | Graphics.TOP), 0); } } frame = (frame + rand(2)) % FRAME_NUM; } int getZ() { return z; } static int rand(int scale) { return (random.nextInt() << 1 >>> 1) % scale; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -