ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

软件设计之基于Java的连连看小游戏(三)——所有功能的实现

2020-01-28 15:54:18  阅读:203  来源: 互联网

标签:Java int add 小游戏 static new y1 连连看 x1


新年快乐!
期末接二连三的考试实在太忙了忘记连连看没有更新完,今天想要学习生信时才发现。所以这次直接把连连看所有功能全部放上。

 

在传统的连连看的基础上,我增加了上传头像的功能,即可以自行上传图片作为游戏中要消除的图片,算是设计的一个亮点。

先把游戏截图放上。

(主界面)

(点击上传头像后的对话框)

(游戏界面)

 

 

 (菜单栏)

 

 

 

 代码(最终版,在一、二的基础上做了改进):

  Shown类包含三个界面,frame1为主界面,frame2为游戏界面,frame3为游戏规则。其中简单的事件处理以内部类的形式直接完成,消除及判断功能在Function类中实现,倒计时判断及时间提示功能在Time类中,游戏帮助功能在NewMap类中,包括游戏结束的判断及无解时自动洗牌功能的实现。

(1)Shown类:

  1 public class Shown extends Thread{                
  2     static JFrame frame1=new JFrame("工大连连看");
  3     static JFrame frame2=new JFrame("工大连连看");
  4     static JFrame frame3=new JFrame("游戏规则");
  5     //8*12
  6     static JButton[][] imgbutton= new JButton[10][14];//第三维的0号元素储存button,第1号元素为rand值
  7     static ImageIcon[][] img=new ImageIcon[10][14];//用数组方便后面判断
  8     static ArrayList<String> logo=new ArrayList<String>(Arrays.asList("E:\\学习\\Sophomore\\软件设计\\picture\\c++.jpg","E:\\学习\\Sophomore\\软件设计\\picture\\c.jpg","E:\\学习\\Sophomore\\软件设计\\picture\\java.jpg","E:\\学习\\Sophomore\\软件设计\\picture\\eclipse.jpg","E:\\学习\\Sophomore\\软件设计\\picture\\go.jpg","E:\\学习\\Sophomore\\软件设计\\picture\\codeblocks.jpg","E:\\学习\\Sophomore\\软件设计\\picture\\js.jpg","E:\\学习\\Sophomore\\软件设计\\picture\\liteide.jpg","E:\\学习\\Sophomore\\软件设计\\picture\\PHP.jpg","E:\\学习\\Sophomore\\软件设计\\picture\\python.jpg","E:\\学习\\Sophomore\\软件设计\\picture\\sql.jpg","E:\\学习\\Sophomore\\软件设计\\picture\\vs.jpg"));
  9     static int pic_num=12;//所有的图片数量
 10     static int new_num=0;//已使用新地图次数
 11     public static int help_num=0;//已使用的帮助次数
 12     
 13     static void frame1(){
 14                 
 15         frame1.setLocation(220, 75);//设置窗口出现的位置
 16         JButton pic=new JButton("上传头像");
 17         JButton begin_index=new JButton("开始冒险");
 18         JButton rul=new JButton("游戏规则");
 19         frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
 20                 
 21         //设置窗口图标
 22         Toolkit tool=frame1.getToolkit();
 23         Image image1=tool.getImage("E:\\学习\\Sophomore\\软件设计\\picture\\hit.jpg");
 24         frame1.setIconImage(image1);
 25 
 26         //添加按钮
 27         begin_index.setBackground(Color.yellow);pic.setBackground(Color.white);rul.setBackground(Color.white);//按钮颜色设置
 28         begin_index.setFont(new Font("Dialog",1,20));pic.setFont(new Font("Dialog",1,20));rul.setFont(new Font("Dialog",1,20));//文字大小设置
 29         pic.setFocusPainted(false);    begin_index.setFocusPainted(false);rul.setFocusPainted(false);//去除文字周围虚线框
 30         JPanel panel=new JPanel();
 31         panel.setBackground(null);//panel背景透明
 32         panel.setOpaque(false);
 33         panel.add(pic);
 34         panel.add(begin_index);
 35         panel.add(rul);
 36         pic.setBounds(200, 350, 120, 20);
 37         begin_index.setBounds(400, 350, 150, 40);
 38         rul.setBounds(600, 350, 120, 20);
 39         panel.setBounds(-20, 350, 800, 500);
 40         frame1.add(panel);
 41         
 42         //设置背景图片
 43         frame1.setLayout(null);//只有null才会显示图片
 44         ImageIcon img=new ImageIcon("E:\\学习\\Sophomore\\软件设计\\picture\\frame.jpg");
 45         JLabel imglabel=new JLabel(img);
 46         frame1.add(imglabel);
 47         imglabel.setBounds(0,0,800,450);
 48         
 49         //设置按钮
 50         begin_index.addActionListener(new ActionListener() {//注册事件监听器,点击button时执行方法frame2
 51             public void actionPerformed(ActionEvent event) {
 52                 frame2();
 53             }
 54         });
 55         
 56         pic.addActionListener(new ActionListener() {
 57             public void actionPerformed(ActionEvent event) {
 58                 JFileChooser jfc=new JFileChooser("C:\\");  //打开时的默认路径
 59                 jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);  //只能打开文件
 60                 jfc.addChoosableFileFilter(new FileNameExtensionFilter("jpg","png"));//只能选择JPG或PNG
 61                 jfc.showDialog(jfc, "选择");  
 62                 jfc.setMultiSelectionEnabled(false);//不能同时选择多个图片
 63                 File file=jfc.getSelectedFile();  
 64                 if(file!=null) 
 65                     pic_num++;
 66                 logo.add(file.getAbsolutePath());
 67                 } 
 68         });
 69 
 70         rul.addActionListener(new ActionListener() {
 71             public void actionPerformed(ActionEvent event) {
 72                 frame3();
 73             }
 74         });
 75         
 76         frame1.setSize(810,490);
 77         frame1.setVisible(true);
 78     }
 79     static JPanel p;
 80 
 81     public static void frame2() {
 82         
 83         //获取当前时间        
 84          Calendar now = Calendar.getInstance();
 85          int year=now.get(Calendar.YEAR);
 86          int month=now.get(Calendar.MONTH)+1;
 87          int date=now.get(Calendar.DATE);
 88          int hour=now.get(Calendar.HOUR);
 89          int min=now.get(Calendar.MINUTE)+2;//游戏时间为2分钟
 90          int second=now.get(Calendar.SECOND);
 91         
 92         frame1.setVisible(false);//frame1消失
 93         frame2.setLocation(100, 15);
 94         JPanel panel1=new JPanel();//gridlayout
 95         JPanel panel2=new JPanel();//borderlayout
 96         frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
 97         JLabel jl=new JLabel("剩余洗牌次数:3");
 98         JLabel jl2=new JLabel("剩余帮助次数:3");
 99                 
100         //设置窗口图标
101         Toolkit tool=frame1.getToolkit();
102         Image image1=tool.getImage("E:\\学习\\Sophomore\\软件设计\\picture\\hit.jpg");
103         frame2.setIconImage(image1);
104         
105         //布局
106         GridLayout gridlayout=new GridLayout(10,14,0,0);
107         panel1.setLayout(gridlayout);
108         //borderlayout中添加gridlayout
109         panel2.setLayout(new BorderLayout());
110         frame2.add(panel2);
111         panel2.add(panel1,BorderLayout.CENTER);
112         //frame上为panel2(border),panel2的中间为panel1(grid)
113         
114         //菜单栏
115         JMenuBar menuBar = new JMenuBar();
116         frame2.setJMenuBar(menuBar);
117         JMenu color=new JMenu("背景颜色");
118         JMenu ruler= new JMenu("游戏规则");
119         JMenu help=new JMenu("游戏帮助");
120         //JMenu ex=new JMenu("");
121         menuBar.add(ruler);
122         menuBar.add(color);
123         menuBar.add(help);
124         JMenuItem moren = new JMenuItem("默认");
125         JMenuItem red = new JMenuItem("红色");
126         JMenuItem blue = new JMenuItem("蓝色");
127         JMenuItem green = new JMenuItem("绿色");
128         JMenuItem yellow = new JMenuItem("黄色");
129         JMenuItem ru = new JMenuItem("游戏规则");
130         JMenuItem nm = new JMenuItem("重新洗牌");
131         JMenuItem find=new JMenuItem("查找帮助");
132         
133         help.add(find);
134         help.add(nm);
135         ruler.add(ru);
136         color.add(red);
137         color.add(blue);
138         color.add(green);
139         color.add(yellow);
140         color.add(moren);//#e1edef
141 
142         find.addActionListener(new ActionListener() {
143             @Override
144             public void actionPerformed(ActionEvent e) {
145                 if(help_num<3) {//使用次数小于等于三时可以新地图
146                     help_num++;
147                     jl2.setText("  剩余查找次数:"+(3-Shown.help_num)+"   ");
148                     NewMap.help();
149                 }else {
150                      JOptionPane.showMessageDialog( null, "帮助查找次数已经用完了哦,靠人不如靠自己,加油!");
151                 }
152             }
153         });
154         
155         nm.addActionListener(new ActionListener() {
156             @Override
157             public void actionPerformed(ActionEvent e) {
158                 if(new_num<3) {//使用次数小于等于三时可以新地图
159                     new_num++;
160                     jl.setText("  剩余洗牌次数:"+(3-Shown.new_num)+"  ");
161                     NewMap.newmap();
162                 }else {
163                      JOptionPane.showMessageDialog( null, "洗牌次数已经用完了哦,睁大眼睛继续找吧,fighting!");
164                 }
165             }
166         });
167         
168         moren.addActionListener(new ActionListener() {
169             @Override
170             public void actionPerformed(ActionEvent e) {
171                 for(int i=0;i<10;i++) {
172                     for(int j=0;j<14;j++) {
173                         imgbutton[i][j].setBackground(Color.decode("#e1edef"));
174                     }
175                 }
176             }
177         });
178         red.addActionListener(new ActionListener() {
179                 @Override
180                 public void actionPerformed(ActionEvent e) {
181                     for(int i=0;i<10;i++) {
182                         for(int j=0;j<14;j++) {
183                             imgbutton[i][j].setBackground(Color.red);
184                         }
185                     }
186                 }
187             });
188         blue.addActionListener(new ActionListener() {
189             @Override
190             public void actionPerformed(ActionEvent e) {
191                 for(int i=0;i<10;i++) {
192                     for(int j=0;j<14;j++) {
193                         imgbutton[i][j].setBackground(Color.blue);
194                     }
195                 }
196             }
197         });
198         green.addActionListener(new ActionListener() {
199             @Override
200             public void actionPerformed(ActionEvent e) {
201                 for(int i=0;i<10;i++) {
202                     for(int j=0;j<14;j++) {
203                         imgbutton[i][j].setBackground(Color.green);
204                     }
205                 }
206             }
207         });
208         yellow.addActionListener(new ActionListener() {
209             @Override
210             public void actionPerformed(ActionEvent e) {
211                 for(int i=0;i<10;i++) {
212                     for(int j=0;j<14;j++) {
213                         imgbutton[i][j].setBackground(Color.yellow);
214                     }
215                 }
216             }
217         });
218         ru.addActionListener(new ActionListener() {
219             @Override
220             public void actionPerformed(ActionEvent e) {
221                 frame3();
222             }
223         });
224         
225         //添加按钮及设置菜单背景
226         JPanel panel_button=new JPanel();
227         
228         Time t=new Time();
229         
230         t.timer(year+"-0"+month+"-0"+date+" "+hour+":"+min+":"+second);//yyyy-MM-dd hh:mm:ss
231         
232         //t.timer("2020-01-02 "+hour+":"+min+":"+second);//yyyy-MM-dd HH:mm:ss
233         
234         panel_button.add(jl);
235         panel_button.add(Time.jl0);
236         panel_button.add(jl2);
237         
238         panel_button.setOpaque(true);
239         panel_button.setBackground(Color.decode("#dbe4f5"));
240         panel2.add(panel_button,BorderLayout.NORTH);
241         
242         //添加图形及游戏背景
243         //10行14列的button按矩阵排列,周围button不可点击无img无边框
244 
245         int[] ran=new int[96];
246         List list = new ArrayList();
247         for(int m=0;m<48;m++) {
248             Random r=new Random();
249             int ra=r.nextInt(pic_num);
250             ran[m]=ra;
251             ran[m+48]=ra;
252         }
253          for(int p = 0;p < ran.length;p++){           
254              list.add(ran[p]);
255              } 
256          Collections.shuffle(list);//list打乱顺序
257          for(int i = 0; i < list.size();i++) {//乱序ran[]
258              ran[i]=(int) list.get(i);
259            }
260       
261         Border emptyBorder = BorderFactory.createEmptyBorder(0,0,0,0); 
262         
263         for(int j=0;j<14;j++) {
264             img[0][j]=null;//第0行的img为空
265             img[9][j]=null;//9行的img为空
266         }
267         
268         for (int i=0;i<10;i++) {
269                 img[i][0]=null;
270                 img[i][13]=null;//每一行第一个和最后一个图片为空
271         }
272         int r=0;
273         for(int i=1;i<=8;i++) {
274             for(int j=1;j<=12;j++) {
275                 img[i][j]=new ImageIcon(logo.get(ran[r]));
276                 img[i][j].setImage(img[i][j].getImage().getScaledInstance(60, 56,Image.SCALE_DEFAULT));
277                 r++;
278             }
279         }
280 
281         for(int i=0;i<10;i++) {//0-9行    
282             for(int j=0;j<14;j++) {//0-13列
283                 imgbutton[i][j]=new JButton(img[i][j]);
284                 imgbutton[i][j].setOpaque(true);
285                 imgbutton[i][j].setBackground(Color.decode("#e1edef"));
286                 panel1.add(imgbutton[i][j]);//代表第i行第j列的图形
287                 
288             }
289         
290         }
291     
292         for(int j=0;j<14;j++) {//周围button消除边框和点击功能
293             imgbutton[0][j].setEnabled(false);
294             imgbutton[9][j].setEnabled(false);
295             imgbutton[0][j].setBorder(emptyBorder);
296             imgbutton[9][j].setBorder(emptyBorder);
297         }
298         for(int i=1;i<9;i++) {
299             imgbutton[i][0].setEnabled(false);
300             imgbutton[i][13].setEnabled(false);
301             imgbutton[i][0].setBorder(emptyBorder);
302             imgbutton[i][13].setBorder(emptyBorder);
303         }
304         
305         //添加事件监听器
306         try{
307             for(int i=1;i<9;i++) {
308             for(int j=1;j<13;j++) {
309                 final int m=i;
310                 final int n=j;
311                 imgbutton[i][j].addActionListener(new ActionListener() {
312                     //如果此时栈为空,则入栈;如果栈不为空,则flag判断                
313                     public void actionPerformed(ActionEvent event) {
314                         
315                         imgbutton[m][n].setBackground(Color.red);//点击时变色
316                         
317                         if(Function.stack.empty()) {//如果此时栈为空,点击的button入栈
318                             int[] arr= {m,n};//把点击的button的i j作为数组入栈
319                             Function.stack.push(arr);//数组入栈
320                         }else {
321                             
322                             Function.flag(Function.stack,m,n);//执行flag()函数
323                             
324                             imgbutton[m][n].setBackground(Color.decode("#e1edef"));
325                             imgbutton[Function.num[0]][Function.num[1]].setBackground(Color.decode("#e1edef"));//尝试了很多种表示坐标的方法都执行都出错。只有用Num不报错
326                             //当点击完成一对图形时恢复原色
327                         }    
328                         //frame2.setVisible(false);//检测事件监听器    
329                     }
330                 });
331             }
332          }
333         }catch(Exception e) {
334             System.err.print(e);
335         }
336         frame2.setSize(1000,650);
337         frame2.setVisible(true);
338     }
339     
340     static void frame3() {//游戏规则
341         //frame1.setVisible(false);//frame1消失
342         frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
343         
344         //设置窗口图标
345         Toolkit tool=frame3.getToolkit();
346         Image image1=tool.getImage("E:\\学习\\Sophomore\\软件设计\\picture\\hit.jpg");
347         frame3.setIconImage(image1);
348                 
349         frame3.setLayout(new BorderLayout());
350         frame3.setLocation(250, 75);//设置窗口出现的位置
351         JButton begin_index=new JButton("继续游戏");
352         begin_index.setFocusPainted(false);
353         frame3.setBackground(Color.decode("#e1edef"));
354         
355         //文字
356         JPanel center =new JPanel();
357         ImageIcon img=new ImageIcon("E:\\学习\\Sophomore\\软件设计\\picture\\wenzi.jpg");
358         img.setImage(img.getImage().getScaledInstance(790, 350,Image.SCALE_DEFAULT));        
359         JLabel imglabel=new JLabel(img);
360         center.add(imglabel);
361         frame3.add(center,BorderLayout.CENTER);
362         
363         //设置开始按钮
364         JPanel south=new JPanel();
365         south.setBackground(Color.decode("#ffc6c6"));
366         center.setBackground(Color.decode("#ffc6c6"));
367         south.add(begin_index);
368         frame3.add(south,BorderLayout.SOUTH);
369         begin_index.setBounds(0,0,50,20);//大小设置
370         begin_index.setBackground(Color.white);//按钮颜色设置
371         begin_index.setFont(new Font("Dialog",1,20));//文字大小设置
372         begin_index.addActionListener(new ActionListener() {//注册事件监听器,点击button时执行方法frame2
373             public void actionPerformed(ActionEvent event) {
374                 frame3.setVisible(false);
375             }
376         });
377         
378         frame3.setSize(800,490);
379         frame3.setVisible(true);
380     }
381         
382     static Shown shown1=new Shown();
383     public static void main(String[] args) {
384 
385         shown1.frame1();
386                 
387     }
388 }
Shown

(2)Function类:

  1 public class Function {
  2     public static int num[]=new int[2];
  3     public static Stack<int[]> stack=new Stack<int[]>(); //点击的button的序号入栈
  4     //private Graphics2D g;
  5     
  6     //建立一个数字矩阵表示棋盘,1表示可行,0表示不可行
  7     static int[][] chess=new int[10][14];
  8         
  9     //当栈内不为空时执行flag函数
 10     //先判断是否一致,若一致则road判断路径是否符合要求,再执行link()连线
 11     //ij 为栈内已经存在的button的坐标,m,n为刚刚点击的button的坐标
 12     //使用for循环寻找两个img的
 13     public static void flag(Stack<int[]> stack,int m,int n) {
 14         
 15         num=stack.peek();//peek栈顶元素(未取出)
 16         int i=num[0];int j=num[1];
 17 
 18         //初始化矩阵
 19         for(int x=0;x<14;x++) {
 20             chess[0][x]=1;
 21             chess[9][x]=1;
 22         }
 23         for(int y=1;y<9;y++) {
 24             chess[y][0]=1;
 25             chess[y][13]=1;
 26         }
 27         for(int[] a:chess) {
 28             for(int b:a)
 29                 b=0;
 30         }
 31         
 32         if(i==m&&j==n) {//如果重复点击同一个图片则清空栈,重新开始一对
 33             stack.clear();
 34             Shown.imgbutton[m][n].setBackground(Color.decode("#e1edef"));
 35         }else {
 36             if((Shown.img[i][j].getDescription()==Shown.img[m][n].getDescription())&&road(m,n,i,j)) {//尝试了很多个函数
 37                 //如果两个图片相同且路径满足要求
 38                     
 39                 chess[i][j]=1;
 40                 chess[m][n]=1;//矩阵中相关元素改为1
 41                 
 42                 NewMap.over();//游戏结束对话框
 43                 
 44                 //NewMap.test();
 45                                 
 46                 //把button上的图片改为空白
 47                 ImageIcon img=new ImageIcon("E:\\学习\\Sophomore\\软件设计\\picture\\good.jpg");
 48                 img.setImage(img.getImage().getScaledInstance(70, 70,Image.SCALE_DEFAULT));
 49                                 
 50                 Shown.imgbutton[m][n].setIcon(img);
 51                 Shown.imgbutton[i][j].setIcon(img);
 52                 Border emptyBorder = BorderFactory.createEmptyBorder(0,0,0,0); 
 53                 Shown.imgbutton[m][n].setBorder(emptyBorder);
 54                 Shown.imgbutton[i][j].setBorder(emptyBorder);
 55                 stack.clear();//完成后清空栈        
 56             }else {
 57                 stack.clear();//如果不相等则出栈,重新开始一组比较
 58             }
 59         }
 60     }
 61             
 62     static boolean road0(int x1,int y1,int x2,int y2) {//没有拐点
 63         //相邻时
 64         if((Math.abs(x1-x2)==1&&y1==y2)||(Math.abs(y2-y1)==1&&x1==x2)) {
 65             return true;
 66         }
 67         
 68         if((y1==y2)) {//当在同一列且不相邻时
 69             int temp=0;  
 70             for(int i=(x1>x2?x2:x1)+1;i<(x1>x2?x1:x2);i++) {
 71                 if(chess[i][y1]==1) {
 72                     temp++;
 73                 }else {
 74                     break;
 75                 }
 76             }
 77             if(temp==(x1>x2?x1:x2)-(x1>x2?x2:x1)-1) {
 78                 return true;
 79             
 80             }
 81         }
 82         
 83         if((x1==x2)) {//当在同一行且不相邻时
 84             int temp=0;
 85             for(int i=(y1>y2?y2:y1)+1;i<(y1>y2?y1:y2);i++) {
 86                 if(chess[x1][i]==1) {
 87                     temp++;
 88                 }else {
 89                     break;
 90                 }
 91             }
 92             if(temp==(y1>y2?y1:y2)-(y1>y2?y2:y1)-1) {
 93                 return true;
 94             }
 95         }
 96         return false;
 97     }
 98     
 99    // static int point1_x;
100    // static int point1_y;//一个拐点时的拐点坐标
101     static boolean road1(int x1,int y1,int x2,int y2) {
102         //一个拐点
103         //一个拐点时为矩形的两个对角
104         if(chess[x1][y2]==1) {       
105         if(road0(x1,y2,x1,y1)&&road0(x1,y2,x2,y2)) {
106             return true;
107         }}
108         if(chess[x2][y1]==1) {
109         if(road0(x2,y1,x1,y1)&&road0(x2,y1,x2,y2)) {//(x2,y1)为拐点
110             return true;
111         }}
112         return false;
113     }
114     
115    // static int point2_x1;
116    // static int point2_y1;
117    // static int point2_x2;
118    // static int point2_y2;
119     static boolean road2(int x1,int y1,int x2,int y2) {//两个拐点时
120         //当在同一行且同一列且在外侧时
121         if((x1==x2)&&(x1==1||x1==8)){
122             return true;
123         }
124         if((y1==y2)&&(y1==1||y1==12)) {//同一列且在外侧
125             return true;
126         }
127         //不同行同列
128         for(int i=0;i<14;i++) {
129             if(chess[x1][i]==1) {
130             if(road0(x1,i,x1,y1)&&road1(x1,i,x2,y2)) {
131                 return true;
132             }}
133         }
134         for(int i=0;i<10;i++) {
135             if(chess[i][y1]==1) {
136             if(road0(i,y1,x1,y1)&&road1(i,y1,x2,y2)) {
137                 return true;
138             }}
139         }
140 
141         return false;
142     }
143     
144     static boolean road(int x1,int y1,int x2,int y2) {//判断路径是否正确,正确的话传给flag执行        
145         //有一个成立则返回true
146         return (road0(x1,y1,x2,y2)||road2(x1,y1,x2,y2)||road2(x1,y1,x2,y2));
147             
148     }
149 }
Function

(3)Time类:

 1 public class Time {
 2     JFrame frame;
 3     public static JLabel jl0;
 4     ScheduledThreadPoolExecutor scheduled;//周期执行线程池
 5     
 6      Date String2Date(String dateStr) {
 7         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 8         try {
 9             Date date = simpleDateFormat.parse(dateStr);
10             return date;
11         } catch (ParseException e) {
12             jl0.setText("时间格式传入错误" + dateStr);
13             throw new IllegalArgumentException("时间格式传入错误" + dateStr);
14         }
15      }
16      
17     public void timer(String dateStr) {
18         //System.out.println(String2Date(dateStr));
19         Date end = String2Date(dateStr);
20         scheduled.scheduleAtFixedRate(new Runnable() {
21             @Override
22             public void run() {
23                 long time = (end.getTime() -1 - System.currentTimeMillis()) / 1000;
24                 if (time <= 0) {
25                     //System.out.println(dateStr);
26                     JOptionPane.showMessageDialog( null, "阿偶,超时啦。游戏结束!");
27                     System.exit(0);
28                     return;
29                 }
30                 long hour = time / 3600;
31                 long minute = (time - hour * 3600) / 60;
32                 long seconds = time - hour * 3600 - minute * 60;
33                 long djs=minute*60+seconds;
34                 StringBuilder stringBuilder = new StringBuilder();
35                 stringBuilder.append("   倒计时:").append(djs).append("秒").append("   ");
36                 jl0.setText(stringBuilder.toString());
37                 //System.out.println(time);
38             }
39 
40         }, 0, 1, TimeUnit.SECONDS);
41     }
42 
43     public Time() {
44         scheduled = new ScheduledThreadPoolExecutor(2);
45         init();
46     }
47 
48     void init() {
49         jl0 = new JLabel();
50         Font font = new Font("宋体", Font.PLAIN, 20);//创建1个字体实例
51         jl0.setFont(font);//设置JLabel的字体
52     }
53 
54 }
55 
56 /*
57 int n=JOptionPane.showConfirmDialog(null, "阿偶,时间到了。再来一局吧!", "游戏结束", JOptionPane.YES_NO_OPTION);
58 if(n==0) {//是
59      //Shown.frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
60     //Shown.shown1=null;
61      //Shown.frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//退出程序
62     //System.exit(0);
63     Shown.shown1=null;
64         Shown.frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
65 
66         Shown shown=new Shown();
67     shown.frame1();
68 }
69 if(n==1) {//否
70      System.exit(0);
71      Shown.frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//退出程序
72      Shown.frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
73 }*/
Time

(4)NewMap类:

  1 public class NewMap {
  2     
  3     static void newmap() {//newmap
  4         int exit=0;
  5         for(int i=0;i<10;i++) {//剩余数量
  6             for(int j=0;j<14;j++) {
  7                 if(Function.chess[i][j]==0) 
  8                     exit++;
  9             }
 10         }
 11         int[] ran=new int[96];//数组储存图片代码
 12         List list = new ArrayList();//乱序
 13         for(int m=0;m<exit/2;m++) {//在数组中成对出现
 14             Random r=new Random();
 15             int ra=r.nextInt(Shown.pic_num);
 16             ran[m]=ra;
 17             ran[m+exit/2]=ra;
 18         }
 19          for(int p = 0;p < exit;p++){           
 20              list.add(ran[p]);
 21              } 
 22          Collections.shuffle(list);//list打乱顺序
 23          for(int i = 0; i < list.size();i++) {//得到乱序ran[]
 24              ran[i]=(int) list.get(i);
 25            }
 26          int n=0;
 27          for(int i=0;i<10;i++) {//重新布局
 28                 for(int j=0;j<14;j++) {
 29                     if(Function.chess[i][j]==0) {//如果还有图片
 30                         Shown.img[i][j]=new ImageIcon(Shown.logo.get(ran[n]));
 31                         Shown.img[i][j].setImage(Shown.img[i][j].getImage().getScaledInstance(60, 56,Image.SCALE_DEFAULT));
 32                         Shown.imgbutton[i][j].setIcon(Shown.img[i][j]);
 33                         n++;
 34                     }
 35                 }
 36          }
 37     }
 38     
 39     static boolean test() {//检测有没有解,无解的话重新开始
 40         int temp=0;
 41         int num=0;
 42         for(int i=2;i<11;i++) {
 43             for(int j=2;j<9;j++) {
 44                 if(Function.chess[i][j]==0) {//不是空
 45                     for(int m=2;m<11;m++) {
 46                         for(int n=2;n<9;n++) {
 47                             if(Function.road(m,n,i,j)) {//检测是否有解
 48                                 break;
 49                             }
 50                         }
 51                     }
 52                 }else {
 53                 temp++;//无解
 54                 }
 55             }
 56         }
 57         for(int i=2;i<13;i++) {
 58             for(int j=2;j<9;j++) {
 59                 if(Function.chess[i][j]==0) {
 60                     num++;
 61                 }
 62             }
 63         }
 64         if(num==temp) {//全部无解
 65             NewMap.newmap();
 66             return false;
 67         }
 68         return true;
 69     }
 70     
 71     static void over() {//检测游戏是否结束
 72         int temp=0;
 73         for(int i=0;i<14;i++) {
 74             for(int j=0;j<10;j++) {
 75                 if(Function.chess[j][i]==1)
 76                     temp++;
 77             }
 78         }
 79         if(temp==140) {//所有button都没有图片的时候
 80             JOptionPane.showMessageDialog( null, "Wow,不愧是你!恭喜胜利通关,休息一下眼睛吧!"); 
 81             Shown.frame2.setVisible(false);
 82         }
 83     }
 84     
 85     static void help() {//help
 86         tag:for(int i=1;i<9;i++) {
 87         for(int j=1;j<13;j++) {
 88                 if(Function.chess[i][j]==0) {//如果还没有被消掉
 89                     for(int m=1;m<9;m++) {
 90                     for(int n=1;n<13;n++) {
 91                         if(Function.chess[m][n]==0&&Function.road(i,j,m,n)&&(i!=m||j!=n)&&Shown.img[i][j].getDescription()==Shown.img[m][n].getDescription()) {
 92                             Shown.imgbutton[i][j].setBackground(Color.green);
 93                             Shown.imgbutton[m][n].setBackground(Color.green);
 94                             break tag;//找到解后直接跳出所有循环
 95                         }
 96                     }
 97                     
 98                 }
 99             }
100         }
101     }
102  }
103 }
NewMap

 

标签:Java,int,add,小游戏,static,new,y1,连连看,x1
来源: https://www.cnblogs.com/xioayantongxue/p/12238224.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有