📄 jtetrixl.java
字号:
static final long serialVersionUID = 8;
public showNextPiece(int bw, int bh, int gw, int gh) {
boardWidth = bw;
boardHeight = bh;
gridWidth = gw;
gridHeight = gh;
setBackground(Color.black);
setSize(boardWidth * gridWidth, boardHeight * gridHeight);
}
// 产生下一个方块
public void generateNextPiece() {
nextPiece = new TetrixPiece();
repaint();
}
// 传回下一个方块
public TetrixPiece getNextTetrix() {
return nextPiece;
}
// 画格子
public void drawGrid(Graphics g, int x, int y) {
g.setColor(pieceColor);
g.fillRect(x * gridWidth, y * gridHeight, gridWidth, gridHeight);
g.setColor(Color.lightGray);
g.drawRect(x * gridWidth, y * gridHeight, gridWidth, gridHeight);
}
// 依类型决定色彩并画出方块
public void paint(Graphics g) {
switch (nextPiece.getType()) {
case 1:
pieceColor = Color.red;
break;
case 2:
pieceColor = Color.orange;
break;
case 3:
pieceColor = Color.yellow;
break;
case 4:
pieceColor = Color.green;
break;
case 5:
pieceColor = Color.blue;
break;
case 6:
pieceColor = Color.magenta;
break;
case 7:
pieceColor = Color.gray;
break;
}
g.setColor(Color.black);
g.fillRect(0, 0, boardWidth * gridWidth, boardHeight * gridHeight);
for (int i = 0; i < 4; i++) {
drawGrid(g, nextPiece.getXCoord(i) + 2, nextPiece.getYCoord(i) + 2);
}
}
}
// 游戏区主类别
class gameOpBoard extends JComponent {
private int boardWidth, boardHeight; // 区块格子数目
private int gridWidth, gridHeight; // 格子大小
private TetrixPiece currentPiece; // 现在操作的方块
private int gameBoard[][]; // 游戏的面版阵列,用于记录堆积的方块
private int nClearLines; // 没有方格的行数
private int nLinesRemoved; // 被移除行数
private int score; // 得分
private int xOffset, yOffset; // 依原始TetrixPiece进行位移
private boolean gameover; // Game Over ?
static final long serialVersionUID = 5;
public gameOpBoard(int bw, int bh, int gw, int gh) {
boardWidth = bw;
boardHeight = bh;
gridWidth = gw;
gridHeight = gh;
gameBoard = new int[boardWidth][boardHeight];
nClearLines = boardHeight;
score = 0;
setBackground(Color.black);
setSize(boardWidth * gridWidth, boardHeight * gridHeight);
}
// 设定目前的操作方块
public void setCurrentPiece(TetrixPiece piece) {
xOffset = 5;
yOffset = 1;
currentPiece = piece;
// 判断是否结束游戏
int x, y;
for (int i = 0; i < 4; i++) {
x = currentPiece.getXCoord(i);
y = currentPiece.getYCoord(i);
if (gameBoard[x + xOffset][y + yOffset] != 0) {
gameover = true;
}
}
repaint();
}
// 重新开始游戏的新状态
public void newState() {
nClearLines = boardHeight;
score = 0;
gameover = false;
for (int i = 0; i < boardWidth; i++)
for (int j = 0; j < boardHeight; j++)
gameBoard[i][j] = 0;
}
// 可否移动指定位移?
// xStep: 1 表向右 xStep: -1 表向左 xStep: 0 不移动X方向
// yStep: 1 表向下 yStep: 0 不移动Y方向
public boolean canMoveTo(int xStep, int yStep) {
int x, y;
for (int i = 0; i < 4; i++) {
x = currentPiece.getXCoord(i) + xOffset + xStep;
y = currentPiece.getYCoord(i) + yOffset + yStep;
// 是否超出边界
if (x < 0 || x >= boardWidth || y >= boardHeight)
return false;
// 是否已经有方块
if (gameBoard[x][y] != 0)
return false;
}
return true;
}
// 向左移
public void moveLeft() {
if (canMoveTo(-1, 0))
xOffset--;
repaint();
}
// 向右移
public void moveRight() {
if (canMoveTo(1, 0))
xOffset++;
repaint();
}
// 向下移
public void MoveDown() {
if (canMoveTo(0, 1))
yOffset++;
repaint();
}
// 瞬间下移
public void soonMoveDown() {
while (canMoveTo(0, 1))
yOffset++;
score += 3;
repaint();
}
// 转动方块
// r: 0 表向左转 r: 1 表示向右转
public void RotateRL(int r) {
TetrixPiece tmp = new TetrixPiece(currentPiece.getType());
for (int i = 0; i < 4; i++) {
tmp.setCoord(i, currentPiece.getXCoord(i), currentPiece
.getYCoord(i));
}
if (r == 0)
tmp.rotateLeft();
else
tmp.rotateRight();
// 测试是否可转动
int x, y;
for (int i = 0; i < 4; i++) {
x = tmp.getXCoord(i) + xOffset;
y = tmp.getYCoord(i) + yOffset;
// 超出边界?
if (x < 0 || x >= boardWidth || y >= boardHeight || y < 0)
return;
// 已经有方块?
if (gameBoard[x][y] != 0)
return;
}
currentPiece = tmp;
repaint();
}
// 是否更新游戏画面阵列
public boolean isUpdated() {
if (gameover)
return false;
if (canMoveTo(0, 1))
return false;
int x, y;
for (int i = 0; i < 4; i++) {
x = currentPiece.getXCoord(i) + xOffset;
y = currentPiece.getYCoord(i) + yOffset;
gameBoard[x][y] = currentPiece.getType();
if (y < nClearLines) {
nClearLines = y;
}
}
// 移除完整的行
while (removeFullLines() != 0)
;
repaint();
return true;
}
// 移除完整的行
public int removeFullLines() {
int i, j, k;
int nFullLines = 0; // 完整行的数目
for (j = boardHeight - 1; j >= nClearLines; j--) {
for (i = 0; i < boardWidth; i++) {
if (gameBoard[i][j] == 0) // 不是完整的一行
break;
}
if (i == boardWidth) { // 找到完整的一行
nFullLines = 1;
for (k = j - 1; k >= nClearLines; k--) { // 继续找完整行
for (i = 0; i < boardWidth; i++)
if (gameBoard[i][k] == 0) // 不是完整行
break;
if (i == boardWidth) { // 找到完整行
nFullLines++;
} else {
for (i = 0; i < boardWidth; i++) { // 下移完整行上面的一行
if (gameBoard[i][k + nFullLines] != gameBoard[i][k]) {
gameBoard[i][k + nFullLines] = gameBoard[i][k];
}
}
}
}
// 更新相关数据
nClearLines = nClearLines + nFullLines;
nLinesRemoved = nLinesRemoved + nFullLines;
score = score + 10 * nFullLines;
}
}
// 清除游戏区域阵列移动后的行资料
for (j = 0; j < nClearLines; j++)
for (i = 0; i < boardWidth; i++)
gameBoard[i][j] = 0;
return nFullLines;
}
// 目前得分
public int getScore() {
return score;
}
// 移除总行数
public int getLineRemoved() {
return nLinesRemoved;
}
// 绘图
// 取得格子颜色
public Color getGridColor(int type) {
switch (type) {
case 1:
return Color.red;
case 2:
return Color.orange;
case 3:
return Color.yellow;
case 4:
return Color.green;
case 5:
return Color.blue;
case 6:
return Color.magenta;
case 7:
return Color.gray;
}
return Color.black;
}
// 画格子
// isPiece: true 画格子 isPiece: false 画堆积方块区
public void drawGrid(Graphics g, int x, int y, boolean isPiece) {
if (isPiece) // 这是方块
g.setColor(getGridColor(currentPiece.getType()));
else
// 堆积的区域
g.setColor(getGridColor(gameBoard[x][y]));
g.fillRect(x * gridWidth, y * gridHeight, gridWidth, gridHeight);
g.setColor(Color.lightGray);
g.drawRect(x * gridWidth, y * gridHeight, gridWidth, gridHeight);
}
public void paint(Graphics g) {
// 清除前一个画面
g.setColor(Color.black);
g.fillRect(0, 0, boardWidth * gridWidth, boardHeight * gridHeight);
// 画方块
for (int i = 0; i < 4; i++) {
drawGrid(g, currentPiece.getXCoord(i) + xOffset, currentPiece
.getYCoord(i)
+ yOffset, true);
}
// 画堆积方块区
for (int i = 0; i < boardWidth; i++) {
for (int j = 0; j < boardHeight; j++) {
if (gameBoard[i][j] == 0)
continue;
drawGrid(g, i, j, false);
}
}
// 是否画出Game Over字样
if (gameover) {
g.setFont(new Font("细明体", Font.ITALIC | Font.BOLD, 26));
g.setColor(Color.white);
g.drawString("Game Over", 30, 50);
g.setColor(Color.red);
g.drawString("Game Over", 32, 52);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -