📄 lovehui.java
字号:
plInfo.add(tfScore);
plInfo.setBorder(border);
tfLevel.setEditable(false);
tfScore.setEditable(false);
plButton.add(btPlay);
plButton.add(btPause);
plButton.add(btStop);
plButton.add(btTurnLevelUp);
plButton.add(btTurnLevelDown);
plButton.setBorder(border);
add(plTip);
add(plInfo);
add(plButton);
addKeyListener(new ControlKeyListener());
btPlay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {game.playGame();}
});
btPause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (btPause.getText().equals(new String("暂停"))) game.pauseGame();
else game.resumeGame();
}
});
btStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
game.stopGame();
}
});
btTurnLevelUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
int level = Integer.parseInt(tfLevel.getText());
if (level < lovehui.MAX_LEVEL)
tfLevel.setText("" + (level + 1));
}
catch (NumberFormatException e){}
requestFocus();
}
});
btTurnLevelDown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
int level = Integer.parseInt(tfLevel.getText());
if (level > 1)
tfLevel.setText("" + (level - 1));
}
catch (NumberFormatException e) {}
requestFocus();
}
});
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent ce) {
plTipBlock.fanning();
}
});
timer = new Timer(500, new ActionListener()
{
public void actionPerformed(ActionEvent ae) {
tfScore.setText("" + game.getScore());
int scoreForLevelUpdate =
game.getScoreForLevelUpdate();
if (scoreForLevelUpdate >= lovehui.PER_LEVEL_SCORE
&& scoreForLevelUpdate > 0)
game.levelUpdate();
}
});
timer.start();
}
public void setTipStyle(int style) { //设置预显窗口的样式
plTipBlock.setStyle(style);
}
public int getLevel() { //取得用户设置的游戏等级。
int level = 0;
try {
level = Integer.parseInt(tfLevel.getText());
}
catch (NumberFormatException e) {}
return level;
}
public void setLevel(int level) { //让用户修改游戏难度等级。
if (level > 0 && level < 11) tfLevel.setText("" + level);}
public void setPlayButtonEnable(boolean enable) {//设置"开始"按钮的状态。
btPlay.setEnabled(enable);}
public void setPauseButtonLabel(boolean pause) {btPause.setText(pause ? "暂停" : "继续");}
public void reset() { // 重置控制面板
tfScore.setText("0");
plTipBlock.setStyle(0);
}
public void fanning() {plTipBlock.fanning();}//重新计算TipPanel里的boxes[][]里的小框的大小
private class TipPanel extends JPanel { //预显窗口的实现细节类
private Color backColor = Color.darkGray, frontColor = Color.lightGray;
private ErsBox[][] boxes =new ErsBox[ErsBlock.BOXES_ROWS][ErsBlock.BOXES_COLS];
private int style, boxWidth, boxHeight;
private boolean isTiled = false;
public TipPanel() { //预显窗口类构造函数
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++){
boxes[i][j] = new ErsBox(false);
}}}
public TipPanel(Color backColor, Color frontColor) { //预显窗口类窗口的前景色与窗口的背景色构造函数
this();
this.backColor = backColor;
this.frontColor = frontColor;
}
public void setStyle(int style) {//设置预显窗口的方块样式 @ param style int,对应ErsBlock类的STYLES中的28个值
this.style = style;
repaint();
}
public void paintComponent(Graphics g) { //覆盖JComponent类的函数,画组件。
super.paintComponent(g);
if (!isTiled) fanning();
int key = 0x8000;
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
Color color = (((key & style) != 0) ? frontColor : backColor);
g.setColor(color);
g.fill3DRect(j * boxWidth, i * boxHeight,
boxWidth, boxHeight, true);
key >>= 1;
}}}
public void fanning() {//根据窗口的大小,自动调整方格的尺寸
boxWidth = getSize().width / ErsBlock.BOXES_COLS;
boxHeight = getSize().height / ErsBlock.BOXES_ROWS;
isTiled = true;
}
}
private class ControlKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent ke) {
if (!game.isPlaying()) return;
ErsBlock block = game.getCurBlock();
switch (ke.getKeyCode()) {
case KeyEvent.VK_DOWN:
block.moveDown();
break;
case KeyEvent.VK_LEFT:
block.moveLeft();
break;
case KeyEvent.VK_RIGHT:
block.moveRight();
break;
case KeyEvent.VK_UP:
block.turnNext();
break;
default:
break;
}}}}
class ErsBlock extends Thread { //块类,继承自线程类(Thread)由 4 * 4 个方格(ErsBox)构成一个块,控制块的移动、下落、变形
public final static int BOXES_ROWS = 4;//一个块占的行数是4行
public final static int BOXES_COLS = 4;//一个块占的列数是4列
public final static int LEVEL_FLATNESS_GENE = 3; //让升级变化平滑的因子,避免最后几级之间的速度相差近一倍
public final static int BETWEEN_LEVELS_DEGRESS_TIME = 50;//相近的两级之间,块每下落一行的时间差别为多少(毫秒)
private final static int BLOCK_KIND_NUMBER = 7; //方块的样式数目为7
private final static int BLOCK_STATUS_NUMBER = 4;// 每一个样式的方块的反转状态种类为4
public final static int[][] STYLES ={ // 分别对应对7种模型的28种状态
{0x0f00, 0x4444, 0x0f00, 0x4444}, // 长条型的四种状态
{0x04e0, 0x0464, 0x00e4, 0x04c4}, // 'T'型的四种状态
{0x4620, 0x6c00, 0x4620, 0x6c00}, // 反'Z'型的四种状态
{0x2640, 0xc600, 0x2640, 0xc600}, // 'Z'型的四种状态
{0x6220, 0x1700, 0x2230, 0x0740}, // '7'型的四种状态
{0x6440, 0x0e20, 0x44c0, 0x8e00}, // 反'7'型的四种状态
{0x0660, 0x0660, 0x0660, 0x0660}, // 方块的四种状态
};
private GameCanvas canvas;
private ErsBox[][] boxes = new ErsBox[BOXES_ROWS][BOXES_COLS];
private int style, y, x, level;
private boolean pausing = false, moving = true;
public ErsBlock(int style, int y, int x, int level, GameCanvas canvas) { //构造函数,产生一个特定的块
this.style = style; //style 块的样式,对应STYLES的28个值中的一个
this.y = y; //y 起始位置,左上角在canvas中的坐标行
this.x = x; //x 起始位置,左上角在canvas中的坐标列
this.level = level; //level 游戏等级,控制块的下落速度
this.canvas = canvas; //canvas 画板
int key = 0x8000;
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
boolean isColor = ((style & key) != 0);
boxes[i][j] = new ErsBox(isColor);
key >>= 1;
}
}
display();
}
public void run() { //线程类的run()函数覆盖,下落块,直到块不能再下落
while (moving) {
try {
sleep(BETWEEN_LEVELS_DEGRESS_TIME
*(lovehui.MAX_LEVEL - level + LEVEL_FLATNESS_GENE));
}
catch (InterruptedException ie) { ie.printStackTrace();}
//后边的moving是表示在等待的100毫秒间,moving没被改变
if (!pausing) moving = (moveTo(y + 1, x) && moving);
}
}
public void moveLeft(){moveTo(y, x - 1);} //块向左移动一格
public void moveRight(){moveTo(y, x + 1);}//块向右移动一格
public void moveDown() {moveTo(y + 1, x);}//块向下落一格
public void turnNext(){ // 块变
for (int i = 0; i < 7; i++) {
for (int j = 0; j <4; j++) {
if (STYLES[i][j] == style) {
int newStyle = STYLES[i][(j + 1) %4];
turnTo(newStyle);
return;
}}}}
public void pauseMove(){pausing = true;} //暂停块的下落,对应游戏暂停
public void resumeMove(){pausing = false;}//继续块的下落,对应游戏继续
public void stopMove(){moving = false;} //停止块的下落,对应游戏停止
private void earse(){ //将当前块从画布的对应位置移除,要等到下次重画画布时才能反映出来
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
if (boxes[i][j].isColorBox()) {
ErsBox box = canvas.getBox(i + y, j + x);
if (box == null) continue;
box.setColor(false);
}}}}
private void display() { //让当前块放置在画布的对应位置上,要等到下次重画画布时才能看见
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
if (boxes[i][j].isColorBox()) {
ErsBox box = canvas.getBox(y + i, x + j);
if (box == null) continue;
box.setColor(true);
}}}}
private boolean isMoveAble(int newRow, int newCol) { //当前块能否移动到newRow/newCol所指定的位置
earse();
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
if (boxes[i][j].isColorBox()) {
ErsBox box = canvas.getBox(newRow + i, newCol + j);
if (box == null || (box.isColorBox())) {
display();
return false;
}}}}
display();
return true;
}
private synchronized boolean moveTo(int newRow, int newCol) {//将当前画移动到newRow/newCol所指定的位置
if (!isMoveAble(newRow, newCol) || !moving) return false;
earse();
y = newRow;
x = newCol;
display();
canvas.repaint();
return true;
}
private boolean isTurnAble(int newStyle){ //当前块能否变成newStyle所指定的块样式,主要是要考虑
int key = 0x8000; //边界以及被其它块挡住、不能移动的情况
earse();
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++){
if ((newStyle & key) != 0) {
ErsBox box = canvas.getBox(y + i, x + j);
if (box == null || box.isColorBox()) {
display();
return false;
}
}
key >>= 1;
}
}
display();
return true;
}
private boolean turnTo(int newStyle) { //将当前块变成newStyle所指定的块样式
if (!isTurnAble(newStyle) || !moving) return false;
earse();
int key = 0x8000;
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
boolean isColor = ((newStyle & key) != 0);
boxes[i][j].setColor(isColor);
key >>= 1;
}
}
style = newStyle;
display();
canvas.repaint();
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -