food.java

来自「doja平台下的贪吃蛇游戏」· Java 代码 · 共 56 行

JAVA
56
字号
package greedSnake;

import java.util.Random;
import com.nttdocomo.ui.Graphics;

public class Food {
	private int x;

	private int y;

	private static Random rnd = new Random();

	private Food(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public static Food createRandomFood(int width, int height) {
		return createFood(getRandomInt(width), getRandomInt(height));
	}

	private static int getRandomInt(int limit) {
		return Math.abs(rnd.nextInt() % limit);
	}

	private static Food createFood(int x, int y) {
		return new Food(x, y);

	}

	/**
	 * Get the x coordinate of the base location of this instance.
	 */
	public int getX() {
		return x;
	}

	/**
	 * Get the y coordinate of the base location of this instance.
	 */
	public int getY() {
		return y;
	}

	/** Paint this instance onto the specified Graphics context. */
	public synchronized void paint(Graphics g) {
		int gn = GreedSnake.NODE_SIZE;
		int FoodColor = 0x0000ff;
		g.setColor(FoodColor);
		g.fillRect(this.getX() * gn, this.getY() * gn, gn, gn);
	}
}

// 节点对象类

⌨️ 快捷键说明

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