怎样用JAVA实现扫雷游戏要详细代码?还是只要启动?
java编写实现,代码如下:import Java.awt.*;
import java.awt.event.*;
import javax.Swing.*;
/*按扭类*/
class Bomb extends JButton
{
public int num_x,num_y;//第几号方块
public int BombRoundCount;//周围雷数
public boolean isBomb;//是否为雷
public boolean isClicked;//是否被点击
public int BombFlag;//探雷标记
public boolean isRight;//是否点击右键
public Bomb(int x,int y)
{
BombFlag = 0;
num_x = x;
num_y = y;
BombRoundCount = 0;
isBomb = false;
isClicked = false;
isRight = false;
}
}
/*窗口及算法实现类*/
class MainBomb extends JFrame implements ActionListener,MouseListener
{
public JTextField text;
public Label nowBomb,setBomb;
public int BlockNum,BombNum;//当前方块数当前雷数
public Icon icon_bomb = new ImageIcon("Bomb.gif");//踩雷
public Icon icon_bomb_big = new ImageIcon("bomb_big.gif");//踩雷标记
public Icon icon_flag = new ImageIcon("flag.gif");//雷标记
public Icon icon_question = new ImageIcon("question.gif");//疑惑是否有雷
public JButton start = new JButton(" 开始 ");
public Panel MenuPamel = new Panel();
public Panel mainPanel = new Panel();
public Bomb[][] bombButton;
/*界面设计*/
public MainBomb()
{
super("扫雷Aaron2004制作 2004.8 ");
BlockNum = 64;
BombNum = 10;
Container c=getContentPane();
c.setBackground(Color.gray);
c.setLayout(new BorderLayout());
text=new JTextField("10 ",3);
nowBomb = new Label("当前雷数"+""+BombNum+"");
setBomb= new Label("设置地雷数");
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
BombNum = Integer.parseInt(text.getText().trim());
if(BombNum = 10BombNum50 )
replay();
else
{
JOptionPane msg = new JOptionPane();
JOptionPane.showMessageDialog(null,"您设置的地雷数太多了,请重设!","错误",2);
}
}
} );
MenuPamel.add(setBomb);
MenuPamel.add(text);
MenuPamel.add(start);
MenuPamel.add(nowBomb);
c.add(MenuPamel,"North");
mainPanel.setLayout(new GridLayout( (int)Math.sqrt(BlockNum) , (int)Math.sqrt(BlockNum)) );
bombButton=new Bomb[ (int)Math.sqrt(BlockNum) ][];
for(int i = 0 ; i(int)Math.sqrt(BlockNum) ; i++)
{
bombButton[ i ]=new Bomb[ (int)Math.sqrt(BlockNum) ];
}
for(int i = 0 ; i(int)Math.sqrt(BlockNum) ; i++ )
for(int j = 0 ; j(int)Math.sqrt(BlockNum) ; j++ )
{
bombButton[ i ][ j ]=new Bomb(i,j);
bombButton[ i ][ j ].setForeground( Color.gray);
bombButton[ i ][ j ].addActionListener(this);
bombButton[ i ][ j ].addMouseListener(this);
}
for(int i = 0 ; i(int)Math.sqrt(BlockNum) ; i++ )
for(int j = 0 ; j(int)Math.sqrt(BlockNum) ; j++ )
mainPanel.add(bombButton[ i ][ j ]);
c.add(mainPanel,"Center");
startBomb();
setSize(400,400);
setLocation(350,200);
setResizable(false);
}
/*布雷*/
public void startBomb()
{
for(int i=0;iBombNum;i++)
{
int x =(int)(Math.random()*(int)(Math.sqrt(BlockNum)-1));
int y =(int)(Math.random()*(int)(Math.sqrt(BlockNum)-1));
if(bombButton[ x ][ y ].isBomb==true)
i--;
else
bombButton[ x ][ y ].isBomb=true ;
}
}
/*重新开始*/
public void replay()
{
nowBomb.setText("当前雷数"+""+BombNum+"");
for(int i = 0 ; i(int)Math.sqrt(BlockNum) ; i++)
for(int j = 0 ; j(int)Math.sqrt(BlockNum) ; j++)
{
bombButton[ i ][ j ].isBomb=false;
bombButton[ i ][ j ].isClicked=false;
推荐阅读
- excel如何制作水电费,excel做水电费收据
- python给系统加入自动运行时间,python 自动运行
- 纸巾短视频安卓版,纸巾视频素材
- 直播话术落地练习,直播话术大全完整版
- vb.net的发展历史的简单介绍
- 如何推广卖面膜,怎么推销面膜发朋友圈
- Ios10怎么提取备份,苹果备份怎么提出来
- 怎么看台式机是否减配显卡,怎样看台式机显卡
- php查询数据库的字段名 php查询sqlserver数据库