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

📄 wormfood.java

📁 用JBuilder2005作的java的手机小游戏
💻 JAVA
字号:
package worm;

import javax.microedition.lcdui.Graphics;

/**
 * WormFood is a food item that the worm can eat. It is placed at
 * a pseudo randomly generated location in the pit.
 */
public class WormFood {
    /** Current food location x coordinate in cells. */
    private int cellX;
    /** Current food location y coordinate in cells. */
    private int  cellY;

    /*
     * These members are for convenience only. They are generated using
     * expensive multiplication operations and are cached for quick access
     */
    /** Current food location x coordinate in pixels. */
    private int x;
    /** Current food location y coordinate in pixels. */
    private int y;
    /** Size of arena in cells. */
    private long arenaSize;

    /**
     * Constructor for worm food object.
     * @param pit pit to which the food is associated. (unused currently)
     */
    public WormFood(WormPit pit) {
    arenaSize = WormPit.CellWidth * WormPit.CellHeight;
    regenerate();
    }

    /**
     * Returns true if the food item is at the given cell.
     * @param cx x coordinate of cell to test
     * @param cy y coordinate of cell to test
     * @return true, if the food is at the cell coordinate
     */
    public boolean isAt(int cx, int cy) {
    return ((this.cellX == cx) && (this.cellY == cy));
    }

    /**
     * Get the X coordinate of the cell that contains the food item.
     * @return x coordinate of food cell location
     */
    public int getX() {
    return cellX;
    }

    /**
     * Get the Y coordinate of the cell that contains the food item.
     * @return y coordinate of food cell location
     */
    public int getY() {
    return cellY;
    }

    /**
     * Regenerate the food item. Whenever the worm eats a piece of
     * food, this method is called.
     */
    public void regenerate() {
    int loc = (int)(System.currentTimeMillis() % arenaSize);
    cellY = (int)(loc / WormPit.CellWidth);
    cellX = (int)(loc % WormPit.CellHeight);
    y = cellY * WormPit.CELL_SIZE;      // Cache to save time during paint
    x = cellX * WormPit.CELL_SIZE;      // Cache to save time during paint
    }

    /**
     * Paint the piece of food.
     * @param g graphics object to receive rendering of food object
     */
    public void paint(Graphics g) {
    g.setColor(WormPit.FOOD_COLOUR);
    g.fillRect(x+1, y+1, WormPit.CELL_SIZE-2, WormPit.CELL_SIZE-2);
    g.setColor(WormPit.DRAW_COLOUR);
    g.drawRect(x, y, WormPit.CELL_SIZE-1, WormPit.CELL_SIZE-1);
    }

}

⌨️ 快捷键说明

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