Java完善完成2048小游戲。本站提示廣大學習愛好者:(Java完善完成2048小游戲)文章只能為提供參考,不一定能成為您想要的結果。以下是Java完善完成2048小游戲正文
完善地模擬了2048游戲,是依據網友的一個2048改的。
Block.java
import javax.swing.*; import java.awt.*; public class Block extends JLabel { private int value; public Block() { value = 0;//初始化值為0 setFont(new Font("font", Font.PLAIN, 40));//設定字體 setBackground(Color.gray);//設定初始色彩為灰色 } public int getValue()//獲得值 { return value; } public void setValue(int value) { this.value = value; String text = String.valueOf(value); if (value != 0) setText(text); else setText("");//假如值為0則不顯示 setColor(); } public void setColor() //依據值的分歧設定分歧的配景色彩、label字體 { switch (value) { case 0: setBackground(Color.gray); break; case 2: setBackground(new Color(238, 228, 218)); break; case 4: setBackground(new Color(238, 224, 198)); break; case 8: setBackground(new Color(243, 177, 116)); break; case 16: setBackground(new Color(243, 177, 116)); break; case 32: setBackground(new Color(248, 149, 90)); break; case 64: setBackground(new Color(249, 94, 50)); break; case 128: setBackground(new Color(239, 207, 108)); break; case 256: setBackground(new Color(239, 207, 99)); break; case 512: setBackground(new Color(239, 203, 82)); break; case 1024: setBackground(new Color(239, 199, 57)); break; case 2048: setBackground(new Color(239, 195, 41)); break; case 4096: setBackground(new Color(255, 60, 57)); break; } } }
My2048.java
import java.awt.*; import javax.swing.*; public class My2048 extends JFrame { public My2048()//結構函數 { setTitle("2048");//設置題目 setSize(400, 400);//設定窗口年夜小 setLocation(500, 200);//設定窗口肇端地位 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().setLayout(new GridLayout(4, 4, 5, 5));//設定結構方法為GridLayout型 new Operation(this); this.setVisible(true);//設為可視 } public static void main(String args[]) //法式進口點 { try { UIManager.setLookAndFeel("org.jvnet.substance.skin.SubstanceRavenGraphiteLookAndFeel");//設定UI } //接收拋出的異常 catch (ClassNotFoundException | InstantiationException| IllegalAccessException | UnsupportedLookAndFeelException e) { e.printStackTrace(); } JFrame.setDefaultLookAndFeelDecorated(true);//設定Frame的缺省外不雅 new My2048(); } }
Operation.java
import java.awt.event.*; import javax.swing.*; public class Operation implements KeyListener { Block[] block;//用於貯存16個數據 JPanel panel; public boolean up,down,left,right; int moveFlag;//用於累計挪動的次數 boolean numFlag;//用於斷定能否還能參加新的數字 public Operation(JFrame frame) { this.panel = (JPanel)frame.getContentPane();//結構出panel block = new Block[16];//結構出長度為16的數組 numFlag = true;//初始化 moveFlag = 0; up=true;down=true;left=true;right=true; addBlock(); for (int i = 0; i < 2; i++) appearBlock(); frame.addKeyListener(this); } private void addBlock() { for (int i = 0; i < 16; i++) //往panel裡參加block { block[i] = new Block(); block[i].setHorizontalAlignment(JLabel.CENTER);// 不通明的標簽 block[i].setOpaque(true); panel.add(block[i]); } } public void appearBlock() { while (numFlag) //當還能參加隨機的一個新的值得時刻 { int index = (int) (Math.random() * 16);//取一個0到15的隨機整數,這個數作為隨機參加盤中的2或4的地位 if (block[index].getValue() == 0)//假如這個數地點的block數組中值為0,即在為空的時刻,參加一個2或4的數字 { if (Math.random() < 0.5) { block[index].setValue(2); } else { block[index].setValue(4); } break;//跳出while } } } public void judgeAppear() //統計block數組中能否含有值為0的元素,若沒有,則numFlag變成false { int sum = 0; for (int i = 0; i < 16; i++) { if (block[i].getValue() != 0) { sum++; } } if (sum == 16) numFlag = false; } public int Find(int i,int j,int a,int b) { while(i<b&&i>=a) { if(block[i].getValue()!=0) { return i; } i=i+j; } return -1; } public void upBlock() { int i=0,j=0;int t=0;int valueJ=0;int valueI=0;int index=0; for(i=0;i<4;i++) { index=i; for(j=i+4;j<16;j+=4) { valueJ=0; valueI=0; if(block[index].getValue()==0) { t=Find(index,4,0,16); if(t!=-1) { block[index].setValue(block[t].getValue()); block[t].setValue(0); } else { break; } } valueI=block[index].getValue(); if(block[j].getValue()==0) { t=Find(j,4,0,16); if(t!=-1) { block[j].setValue(block[t].getValue()); block[t].setValue(0); } else { break; } } valueJ=block[j].getValue(); if(valueI==valueJ&&valueI!=0&&valueJ!=0) { block[index].setValue(valueI+valueJ); block[j].setValue(0); numFlag = true; } index=j; } } } public void downBlock() { int i=0,j=0;int t=0;int valueJ=0;int valueI=0;int index=0; for(i=12;i<16;i++) { index=i; for(j=i-4;j>=0;j-=4) { valueJ=0; valueI=0; if(block[index].getValue()==0) { t=Find(index,-4,0,16); if(t!=-1) { block[index].setValue(block[t].getValue()); block[t].setValue(0); } else { break; } } valueI=block[index].getValue(); if(block[j].getValue()==0) { t=Find(j,-4,0,16); if(t!=-1) { block[j].setValue(block[t].getValue()); block[t].setValue(0); } else { break; } } valueJ=block[j].getValue(); if(valueI==valueJ&&valueI!=0&&valueJ!=0) { block[index].setValue(valueI+valueJ); block[j].setValue(0); numFlag = true; } index=j; } } } public void rightBlock() { int i=0,j=0;int t=0;int valueJ=0;int valueI=0;int index=0; for(i=3;i<16;i+=4) { index=i; for(j=i-1;j>i-4;j--) { valueJ=0; valueI=0; if(block[index].getValue()==0) { t=Find(index,-1,i-3,index+1); if(t!=-1) { block[index].setValue(block[t].getValue()); block[t].setValue(0); } else { break; } } valueI=block[index].getValue(); if(block[j].getValue()==0) { t=Find(j,-1,i-3,j+1); if(t!=-1) { block[j].setValue(block[t].getValue()); block[t].setValue(0); } else { break; } } valueJ=block[j].getValue(); if(valueI==valueJ&&valueI!=0&&valueJ!=0) { block[index].setValue(valueI+valueJ); block[j].setValue(0); numFlag = true; } index=j; } } } public void leftBlock() { int i=0,j=0;int t=0;int valueJ=0;int valueI=0;int index=0; for(i=0;i<16;i+=4) { index=i; for(j=i+1;j<i+4;j++) { valueJ=0; valueI=0; if(block[index].getValue()==0) { t=Find(index,1,index,i+4); if(t!=-1) { block[index].setValue(block[t].getValue()); block[t].setValue(0); } else { break; } } valueI=block[index].getValue(); if(block[j].getValue()==0) { t=Find(j,1,j,i+4); if(t!=-1) { block[j].setValue(block[t].getValue()); block[t].setValue(0); } else { break; } } valueJ=block[j].getValue(); if(valueI==valueJ&&valueI!=0&&valueJ!=0) { block[index].setValue(valueI+valueJ); block[j].setValue(0); numFlag = true; } index=j; } } } public void over() { if (numFlag ==false&& up==false&&down==false&&left==false&&right==false) //當不克不及添加元素,而且弗成挪動的步數跨越36就輸了,輸了的時刻在盤中心顯示GAMEOVER { block[4].setText("G"); block[5].setText("A"); block[6].setText("M"); block[7].setText("E"); block[8].setText("O"); block[9].setText("V"); block[10].setText("E"); block[11].setText("R"); block[11].addMouseListener(new MouseAdapter() {public void mousePressed(MouseEvent e){reStart();}}); } } public void win() //同OVER { block[0].setText("Y"); block[1].setText("O"); block[2].setText("U"); block[13].setText("W"); block[14].setText("I"); block[15].setText("N"); block[15].addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { reStart(); } }); } public void reStart()//重啟游戲,和結構函數相似,不在累述 { numFlag=true; moveFlag=0; up=true;down=true;left=true;right=true; for(int i=0;i<16;i++) block[i].setValue(0); for (int i = 0; i < 2; i++) appearBlock(); } public void keyPressed(KeyEvent e) //斷定按的高低閣下鍵,並順次挪用挪動函數、斷定函數、添加函數、斷定能否輸失落的函數 { switch (e.getKeyCode()) { case KeyEvent.VK_UP: if(up){ upBlock();} judgeAppear(); appearBlock(); over(); if(numFlag==false) { up=false; } else { up=true;down=true;left=true;right=true; } break; case KeyEvent.VK_DOWN: if(down){ downBlock();} judgeAppear(); appearBlock(); over(); if(numFlag==false) { down=false; } else { up=true;down=true;left=true;right=true; } break; case KeyEvent.VK_LEFT: if(left){ leftBlock();} judgeAppear(); appearBlock(); over(); if(numFlag==false) { left=false; } else { up=true;down=true;left=true;right=true; } break; case KeyEvent.VK_RIGHT: if(right){ rightBlock();} judgeAppear(); appearBlock(); over(); if(numFlag==false) { right=false; } else { up=true;down=true;left=true;right=true; } break; } } public void keyTyped(KeyEvent e) { } public void keyReleased(KeyEvent e) { } }
以上所述就是本文給年夜家分享的關於java完善完成2048小游戲的全體代碼了,願望對年夜家進修java可以或許有所贊助。