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

📄 ground.java

📁 这是一个贪食蛇的游戏源代码 代码的设计模式室调停者
💻 JAVA
字号:
package com.zhanggang.teris.entities;

import java.awt.Color;
import java.awt.Graphics;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import com.zhang.teris.util.Global;

public class Ground {

	private int[][] obstacle = new int[Global.HEIGHT][Global.WIDTH];

	public void accept(Shape shape) {
		System.out.println("Ground's accept");
		int left = shape.getLeft();
		int top = shape.getTop();
		for (int y = 0; y < 4; y++) {
			for (int x = 0; x < 4; x++) {
				if (shape.getFlagByPoint(x, y)) {
					obstacle[top + y][left + x] = 1;
				}
			}
		}
		deleteFullLine();
	}

	public void drawMe(Graphics g) {
		System.out.println("Groud's drawMe");
		for (int y = 0; y < Global.HEIGHT; y++) {
			for (int x = 0; x < Global.WIDTH; x++) {
				if (obstacle[y][x] == 1) {
					g.fill3DRect(x * Global.CELL_SIZE, y * Global.CELL_SIZE,
							Global.CELL_SIZE, Global.CELL_SIZE, true);
				} else {
					Color c = g.getColor();
					g.setColor(new Color(0xd7d7de));
					g.drawRect(x * Global.CELL_SIZE, y * Global.CELL_SIZE,
							Global.CELL_SIZE, Global.CELL_SIZE);
					g.setColor(c);
				}
			}
		}
	}

	public boolean isMovable(Shape shape, int action) {
		int left = shape.getLeft();
		int top = shape.getTop();
		int[] body = shape.getPresentBody();
		switch (action) {
		case Shape.LEFT:
			left--;
			break;
		case Shape.RIGHT:
			left++;
			break;
		case Shape.DOWN:
			top++;
			break;
		case Shape.ROTATE:
			body = shape.getRotateBody();
			break;
		default:
			break;
		}

		for (int y = 0; y < 4; y++) {
			for (int x = 0; x < 4; x++) {
				if (body[y * 4 + x] == 1) {// top 和 left
											// 是从0开始的,所以它们不能等于边界,如果允许它们在边界时也可以变形,这回引起存储障碍物的数组下标越界
					if ((top + y) >= Global.HEIGHT || (left + x) < 0
							|| (left + x) >= Global.WIDTH
							|| obstacle[top + y][left + x] == 1) {
						return false;
					}
				}
			}
		}
		return true;
	}



	private void deleteFullLine() {
		for (int y = 0; y < Global.HEIGHT; y++) {
			boolean isFullLine = true;
			for (int x = 0; x < Global.WIDTH; x++) {
				if (obstacle[y][x] == 0) {
					isFullLine = false;
				}
			}
			if (isFullLine) {
				deleteLine(y);
			}
		}

	}

	private void deleteLine(int line) {

		for (int y = line; y > 0; y--) {
			for (int x = 0; x < Global.WIDTH; x++) {
				obstacle[y][x] = obstacle[y - 1][x];
			}
		}

		for (int x = 0; x < Global.WIDTH; x++) {
			obstacle[0][x] = 0;
		}

	}

	public boolean isFull() {
		for (int x = 0; x < Global.WIDTH; x++) {
			if (obstacle[0][x] == 1) {
				return true;
			}
		}
		return false;
	}
}

⌨️ 快捷键说明

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