java怎么实现2048游戏-创新互联
这篇文章主要介绍java怎么实现2048游戏,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
创新互联公司专注于企业全网营销推广、网站重做改版、依安网站定制设计、自适应品牌网站建设、HTML5建站、购物商城网站建设、集团公司官网建设、成都外贸网站建设公司、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为依安等各大城市提供网站开发制作服务。本文实例讲述了java实现的2048游戏。分享给大家供大家参考,具体如下:
先来看看运行效果:
具体代码如下:
package awtDemo; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; @SuppressWarnings("serial") public class Game2048 extends JApplet{ @SuppressWarnings("unused") private static final int UP=0,DOWN=1,LEFT=2,RIGHT=3; private static final int WID=150,SP=10; @SuppressWarnings("unused") private int scores=0; private static JLabel scoreLabel; private boolean change,CheckMode=false,gameOverFlag=false,successFlag=false; private int[] label={2,4,8,16,32,64,128}; private Color[] clo={ new Color(200,200,200),new Color(228,228,160),new Color(214,163,92), new Color(234,124,72),new Color(240,84,77),new Color(255,68,53),new Color(200,200,64) }; @SuppressWarnings("rawtypes") private Map cmap=new HashMap(); public static RectObject[][] rset=new RectObject[4][4]; public RectObject[][] list=new RectObject[4][4]; private My2048Panel myp; @SuppressWarnings("rawtypes") private LinkedList saveList=new LinkedList(); private JButton goBackButton; KeyListener kl=new KeyListener(){ public void keyPressed(KeyEvent e){ saveTheStep(); goBackButton.setVisible(true); if(gameOverFlag==true){ return; } if(!aDirAble()){ gameOver(); } int key=e.getKeyCode(); switch(key){ case KeyEvent.VK_UP: change=false; moveUp(true); if(change==true){ getARandomRect(); //saveTheStep(); } break; case KeyEvent.VK_DOWN: change=false; moveDown(true); if(change==true){ getARandomRect(); //saveTheStep(); } break; case KeyEvent.VK_LEFT: change=false; moveLeft(true); if(change==true){ getARandomRect(); //saveTheStep(); } break; case KeyEvent.VK_RIGHT: change=false; moveRight(true); if(change==true){ getARandomRect(); //saveTheStep(); } break; } //saveTheStep(); } public void keyTyped(KeyEvent e){} public void keyReleased(KeyEvent e){} }; class RectObject{ private int value; public RectObject(){ value=0; } public RectObject(RectObject obj){ value=obj.value; } public boolean equals(Object inobj){ RectObject obj=(RectObject)inobj; if(obj.value==value){ return true; } return false; } } class Point{ int x; int y; public Point(int i,int j){ x=i; y=j; } } class My2048Panel extends JPanel{ private int[] xindex={SP,2*SP+WID,3*SP+2*WID,4*SP+3*WID}; private int[] yindex={SP,2*SP+WID,3*SP+2*WID,4*SP+3*WID}; @SuppressWarnings("deprecation") public void paintComponent(Graphics g){ //background super.paintComponent(g); for(int i=0;i-1;j--){ if(rset[i][j]!=null){ list[i][k--]=new RectObject(rset[i][j]); } } } for(int i=0;i<4 && flag;i++){ for(int j=3;j>0;j--){ if(list[i][j]!=null && list[i][j-1]!=null && list[i][j].value==list[i][j-1].value){ list[i][j].value*=2; if(CheckMode==false){ int sum=Integer.parseInt(scoreLabel.getText()); sum+=list[i][j].value; scoreLabel.setText(""+sum); } list[i][j-1]=null; j--; } } } if(isChange()){ if(CheckMode==false){ copySet(rset,list); repaint(); moveRight(false); } change=true; }else{ repaint(); } } //moveup public void moveUp(boolean flag){ clearList(list); for(int j=0;j<4;j++){ int k=0; for(int i=0;i<4;i++){ if(rset[i][j]!=null){ list[k++][j]=new RectObject(rset[i][j]); } } } for(int j=0;j<4 && flag;j++){ for(int i=0;i<3;i++){ if(list[i][j]!=null && list[i+1][j]!=null && list[i][j].value==list[i+1][j].value){ list[i][j].value*=2; if(CheckMode==false){ int sum=Integer.parseInt(scoreLabel.getText()); sum+=list[i][j].value; scoreLabel.setText(""+sum); } list[i+1][j]=null; i++; } } } if(isChange()){ if(CheckMode==false){ copySet(rset,list); repaint(); moveUp(false); } change=true; }else{ repaint(); } } //movedown public void moveDown(boolean flag){ clearList(list); for(int j=0;j<4;j++){ int k=3; for(int i=3;i>-1;i--){ if(rset[i][j]!=null){ list[k--][j]=new RectObject(rset[i][j]); } } } for(int j=0;j<4 && flag;j++){ for(int i=3;i>0;i--){ if(list[i][j]!=null && list[i-1][j]!=null && list[i][j].value==list[i-1][j].value){ list[i][j].value*=2; if(CheckMode==false){ int sum=Integer.parseInt(scoreLabel.getText()); sum+=list[i][j].value; scoreLabel.setText(""+sum); } list[i-1][j]=null; i--; } } } if(isChange()){ if(CheckMode==false){ copySet(rset,list); repaint(); moveDown(false); } change=true; }else{ repaint(); } } //other functions private void copySet(RectObject[][] dst, RectObject[][] src){ for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ dst[i][j]=src[i][j]; } } } //detect whether rset is different from list or not private boolean isChange(){ for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ if(rset[i][j]!=null && list[i][j]!=null && !rset[i][j].equals(list[i][j])){ return true; } if(rset[i][j]!=null && list[i][j]==null){ return true; } if(rset[i][j]==null && list[i][j]!=null){ return true; } } } return false; } private void clearList(RectObject[][] s){ for(int i=0;i best){ file.writeInt(cur); } file.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); }catch(IOException e2){ e2.printStackTrace(); } } @SuppressWarnings("resource") public static void saveRecord(){ try{ RandomAccessFile file=new RandomAccessFile(new File("LASTRECORD"),"rw"); int score=Integer.parseInt(scoreLabel.getText()); file.writeInt(score); for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ if(rset[i][j]!=null){ file.writeInt(rset[i][j].value); }else{ file.writeInt(0); } } } }catch(FileNotFoundException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } } public static void run(JApplet applet,int width,int height){ JFrame frame=new JFrame(title(applet)); frame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ refreshBest(); saveRecord(); //System.out.println("The score is "+scoreLabel.getText()); } }); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(applet); frame.setSize(width,height); applet.init(); applet.start(); frame.setVisible(true); } public static void main(String[] args){ run(new Game2048(), 900, 800); } }
以上是“java怎么实现2048游戏”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!
本文题目:java怎么实现2048游戏-创新互联
文章来源:http://azwzsj.com/article/ccsdgp.html