📄 tetrispanel.java
字号:
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class TetrisPanel extends JPanel { public TetrisPanel () { table = new Color[15][30]; newGame(); } public Color table[][]; private int score = 0; //interface between'listen for user' and //'move piece' threads. private int currentCommand = Piece.Down; //sets and accesses current command public synchronized int getCommand(int nextCommand) { int oldCommand = currentCommand; currentCommand = nextCommand; return oldCommand; } public void newGame () { for (int i = 0; i < 30; i++) for (int j = 0; j < 15; j++) table[j][i] = Color.white; } public void paintComponent(Graphics g) { super.paintComponent(g); for (int i = 0; i < 30; i++) for (int j = 0; j < 15; j++) { g.setColor(table[j][i]); g.fillRect(20+10*j, 350-10*i, 10, 10); } g.setColor(Color.blue); g.drawString("Score " + score, 200, 200); } public void checkScore() { for (int i = 0; i < 30; i++) { boolean scored = true; for (int j = 0; j < 15; j++) if (table[j][i] == Color.white) scored = false; if (scored) { score += 10; moveDown(i); repaint(); i = i - 1; // check row again } } } private void moveDown (int start) { for (int i = start; i < 30; i++) for (int j = 0; j < 15; j++) if (i < 29) table[j][i] = table[j][i+1]; else table[j][i] = Color.white; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -