Re: [情報] 踩地雷

看板mud_sanc (Sanctuary - 聖殿)作者 (小太保)時間17年前 (2008/12/23 19:12), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
踩地雷遊戲的 java 版本 程式下載點: http://elearning.stut.edu.tw/media/java/ch15/winmine.rar (網路找到的) //踩地雷遊戲 import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.net.*; public class winmine extends JApplet implements ActionListener,MouseListener { private Timer timer; private JButton but[]=new JButton[64]; private JLabel lb[]=new JLabel[64]; private ImageIcon img[]=new ImageIcon[3]; private JButton start_button,stop_button; JLabel time1 = new JLabel("time : 0",JLabel.CENTER); JLabel landmine1 = new JLabel("landmine : 0",JLabel.CENTER); JLabel feedback = new JLabel("Successful!",JLabel.CENTER); private int p_time,game_status,free_pos; int llist[]=new int[64]; int flag_list[]=new int[64]; public void init() { int col,row; Container c=getContentPane(); c.setLayout(null); c.setSize(500,400); Image image1,image2; ImageIcon icon1=null; try {image1 = getImage(new URL(getCodeBase(),"pic/background1.jpg")); } catch(MalformedURLException e) {System.out.println("下載圖形檔發生錯誤 URL:"+e+"\n"); return;} try {image2 = getImage(new URL(getCodeBase(),"pic/playarea.jpg")); } catch(MalformedURLException e) {System.out.println("下載圖形檔發生錯誤 URL:"+e+"\n"); return;} ImgJPanel pan1=new ImgJPanel(image1); pan1.setLayout(null); pan1.setSize(500,400); try { icon1=new ImageIcon(new URL(getCodeBase(),"pic/start.jpg"));} catch(MalformedURLException e) {System.out.println("下載圖形檔發生錯誤 URL:"+e+"\n"); return;} start_button=new JButton( icon1); start_button.setSize(97,33); start_button.setLocation(403,362); start_button.addActionListener(this); pan1.add(start_button); try { icon1=new ImageIcon(new URL(getCodeBase(),"pic/stop.jpg"));} catch(MalformedURLException e) {System.out.println("下載圖形檔發生錯誤 URL:"+e+"\n");return;} stop_button=new JButton(icon1); stop_button.setSize(97,33); stop_button.setLocation(403,362); stop_button.setVisible(false); stop_button.addActionListener(this); pan1.add(stop_button); time1.setFont(new Font("Arial",Font.PLAIN,18)); time1.setBounds(215,20,80,25); time1.setForeground(Color.blue); c.add(time1); landmine1.setFont(new Font("Arial",Font.PLAIN,18)); landmine1.setBounds(315,20,180,25); landmine1.setForeground(new Color(0,0,255)); c.add(landmine1); feedback.setBounds(152,340,242,50); feedback.setForeground(Color.blue); feedback.setFont(new Font("標楷體",Font.PLAIN,32)); feedback.setVisible(false); c.add(feedback); try { img[0]=new ImageIcon(new URL(getCodeBase(),"pic/but01.jpg"));} catch(MalformedURLException e) {System.out.println("下載圖形檔發生錯誤 URL:"+e+"\n");return;} try { img[1]=new ImageIcon(new URL(getCodeBase(),"pic/but02.jpg"));} catch(MalformedURLException e) {System.out.println("下載圖形檔發生錯誤 URL:"+e+"\n");return;} try { img[2]=new ImageIcon(new URL(getCodeBase(),"pic/but03.jpg"));} catch(MalformedURLException e) {System.out.println("下載圖形檔發生錯誤 URL:"+e+"\n");return;} for(int i=0;i<64;i++){ but[i]=new JButton(img[0]); // but[i].setToolTipText("小心有地雷喔!"); but[i].setSize(29,29); but[i].setLocation(153+(i%8)*30,95+(int)(i/8)*30); but[i].addMouseListener(this); pan1.add(but[i]); lb[i]=new JLabel("",JLabel.CENTER); lb[i].setBounds(153+(i%8)*30,95+(int)(i/8)*30,29,29); lb[i].setVisible(false); pan1.add(lb[i]); } pan1.setLocation( 0, 0); c.add(pan1); timer=new Timer(1000,this); } //開始執行applet public void start(){ game_status=0; } public void actionPerformed(ActionEvent e){ Point p1,p2; int index; if(e.getSource() == start_button){ p_time=0; time1.setText("time : " + p_time); landmine1.setText("landmine : 10"); reset_game(); game_status=1; feedback.setVisible(false); timer.start(); start_button.setVisible(false); stop_button.setVisible(true); } if(e.getSource() == stop_button){ game_status=0; timer.stop(); feedback.setText("Stop!"); feedback.setVisible(true); start_button.setVisible(true); stop_button.setVisible(false); } if(e.getSource() == timer && game_status==1){ p_time++; time1.setText("time : " + p_time); } } 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,index,px,py; bt=1; if((e.getModifiers() & InputEvent.BUTTON3_MASK)== InputEvent.BUTTON3_MASK){ bt=2; } if(game_status==1){ for(index=0;index<64;index++){ if(e.getSource() == but[index] ){ if(bt == 1 && flag_list[index] == 0) { //按下左鍵 switch(llist[index]){ case 0: px = index % 8; py = (int)(index / 8); free_landmine(px, py); break; case 9: but[index].setIcon(img[2]); game_status = 0; feedback.setText("踩到地雷了!"); feedback.setVisible(true); start_button.setVisible(true); stop_button.setVisible(false); break; default: but[index].setVisible(false); lb[index].setVisible(true); flag_list[index] = 1; break; } } if(bt == 2 && flag_list[index] !=1) { //按下右鍵 flag_list[index] = 2 - flag_list[index]; if(flag_list[index] == 0) { but[index].setIcon(img[0]);; } else { but[index].setIcon(img[1]); } } if(check_finish() == false) { game_status = 0; start_button.setVisible(true); stop_button.setVisible(false); feedback.setText("恭喜您!過關了!"); feedback.setVisible(true); } } } } } //重設遊戲狀態的副程式 void reset_game(){ int i, j, k,x; //產生地雷陣列 for(i=0;i<64;i++){ if(i<10) { //以數字9代表地雷 llist[i] = 9; } else { //以數字0代表沒有地雷 llist[i] = 0; } } //隨機排列地雷陣列 for(i=0;i<500;i++){ j = (int)(Math.random() * 64); k = (int)(Math.random() * 64); x = llist[j]; llist[j] = llist[k]; llist[k] = x; } count_mine(); //設定按鈕狀態 for(i=0;i<64;i++){ but[i].setIcon(img[0]); but[i].setVisible(true); flag_list[i] = 0; if(llist[i] == 0) { lb[i].setText(""); } else { lb[i].setText(String.valueOf(llist[i])); } lb[i].setVisible(false); } p_time=0; } // 統計週邊地雷數 void count_mine(){ int c, i, px, py, xsize, xwidth; xwidth = 8; xsize = xwidth * xwidth; for(i=0;i<xsize;i++){ if(llist[i]!=9){ px = i % xwidth; py = (int)(i/xwidth); c = 0; //左上方(-1,-1) if(((px - 1) >= 0 && (py - 1) >= 0)){ if(llist[(px - 1) + (py - 1) * xwidth] == 9){ c = c + 1; } } //上方(0,-1) if((py - 1) >= 0){ if(llist[px + (py - 1) * xwidth] == 9){ c = c + 1; } } //右上方(+1,-1) if((px + 1) < xwidth && (py - 1) >= 0){ if(llist[(px + 1) + (py - 1) * xwidth] == 9){ c = c + 1; } } //左方(-1,0) if((px - 1) >= 0){ if(llist[(px - 1) + py * xwidth] == 9){ c = c + 1; } } //右方(+1,0) if((px + 1) < xwidth){ if(llist[(px + 1) + py * xwidth] == 9){ c = c + 1; } } //左下方(-1,+1) if((px - 1) >= 0 && (py + 1) < xwidth){ if(llist[(px - 1) + (py + 1) * xwidth] == 9){ c = c + 1; } } //下方(0, +1) if((py + 1) < xwidth){ if(llist[px + (py + 1) * xwidth] == 9){ c = c + 1; } } //右下方(+1,+1) if((px + 1) < xwidth && (py + 1) < xwidth){ if(llist[(px + 1) + (py + 1) * xwidth] == 9){ c = c + 1; } } llist[i] = c; } } } //顯示安全區 void free_landmine(int px, int py){ int i, xwidth; xwidth = 8; i = px + py * xwidth; flag_list[i] = 1; //左上方(-1,-1) if((px - 1) >= 0 && (py - 1) >= 0) { i = (px - 1) + (py - 1) * xwidth; but[i].setVisible(false); lb[i].setVisible(true); if(llist[i] == 0 && flag_list[i] == 0) { free_landmine(px - 1, py - 1); } flag_list[i] = 1; } //上方(0,-1) if((py - 1) >= 0) { i = px + (py - 1) * xwidth; but[i].setVisible(false); lb[i].setVisible(true); if(llist[i] == 0 && flag_list[i] == 0) { free_landmine(px, py - 1); } flag_list[i] = 1; } //右上方(+1,-1) if((px + 1) < xwidth && (py - 1) >= 0) { i = (px + 1) + (py - 1) * xwidth; but[i].setVisible(false); lb[i].setVisible(true); if(llist[i] == 0 && flag_list[i] == 0) { free_landmine(px + 1, py - 1); } flag_list[i] = 1; } //左方(-1,0) if((px - 1) >= 0) { i = (px - 1) + py * xwidth; but[i].setVisible(false); lb[i].setVisible(true); if(llist[i] == 0 && flag_list[i] == 0) { free_landmine(px - 1, py); } flag_list[i] = 1; } //右方(+1,0) if((px + 1) < xwidth) { i = (px + 1) + py * xwidth; but[i].setVisible(false); lb[i].setVisible(true); if(llist[i] == 0 && flag_list[i] == 0) { free_landmine(px + 1, py); } flag_list[i] = 1; } //左下方(-1,+1) if((px - 1) >= 0 && (py + 1) < xwidth) { i = (px - 1) + (py + 1) * xwidth; but[i].setVisible(false); lb[i].setVisible(true); if(llist[i] == 0 && flag_list[i] == 0) { free_landmine(px - 1, py + 1); } flag_list[i] = 1; } //下方(0,+1) if((py + 1) < xwidth) { i = px + (py + 1) * xwidth; but[i].setVisible(false); lb[i].setVisible(true); if(llist[i] == 0 && flag_list[i] == 0) { free_landmine(px, py + 1); } flag_list[i] = 1; } //右下方(+1,+1) if((px + 1) < xwidth && (py + 1) < xwidth) { i = (px + 1) + (py + 1) * xwidth; but[i].setVisible(false); lb[i].setVisible(true); if(llist[i] == 0 && flag_list[i] == 0) { free_landmine(px + 1, py + 1); } flag_list[i] = 1; } } //檢查是否已經完成的副程式 public boolean check_finish(){ int i; boolean flag; flag = false; for(i=0;i<64;i++){ if(llist[i] == 9 && flag_list[i] != 2) { flag = true; break; } if(flag_list[i] == 0) { flag = true; break; } } return flag; } // JPanel的延伸物件 class ImgJPanel extends JPanel { private Image image1; public ImgJPanel(Image image_a){ this.image1 = image_a; } public void paintComponent(Graphics g) { g.drawImage(image1, 0, 0, this); } } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 192.192.102.143
文章代碼(AID): #19KCU6_u (mud_sanc)
討論串 (同標題文章)
文章代碼(AID): #19KCU6_u (mud_sanc)