1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
| import java.awt.*; import javax.swing.*; import java.awt.event.*; import sun.audio.*; import java.io.*; import java.applet.*;
public class memorycard extends JFrame implements Runnable { private ButtonHandler baf = new ButtonHandler(); private JMenuBar jmb; private int row = 4 ; private int col = 4; private int LK_SEC = 1; private int add = 1000, dec = 100; private int LimPoint; private long LimSec;
private int sec = 1; private int imgH = 0,imgW =0,score = 0; private int count = 0; private long startTime =0; private int level ;
private JMenu game = new JMenu("遊戲設定"),other = new JMenu("其他資訊"); private JTextField usedtime; private JTextField stage ;
private JMenuItem [] gm = new JMenuItem[5]; private JMenuItem [] oth = new JMenuItem[2]; private JPanel jpl; private JLabel jl = new JLabel("分數 : 0"); private int [] m; private int [] twoImg = {-1,-1}; private int [] btnIndex = {-1,-1}; private JButton [] imgbtn; private ImageIcon []img ; private boolean [] imgShow; private boolean [] btnDown; private boolean isChange = false; private boolean isStart = false; private boolean Limplay = false; public memorycard(int mul,int L) { super("記憶翻牌"); level = L; LK_SEC = level; if(L==1) row = col = 2; else if(L == 2) row = col = 4; else if(L == 3) row = col = 6; LimPoint = (row*col*500); LimSec = (row*col/2)+2; if(mul == 1) Limplay=true; imgbtn = new JButton[row * col]; img = new ImageIcon[row * col]; imgShow = new boolean[row*col/2]; btnDown = new boolean [row*col];
jpl = new JPanel(new GridLayout(row,col,0,0)); Container c = getContentPane(); c = this.getContentPane(); jmb = new JMenuBar(); this.setJMenuBar(jmb); usedtime = new JTextField(); usedtime.setEditable(false); stage = new JTextField("Round "+ level +":"); stage.setEditable(false); setLocation(200,50); for(int i = 0; i < img.length; i++) { img[i] = new ImageIcon(getClass().getResource("images/p0.jpg")); imgbtn[i] = new JButton(img[i]); imgbtn[i].addActionListener(baf); imgbtn[i].setEnabled(false); jpl.add(imgbtn[i]); jpl.add(jl); } c.add(jpl,BorderLayout.CENTER); c.add(jl,BorderLayout.SOUTH); c.add(stage,BorderLayout.NORTH); jmb.add(game); gm[0] = new JMenuItem("開始遊戲"); gm[1] = new JMenuItem("重新遊戲"); gm[1].setEnabled(false); gm[2] = new JMenuItem("離開遊戲"); if(level == 3) { gm[3] = new JMenuItem("前往下關"); gm[3].setEnabled(false); } else gm[3] = new JMenuItem("前往下關"); gm[4] = new JMenuItem("極限模式"); game.add(gm[0]); game.add(gm[1]); game.add(gm[3]); if(mul == 0) game.add(gm[4]); game.addSeparator(); game.add(gm[2]); jmb.add(other); oth[0] = new JMenuItem("遊戲規則"); oth[1] = new JMenuItem("關於作者"); other.add(oth[0]); other.add(oth[1]); imgH = img[0].getIconHeight() * row; imgW = img[0].getIconWidth() * col; setSize(imgW,imgH); setResizable(false); setVisible(true); setIconImage(new ImageIcon("images/iconFrame.png").getImage()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); for(int m=0;m<gm.length;m++) gm[m].addActionListener(baf); oth[0].addActionListener(baf); oth[1].addActionListener(baf); } private class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent ae) { for(int i = 0; i < img.length; i++) { if(ae.getSource() == imgbtn[i]) { checkIsRight(i,imgIndex(m[i])); break; } } if(ae.getSource() == gm[0]) { replay(Limplay,level); } else if(ae.getSource() == gm[1]) { replay(Limplay,level); } else if(ae.getSource() == gm[2]) { System.exit(0); } else if(ae.getSource() == gm[3]) { if(level == 3) level = 1; else if(level < 3) level++; new memorycard(0,level); } else if(ae.getSource() == gm[4]) { new memorycard(1,level); } else if(ae.getSource() == oth[0]) { if(Limplay == false) { JOptionPane.showMessageDialog(jpl, "1.圖片為 " + row + " 乘 " + col +" ,本關共有 " + (row * col) + " 個按鈕\n" + "2.一開始會讓玩家看 " + LK_SEC + " 秒記圖片\n"+ "3.翻錯圖片時,錯誤圖片會停留 " + sec+ " 秒讓玩家記\n"+ "4.猜對得 " + add + " 分,猜錯扣 " + dec +" 分\n" + "5.關卡名稱顯示在左上方,關卡分數顯示在左下方\n" + "6.全部共有三關" , "遊戲說明",JOptionPane.PLAIN_MESSAGE,new ImageIcon(getClass().getResource("images/BattleIcon.png"))); } else { JOptionPane.showMessageDialog(jpl, "請努力達成以下條件:\n" + "1.目標分數為滿分 〔 " + LimPoint + " 〕 分\n"+ "2.目標完成時間為 〔 " + LimSec+ " 〕 秒\n"+ "3.只要猜錯一次就會失敗\n" + "4.以上兩個目標皆完成才會出現通關視窗\n", "極限關卡", JOptionPane.PLAIN_MESSAGE,new ImageIcon(getClass().getResource("images/BatLim.png"))); } } else if(ae.getSource() == oth[1]) { JOptionPane.showMessageDialog(jpl, "程式設計與介面修改:陳煜仁\n"+ "素材取得來源:https://granbluefantasy.jp/\n"+ "此程式僅用於學術研究,參考許多網路上資料完成\n", "作者資訊", JOptionPane.PLAIN_MESSAGE,new ImageIcon(getClass().getResource("images/ProgIcon.png"))); } } } private int [] rand(int N) { int temp [] = new int[N]; for(int i = 0; i < N; i++) { temp[i] = i; } for(int i = 0; i < N; i++) { int j = (int) (Math.random() * N); int tmp = temp[i]; temp[i] = temp[j]; temp[j] = tmp; } return temp; } private int imgIndex(int n) { return ((n/2)+1); } private void replay(boolean lm,int lev) { stage.setText("Round "+ lev+":"); startTime = System.currentTimeMillis(); gm[0].setEnabled(false); if(lm == true) gm[4].setEnabled(false); gm[1].setEnabled(true); m = rand(row*col); for(int i = 0; i < img.length; i++) { imgbtn[i].setEnabled(true); imgbtn[i].setIcon(new ImageIcon(getClass().getResource("images/" + imgIndex(m[i]) + ".jpg"))); } twoImg[0] = twoImg[1] = btnIndex[0] = btnIndex[1] = -1; isChange = false; score = 0; jl.setText("分數 : " + score); count=0; isStart = false; new Thread(new Runnable() { public void run() { try { Thread.sleep(LK_SEC * 1000); }catch(InterruptedException e){} for(int i = 0; i < img.length; i++) { img[i] = new ImageIcon(getClass().getResource("images/p0.jpg")); imgbtn[i].setIcon(img[i]); btnDown[i] = false; } isStart = true; } }).start(); for(int i = 0; i < imgShow.length; i++) imgShow[i] = false; } private void checkIsRight(int btnN, int jpgNum) { if(!isStart) return; if(btnDown[btnN]) return; btnDown[btnN] = true; imgbtn[btnN].setIcon(new ImageIcon(getClass().getResource("images/" + jpgNum + ".jpg"))); int temp = twoImg[0]; int btnTemp = btnIndex[0];
twoImg[0] = jpgNum; btnIndex[0] = btnN; twoImg[1] = temp; btnIndex[1] = btnTemp; if(isChange) { if(imgShow[jpgNum-1]) return; if(twoImg[1] == twoImg[0]) { imgShow[jpgNum-1] = true; twoImg[1] = -1; btnIndex[1] = -1; isChange = false; count++; score += add; jl.setText("分數 : " + score); if(count == row*col/2) { Long t =((System.currentTimeMillis()-startTime)/1000); usedtime.setText(((System.currentTimeMillis()-startTime)/1000)+""); if(Limplay == false) { JOptionPane.showMessageDialog(jpl, "恭喜通關!\n" + "你本次遊戲花了【 " + usedtime.getText() + " 】秒\n"+ "在這一關獲得總分數為【 "+score+" 】分!!", "關卡訊息", JOptionPane.PLAIN_MESSAGE,new ImageIcon(getClass().getResource("images/icon.png"))); } else { if(LimPoint == score && LimSec >= t) { JOptionPane.showMessageDialog(jpl, "超越極限!!!\n" + "你的翻牌沒有絲毫猶豫!竟然只花了【 " + usedtime.getText() + " 】秒\n"+ "沒有任何錯誤的通關了! 恭喜你!", "極限關卡", JOptionPane.PLAIN_MESSAGE,new ImageIcon(getClass().getResource("images/LimIcon1.png"))); } else { int x =JOptionPane.showConfirmDialog(jpl, "可惜...\n你還沒達成所有條件....\n" + 你本次遊戲花了【 " + usedtime.getText() + " 】秒\n你獲得分數為【 "+score+" 】分\n"+ "你還想要繼續挑戰嗎?", "極限關卡", JOptionPane.OK_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE); if(x ==JOptionPane.YES_OPTION) { replay(Limplay,level); } } } } return; } //設定翻開兩張圖片時,其他圖片不能翻 for(int i = 0; i < btnDown.length; i++) if(!btnDown[i]) imgbtn[i].setEnabled(false); new Thread(this).start(); } isChange = !isChange; } public void run () { try{ Thread.sleep(sec * 1000); }catch(InterruptedException e){} //蓋住原本翻開的圖片 btnDown[btnIndex[0]] = false; btnDown[btnIndex[1]] = false; imgbtn[btnIndex[0]].setIcon(new ImageIcon(getClass().getResource("images/p0.jpg"))); imgbtn[btnIndex[1]].setIcon(new ImageIcon(getClass().getResource("images/p0.jpg"))); //設定蓋回兩張翻開圖片時,其他圖片可以回復可以翻的狀態 for(int i = 0; i < btnDown.length; i++) if(!btnDown[i]) imgbtn[i].setEnabled(true); score -= dec; //猜錯扣分 jl.setText("分數 : " + score); } public static void main(String [] args) { new memorycard(0,1); } }
|