扫雷 java源代码

本文介绍了一个使用Java Swing实现的扫雷游戏项目。该项目详细展示了如何创建游戏界面、放置地雷、实现点击和标记功能等关键步骤。文章还提供了完整的源代码供读者学习和参考。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class PlayGame extends JFrame 
{        
        Playing   listener = new Playing(this);    
        JPanel landminePanel = new JPanel();
        JPanel topPanel = new JPanel();
        //public static ImageIcon dead = new ImageIcon("images/dead.gif");
        public ImageIcon dead = new ImageIcon(this.getClass().getResource("images/dead.gif"));
        //public static ImageIcon smallIcon   = new ImageIcon("images/smallIcon_10.png");  
        public ImageIcon smallIcon   = new ImageIcon(this.getClass().getResource("images/smallIcon_10.png"));  
        //public static ImageIcon bombIcon   = new ImageIcon("images/Bomb.gif");        
        public ImageIcon bombIcon   = new ImageIcon(this.getClass().getResource("images/Bomb.gif"));        
       
         
        static int[] ii = new int[40];
        static int[] jj = new int[40];
         
        public static MyButton [][] lei;
        public static int numberOfClicked ;
        public static int numberOfUnflaged ;
        public static JLabel numberOfUnflagedLabel = new JLabel();
         
        public PlayGame()
        {
                super("扫雷");
                setBounds(300,50,565,600);//窗口尺寸
                setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
BorderLayout ff = new BorderLayout();
setLayout(ff);
setResizable(false);
numberOfClicked = 0;
numberOfUnflaged = 40;
//设置地雷面板
JMenuBar bar =   createMenuBar();
setJMenuBar(bar);
GridLayout dd = new GridLayout(16,16);  
landminePanel.setLayout(dd);
landminePanel = createCenterPane();
add(landminePanel,BorderLayout.CENTER);
numberOfUnflagedLabel.setText("剩余雷数:"+numberOfUnflaged);
numberOfUnflagedLabel.setFont(new Font("null",Font.BOLD,20));
numberOfUnflagedLabel.setIcon(bombIcon);
topPanel.add(numberOfUnflagedLabel);
add(topPanel,BorderLayout.NORTH);
addLandmine();
setVisible(true);
        }
         
       
        public static void addLandmine()
        {
                for(int count = 0; count<40;)
                {
                        int i = (int)(Math.random()*100 % 16 +1 ) ;
                        int j = (int)(Math.random()*100 % 16 +1 ) ;
                        if(lei[i][j].isBomb == false)
                        {
                        ii[count] = i;
                        jj[count] = j;
                                lei[i][j].isBomb = true;
                                count++;
                           
                        }                                                      
                }
       
         
         
         
        private JMenuBar createMenuBar(){
    JMenu[] m = {new JMenu("开始(B)"),new JMenu("关于(A)")};

    JMenuBar mBar = new JMenuBar();
    //this.setJMenuBar(mBar);

    mBar.add(m[0]);
    mBar.add(m[1]);

    m[0].setMnemonic('B');
    m[1].setMnemonic('A');

    initMenuBegin(m);
    initMenuAbout(m);

    return mBar;
    }
        private void initMenuBegin(JMenu[] m){
    JMenuItem [] mI = {
    new JMenuItem("新开局(N)"),
    new JMenuItem("退出(E)")
    };

    mI[0].setMnemonic('N');
    mI[1].setMnemonic('E');

    mI[0].setAccelerator(KeyStroke.getKeyStroke("ctrl N"));
    mI[1].setAccelerator(KeyStroke.getKeyStroke("ctrl W"));

    mI[0].addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    menuNewClick();
    }
    }
    );
    mI[1].addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    System.exit(0);
    }
    }
    );

    m[0].add(mI[0]);
    m[0].add(mI[1]);
    m[0].insertSeparator(1);
    }
        public void menuNewClick(){
        landminePanel.removeAll();
    this.remove(landminePanel);
    this.landminePanel = this.createCenterPane();
    addLandmine();
    numberOfClicked = 0;
numberOfUnflaged = 40;
    numberOfUnflagedLabel.setText("剩余雷数:40");
    this.add(this.landminePanel,BorderLayout.CENTER);
    this.validate();
    }
   
    private JPanel createCenterPane() {
    lei = new MyButton[18][18];
for(int i=0; i<18; ++i)
{
        for(int j=0; j<18; ++j)
        {
                lei[i][j] = new MyButton(i,j);
       
}
for(int i=1; i<17; ++i)
{
        for(int j=1; j<17; ++j)
        {
              landminePanel.add(lei[i][j]);    
              lei[i][j].setIcon(smallIcon);    
              //lei[i][j].setSize(30, 30);
              lei[i][j].isClicked = false;      
              lei[i][j].addActionListener(listener); 
              lei[i][j].addMouseListener(listener);
       
}
return landminePanel;
}

private void initMenuAbout(JMenu[] m){
    JMenuItem [] mI = {new JMenuItem("游戏说明(H)"),new JMenuItem("关于作者(A)")};

    mI[0].setMnemonic('H');
    mI[1].setMnemonic('A');

    mI[0].setAccelerator(KeyStroke.getKeyStroke("F1"));

    mI[0].addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    String help0 = "睡不着睡不着   无聊\n";
    String help1 = "夜未眠夜未眠   失眠";
    JOptionPane.showMessageDialog(null, help0 + help1);
    }
    }
    );
    mI[1].addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    String version = "版本: 1.0最终版\n";
    String author = "作者: 你青春还有多久\n";
    String email = "E-mail: 1164191183@qq.com";
    JOptionPane.showMessageDialog(null, version + author + email);
    }
    }
    );

    m[1].add(mI[0]);
    m[1].add(mI[1]);
    }
         
        public static void main(String[] argus)
        {
                PlayGame Zhang = new PlayGame();
             
        }
}


class Playing implements ActionListener,MouseListener
{
        public static PlayGame gui;
//         public static ImageIcon dead2 = new ImageIcon("images/dead.gif");
        public ImageIcon dead2 = new ImageIcon(this.getClass().getResource("images/dead.gif"));
//         public static ImageIcon flagIcon = new ImageIcon("images/flag_2.png");
        public ImageIcon flagIcon = new ImageIcon(this.getClass().getResource("images/flag_2.png"));
        // public static Font zhangCao = new Font("章草",Font.PLAIN,20);
        public Playing(PlayGame in )
        {
                gui = in;
                dead2 = new ImageIcon(this.getClass().getResource("images/dead.gif"));
        }
         
        public void actionPerformed(ActionEvent event)
        {
     
                if(event.getActionCommand()=="关于")
                        JOptionPane.showMessageDialog(null,"扫雷1.0版");
           
                               
                MyButton receive = (MyButton)event.getSource();                      
                if(receive.isBomb==true)
                {
               
                for(int i=0;i<40;i++){
                gui.lei[gui.ii[i]][gui.jj[i]].setIcon(dead2);
                }
               
               
               
               
                   
                        Thread t = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JOptionPane.showMessageDialog(null,"亲,你被shutdown了吗…");
        int yourChose = JOptionPane.showConfirmDialog(null,"好吧我原谅你,再来一局?" );//显示对话框
        if(yourChose == JOptionPane.OK_OPTION)
                        {
                                gui.menuNewClick();
                        }
                        else
                        {
                                System.exit(0);
                        }    
}
});
t.start();
                       
                         
                        //   System.out.println("hahahah");
                }
                else if(receive.isClicked ==false)
                {
                        showBombNumber(receive);
                }
                //receive.setEnabled(false); 
                // System.out.println("hahahah");
                 
               
        }    
        public void showBombNumber(MyButton in)
        {
                int numberOfLandmine = 0;
                in.isClicked = true;    
               
                if(gui.lei[in.num_x-1][in.num_y-1].isBomb == true) numberOfLandmine++;
                if(gui.lei[in.num_x][in.num_y-1].isBomb == true) numberOfLandmine++;                
                if(gui.lei[in.num_x+1][in.num_y-1].isBomb == true) numberOfLandmine++;
                if(gui.lei[in.num_x+1][in.num_y].isBomb == true) numberOfLandmine++;
                if(gui.lei[in.num_x+1][in.num_y+1].isBomb == true) numberOfLandmine++;
                if(gui.lei[in.num_x][in.num_y+1].isBomb == true) numberOfLandmine++;
                if(gui.lei[in.num_x-1][in.num_y+1].isBomb == true) numberOfLandmine++;
                if(gui.lei[in.num_x-1][in.num_y].isBomb == true) numberOfLandmine++;
                in.setIcon(new ImageIcon(this.getClass().getResource("images/"+numberOfLandmine+".png")));
                 
                gui.numberOfClicked++;                
                if(gui.numberOfClicked==216)
                {
                        int yourChoice = JOptionPane.showConfirmDialog(null,"好吧,你赢了!再来一盘吗?");
                        if(yourChoice == JOptionPane.OK_OPTION)
                        gui.menuNewClick();
                        else 
                                System.exit(0);
                }
                        //ifPassed();                
                 
                if(numberOfLandmine==0)
                {
                        if(gui.lei[in.num_x-1][in.num_y-1].isClicked == false)
                                showBombNumber(gui.lei[in.num_x-1][in.num_y-1]);
                        if(gui.lei[in.num_x][in.num_y-1].isClicked == false)
                                showBombNumber(gui.lei[in.num_x][in.num_y-1]);
                        if(gui.lei[in.num_x+1][in.num_y-1].isClicked == false)
                                showBombNumber(gui.lei[in.num_x+1][in.num_y-1]);
                        if(gui.lei[in.num_x+1][in.num_y].isClicked == false)
                                showBombNumber(gui.lei[in.num_x+1][in.num_y]);
                        if(gui.lei[in.num_x+1][in.num_y+1].isClicked == false)
                                showBombNumber(gui.lei[in.num_x+1][in.num_y+1]);
                        if(gui.lei[in.num_x][in.num_y+1].isClicked == false)
                                showBombNumber(gui.lei[in.num_x][in.num_y+1]);
                        if(gui.lei[in.num_x-1][in.num_y+1].isClicked == false)
                                showBombNumber(gui.lei[in.num_x-1][in.num_y+1]);
                        if(gui.lei[in.num_x-1][in.num_y].isClicked == false)
                                showBombNumber(gui.lei[in.num_x-1][in.num_y]);                        
                }
                 
        }
     
        public void mouseClicked(MouseEvent e)
        {
                int mods = e.getModifiers();
                MyButton receive = (MyButton)e.getSource();
                 
                if((mods & InputEvent.BUTTON3_MASK) != 0)//鼠标右键 
                {
                        if(receive.isClicked == false)
                        {      
                                receive.isRight = (receive.isRight==true)?false:true;
                                if(receive.isRight)
                                {
                                        gui.numberOfUnflaged--;
                                        receive.setIcon(flagIcon);
                                }
                                else
                                {
                                        gui.numberOfUnflaged++;
                                        receive.setIcon(new ImageIcon(this.getClass().getResource("images/smallIcon_10.png")));
                                }
                                gui.numberOfUnflagedLabel.setText("剩余雷数:"+gui.numberOfUnflaged);
                        }

                }                
        }
        public void mouseEntered(MouseEvent e){}
        public void mousePressed(MouseEvent e){}
        public void mouseReleased(MouseEvent e){}
        public void mouseExited(MouseEvent e) {}  
}



public class MyButton extends JButton  
        public int num_x,num_y;        
        public int BombRoundCount;    
        public boolean isBomb;        
        public boolean isClicked;    
        public int BombFlag;          
        public boolean isRight;        
 
        public   MyButton(int x, int y) 
       
                BombFlag = 0; 
                num_x = x;
                num_y = y; 
                BombRoundCount = 0; 
                isBomb = false; 
                isClicked = true;
                isRight = false;
        }
必须包含以下图片,个人喜好不同,自己选择喜欢的图片做吧
//...........
扫雷 <wbr>java源代码



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值