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

📄 wormlink.java

📁 用JBuilder2005作的java的手机小游戏
💻 JAVA
字号:
package worm;
/**
 *
 * <p>Title: Worm</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author jimy
 * @version 1.0
 */

public class WormLink {

    private int x, y;
    private int len;
    private byte dir;

    private WormLink() {
    }

    public WormLink(int startX, int startY, int length, byte direction) {
    x = startX;
    y = startY;
    dir = direction;
    len = length-1;
    }

    /**
     * Create a worm link with a length of 1. This contstructor is
     * used when the worm changes direction.
     */
    public WormLink(int startX, int startY, byte direction) {
    this(startX, startY, 1, direction);
    }

    /**
     * Add one cell length to the head of this segment.
     */
    public void increaseLength() {
    len++;
    }

    /**
     * Remove one cell length from the tail of this segment.
     */
    public void decreaseLength() {
    len--;
    switch (dir) {
    case Worm.LEFT:
        x--;
        break;
    case Worm.RIGHT:
        x++;
        break;
    case Worm.UP:
        y--;
        break;
    case Worm.DOWN:
        y++;
        break;
    }
    }

    /**
     * Get the length, in cells, of this segment.
     */
    public int getLength() {
    return len+1;
    }

    /**
     * Get the X coordinate of the cell that contains the head of this
     * worm segment.
     */
    public int getX() {
    return x;
    }

    /**
     * Get the Y coordinate of the cell that contains the head of this
     * worm segment.
     */
    public int getY() {
    return y;
    }

    /**
     * Get the X coordinate of the cell that contains the tail of this
     * worm segment.
     */
    public int getEndX() {
    if (dir == Worm.LEFT)
        return x-len;
    if (dir == Worm.RIGHT)
        return x+len;
    return x;
    }

    /**
     * Get the Y coordinate of the cell that contains the tail of this
     * worm segment.
     */
    public int getEndY() {
    if (dir == Worm.DOWN)
        return y+len;
    if (dir == Worm.UP)
        return y-len;
    return y;
    }

    /**
     * Get the direction this worm segment is pointing.
     */
    public byte getDirection() {
    return dir;
    }

    /**
     * Returns true if the worm segment is at the given cell
     */
    public boolean contains(int x, int y) {
    switch (dir) {
    case Worm.LEFT:
        return ((y == this.y) && ((x <= this.x) && (x >= getEndX())));
    case Worm.RIGHT:
        return ((y == this.y) && ((x >= this.x) && (x <= getEndX())));
    case Worm.UP:
        return ((x == this.x) && ((y <= this.y) && (y >= getEndY())));
    case Worm.DOWN:
        return ((x == this.x) && ((y >= this.y) && (y <= getEndY())));
    }
    return false;
    }

    /**
     * Debug method.
     */
    public String toString() {
    String dirString;
    switch (dir) {
    case Worm.LEFT:
        dirString = "Left";
        break;
    case Worm.RIGHT:
        dirString = "Right";
        break;
    case Worm.UP:
        dirString = "Up";
        break;
    case Worm.DOWN:
        dirString = "Down";
        break;
    default:
        dirString = "UNKNOWN -- " + dir;
    }

    return " pos == [" + x + "," + y + "]" +
        " - [" + getEndX() + "," + getEndY() + "]" +
        "   len == " + getLength() +
        "   dir == " + dirString;
    }
}

⌨️ 快捷键说明

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