您好,欢迎访问三七文档
1、图形用户界面:2048的最大特点就是玩家对图形界面里的数字进行操作,也就是是玩家与游戏的互动2、当前分数SCORE与最高分数的显示:在我们设计的2048游戏中当前分数取了页面内所有数字相加的值为分数,对玩家玩游戏的进展有直接性、客观性的展现;同时,最高分数取了以往玩家退出游戏时所保存分数的最高分3、数字颜色:游戏中数字的颜色以2为首项的等比数列变化,即2、4、8、16、32、64、128、256、512、1024、2048.......对应的数字卡片变色4、游戏的退出:游戏退出时,我们采用弹出对话框的确认玩家是否真的要退出游戏,当然这样做更符合游戏人性化设计的观念。功能图如下:一、总体设计简要设计流程:2048游戏画图监听器游戏算法移动方向随机数是否结束前景色背景色字体键盘事件鼠标事件程序结构说明://重新开始,若是初次运行游戏,则从文件中读入最高分voidrestart()//控制方法publicvoidkeyPressed(KeyEvente)//绘图方法publicvoidpaint(Graphicsg)//判断是否已经失败,若失败则返回true,否则返回falsepublicbooleanjudgeFail()//向下滑动,若各列均没有方块相消或移动,则返回false,否则返回true;publicvoidmoveDown()//向左滑动,若各行均没有方块相消或移动,则返回false,否则返回true;publicvoidmoveLeft()//向右滑动,若各行均没有方块相消或移动,则返回false,否则返回true;publicvoidmoveRight()//向上滑动,若各行均没有方块相消或移动,则返回false,否则返回true;PublicvoidmoveUp()//在游戏区空闲处随机生成2或4publicvoidgenerateRandom2or4()二、程序模块设计源码如下:Main类:importjavax.swing.JFrame;publicclassMain{publicstaticvoidmain(String[]args){JFrameframe=newJava2048();frame.setTitle(Java2048);frame.setSize(455,610);frame.setLocationRelativeTo(null);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);}}Java2048类:importjava.awt.*;importjava.awt.event.*;importjava.io.*;importjava.util.*;importjavax.swing.*;publicclassJava2048extendsJFrameimplementsKeyListener{privatestaticfinallongserialVersionUID=1L;Colorbackground=newColor(190,173,160);//背景色Colorforeground=newColor(204,192,178);//前景色ColorwordColor=newColor(232,216,203);//单词色FontwordFont=newFont(微软雅黑,Font.PLAIN,20);//单词字体FontnumberFont=newFont(微软雅黑,Font.BOLD,40);//数字字体Randomrandom=newRandom();//随机数发生器int[][]array;//游戏用2维数组//逐行或逐列处理数组,第一个参数为实际数字,第二个为判断值,用来判断是否应该消除相同的数字int[][]process=newint[4][2];intscore;//分数,初始化为零inthighestScore;//游戏中最高分inthighestScore2=0;//文件中最高分intbiggestNumber=0;booleanifGenerate2or4;int[]rd=newint[16];//生成随机2或4的位置privateGraphicsgBuffer;//构造方法publicJava2048(){addKeyListener(this);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){if(JOptionPane.showConfirmDialog(null,确定退出?,提示,JOptionPane.YES_NO_OPTION)==JOptionPane.YES_NO_OPTION){if(highestScorehighestScore2){//若在本次游戏中破了纪录,则更新文件中的最高分highestScore2=highestScore;Filefile=newFile(Java2048beta1.txt);BufferedWriterbw;try{bw=newBufferedWriter(newFileWriter(file));bw.write(String.valueOf(highestScore2),0,String.valueOf(highestScore2).length());bw.close();}catch(Exceptione1){JOptionPane.showMessageDialog(null,找不到同目录下Java2048beta1.txt文件或文件已损坏!,提示,JOptionPane.INFORMATION_MESSAGE);};}System.exit(0);}}});restart();}voidrestart(){//重新开始//若是初次运行游戏,则从文件中读入最高分if(highestScore==0){}Filefile=newFile(Java2048beta1.txt);BufferedReaderbr;try{br=newBufferedReader(newFileReader(file));highestScore2=Integer.valueOf(br.readLine());br.close();}catch(Exceptione){JOptionPane.showMessageDialog(this,找不到同目录下Java2048beta1.txt文件或文件已损坏!,提示,JOptionPane.INFORMATION_MESSAGE);System.exit(0);};highestScore=highestScore2;array=newint[4][4];//清空游戏用2维数组score=0;//重置分数为零biggestNumber=0;//重置最大数字为零//在游戏区随机生成两个2或4generateRandom2or4();generateRandom2or4();}//绘图方法publicvoidpaint(Graphicsg){gBuffer=g;gBuffer.setColor(background);gBuffer.drawRoundRect(30,40,185,90,20,20);//画分数显示区gBuffer.fillRoundRect(30,40,185,90,20,20);gBuffer.drawRoundRect(245,40,185,90,20,20);//画最高分显示区gBuffer.fillRoundRect(245,40,185,90,20,20);gBuffer.drawRoundRect(0,150,454,460,10,10);//画主游戏区gBuffer.fillRoundRect(0,150,454,460,10,10);gBuffer.setFont(wordFont);gBuffer.setColor(wordColor);gBuffer.drawString(SCORE,90,70);//画SCOREgBuffer.drawString(BEST,315,70);//画BESTgBuffer.setFont(numberFont);gBuffer.setColor(Color.white);if(score10){gBuffer.drawString(String.valueOf(score),110,115);//画分数数字}elseif(score100){gBuffer.drawString(String.valueOf(score),100,115);//画分数数字}elseif(score1000){gBuffer.drawString(String.valueOf(score),85,115);//画分数数字}elseif(score10000){gBuffer.drawString(String.valueOf(score),73,115);//画分数数字}elseif(score100000){gBuffer.drawString(String.valueOf(score),60,115);//画分数数字}elseif(score1000000){gBuffer.drawString(String.valueOf(score),50,115);//画分数数字}else{gBuffer.drawString(String.valueOf(score),37,115);//画分数数字}if(highestScore10){gBuffer.drawString(String.valueOf(highestScore),325,115);//画分数数字}elseif(highestScore100){gBuffer.drawString(String.valueOf(highestScore),315,115);//画分数数字}elseif(highestScore1000){gBuffer.drawString(String.valueOf(highestScore),300,115);//画分数数字}elseif(highestScore10000){gBuffer.drawString(String.valueOf(highestScore),288,115);//画分数数字}elseif(highestScore100000){gBuffer.drawString(String.valueOf(highestScore),275,115);//画分数数字}elseif(highestScore1000000){gBuffer.drawString(String.valueOf(highestScore),265,115);//画分数数字}else{gBuffer.drawString(String.valueOf(highestScore),252,115);//画分数数字}gBuffer.setColor(foreground);for(inti=0;i4;i++){for(intj=0;j4;j++){gBuffer.drawRoundRect(10+j*112,160+i*112,100,100,10,10);gBuffer.fillRoundRect(10+j*112,160+i*112,100,100,10,10);}}for(inti=0;i4;i++){for(intj=0;j4;j++){switch(array[i][j]){case2:{gBuffer.setColor(newColor(238,228,218));gBuffer.drawRoundRect(10+j*112,160+i*112,100,100,10,10);gBuffer.fil
本文标题:java版2048
链接地址:https://www.777doc.com/doc-5650231 .html