本帖最后由 lsy_loren 于 2022-9-3 13:58 编辑
源码地址:https://gitee.com/lsy_loren/loren-tetris.git
程序截图:
1
2
因为第一次发帖,不太知道要注意啥,版规说要贴源码。那就贴一下消行的处理吧。
[Java] 纯文本查看 复制代码 private int removeLine() {
Block[][] gameMap = this.gameData.getGameMap();
// 存储需要消除行的Y坐标
List<Integer> removeYList = new ArrayList<>();
for (int y = 0; y < gameMap[0].length; y++) {
boolean remove = true;
for (Block[] blocks : gameMap) {
if (blocks[y] == null || !blocks[y].isFrozen()) {
remove = false;
break;
}
}
if (remove) {
removeYList.add(y);
}
}
int removeLineCount = removeYList.size();
if (removeLineCount > 0) {
if (DataConstant.ENABLE_TWINKLE) { // 消行闪烁
this.removeLineTwinkle(gameMap, removeYList);
}
for (Integer count : removeYList) {
for (int y = count; y >= 0; y--) {
for (int x = 0; x < gameMap.length; x++) {
if (y - 1 >= 0) {
gameMap[x][y] = gameMap[x][y - 1];
} else {
gameMap[x][y] = null;
}
}
}
}
}
return removeLineCount;
}
[Java] 纯文本查看 复制代码 /**
* 消行闪烁
*/
private void removeLineTwinkle(Block[][] gameMap, List<Integer> removeYList) {
for (int count = 0; count < DataConstant.REMOVE_LINE_TWINKLE_COUNT; count++) { // 使要消除的行闪烁
for (int y : removeYList) {
for (int x = 0; x < gameMap.length; x++) {
gameMap[x][y] = (count % 2 == 0) ? null : Block.blockFrozen(x, y);
}
}
// repaint不会立即重绘,有个延时的过程,所以要用paintImmediately立即重绘
this.gamePanel.paintImmediately(0, 0, this.gamePanel.getWidth(), this.gamePanel.getHeight());
try {
TimeUnit.MILLISECONDS.sleep(DataConstant.REMOVE_LINE_TWINKLE_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} |