不到400行
package ffff;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Tetris extends JFrame implements ActionListener {
TetrisPanel tpanel = new TetrisPanel();
public Tetris() {
addKeyListener(tpanel);
add(tpanel);
}
private class TetrisPanel extends JPanel implements KeyListener { // 画板内部使用私有的画布
// blockType 代表方块类型
// turnState代表方块状态
private int blockType;
private int score = 0;
private Timer timer;
private int turnState;
private boolean qwert = true;
private int x;
private int y;
private int i = 0;
int j = 0;
int flag = 0;
// 定义已经放下的方块x=0-11,y=0-21;
private int map[][] = new int[13][23];
private final int shapes[][][] = {
// i型
{ { 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 },
{ 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 } },
// s型
{ { 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 } },
// z型
{ { 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 } },
// j型
{ { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
// o型
{ { 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
// L型
{ { 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
// t型
{ { 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0 } } };
// 生成新方块的方法
public void newblock() {
blockType = (int) (Math.random() * 1000) % 7;
turnState = (int) (Math.random() * 1000) % 4;
x = 4;
y = 0;
if (gameover(x, y) == 1) {
cleanMap();
drawWall();
score = 0;
JOptionPane.showMessageDialog(null, "GAME OVER");
}
}
public TetrisPanel() {// 构造函数
cleanMap();// 数据初始化
drawWall();
timer = new Timer(1000, new TimerListener());
timer.start();
}
public void cleanMap() {// 新地图
for (int i = 1; i < 11; i++) {
for (int j = 0; j < 21; j++) {
map[i][j] = 0;
}
}
}
public void drawWall() {
for (int i = 0; i < 12; i++) {// 遍历每一列
map[i][21] = 2;// 最后一行
}
for (int j = 0; j < 22; j++) {// 遍历每一行
map[0][j] = 2;// 最左边列
map[11][j] = 2;// 最右边列围墙
}
}
public void paintComponent(Graphics g) {// 画方块方法
super.paintComponent(g);
// 画当前方块
for (j = 0; j < 16; j++) {
if (shapes[blockType][turnState][j] == 1) {
g.setColor(Color.blue);
g.fillRect((j % 4 + x + 1) * 10, (j / 4 + y) * 10, 10, 10);
}
}
// 画已经固定的方块
for (int j = 0; j < 22; j++) {
for (int i = 0; i < 12; i++) {
if (map[i][j] == 1) {
g.setColor(Color.black);
g.fillRect(i * 10, j * 10, 10, 10);
}
if (map[i][j] == 2) {
g.drawRect(i * 10, j * 10, 10, 10);
}
}
}
g.setColor(Color.red);
g.drawString("score=" + score, 125, 10);
g.drawString("抵制不良游戏,", 125, 50);
g.drawString("拒绝盗版游戏。", 125, 70);
g.drawString("注意自我保护,", 125, 90);
g.drawString("谨防受骗上当。", 125, 110);
g.drawString("适度游戏益脑,", 125, 130);
g.drawString("沉迷游戏伤身。", 125, 150);
g.drawString("合理安排时间,", 125, 170);
g.drawString("享受健康生活。", 125, 190);
}
// 旋转的方法
public void turn() {
int tempturnState = turnState;
turnState = (turnState + 1) % 4;
if (blow(x, y, blockType, turnState) == 1) {
}
if (blow(x, y, blockType, turnState) == 0) {
turnState = tempturnState;
}
repaint();
}
// 左移的方法
public void left() {
if (blow(x - 1, y, blockType, turnState) == 1) {
x = x - 1;
}
;
repaint();
}
// 右移的方法
public void right() {
if (blow(x + 1, y, blockType, turnState) == 1) {
x = x + 1;
}
;
repaint();
}
// 下落的方法
public void down() {
if (blow(x, y + 1, blockType, turnState) == 1) {
y = y + 1;
delline();
}
;
if (blow(x, y + 1, blockType, turnState) == 0) {
add(x, y, blockType, turnState);
newblock();
delline();
}
;
repaint();
}
// 是否合法的方法
public int blow(int x, int y, int blockType, int turnState) {
for (int a = 0; a < 4; a++) {
for (int b = 0; b < 4; b++) {
if (((shapes[blockType][turnState][a * 4 + b] == 1) && (map[x + b + 1][y + a] == 1))
|| ((shapes[blockType][turnState][a * 4 + b] == 1) && (map[x + b + 1][y + a] == 2))) {
return 0;
}
}
}
return 1;
}
// 消行的方法
public void delline() {
int c = 0;
for (int b = 0; b < 22; b++) {
for (int a = 0; a < 12; a++) {
if (map[a][b] == 1) {
c = c + 1;
if (c == 10) {
score += 10;
for (int d = b; d > 0; d--) {
for (int e = 0; e < 11; e++) {
map[e][d] = map[e][d - 1];
}
}
}
}
}
c = 0;
}
}
// 判断你挂的方法
public int gameover(int x, int y) {
if (blow(x, y, blockType, turnState) == 0) {
return 1;
}
return 0;
}
// 把当前添加map
public void add(int x, int y, int blockType, int turnState) {
int j = 0;
for (int a = 0; a < 4; a++) {
for (int b = 0; b < 4; b++) {
if (map[x + b + 1][y + a] == 0) {
map[x + b + 1][y + a] = shapes[blockType][turnState][j];
}
;
j++;
}
}
}
// 键盘监听
public void keyPressed(KeyEvent e) {
if (qwert) {
switch (e.getKeyCode()) {
case KeyEvent.VK_DOWN:
down();
break;
case KeyEvent.VK_UP:
turn();
break;
case KeyEvent.VK_RIGHT:
right();
break;
case KeyEvent.VK_LEFT:
left();
break;
}
}
}
private void newgame() {
timer.stop();
cleanMap();// 数据初始化
drawWall();
y = 0;
repaint();
timer.start();
cleanMap();// 数据初始化
drawWall();
qwert = true;
}
private void pause() {
timer.stop();
qwert = false;
}
private void llll() {
timer.start();
qwert = true;
}
private void et() {
int result = JOptionPane.showConfirmDialog(this, "是否退出游戏?");
if (result == 0) {
System.exit(0);
}
}
private void ab() {
JOptionPane.showMessageDialog(this, "umbrella制作,感谢屠同学");
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
// 定时器监听
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
repaint();
if (blow(x, y + 1, blockType, turnState) == 1) {
y = y + 1;
delline();
};
if (blow(x, y + 1, blockType, turnState) == 0) {
if (flag == 1) {
add(x, y, blockType, turnState);
delline();
newblock();
flag = 0;
}
flag = 1;
};
}
}
}
public static void main(String[] args) {
Tetris frame = new Tetris();
JMenuBar menubar = new JMenuBar();// 菜单栏
frame.setJMenuBar(menubar);
JMenu game = new JMenu("游戏");
JMenuItem newgame = game.add("新游戏");
newgame.addActionListener(frame);
JMenuItem pause = game.add("暂停");
pause.addActionListener(frame);
JMenuItem goon = game.add("继续");
goon.addActionListener(frame);
JMenuItem exit = game.add("退出");
exit.addActionListener(frame);
JMenu help = new JMenu("帮助");
JMenuItem about = help.add("关于");
about.addActionListener(frame);
menubar.add(game);// 添加菜单到菜单栏
menubar.add(help);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(220, 275);
frame.setTitle("俄罗斯方块测试版");
frame.setLocationRelativeTo(null);// 创建的窗口不参考任何坐标,默认中间
frame.setVisible(true);
frame.setResizable(false);
}
@Override
public void actionPerformed(ActionEvent e) {
String type;
JMenuItem mnit = null;
JButton btn = null;
type = e.getSource().toString();
if (type.contains("JMenuItem"))
{
mnit = (JMenuItem) e.getSource();
if (mnit.getText() == "新游戏")
{
tpanel.newgame();
}
if (mnit.getText() == "暂停")
{
tpanel.pause();
}
if (mnit.getText() == "继续")
{
tpanel.llll();
}
if (mnit.getText() == "退出") {
tpanel.et();
}
if (mnit.getText() == "关于") {
tpanel.ab();
}
}
}
}