Java_电脑版_2048小游戏

  今天翻了翻电脑, 突然看到这个小东西, 拿来和大家分享一下

import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

/**
 * @author 吴庆龙
 */
public class Game_2048 {

    private JFrame jf = null;
    private JPanel contentPane = null;
    private int[][] num = new int[4][4];
    private JLabel[][] labels = new JLabel[4][4];
    private Random r = new Random();

    boolean flag = false;
    boolean isWin = false;

    public static void main(String[] args) {
        new Game_2048().go();
    }

    public void go() {
        jf = new JFrame("2048小游戏");
        jf.setResizable(false);
        init();
        initNum();
        jf.setVisible(true);
        jf.addKeyListener(new MyKeyListener());
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /**
         * 开启一个线程用来判断用户的输赢的
         */
        new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    int length = num.length;
                    while (!isWin) {
                        int count = 0;
                        for (int i = 0; i < length; i++) {
                            for (int j = 0; j < length - 1; j++) {
                                if (num[i][j] != num[i][j + 1] && num[i][j] != 0 && num[i][j + 1] != 0) {
                                    count++;
                                }
                            }
                        }
                        for (int i = 0; i < length; i++) {
                            for (int j = 0; j < length - 1; j++) {
                                if (num[j][i] != num[j + 1][i] && num[j][i] != 0 && num[j + 1][i] != 0) {
                                    count++;
                                }
                            }
                        }
                        for (int i = 0; i < length; i++) {
                            for (int j = 0; j < length - 1; j++) {
                                if (num[j][i] == 2048) {
                                    JOptionPane.showMessageDialog(jf, "你是王者!");
                                    isWin = true;
                                }
                            }
                        }
                        if (count == 24) {
                            JOptionPane.showMessageDialog(jf, "没关系,再来一次");
                            isWin = true;
                        }
                    }
                }
            }
        }).start();
    }

    /**
     * 初始化JLabel
     */
    public void initNum() {
        for (int i = 0; i < labels.length; i++) {
            for (int j = 0; j < labels[i].length; j++) {
                if (num[i][j] != 0) {
                    labels[i][j].setText(num[i][j] + "");
                    if (num[i][j] == 2) {
                        labels[i][j].setBackground(new Color(255, 255, 0));
                    } else if (num[i][j] == 4) {
                        labels[i][j].setBackground(new Color(223, 159, 0));
                    } else if (num[i][j] == 8) {
                        labels[i][j].setBackground(new Color(231, 69, 8));
                    } else if (num[i][j] == 16) {
                        labels[i][j].setBackground(new Color(102, 118, 151));
                    } else if (num[i][j] == 32) {
                        labels[i][j].setBackground(new Color(43, 68, 235));
                    } else if (num[i][j] == 64) {
                        labels[i][j].setBackground(new Color(255, 0, 255));
                    } else if (num[i][j] == 128) {
                        labels[i][j].setBackground(new Color(255, 80, 6));
                    } else if (num[i][j] == 256) {
                        labels[i][j].setBackground(new Color(6, 219, 0));
                    } else if (num[i][j] == 512) {
                        labels[i][j].setBackground(new Color(205, 254, 104));
                    } else if (num[i][j] == 1024) {
                        labels[i][j].setBackground(new Color(165, 165, 165));
                    } else if (num[i][j] == 2048) {
                        labels[i][j].setBackground(new Color(232, 192, 58));
                    } else if (num[i][j] == 4096) {
                        labels[i][j].setBackground(new Color(255, 80, 6));
                    }
                } else {
                    labels[i][j].setText("");
                    labels[i][j].setBackground(new Color(255, 255, 255));
                }
            }
        }
    }

    /**
     * 初始化界面
     */
    public void init() {
        num[r.nextInt(4)][r.nextInt(4)] = 2;
        for (int i = 0; i < labels.length; i++) {
            for (int j = 0; j < labels[i].length; j++) {
                labels[i][j] = new JLabel();
            }
        }

        JMenu menu = new JMenu("重新开始");
        menu.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                for (int i = 0; i < num.length; i++) {
                    for (int j = 0; j < num[i].length; j++) {
                        num[i][j] = 0;
                    }
                }
                num[r.nextInt(4)][r.nextInt(4)] = 2;
                initNum();
                isWin = false;
            }
        });

        JMenu help = new JMenu("帮助");
        help.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JOptionPane.showMessageDialog(jf, "用键盘上的:↑ ← → ↓ 来控制移动。");
            }
        });
        JMenu about = new JMenu("关于");
        about.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JOptionPane.showMessageDialog(jf, "制作:吴庆龙\nQQ:1554165134");
            }
        });

        JMenuBar bar = new JMenuBar();
        bar.add(menu);
        bar.add(help);
        bar.add(about);

        jf.setJMenuBar(bar);

        jf.setFont(null);
        jf.setBounds(100, 100, 368, 410);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        jf.setContentPane(contentPane);
        contentPane.setLayout(null);

        labels[0][0] = new JLabel();
        labels[0][0].setOpaque(true);
        labels[0][0].setFont(new Font("宋体", Font.PLAIN, 40));
        labels[0][0].setHorizontalAlignment(SwingConstants.CENTER);
        labels[0][0].setBounds(0, 0, 85, 85);
        contentPane.add(labels[0][0]);

        labels[0][1] = new JLabel();
        labels[0][1].setFont(new Font("宋体", Font.PLAIN, 40));
        labels[0][1].setOpaque(true);
        labels[0][1].setHorizontalAlignment(SwingConstants.CENTER);
        labels[0][1].setBounds(90, 0, 85, 85);
        contentPane.add(labels[0][1]);

        labels[0][2] = new JLabel();
        labels[0][2].setFont(new Font("宋体", Font.PLAIN, 40));
        labels[0][2].setOpaque(true);
        labels[0][2].setHorizontalAlignment(SwingConstants.CENTER);
        labels[0][2].setBounds(180, 0, 85, 85);
        contentPane.add(labels[0][2]);

        labels[0][3] = new JLabel();
        labels[0][3].setFont(new Font("宋体", Font.PLAIN, 40));
        labels[0][3].setOpaque(true);
        labels[0][3].setHorizontalAlignment(SwingConstants.CENTER);
        labels[0][3].setBounds(270, 0, 85, 85);
        contentPane.add(labels[0][3]);

        labels[1][0] = new JLabel();
        labels[1][0].setFont(new Font("宋体", Font.PLAIN, 40));
        labels[1][0].setOpaque(true);
        labels[1][0].setHorizontalAlignment(SwingConstants.CENTER);
        labels[1][0].setBounds(0, 90, 85, 85);
        contentPane.add(labels[1][0]);

        labels[1][1] = new JLabel();
        labels[1][1].setFont(new Font("宋体", Font.PLAIN, 40));
        labels[1][1].setOpaque(true);
        labels[1][1].setHorizontalAlignment(SwingConstants.CENTER);
        labels[1][1].setBounds(90, 90, 85, 85);
        contentPane.add(labels[1][1]);

        labels[1][2] = new JLabel();
        labels[1][2].setFont(new Font("宋体", Font.PLAIN, 40));
        labels[1][2].setOpaque(true);
        labels[1][2].setHorizontalAlignment(SwingConstants.CENTER);
        labels[1][2].setBounds(180, 90, 85, 85);
        contentPane.add(labels[1][2]);

        labels[1][3] = new JLabel();
        labels[1][3].setFont(new Font("宋体", Font.PLAIN, 40));
        labels[1][3].setOpaque(true);
        labels[1][3].setHorizontalAlignment(SwingConstants.CENTER);
        labels[1][3].setBounds(270, 90, 85, 85);
        contentPane.add(labels[1][3]);

        labels[2][0] = new JLabel();
        labels[2][0].setFont(new Font("宋体", Font.PLAIN, 40));
        labels[2][0].setOpaque(true);
        labels[2][0].setHorizontalAlignment(SwingConstants.CENTER);
        labels[2][0].setBounds(0, 180, 85, 85);
        contentPane.add(labels[2][0]);

        labels[2][1] = new JLabel();
        labels[2][1].setFont(new Font("宋体", Font.PLAIN, 40));
        labels[2][1].setOpaque(true);
        labels[2][1].setHorizontalAlignment(SwingConstants.CENTER);
        labels[2][1].setBounds(90, 180, 85, 85);
        contentPane.add(labels[2][1]);

        labels[2][2] = new JLabel();
        labels[2][2].setFont(new Font("宋体", Font.PLAIN, 40));
        labels[2][2].setOpaque(true);
        labels[2][2].setHorizontalAlignment(SwingConstants.CENTER);
        labels[2][2].setBounds(180, 180, 85, 85);
        contentPane.add(labels[2][2]);

        labels[2][3] = new JLabel();
        labels[2][3].setFont(new Font("宋体", Font.PLAIN, 40));
        labels[2][3].setOpaque(true);
        labels[2][3].setHorizontalAlignment(SwingConstants.CENTER);
        labels[2][3].setBounds(270, 180, 85, 85);
        contentPane.add(labels[2][3]);

        labels[3][0] = new JLabel();
        labels[3][0].setFont(new Font("宋体", Font.PLAIN, 40));
        labels[3][0].setOpaque(true);
        labels[3][0].setHorizontalAlignment(SwingConstants.CENTER);
        labels[3][0].setBounds(0, 270, 85, 85);
        contentPane.add(labels[3][0]);

        labels[3][1] = new JLabel();
        labels[3][1].setFont(new Font("宋体", Font.PLAIN, 40));
        labels[3][1].setOpaque(true);
        labels[3][1].setHorizontalAlignment(SwingConstants.CENTER);
        labels[3][1].setBounds(90, 270, 85, 85);
        contentPane.add(labels[3][1]);

        labels[3][2] = new JLabel();
        labels[3][2].setFont(new Font("宋体", Font.PLAIN, 40));
        labels[3][2].setOpaque(true);
        labels[3][2].setHorizontalAlignment(SwingConstants.CENTER);
        labels[3][2].setBounds(180, 270, 85, 85);
        contentPane.add(labels[3][2]);

        labels[3][3] = new JLabel();
        labels[3][3].setFont(new Font("宋体", Font.PLAIN, 40));
        labels[3][3].setOpaque(true);
        labels[3][3].setHorizontalAlignment(SwingConstants.CENTER);
        labels[3][3].setBounds(270, 270, 85, 85);
        contentPane.add(labels[3][3]);

    }

    /**
     * 随机产生2或者4
     */
    public void randomNum() {
        while (true) {
            int i = r.nextInt(4);
            int j = r.nextInt(4);
            int k = r.nextInt(2);
            if (k == 1) {
                if (num[i][j] == 0) {
                    num[i][j] = 4;
                    break;
                }
            }
            if (k == 0) {
                if (num[i][j] == 0) {
                    num[i][j] = 2;
                    break;
                }
            }
        }
    }

    /**
     * 键盘监听器
     * 
     * @author Administrator
     * 
     */
    class MyKeyListener implements KeyListener {

        @Override
        public void keyTyped(KeyEvent e) {
        }

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_UP) {
                // [0][0] [0][1] [0][2] [0][3]
                // [1][0] [1][1] [1][2] [1][3]
                // [2][0] [2][1] [2][2] [2][3]
                // [3][0] [3][1] [3][2] [3][3]
                int length = num.length;
                for (int i = 0; i < length; i++) {
                    for (int j = 0; j < length - 1; j++) {
                        // 中间有一个 0
                        if (num[j][i] == 0) {
                            for (int j2 = j + 1; j2 < length - 1; j2++) {
                                if (num[j2][i] != 0) {
                                    num[j][i] = num[j2][i];
                                    num[j2][i] = 0;
                                    flag = true;
                                    break;
                                }
                            }
                        }
                    }
                }
                for (int i = 0; i < length; i++) {
                    if (num[3][i] != 0) {
                        for (int j2 = 0; j2 < length; j2++) {
                            if (num[j2][i] == 0) {
                                num[j2][i] = num[3][i];
                                num[3][i] = 0;
                                flag = true;
                            }
                        }

                    }
                }
                for (int i = 0; i < length; i++) {
                    for (int j = 0; j < length - 1; j++) {
                        // 中间没有 0
                        if (num[j][i] == num[j + 1][i] && num[j][i] != 0) {
                            num[j][i] *= 2;
                            for (int j2 = j + 1; j2 < length - 1; j2++) {
                                num[j2][i] = num[j2 + 1][i];
                            }
                            num[3][i] = 0;
                            flag = true;
                        }
                    }
                }
                if (flag) {
                    randomNum();
                    initNum();
                    flag = false;
                }
            } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                int length = num.length;
                for (int i = length - 1; i >= 0; i--) {
                    for (int j = length - 1; j >= 0; j--) {
                        // 中间有一个 0
                        if (num[j][i] == 0) {
                            for (int j2 = j - 1; j2 > 0; j2--) {
                                if (num[j2][i] != 0) {
                                    num[j][i] = num[j2][i];
                                    num[j2][i] = 0;
                                    flag = true;
                                    break;
                                }
                            }
                        }
                    }
                }
                for (int i = length - 1; i >= 0; i--) {
                    if (num[0][i] != 0) {
                        for (int j2 = length - 1; j2 > 0; j2--) {
                            if (num[j2][i] == 0) {
                                num[j2][i] = num[0][i];
                                num[0][i] = 0;
                                flag = true;
                            }
                        }
                    }
                }

                for (int i = length - 1; i >= 0; i--) {
                    for (int j = length - 1; j > 0; j--) {
                        // 中间没有 0
                        if (num[j][i] == num[j - 1][i] && num[j][i] != 0) {
                            num[j][i] *= 2;
                            for (int j2 = j - 1; j2 > 0; j2--) {
                                num[j2][i] = num[j2 - 1][i];
                            }
                            num[0][i] = 0;
                            flag = true;
                        }
                    }
                }
                if (flag) {
                    randomNum();
                    initNum();
                    flag = false;
                }
            } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                int length = num.length;
                for (int i = 0; i < length; i++) {
                    for (int j = 0; j < length - 1; j++) {
                        // 中间有一个 0
                        if (num[i][j] == 0) {
                            for (int j2 = j + 1; j2 < length - 1; j2++) {
                                if (num[i][j2] != 0) {
                                    num[i][j] = num[i][j2];
                                    num[i][j2] = 0;
                                    flag = true;
                                    break;
                                }
                            }
                        }
                    }
                }
                for (int i = 0; i < length; i++) {
                    if (num[i][3] != 0) {
                        for (int j2 = 0; j2 < length - 1; j2++) {
                            if (num[i][j2] == 0) {
                                num[i][j2] = num[i][3];
                                num[i][3] = 0;
                                flag = true;
                            }
                        }

                    }
                }

                for (int i = 0; i < length; i++) {
                    for (int j = 0; j < length - 1; j++) {
                        // 中间已经没有 0 了
                        if (num[i][j] == num[i][j + 1] && num[i][j] != 0) {
                            num[i][j] *= 2;
                            for (int j2 = j + 1; j2 < length - 1; j2++) {
                                num[i][j2] = num[i][j2 + 1];
                            }
                            num[i][3] = 0;
                            flag = true;
                        }
                    }
                }
                if (flag) {
                    randomNum();
                    initNum();
                    flag = false;
                }
            } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                int length = num.length;
                for (int i = 3; i >= 0; i--) {
                    for (int j = 3; j > 0; j--) {
                        // 中间有一个 0
                        if (num[i][j] == 0) {
                            for (int j2 = j - 1; j2 > 0; j2--) {
                                if (num[i][j2] != 0) {
                                    num[i][j] = num[i][j2];
                                    num[i][j2] = 0;
                                    flag = true;
                                    break;
                                }
                            }
                        }
                    }
                }
                for (int i = 3; i >= 0; i--) {
                    if (num[i][0] != 0) {
                        for (int j2 = length - 1; j2 > 0; j2--) {
                            if (num[i][j2] == 0) {
                                num[i][j2] = num[i][0];
                                num[i][0] = 0;
                                flag = true;
                            }
                        }

                    }
                }

                for (int i = length - 1; i >= 0; i--) {
                    for (int j = length - 1; j > 0; j--) {
                        // 中间已经没有 0 了
                        if (num[i][j] == num[i][j - 1] && num[i][j] != 0) {
                            num[i][j] *= 2;
                            for (int j2 = j - 1; j2 > 0; j2--) {
                                num[i][j2] = num[i][j2 - 1];
                            }
                            num[i][0] = 0;
                            flag = true;
                        }
                    }
                }
                if (flag) {
                    randomNum();
                    initNum();
                    flag = false;
                }
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {

        }

    }

}

 

你可能感兴趣的:(Java_电脑版_2048小游戏)