tetrispanel.java

来自「一些JAVA的小程序」· Java 代码 · 共 73 行

JAVA
73
字号
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 + =
减小字号Ctrl + -
显示快捷键?