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

📄 rect.java

📁 贪吃蛇游戏java实现,里面的结构较为清晰
💻 JAVA
字号:
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

public class Rect {

	public static final int WIDTH = 20;
	public static final int HEIGHT = 20;
	static Random r = new Random();
	int x;
	int y;

	public Rect(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public static Rect getBlood() {
		Rect c = new Rect(0, 0);
		while (true) {
			int x = (r.nextInt(MainFrame.WIDTH) / Rect.WIDTH) * Rect.WIDTH;

			int y = (r.nextInt(MainFrame.HEIGHT - 30) / Rect.HEIGHT)
					* Rect.HEIGHT + 30;
			if (!isInSnakeBody(new Rect(x, y))) {
				c.x = x;
				c.y = y;
				break;
			}
		}

		return c;
	}

	public static boolean isInSnakeBody(Rect rect) {
		for (Rect c : Snake.snakeList) {
			if (rect.equals(c))
				return true;
		}
		return false;
	}

	public void drawRect(Graphics g) {
		Color color = g.getColor();
		g.setColor(new Color(255, 0, 0));
		g.fillRect(this.x, this.y, WIDTH, HEIGHT);
		g.setColor(color);
	}

	@Override
	public boolean equals(Object obj) {
		Rect c = (Rect) obj;

		if (this.x == c.x && this.y == c.y)
			return true;

		return false;
	}

}

⌨️ 快捷键说明

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