page contents

java教程—实现简单的俄罗斯方块

本文讲述了关于Java教程—实现简单的俄罗斯方块!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

attachments-2023-03-DQlVdu3F64262e6807c94.png本文讲述了关于Java教程—实现简单的俄罗斯方块!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

结合网上的资料刚做完课程设计,具体代码如下:

public class TetrisPanel extends JPanel{

    private final int[][] map = new int[13][23];// map[列号][行号]。真正的方块区是:21行*10列。边框(2列,1行)

    // 方块的形状:

    // 第一维代表方块类型(包括7种:S、Z、L、J、I、O、T)

    // 第二`在这里插入代码片`维代表旋转次数

    // 第三四维代表方块矩阵

    // shapes[type][turnState][i] i--> block[i/4][i%4]

    int[][][] shapes = new int[][][] {

            /*

             * 模板 { {0,0,0,0,0,0,0,0, 0,0,0,0, 0,0,0,0}, {0,0,0,0,0,0,0,0, 0,0,0,0,

             * 0,0,0,0}, {0,0,0,0,0,0,0,0, 0,0,0,0, 0,0,0,0}, {0,0,0,0,0,0,0,0, 0,0,0,0,

             * 0,0,0,0} }

             */

            // I

            { { 1, 1, 1, 1,

                0, 0, 0, 0,

                0, 0, 0, 0,

                0, 0, 0, 0 },

              { 0, 1, 0, 0,

                0, 1, 0, 0,

                0, 1, 0, 0,

                0, 1, 0, 0 },

              { 1, 1, 1, 1,

                0, 0, 0, 0,

                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, 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 } },

            // Z 第3行: shapes[2][2][]

            { { 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, 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 } },

            // O

            { { 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, 1, 1, 0,

                0, 1, 1, 0,

                0, 0, 0, 0,

                0, 0, 0, 0 } },

            // L

            { { 0, 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 } },

            // 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 },

              { 0, 0, 0, 0,

                1, 1, 1, 0,

                0, 1, 0, 0,

                0, 0, 0, 0 },

              { 0, 1, 0, 0,

                0, 1, 1, 0,

                0, 1, 0, 0,

                0, 0, 0, 0 } } };


    private int type;

    private int turnState;

    private int x, y;// 当前块的位置---左上角的坐标

    private int score = 0;


    TimerListener listener1 = new TimerListener();    //监听器

    Timer timer = new Timer(1000, listener1);  //定时器


    public TetrisPanel() {

        newGame();

        nextBlock();

        timer.start();

    }


    void newGame() {

        // 初始化游戏地图

        for (int i = 0; i < 12; i++) {  //列

            for (int j = 0; j < 21; j++) {  //行

                if (i == 0 || i == 11) {// 边框

                    map[i][j] = 1;  //透明度

                } else {

                    map[i][j] = 0;

                }

            }

            map[i][21] = 1;

        }

        score = 0;

    }


    void nextBlock() {

        type = (int) (Math.random() * 1000) % 7; // 总共七种类型

        turnState = (int) (Math.random() * 1000) % 4; // 总共四种转换方法


        x = 3;   //起始下落坐标

        y = 0;

        if (crash(x, y, type, turnState) == 0) {

            timer.stop();

            int op = JOptionPane.showConfirmDialog(null,

                    "Game Over!...再来一局吗?!","提示",JOptionPane.YES_NO_OPTION);

            if (op == JOptionPane.YES_OPTION) {

                newGame();

                listener1 = new TimerListener();

                Timer timer = new Timer(1000, listener1);

                timer.start();

            } else if (op == JOptionPane.NO_OPTION) {

                System.exit(0);

            }

        }

    }


    void down() {

        if (crash(x, y + 1, type, turnState) == 0) {// 判断当前块往下落一格后是否和地图存在填充块完全重合---注意实参:y+1

            add(x, y, type, turnState);// 把该块加到地图---形成堆积块

            nextBlock();

        } else {

            y++;

        }

        repaint();    //刷新页面

    }


    void left() {

        if (x >= 0) {

            x -= crash(x - 1, y, type, turnState);

        }

        repaint();

    }


    void right() {

        if (x < 8) {

            x += crash(x + 1, y, type, turnState);

        }

        repaint();

    }

    void turn() {

        if (crash(x, y, type, (turnState + 1) % 4) == 1) {

            turnState = (turnState + 1) % 4;

        }

        repaint();

    }


    // 让一个块堆积,其实是把当前块中的填充块信息记录到map[][]中

    private void add(int x, int y, int type, int turnState) {

        for (int a = 0; a < 4; a++) {

            for (int b = 0; b < 4; b++) {

                if (shapes[type][turnState][a * 4 + b] == 1) {

                    map[x + b + 1][y + a] = 1;

                }

            }

        }

        tryDelLine();

    }


    // 消块

    private void tryDelLine() {

        // 从上往下,一行行依次遍历,如果某一行的map[i][j]值全是1,则把这一行消掉---上一行往下落

        for (int b = 0; b < 21; b++) {

            int c = 1;

            for (int a = 0; a < 12; a++) {

                c &= map[a][b];

            }

            if (c == 1) {// 全是1--下落一行

                score += 10;

                for (int d = b; d > 0; d--) {

                    for (int e = 0; e < 11; e++) {

                        map[e][d] = map[e][d - 1];

                    }

                }

            }

        }

    }


    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;// 没有碰撞

    }


    // 表现层

    @Override

    public void paint(Graphics g) {

        super.paint(g);// 清除残影


        // 画当前块

        for (int j = 0; j < 16; j++) {

            if (shapes[type][turnState][j] == 1) {

                g.setColor(Color.green);

                g.fillRect((j % 4 + x + 1) * 30, (j / 4 + y) * 30, 30, 30);

            }

        }


        // 画地图(整个游戏的方块区和边框)

        for (int i = 0; i < 12; i++) {

            for (int j = 0; j < 22; j++) {

                if (map[i][j] == 1) {

                    g.setColor(Color.black);    //边框,墙体

                    g.fillRect(i * 30, j * 30, 30, 30);// 填充

                }

            }

        }

        // 显示分数,同时为版面美观,在界面上再加点东西

        // 画方块区右侧部分

        g.setColor(Color.blue);

        g.setFont(new Font("aa", Font.BOLD, 26));

        g.drawString("score : " + score, 395, 100);


        //画游戏区的线格

        for (int i = 1; i < 11; i++) {

            for (int j = 0; j < 21; j++) {

                g.setColor(Color.CYAN);

                g.drawRect(i * 30, j * 30, 30, 30);

            }

        }

    }


    class TimerListener extends KeyAdapter implements ActionListener {

        @Override

        public void keyPressed(KeyEvent e) {

            switch (e.getKeyCode()) {

                case KeyEvent.VK_DOWN:

                case KeyEvent.VK_S:

                    down();

                    break;

                case KeyEvent.VK_LEFT:

                case KeyEvent.VK_A:

                    left();

                    break;

                case KeyEvent.VK_RIGHT:

                case KeyEvent.VK_D:

                    right();

                    break;

                case KeyEvent.VK_UP:

                case KeyEvent.VK_W:

                    turn();

            }

        }


        @Override

        public void actionPerformed(ActionEvent e) {

            down();

        }

    }

}

public class Tetris extends JFrame {


    static JButton button1 = new JButton("重来");

    static JButton button2 = new JButton("暂停");

    static JButton button3 = new JButton("退出");


    static int flag = 0;

    Tetris(){

        setTitle("俄罗斯方块");

        setVisible(true);

        setLocation(450, 100);

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        setSize(550, 697);

        setResizable(false);

        setFocusable(true);  //表明此 Component 是否可以获得焦点

    }

    public static void main(String[] args) {

        Tetris te = new Tetris();

        TetrisPanel tp = new TetrisPanel();

        te.add(tp);


        button1.setBounds(390,300,120,40);

        button2.setBounds(390,390,120,40);

        button3.setBounds(390,480,120,40);

        button1.setFocusable(false);

        button2.setFocusable(false);

        tp.add(button1);

        tp.add(button2);

        tp.add(button3);

        tp.setLayout(null);

        te.addKeyListener(tp.listener1); // 让框架来监听键盘


        button1.addActionListener(e -> {

            if(e.getSource() == button1){

                tp.timer.stop();

                int a = JOptionPane.showConfirmDialog(null, "确定要重新开始吗?","提示",JOptionPane.YES_NO_OPTION);

                if(a == JOptionPane.YES_OPTION){

                    button2.setText("暂停");

                    tp.timer.start();

                    tp.newGame();

                    tp.nextBlock();

                }else if(a == JOptionPane.NO_OPTION){

                    System.exit(0);

                }

            }

        });

        button2.addActionListener(e -> {

            if(e.getSource() == button2){

                if(flag == 0){

                    button2.setText("继续游戏");

                    tp.timer.stop();

                    flag = 1;

                }else if(flag == 1){

                    button2.setText("暂停");

                    tp.timer.start();

                    flag = 0;

                }

            }

        });

        button3.addActionListener(e -> {

            if(e.getSource() == button3){

                tp.timer.stop();

                int a = JOptionPane.showConfirmDialog(null,"确定要退出游戏吗?","退出",JOptionPane.YES_NO_OPTION);

                if(a == JOptionPane.YES_OPTION){

                    System.exit(0);

                }

            }

        });

    }

}

代码实现效果如下:

attachments-2023-03-DpIMvtu964262e10afbe0.png

更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。

想高效系统的学习Java编程语言,推荐大家关注一个微信公众号:Java圈子。每天分享行业资讯、技术干货供大家阅读,关注即可免费领取整套Java入门到进阶的学习资料以及教程,感兴趣的小伙伴赶紧行动起来吧。

attachments-2023-03-2AoKIjPQ64014b4ad30a3.jpg

  • 发表于 2023-03-31 08:51
  • 阅读 ( 266 )
  • 分类:Java开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
王昭君
王昭君

209 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1320 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章