📄 clear_bomb.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
//载入背景图片的类
class ImgJPanel extends JPanel
{
private ImageIcon icon1;
public ImgJPanel(ImageIcon icon_a)
{
this.icon1 = icon_a;
}
public void paintComponent(Graphics g)
{
g.drawImage(icon1.getImage(),0,0,this);
}
}
public class Clear_Bomb implements ActionListener,MouseListener
{
private Timer timer;
private JButton but[]=new JButton[480];
private JLabel lb[]=new JLabel[480];
private ImageIcon img0,img1,img2,img3,img4,img5;
private JButton start_button,stop_button,win_button,replay_button,toplist_button,saveAs_button,loadAs_button;
private JButton time_button,name_button;
private JLabel time1 = new JLabel("000 s",JLabel.CENTER);
private JLabel count1 = new JLabel("000 s 99",JLabel.CENTER);
private String name1,name2,name3,timer1,timer2,timer3;
private int p_time,mine,game_status;
private int s,l,cwidth;
private int llist[]=new int[480]; //记录该方格是否有地雷
private int flag_list[]=new int[480]; //记录该方格状态(0是未被点击过,1是被左键点击过,既不可恢复,2是右键点击过,即插旗,可通过再次点击右键恢复)
private int replay[]=new int[480]; //按顺序纪录被点击方格
private int vflag[]=new int[480]; //和replay[]对应顺序纪录是左键还是右键点击的(1是左键,2是右键)
public Clear_Bomb()
{
JFrame f=new JFrame("Mine--Made by lanfrog");
ImageIcon ii = new ImageIcon("jmine.jpg");
f.setIconImage(ii.getImage());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setSize(518,324);
f.setLocation(2480,200);
f.setResizable(false);
Container contentPane=f.getContentPane();
ImageIcon icon1=new ImageIcon("start.jpg");
start_button=new JButton(icon1);
start_button.setSize(22,22);
start_button.setLocation(2480,0);
start_button.addActionListener(this);
contentPane.add(start_button);
ImageIcon icon2=new ImageIcon("stop.jpg");
stop_button=new JButton(icon2);
stop_button.setSize(22,22);
stop_button.setLocation(2480,0);
stop_button.setVisible(false);
stop_button.addActionListener(this);
contentPane.add(stop_button);
ImageIcon icon3=new ImageIcon("win.jpg");
win_button=new JButton(icon3);
win_button.setSize(22,22);
win_button.setLocation(2480,0);
win_button.setVisible(false);
win_button.addActionListener(this);
contentPane.add(win_button);
replay_button=new JButton();
replay_button.setSize(75,25);
replay_button.setLocation(80,0);
replay_button.setText("Replay");
replay_button.setVisible(false);
replay_button.addActionListener(this);
contentPane.add(replay_button);
saveAs_button=new JButton();
saveAs_button.setSize(75,25);
saveAs_button.setLocation(0,0);
saveAs_button.setText("saveAs..");
saveAs_button.addActionListener(this);
contentPane.add(saveAs_button);
loadAs_button=new JButton();
loadAs_button.setSize(75,25);
loadAs_button.setLocation(80,0);
loadAs_button.setText("load..");
loadAs_button.addActionListener(this);
contentPane.add(loadAs_button);
toplist_button=new JButton();
toplist_button.setSize(75,25);
toplist_button.setLocation(160,0);
toplist_button.setText("Top3");
toplist_button.addActionListener(this);
contentPane.add(toplist_button);
ImageIcon grdIcon=new ImageIcon("background.jpg");
ImgJPanel pan1=new ImgJPanel(grdIcon);
pan1.setLayout(null);
pan1.setSize(512,273);
pan1.setLocation(0,25);
contentPane.add(pan1);
count1.setBounds(300,0,130,23);
count1.setForeground(Color.blue);
contentPane.add(count1);
time1.setBounds(2480,0,60,23);
time1.setForeground(Color.red);
contentPane.add(time1);
img0=new ImageIcon("but01.jpg");
img1=new ImageIcon("but02.jpg");
img2=new ImageIcon("but03.jpg");
img3=new ImageIcon("but04.jpg");
img4=new ImageIcon("but05.jpg");
img5=new ImageIcon("but06.jpg");
for(int i=0;i<480;i++)
{
but[i]=new JButton(img0);
but[i].setSize(16,16);
but[i].setLocation(1+(i%30)*17,(int)(i/30)*17);
but[i].addMouseListener(this);
pan1.add(but[i]);
lb[i]=new JLabel("",JLabel.CENTER);
lb[i].setBounds(1+(i%30)*17,(int)(i/30)*17,16,16);
lb[i].setVisible(false);
pan1.add(lb[i]);
}
f.show();//使JFrame变成可见的
f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});
timer=new Timer(1000,this);
}
public static void main(String[] args)
{
new Clear_Bomb();
}
public void actionPerformed(ActionEvent e)
{
int h;
if(e.getSource()==win_button || e.getSource()==stop_button || e.getSource()==start_button)
{
p_time=0;
mine=99;
count1.setText("000 s 99");
reset_game();
game_status=1;
timer.start();
win_button.setVisible(false);
stop_button.setVisible(false);
start_button.setVisible(true);
replay_button.setVisible(false);
}
if(e.getSource() == timer && game_status!=0)
{
p_time++;
if(game_status==1)
{ if((int)p_time/10 == 0)
{ count1.setText("00" + p_time + " s " + String.valueOf(mine));}
else if((int)p_time/100 == 0)
{ count1.setText("0" + p_time + " s " + String.valueOf(mine));}
else { count1.setText(p_time + " s " + String.valueOf(mine));}
}
if(game_status==2)
{ if(l < s)
{ if(p_time % 2 == 0)
{ but[replay[l]].setIcon(img3);}
if(p_time % 2 == 1)
{ if(vflag[l]==1) { mouse(replay[l],1);}
else { mouse(replay[l],2);}
l++;
}
}
replay_button.setVisible(false);
}
}
if(e.getSource() == replay_button)
{
p_time=0;
mine=99;
count1.setText("000 s 99");
game_status=2;
but_solu();
timer.start();
win_button.setVisible(false);
stop_button.setVisible(false);
start_button.setVisible(false);
replay_button.setVisible(false);
}
if(e.getSource() == toplist_button)
{
showToplist();
}
if (e.getSource() == saveAs_button)
{
saveAS();
}
if (e.getSource() == loadAs_button)
{
loadAs();
}
}
//重设游戏状态
void reset_game()
{ int i,j,k,x;
//产生地雷组数
for(i=0;i<480;i++)
{ if(i<99)
{ llist[i] = 9;}
else
{ llist[i] = 0;}
}
//随即排列地雷组数
for(i=0;i<4800;i++)
{ j = (int)(Math.random() * 480);
k = (int)(Math.random() * 480);
x = llist[j];
llist[j] = llist[k];
llist[k] = x;
}
count_mine();
but_solu();
p_time=0;
mine=99;
s=0;
l=0;
}
//设定方块状态
void but_solu()
{ for(int i=0;i<480;i++)
{ but[i].setIcon(img0);
but[i].setVisible(true);
flag_list[i] = 0;
if(llist[i] == 9)
{ lb[i].setText("");}
else { lb[i].setText(String.valueOf(llist[i]));}
lb[i].setVisible(false);
if(game_status==1)
{
replay[i] = 0;
vflag[i] = 0;
}
}
}
//统计周围地雷数
void count_mine()
{ int c,i,px,py;
for(i=0;i<480;i++)
{ if(llist[i] != 9)
{ px = i % 30;
py = (int)(i/30);
c = 0;
if((px - 1) >= 0 && (py - 1) >= 0) //左上方(-1,-1)
{ if(llist[(px - 1) + (py - 1) * 30] == 9)
{ c = c + 1;}
}
if((py - 1) >= 0) //上方(0,-1)
{ if(llist[px + (py - 1) * 30] == 9)
{ c = c + 1;}
}
if((px + 1) < 30 && (py - 1) >= 0) //右上方(+1,-1)
{ if(llist[(px + 1) + (py - 1) * 30] == 9)
{ c = c + 1;}
}
if((px - 1) >= 0) //左方(-1,0)
{ if(llist[(px - 1) + py * 30] == 9)
{ c = c + 1;}
}
if((px + 1) < 30) //右方(+1,0)
{ if(llist[(px + 1) + py * 30] == 9)
{ c = c + 1;}
}
if((px - 1) >= 0 && (py + 1) < 16) //左下方(-1,+1)
{ if(llist[(px - 1) + (py + 1) * 30] == 9)
{ c = c + 1;}
}
if((py + 1) < 16) //下方(0,+1)
{ if(llist[px + (py + 1) * 30] == 9)
{ c = c + 1;}
}
if((px + 1) < 30 && (py + 1) < 16) //右下方(+1,+1)
{ if(llist[(px + 1) + (py + 1) * 30] == 9)
{ c = c + 1;}
}
llist[i] = c;
}
}
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e)
{ int bt,m1,m2;
m2=1;
if((e.getModifiers() & InputEvent.BUTTON3_MASK)==InputEvent.BUTTON3_MASK)
{ m2=2; }
if(game_status == 1)
{ for(m1=0;m1<480;m1++)
{ if(e.getSource() == but[m1])
mouse(m1,m2);
}
}
}
//方格处理程序
void mouse(int index,int bt)
{ int n,fg1,fg2,px,py;
if(bt == 1 && flag_list[index] == 0) //在该格处首次按下左键
{
flag_list[index] = 1;
replay[s] = index;
vflag[s] = 1;
s++;
switch(llist[index])
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -