Java完美实现2048小游戏
程序员文章站
2024-03-02 15:42:28
完美地模仿了2048游戏,是根据网友的一个2048改的。
block.java
import javax.swing.*;
import java.awt.*...
完美地模仿了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能够有所帮助。