fish.java

来自「j2me的一款钓鱼的游戏」· Java 代码 · 共 162 行

JAVA
162
字号
package Kidfishing;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import tool.Tools;

public class Fish {

	static final int _LEFT = 3;

	static final int _RIGHT = 4;

	static final int _UP = 5;

	static final int SMALL = 0;

	static final int MIDDLE = 1;

	static final int BIG = 2;

	private int direct;// 鱼方向

	private int COIN;// 鱼奖励

	private float v;// 鱼速度

	Image image[];// 鱼图象数组

	Image upimage[];// 上钩图象

	int x, y;// 鱼坐标

	boolean isfish;// 是否被钓

	int frame;// 图象贞

	Fish(Image image[], Image upi[], int x, int y, float speed, int count,
			int direct) {
		this.image = image;
		upimage = upi;
		this.x = x;
		this.y = y;
		v = speed;
		COIN = count;
		this.direct = direct;
	}

	// 关于移动
	boolean isMove() {
		if (direct != 0 && !isfish) {
			return true;
		}
		return false;
	}

	// 咬钩
	boolean isCatch(int x, int y) {

		switch (direct) {
		case _LEFT:

			if (Tools.isCollRect(x, y, 1, 1, this.x, this.y - 6, 5, 5)) {

				isfish = true;
				return true;
			}
			break;
		case _RIGHT:
			if (Tools.isCollRect(x, y, 1, 1, this.x + 20, this.y - 6, 5, 5)) {

				isfish = true;
				return true;
			}
			break;
		}
		return false;
	}

	void move() {

		switch (direct) {
		case _LEFT:
			if (isMove() == true) {
				x -= v;
				nextFrame();

				if (x <= 0) {
					direct = _RIGHT;
					frame = 3;
				}
			}
			break;
		case _RIGHT:
			if (isMove() == true) {
				x += v;
				nextFrame();
				if (x + 40 >= Mycan.WID) {
					direct = _LEFT;
					frame = 0;
				}
			}
			break;
		case _UP:

			y -= 2;
			nextFrame();
			if (y - 30 <= 0) {
				direct = 0;
			}
			break;
		}
	}

	public void drawFish(Graphics g) {
		switch (direct) {
		case _LEFT:

			g.drawImage(image[frame], x, y, Graphics.TOP | Graphics.LEFT);

			break;
		case _RIGHT:

			g.drawImage(image[frame], x, y, Graphics.TOP | Graphics.LEFT);

			break;
		case _UP:

			g.drawImage(upimage[frame], x, y, Graphics.TOP | Graphics.LEFT);
		}

	}

	public void nextFrame() {

		if (direct == _LEFT && frame++ >= 2) {
			frame = 0;
		} else if (direct == _RIGHT && frame++ >= 5) {
			frame = 3;
		} else if (direct == _UP && frame++ >= 1) {
			frame = 0;
		}
	}

	void setdirect(int c) {
		direct = c;
	}

	void setXY(int a, int b) {
		x = a;
		y = b;
	}

	void turnV(int a) {
		v = a;
	}

	int getCoin() {
		return COIN;
	}
}

⌨️ 快捷键说明

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