首页 > 代码库 > 回忆童年的电子游戏 俄罗斯方块的Java代码

回忆童年的电子游戏 俄罗斯方块的Java代码

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Tetris extends JFrame {
    public Tetris() {
        TetrisPanel a = new TetrisPanel();
        addKeyListener(a.listener);
        add(a);
        JMenuBar menu = new JMenuBar();
        JMenu game = new JMenu("游戏");
        game.add("新游戏");
        game.add("暂停");
        game.add("继续");
        game.add("退出");
        JMenu help = new JMenu("帮助");
        help.add("关于");
        menu.add(game);
        menu.add(help);
        this.setJMenuBar(menu);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(220, 275);
        setTitle("Tetris内测版");
        setResizable(false);
    }

    public static void main(String[] args) {
        new Tetris().setVisible(true);
    }
}

// 创建一个俄罗斯方块类
class TetrisPanel extends JPanel {
    public TimerListener listener = new TimerListener();
    // blockType 代表方块类型
    // turnState代表方块状态
    private int blockType;
    private int score = 0;

    private int turnState;

    private int x;

    private int y;

    int flag = 0;
    // 定义已经放下的方块x=0-11,y=0-21;
    int[][] map = new int[13][23];

    // 方块的形状 第一组代表方块类型有S、Z、L、J、I、O、T 7种 第二组 代表旋转几次 第三四组为 方块矩阵
    private final int shapes[][][] = new int[][][] {
    // 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 TetrisPanel() {
        nextBlock();
        newGame();
        new Timer(1000, listener).start();
    }

    // 生成新方块的方法
    private void nextBlock() {
        blockType = (int) (Math.random() * 1000) % 7;
        turnState = (int) (Math.random() * 1000) % 4;
        x = 4;
        y = 0;
        if (crash(x, y, blockType, turnState) == 0) {
            JOptionPane.showMessageDialog(null, "GAME OVER");
            newGame();
        }
    }

    // 初始化地图
    private void newGame() {
        score = 0;
        for (int i = 0; i < 12; i++) {
            for (int j = 0; j < 22; j++) {
                map[i][j] = 0;
                map[11][j] = map[0][j] = 3;
            }
            map[i][21] = 3;
        }
    }

    // 旋转的方法
    private void turn() {
        turnState = (crash(x, y, blockType, (turnState + 1) % 4) + turnState) % 4;
        repaint();
    }

    // 左移的方法
    private void left() {
        x -= crash(x - 1, y, blockType, turnState);
        repaint();
    }

    // 右移的方法
    private void right() {
        x += crash(x + 1, y, blockType, turnState);
        repaint();
    }

    // 下落的方法
    private void down() {
        y += crash(x, y + 1, blockType, turnState);
        if (crash(x, y + 1, blockType, turnState) == 0) {
            add(x, y, blockType, turnState);
            nextBlock();
        }
        repaint();
    }

    // 是否碰撞的方法
    private int crash(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] & map[x + b + 1][y
                        + a]) == 1) {
                    return 0;
                }
            }
        }
        return 1;
    }

    // 尝试消行的方法
    private void tryDelLine() {
        for (int b = 0; b < 21; b++) {
            int c = 1;
            for (int a = 0; a < 12; a++) {
                c &= map[a][b];
            }
            if (c == 1) {
                score += 10;
                for (int d = b; d > 0; d--) {
                    for (int e = 0; e < 11; e++) {
                        map[e][d] = map[e][d - 1];
                    }
                }
            }
        }
    }

    // 把当前添加map
    private void add(int x, int y, int blockType, int turnState) {
        for (int a = 0; a < 4; a++) {
            for (int b = 0; b < 4; b++) {
                map[x + b + 1][y + a] |= shapes[blockType][turnState][a * 4 + b];
            }
        }
        tryDelLine();
    }

    // 画方块的的方法
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        // 画当前方块
        for (int j = 0; j < 16; j++) {
            if (shapes[blockType][turnState][j] == 1) {
                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.fillRect(i * 10, j * 10, 10, 10);
                } else if (map[i][j] == 3) {
                    g.drawRect(i * 10, j * 10, 10, 10);
                }
            }
        }
        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);
    }

    // 定时器监听和键盘监听
    class TimerListener extends KeyAdapter implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            down();
        }

        public void keyPressed(KeyEvent e) {
            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();
            }
        }
    }
}