📄 square.java
字号:
/**
* Square.java
* Summary 各个方块的基类
* Summary 控制方块的移动、变形、是否可移动
* Created on 2002-3-2
* @author Dorian
*/
package com.Dorian.Tetris.square;
public abstract class Square {
//判断是否可以向左移动
public boolean isMoveLeft() {
if (localX - 1 < 0)
return false;
int i = 0;
for (int j = 0; j < squareHeight; j++) {
if (squareThis[i][j] == true) {
if (SquarePanel.squareArray[localX + i - 1][localY + j]
== true)
return false;
continue;
}
if (squareThis[i][j] == false && squareThis[i + 1][j] == false) {
if (SquarePanel.squareArray[localX + i + 1][localY + j]
== true)
return false;
continue;
}
if (squareThis[i][j] == false)
if (SquarePanel.squareArray[localX + i][localY + j] == true)
return false;
}
return true;
}
//判断是否可以向右移动
public boolean isMoveRight() {
if (localX + squareWidth + 1 > SquarePanel.WIDTH)
return false;
int i = squareWidth - 1;
for (int j = 0; j < squareHeight; j++) {
if (squareThis[i][j] == true) {
if (SquarePanel.squareArray[localX + i + 1][localY + j]
== true)
return false;
continue;
}
if (squareThis[i][j] == false && squareThis[i - 1][j] == false) {
if (SquarePanel.squareArray[localX + i - 1][localY + j]
== true)
return false;
continue;
}
if (squareThis[i][j] == false) {
if (SquarePanel.squareArray[localX + i][localY + j] == true)
return false;
}
}
return true;
}
//判断是否可以向下移动
public boolean isMoveDown() {
if (localY + changeLength + 1 > SquarePanel.HEIGHT)
return false;
int j = squareHeight - 1;
for (int i = 0; i < squareWidth; i++) {
if (squareThis[i][j] == true) {
if (SquarePanel.squareArray[localX + i][localY + j + 1]
== true)
return false;
continue;
}
if (squareThis[i][j] == false && squareThis[i][j - 1] == false) {
if (SquarePanel.squareArray[localX + i][localY + j - 1]
== true)
return false;
continue;
}
if (squareThis[i][j] == false)
if (SquarePanel.squareArray[localX + i][localY + j] == true)
return false;
}
return true;
}
//判断初始化方块的地方是否为空
public boolean isEmpty() {
for (int i = 0; i < squareWidth; i++)
for (int j = 0; j < squareHeight; j++)
if (squareThis[i][j] == true)
if (getSquareArray(localX + i, localY + j))
return false;
return true;
}
//初始化方块
public void initSquare() {
for (int i = 0; i < squareWidth; i++)
for (int j = 0; j < squareHeight; j++)
if (squareThis[i][j])
SquarePanel.squareArray[localX + i][localY + j] = true;
}
//是否可以改变方块形状
public boolean isChange() {
for (int i = 0; i < changeLength; i++) {
if (localX + i + 1 > SquarePanel.WIDTH)
return false;
for (int j = 0; j < changeLength; j++)
if (squareThis[i][j] == false)
if (SquarePanel.squareArray[localX + i][localY + j]
== true)
return false;
}
return true;
}
//封装游戏面板数组
public void setSquareArray(int x, int y, boolean arg) {
SquarePanel.squareArray[x][y] = arg;
}
public boolean getSquareArray(int x, int y) {
return SquarePanel.squareArray[x][y];
}
//封住自身坐标数组
public boolean setSquareThis(int x, int y, boolean arg) {
squareThis[x][y] = arg;
return arg;
}
public boolean getSquareThis(int x, int y) {
return squareThis[x][y];
}
//向下移动
public void moveDown() {
if (isMoveDown()) {
for (int x = 0; x < changeLength; x++)
for (int y = 0; y < changeLength; y++)
if (squareThis[x][y] == true)
setSquareArray(localX + x, localY + y, false);
for (int x = 0; x < changeLength; x++)
for (int y = 0; y < changeLength; y++)
if (squareThis[x][y] == true)
setSquareArray(localX + x, localY + y + 1, true);
localY += 1;
}
}
//向左移动
public void moveLeft() {
if (isMoveLeft()) {
for (int x = 0; x < changeLength; x++)
for (int y = 0; y < changeLength; y++)
if (squareThis[x][y] == true)
setSquareArray(localX + x, localY + y, false);
for (int x = 0; x < changeLength; x++)
for (int y = 0; y < changeLength; y++)
if (squareThis[x][y] == true)
setSquareArray(localX + x - 1, localY + y, true);
localX -= 1;
}
}
//向右移动
public void moveRight() {
if (isMoveRight()) {
for (int x = 0; x < changeLength; x++)
for (int y = 0; y < changeLength; y++)
if (squareThis[x][y] == true)
setSquareArray(localX + x, localY + y, false);
for (int x = 0; x < changeLength; x++)
for (int y = 0; y < changeLength; y++)
if (squareThis[x][y] == true)
setSquareArray(localX + x + 1, localY + y, true);
localX += 1;
}
}
public int getLocalX() {
return localX;
}
public int getLocalY() {
return localY;
}
public abstract void change(); //改变方块形状
protected int squareWidth; //方块在自身坐标的占位宽
protected int squareHeight; //方块在自身坐标的占位高
/* 0 1 2 3
** _______________
** 0| | | | |
** |___|___|___|___|
** 1| | | | | //此为方块的自身坐标
** |___|___|___|___| //用来保存方块的形状
** 2| | | | | //boolean[][] squareThis &&
** |___|___|___|___| //boolean[][] squareTemp
** 3| | | | |
** |___|___|___|___|
*/
protected boolean[][] squareThis = new boolean[4][4];
//方块在4×4格中的自身坐标,用以保存方块当前状态
//otected boolean[][] squareTemp = new boolean[4][4]; //方块在改变形状时的临时自身坐标
//方块在游戏面板中的左上脚坐标
protected int localX = (SquarePanel.WIDTH - 1) / 2; //方块启始横坐标为游戏面板的中间
protected int localY = 0; //方块启始纵坐标为游戏面板最上端
protected int state = 1; //方块当前状态
protected int changeLength; //形状的长宽取其最大值
//用于确定其在自身坐标中的最小正方形
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -