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

📄 food.java

📁 用Java编写的小游戏
💻 JAVA
字号:
package com.cienet.levi;
/**
 * Copyright cienet.levi
 * 
 * Snake.Food
 * 
 * @author cienet
 * @create 2008/01/11 13:34:10 - ver1.0
 */
import java.util.Random;
import javax.microedition.lcdui.Graphics;

public class Food extends Pixel{
	
	//Food's color which will be shown on the screen
	private int FOOD_COLOR = 0x00ff00;
	//Random generator
	private static Random random = new Random();

	/**
	 * Constructor of Food 
	 * @param snakeCanvas snakeCanvas will be used go draw picture
	 * @param snake snake will be used to check if the snake touch food when the food is generate 
	 */
	public Food(SnakeCanvas snakeCanvas, Snake snake){
		int foodX, foodY;
		do{
			foodX = random.nextInt(snakeCanvas.getRelativeWidth());
			foodY = random.nextInt(snakeCanvas.getRelativeHeight());
		}
		while(snake.isTouchFood(foodX, foodY));
		setPixel(foodX, foodY);
	}
	
	/**
	 * Draw food will draw food object in the screen
	 * @param g Graphics object which will be a tool to draw food
	 */
	public void drawFood(Graphics g){
		Logger.log(this.coordinateX, this.coordinateY, "drawFood.xy");
		g.setColor(FOOD_COLOR);
		g.fillRect(coordinateX * SnakeCanvas.pixelUnit, 
				coordinateY * SnakeCanvas.pixelUnit, 
				SnakeCanvas.pixelUnit, SnakeCanvas.pixelUnit);
	}
}

⌨️ 快捷键说明

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