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

📄 tetrismodel.java

📁 用J2ME开发的俄罗斯方块
💻 JAVA
字号:
package tetris;

public class TetrisModel {

	public static final int POINT_COUNT_IN_BOX = 4;
	
	public static final int STYLE_COUNT = 7;

	private static final int BOX_COUNT = 19;

	public static final int COLUMN_COUNT = 19;

	public static final int ROW_COUNT = 29;
	
	public static final int STYLE_INITIAL_CENTER_COLUMN = COLUMN_COUNT / 2;
	
	public static final int STYLE_INITIAL_CENTER_ROW = 1;

	private final static byte[] STYLE_INITIAL_BOX_INDEX = { 0, 1, 3, 5, 7, 11, 15 };
	
	private PlayerDemo player;

	class BOX {
		public byte cells[][] = new byte[POINT_COUNT_IN_BOX][2]; // 以旋转点所在单元为零点的单元相对坐标

		public byte nextBoxIndex = 0;
	}

	// 存储相对坐标信息和下次变形的索引号
	// [19][9]
	private byte boxInfo[][] = { 
			{ 0, 0, 1, 0, 0, 1, 1, 1, 0 }, // #
			{ 0, 0, 1, 0, 2, 0, -1, 0, 2 }, // l1
			{ 0, 0, 0, -1, 0, 1, 0, 2, 1 }, // l2
			{ 0, 0, -1, -1, 0, -1, 1, 0, 4 }, // Z1
			{ 0, 0, 0, 1, 1, -1, 1, 0, 3 }, // Z2
			{ 0, 0, -1, 0, 0, -1, 1, -1, 6 }, // S1
			{ 0, 0, -1, -1, -1, 0, 0, 1, 5 }, // S2
			{ 0, 0, -1, 0, 1, 0, 0, 1, 8 }, // T1
			{ 0, 0, 0, -1, 0, 1, -1, 0, 9 }, // T2
			{ 0, 0, -1, 0, 1, 0, 0, -1, 10 }, // T3
			{ 0, 0, 0, -1, 0, 1, 1, 0, 7 }, // T4
			{ 0, 0, -1, 0, 1, 0, 1, 1, 12 }, // J1
			{ 0, 0, 0, -1, 0, 1, -1, 1, 13 }, // J2
			{ 0, 0, -1, -1, -1, 0, 1, 0, 14 }, // J3
			{ 0, 0, 0, -1, 0, 1, 1,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byte currentBoxCenterX = 0;

	private byte currentBoxCenterY = 0;

	byte staticPanel[][] = new byte[ROW_COUNT][COLUMN_COUNT];

	byte dynamicPanel[][] = new byte[ROW_COUNT][COLUMN_COUNT];

	public TetrisModel(PlayerDemo player2) {
		player=player2;
		initBox();
	}

	public void initPanel() {
		for (int b = 0; b < ROW_COUNT; b++) {
			for (int a = 0; a < COLUMN_COUNT; a++) {
				staticPanel[b][a] = panelData[b][a];
				dynamicPanel[b][a] = panelData[b][a];
			}
		}
	}

	private void initBox() {
		allBox = new BOX[BOX_COUNT];
		for (int i = 0; i < BOX_COUNT; i++) {
			allBox[i] = new BOX();
			for (int j = 0; j < 2 * POINT_COUNT_IN_BOX; j++) {
				int b = (j / 2);
				int a = (j % 2);
				allBox[i].cells[b][a] = boxInfo[i][j];
			}
			allBox[i].nextBoxIndex = boxInfo[i][2 * POINT_COUNT_IN_BOX];
		}
	}

	private void clearDynamicPanel() {
		byte column, row;
		for (byte i = 0; i < POINT_COUNT_IN_BOX; i++) {
			column = (byte) (currentBoxCenterX + allBox[currentBoxIndex].cells[i][0]);
			row = (byte) (currentBoxCenterY + allBox[currentBoxIndex].cells[i][1]);
			dynamicPanel[row][column] = 0;
		}
	}

	private void setDynamicPanel() {
		byte column, row;
		for (byte i = 0; i < POINT_COUNT_IN_BOX; i++) {
			column = (byte) (currentBoxCenterX + allBox[currentBoxIndex].cells[i][0]);
			row = (byte) (currentBoxCenterY + allBox[currentBoxIndex].cells[i][1]);
			dynamicPanel[row][column] = currentBoxStyle;
		}
	}

	public void setCurrentBoxInfo(byte style) {
		currentBoxStyle = (byte) (style + 1);
		currentBoxIndex = (byte) getBoxIndex(style);
		currentBoxCenterX = STYLE_INITIAL_CENTER_COLUMN;
		currentBoxCenterY = STYLE_INITIAL_CENTER_ROW;
	}

	public void fallBoxToGround() {
		clearDynamicPanel();
		byte column, row;
		for (byte i = 0; i < POINT_COUNT_IN_BOX; i++) {
			column = (byte) (currentBoxCenterX + allBox[currentBoxIndex].cells[i][0]);
			row = (byte) (currentBoxCenterY + allBox[currentBoxIndex].cells[i][1]);
			staticPanel[row][column] = currentBoxStyle;
		}
	}

	public void down() {
		clearDynamicPanel();
		currentBoxCenterY ++;
		setDynamicPanel();
	}

	public void left() {
		clearDynamicPanel();
		currentBoxCenterX--;
		setDynamicPanel();
	}

	public void right() {
		clearDynamicPanel();
		currentBoxCenterX++;
		setDynamicPanel();
	}

	public void transform() {
		clearDynamicPanel();
		currentBoxIndex = allBox[currentBoxIndex].nextBoxIndex;
		setDynamicPanel();
	}

	public boolean canLeft() {
		byte column, row;
		for (byte i = 0; i < POINT_COUNT_IN_BOX; i++) {
			column = (byte) (currentBoxCenterX + allBox[currentBoxIndex].cells[i][0]);
			row = (byte) (currentBoxCenterY + allBox[currentBoxIndex].cells[i][1]);
			if (staticPanel[row][column - 1] > 0) {
				return false;
			}
		}
		return true;
	}

	public boolean canRight() {
		byte column, row;
		for (byte i = 0; i < POINT_COUNT_IN_BOX; i++) {
			column = (byte) (currentBoxCenterX + allBox[currentBoxIndex].cells[i][0]);
			row = (byte) (currentBoxCenterY + allBox[currentBoxIndex].cells[i][1]);
			if (staticPanel[row][column + 1] > 0) {
				return false;
			}
		}
		return true;
	}

	public boolean canDown() {
		byte column, row;
		for (byte i = 0; i < POINT_COUNT_IN_BOX; i++) {
			column = (byte) (currentBoxCenterX + allBox[currentBoxIndex].cells[i][0]);
			row = (byte) (currentBoxCenterY + allBox[currentBoxIndex].cells[i][1]);
			if (staticPanel[row + 1][column] > 0) {
				return false;
			}
		}
		return true;
	}

	public boolean isTransformable() {
		byte column, row;
		int index = allBox[currentBoxIndex].nextBoxIndex;
		for (int i = 0; i < POINT_COUNT_IN_BOX; i++) {
			column = (byte) (currentBoxCenterX + allBox[index].cells[i][0]);
			row = (byte) (currentBoxCenterY + allBox[index].cells[i][1]);
			if (staticPanel[row][column] > 0) {
				return false;
			}
		}
		return true;
	}

	public boolean isGameOver() {
		for (byte column = 1; column < COLUMN_COUNT - 1; column++) {
			if (staticPanel[1][column] > 0) {
				player.playgameover();
				return true;
			}
		}
		return false;
	}

	public int handleFullRow() {
		byte lines = 0;
		for (byte row = ROW_COUNT - 2; row > 1; row--) {
			if (!existBlank(row)) {
				lines++;
				moveRowDown(row);
				row++;
			}
		}
		return lines;
	}
	
	public int getBoxIndex(int style) {
		return STYLE_INITIAL_BOX_INDEX[style];
	}

	private void moveRowDown(byte row) {
		for (int preRow = row - 1; preRow > 0; preRow--) {
			for (byte column = 1; column < COLUMN_COUNT - 1; column++) {
				staticPanel[preRow + 1][column] = staticPanel[preRow][column];
			}
		}
	}

	private boolean existBlank(byte row) {
		for (int column = 1; column < COLUMN_COUNT - 1; column++) {
			if (staticPanel[row][column] == 0) {
//				player.playdispear();
				return true;
			}
		}
		player.playdispear();
		return false;
	}
}

⌨️ 快捷键说明

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