mud_sanc 板


LINE

踩地雷游戏的 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







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP