这篇文章主要讲解了“怎么用Java实现2048小游戏”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用Java实现2048小游戏”吧!

实现文件

APP.java

importjavax.swing.*;publicclassAPP{publicstaticvoidmain(String[]args){newMyFrame();}}类文件

importjavax.swing.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;importjava.util.Random;//定义自己的类(主类)去继承JFrame类并实现KeyListener接口和ActionListener接口publicclassMyFrameextendsJFrameimplementsKeyListener,ActionListener{//用于存放游戏各位置上的数据int[][]data=newint[4][4];//用于判断是否失败intloseFlag=1;//用于累计分数intscore=0;//用于切换主题Stringtheme="A";//设置三个菜单项目JMenuItemitem1=newJMenuItem("经典");JMenuItemitem2=newJMenuItem("霓虹");JMenuItemitem3=newJMenuItem("糖果");//核心方法publicMyFrame(){//初始化窗口initFrame();//初始化菜单initMenu();//初始化数据initData();//绘制界面paintView();//为窗体提供键盘监听,该类本身就是实现对象this.addKeyListener(this);//设置窗体可见setVisible(true);}//窗体初始化publicvoidinitFrame(){//设置尺寸setSize(514,538);//设置居中setLocationRelativeTo(null);//设置总在最上面setAlwaysOnTop(true);//设置关闭方式setDefaultCloseOperation(3);//设置标题setTitle("2048小游戏");//取消默认布局setLayout(null);}//初始化菜单publicvoidinitMenu(){//菜单栏目JMenuBarmenuBar=newJMenuBar();JMenumenu1=newJMenu("换肤");JMenumenu2=newJMenu("关于我们");//添加上menuBarmenuBar.add(menu1);menuBar.add(menu2);//添加上menumenu1.add(item1);menu1.add(item2);menu1.add(item3);//注册监听item1.addActionListener(this);item2.addActionListener(this);item3.addActionListener(this);//添加进窗体super.setJMenuBar(menuBar);}//初始化数据,在随机位置生成两个2publicvoidinitData(){generatorNum();generatorNum();}//重新绘制界面的方法publicvoidpaintView(){//调用父类中的方法清空界面getContentPane().removeAll();//判断是否失败if(loseFlag==2){//绘制失败界面JLabelloseLable=newJLabel(newImageIcon("D:\Download\BaiDu\image\"+theme+"-lose.png"));//设置位置和高宽loseLable.setBounds(90,100,334,228);//将该元素添加到窗体中getContentPane().add(loseLable);}//根据现有数据绘制界面for(inti=0;i<4;i++){//根据位置循环绘制for(intj=0;j<4;j++){JLabelimage=newJLabel(newImageIcon("D:\Download\BaiDu\image\"+theme+"-"+data[i][j]+".png"));//提前计算好位置image.setBounds(50+100*j,50+100*i,100,100);//将该元素添加进窗体getContentPane().add(image);}}//绘制背景图片JLabelbackground=newJLabel(newImageIcon("D:\Download\BaiDu\image\"+theme+"-Background.jpg"));//设置位置和高宽background.setBounds(40,40,420,420);//将该元素添加进窗体getContentPane().add(background);//得分模板设置JLabelscoreLable=newJLabel("得分:"+score);//设置位置和高宽scoreLable.setBounds(50,20,100,20);//将该元素添加进窗体getContentPane().add(scoreLable);//重新绘制界面getContentPane().repaint();}//用不到的但是必须重写的方法,无需关注@OverridepublicvoidkeyTyped(KeyEvente){}//键盘被按下所触发的方法,在此方法中加入区分上下左右的按键@OverridepublicvoidkeyPressed(KeyEvente){//keyCode接收按键信息intkeyCode=e.getKeyCode();//左移动if(keyCode==37){moveToLeft(1);generatorNum();}//上移动elseif(keyCode==38){moveToTop(1);generatorNum();}//右移动elseif(keyCode==39){moveToRight(1);generatorNum();}//下移动elseif(keyCode==40){moveToBottom(1);generatorNum();}//忽视其他按键else{return;}//检查是否能够继续移动check();//重新根据数据绘制界面paintView();}//左移动的方法,通过flag判断,传入1是正常移动,传入2是测试移动publicvoidmoveToLeft(intflag){for(inti=0;i<data.length;i++){//定义一维数组接收一行的数据int[]newArr=newint[4];//定义下标方便操作intindex=0;for(intx=0;x<data[i].length;x++){//将有数据的位置前移if(data[i][x]!=0){newArr[index]=data[i][x];index++;}}//赋值到原数组data[i]=newArr;//判断相邻数据是否相邻,相同则相加,不相同则略过for(intx=0;x<3;x++){if(data[i][x]==data[i][x+1]){data[i][x]*=2;//如果是正常移动则加分if(flag==1){score+=data[i][x];}//将合并后的数据都前移,实现数据覆盖for(intj=x+1;j<3;j++){data[i][j]=data[i][j+1];}//末尾补0data[i][3]=0;}}}}//右移动的方法,通过flag判断,传入1是正常移动,传入2是测试移动publicvoidmoveToRight(intflag){//翻转二维数组reverse2Array();//对旋转后的数据左移动moveToLeft(flag);//再次翻转reverse2Array();}//上移动的方法,通过flag判断,传入1是正常移动,传入2是测试移动publicvoidmoveToTop(intflag){//逆时针旋转数据anticlockwise();//对旋转后的数据左移动moveToLeft(flag);//顺时针还原数据clockwise();}//下移动的方法,通过flag判断,传入1是正常移动,传入2是测试移动publicvoidmoveToBottom(intflag){//顺时针旋转数据clockwise();//对旋转后的数据左移动moveToLeft(flag);//逆时针旋转还原数据anticlockwise();}//检查能否左移动publicbooleancheckLeft(){//开辟新二维数组用于暂存数据和比较数据int[][]newArr=newint[4][4];//复制数组copyArr(data,newArr);//测试移动moveToLeft(2);booleanflag=false;//设置break跳出的for循环标记lo:for(inti=0;i<data.length;i++){for(intj=0;j<data[i].length;j++){//如果有数据不相同,则证明能够左移动,则返回trueif(data[i][j]!=newArr[i][j]){flag=true;breaklo;}}}//将原本的数据还原copyArr(newArr,data);returnflag;}//检查能否右移动,与checkLeft()方法原理相似publicbooleancheckRight(){int[][]newArr=newint[4][4];copyArr(data,newArr);moveToRight(2);booleanflag=false;lo:for(inti=0;i<data.length;i++){for(intj=0;j<data[i].length;j++){if(data[i][j]!=newArr[i][j]){flag=true;breaklo;}}}copyArr(newArr,data);returnflag;}//检查能否上移动,与checkLeft()方法原理相似publicbooleancheckTop(){int[][]newArr=newint[4][4];copyArr(data,newArr);moveToTop(2);booleanflag=false;lo:for(inti=0;i<data.length;i++){for(intj=0;j<data[i].length;j++){if(data[i][j]!=newArr[i][j]){flag=true;breaklo;}}}copyArr(newArr,data);returnflag;}//检查能否下移动,与checkLeft()方法原理相似publicbooleancheckBottom(){int[][]newArr=newint[4][4];copyArr(data,newArr);moveToBottom(2);booleanflag=false;lo:for(inti=0;i<data.length;i++){for(intj=0;j<data[i].length;j++){if(data[i][j]!=newArr[i][j]){flag=true;breaklo;}}}copyArr(newArr,data);returnflag;}//检查是否失败publicvoidcheck(){//上下左右均不能移动,则游戏失败if(checkLeft()==false&&checkRight()==false&&checkTop()==false&&checkBottom()==false){loseFlag=2;}}//复制二维数组的方法,传入原数组和新数组publicvoidcopyArr(int[][]src,int[][]dest){for(inti=0;i<src.length;i++){for(intj=0;j<src[i].length;j++){//遍历复制dest[i][j]=src[i][j];}}}//键盘被松开@OverridepublicvoidkeyReleased(KeyEvente){}//翻转一维数组publicvoidreverseArray(int[]arr){for(intstart=0,end=arr.length-1;start<end;start++,end--){inttemp=arr[start];arr[start]=arr[end];arr[end]=temp;}}//翻转二维数组publicvoidreverse2Array(){for(inti=0;i<data.length;i++){reverseArray(data[i]);}}//顺时针旋转publicvoidclockwise(){int[][]newArr=newint[4][4];for(inti=0;i<4;i++){for(intj=0;j<4;j++){//找规律啦~newArr[j][3-i]=data[i][j];}}data=newArr;}//逆时针旋转publicvoidanticlockwise(){int[][]newArr=newint[4][4];for(inti=0;i<4;i++){for(intj=0;j<4;j++){//规律newArr[3-j][i]=data[i][j];}}data=newArr;}//空位置随机生成2publicvoidgeneratorNum(){int[]arrarI={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};int[]arrarJ={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};intw=0;for(inti=0;i<data.length;i++){for(intj=0;j<data[i].length;j++){if(data[i][j]==0){//找到并存放空位置arrarI[w]=i;arrarJ[w]=j;w++;}}}if(w!=0){//随机数找到随机位置Randomr=newRandom();intindex=r.nextInt(w);intx=arrarI[index];inty=arrarJ[index];//空位置随机生成2data[x][y]=2;}}//换肤操作@OverridepublicvoidactionPerformed(ActionEvente){//接收动作监听,if(e.getSource()==item1){theme="A";}elseif(e.getSource()==item2){theme="B";}elseif(e.getSource()==item3){theme="C";}//换肤后重新绘制paintView();}}//测试失败效果的数据/*int[][]data={{2,4,8,4},{16,32,64,8},{128,2,256,2},{512,8,1024,2048}};*/

感谢各位的阅读,以上就是“怎么用Java实现2048小游戏”的内容了,经过本文的学习后,相信大家对怎么用Java实现2048小游戏这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!