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

📄 ground.java

📁 java编写的贪吃蛇的源代码,myeclipse平台实现
💻 JAVA
字号:
package com.nilaiya.snake.entities;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Random;

import com.nilaiya.snake.util.Global;

/**
 * @author yellow
 *
 */
public class Ground {

	//用数组来保存石头
	private int[][] rockes = new int[Global.WIDTH][Global.HEIGHT];

	/**
	 * 构造器
	 *
	 */
	public Ground() {
		for (int x = 0; x < Global.WIDTH; x++) {
			rockes[x][0] = 1;
			rockes[x][Global.HEIGHT - 1] = 1;
		}
	}

	/**
	 * 食物随机产生
	 * @return Point
	 * @return the new Point(x, y)
	 */
	public Point getPoint() {
		Random random = new Random();
		int x = 0;
		int y = 0;
		do {
			x = random.nextInt(Global.WIDTH);
			y = random.nextInt(Global.HEIGHT);
		} while (rockes[x][y] == 1);
		return new Point(x, y);
	}

	/**
	 * 判断蛇吃否吃到了石头
	 * @param snake
	 * @return boolean
	 */
	public boolean isSnakeEatRock(Snake snake) {
		System.out.println("蛇吃到了石头!");

		for (int x = 0; x < Global.WIDTH; x++) {
			for (int y = 0; y < Global.HEIGHT; y++) {
				if (rockes[x][y] == 1
						&& (x == snake.getHead().x && y == snake.getHead().y)) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 显示石头的方法
	 * @param g
	 */
	public void drawMe(Graphics g) {
		System.out.println("显示石头的方法!");

		g.setColor(Color.DARK_GRAY);

		//画石头
		for (int x = 0; x < Global.WIDTH; x++) {
			for (int y = 0; y < Global.HEIGHT; y++) {
				if (rockes[x][y] == 1) {
					g.fill3DRect(x * Global.CELL_SIZE, y * Global.CELL_SIZE,
							Global.CELL_SIZE, Global.CELL_SIZE, true);
				}
			}
		}
	}
}

⌨️ 快捷键说明

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