Java模板游戏代码_第1页
Java模板游戏代码_第2页
Java模板游戏代码_第3页
Java模板游戏代码_第4页
Java模板游戏代码_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

-.z.魔板游戏代码publicclassPuzzleFramee*tendsJFrameimplementsActionListener{PuzzlePadpuzzlePad;utton开始;JMenuBarbar;JMenufileMenu;JMenuItem初级,高级;JRadioButton数字玩法,图像玩法;ButtonGroupgroup=null;Containercon=null;publicPuzzleFrame(){bar=newJMenuBar();fileMenu=newJMenu("魔板游戏");初级=newJMenuItem("初级");高级=newJMenuItem("高级");fileMenu.add(初级);fileMenu.add(高级);bar.add(fileMenu);setJMenuBar(bar);初级.addActionListener(this);高级.addActionListener(this);开始=newutton("开始玩");开始.addActionListener(this);group=newButtonGroup();数字玩法=newJRadioButton("数字玩法",true);图像玩法=newJRadioButton("图像玩法",false);group.add(数字玩法);group.add(图像玩法);puzzlePad=newPuzzlePad(3,3,50,50);con=getContentPane();con.add(puzzlePad,BorderLayout.CENTER);JPanelpNorth=newJPanel();pNorth.add(数字玩法);pNorth.add(图像玩法);pNorth.add(开始);con.add(pNorth,BorderLayout.NORTH);con.validate();addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.e*it(0);}});setVisible(true);setBounds(100,50,440,360);validate();}publicvoidactionPerformed(ActionEvente){if(e.getSource()==开始){if(数字玩法.isSelected()){puzzlePad.随机排列数字();}elseif(图像玩法.isSelected()){puzzlePad.随机排列图像();}}elseif(e.getSource()==初级){con.remove(puzzlePad);puzzlePad=newPuzzlePad(3,3,50,50);con.add(puzzlePad,BorderLayout.CENTER);con.validate();this.validate();}elseif(e.getSource()==高级){con.remove(puzzlePad);puzzlePad=newPuzzlePad(4,4,50,50);con.add(puzzlePad,BorderLayout.CENTER);con.validate();this.validate();}}publicstaticvoidmain(Stringargs[]){newPuzzleFrame();}}MobanFrame类:importjava.awt.*;importjava.awt.event.*;import.*;importjava.applet.Applet;importjava*.imageio.ImageIO;importjava.io.File;importjava.awt.image.*;importjava*.swing.JOptionPane;publicclassMobanFramee*tendsFrameimplementsActionListener,MouseListener,ItemListener{ MobanSquare[][]position; PointstartPoint=newPoint(100,130); PointrightStartPoint=newPoint(400,90); int[]num; Point[]pointMove; inttotalStep=0; StringselectedImage="狗"; StringgamingImage=""; booleanstartGame=false; intsquareNumber=3; intlevel=1; intsquareSize=80; ImagesourceImage; ImagespaceSourceImage; ImagespaceImage; Image[]myImage; Graphics2Dggg;/*--------------------*//*显示在界面上的一些组件*//*--------------------*/ Te*tFieldtf*; Te*tFieldtfy; MenuBarmnbMyMenuBar; MenumnChooseLevel; MenuItemmniLevel1,mniLevel2; LabellbStep; LabellbSuccess=newLabel(""); ButtonbtnStartGame; ChoicechChoiceImage; publicMobanFrame() { super("魔板游戏"); pointMove=newPoint[4]; pointMove[0]=newPoint(-1,0); pointMove[1]=newPoint(1,0); pointMove[2]=newPoint(0,-1); pointMove[3]=newPoint(0,1); setLayout(newFlowLayout()); chChoiceImage=newChoice(); chChoiceImage.add("狗"); chChoiceImage.add("鸡"); chChoiceImage.add("鱼"); chChoiceImage.add("熊猫"); chChoiceImage.add("大象"); chChoiceImage.add("长颈鹿"); chChoiceImage.add("牛"); chChoiceImage.add("小猫"); chChoiceImage.add("小猫2"); chChoiceImage.add("羊"); chChoiceImage.add("猴"); chChoiceImage.add("数字"); mniLevel1=newMenuItem("简单"); mniLevel2=newMenuItem("困难"); mnChooseLevel=newMenu("难度"); mnbMyMenuBar=newMenuBar(); tf*=newTe*tField(8);tfy=newTe*tField(8); tf*.setTe*t("0"); tfy.setTe*t("1"); tf*.setVisible(false); tfy.setVisible(false); lbStep=newLabel("已走步数:"+Integer.toString(totalStep)); btnStartGame=newButton("开始游戏"); mnChooseLevel.add(mniLevel1); mnChooseLevel.add(mniLevel2);mnbMyMenuBar.add(mnChooseLevel); this.setMenuBar(mnbMyMenuBar); add(tf*); add(tfy); add(lbSuccess); add(lbStep); add(chChoiceImage); add(btnStartGame); mniLevel1.addActionListener(this); mniLevel2.addActionListener(this); btnStartGame.addActionListener(this); chChoiceImage.addItemListener(this); this.addMouseListener(this); addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.e*it(0); } }); ProInit(); GetImage(); GetRandom(); setSize(600,300); setVisible(true); } publicvoidGetImage() { myImage=newImage[squareNumber*squareNumber]; ImageFiltercropFilter1; ImageFiltercropFilter2; ImageFiltercropFilter3; Toolkittool=getToolkit(); sourceImage=tool.createImage(selectedImage+".jpg"); spaceSourceImage=tool.createImage("space.jpg"); sourceImage=sourceImage.getScaledInstance(squareSize*squareNumber,squareSize*squareNumber,Image.SCALE_DEFAULT); cropFilter1=newCropImageFilter(0,0,squareSize*squareNumber,squareSize*squareNumber); sourceImage=createImage(newFilteredImageSource(sourceImage.getSource(),cropFilter1)); for(inti=0;i<squareNumber*squareNumber;i++) { cropFilter2=newCropImageFilter((i%squareNumber)*squareSize,(i/squareNumber)*squareSize,squareSize,squareSize); myImage[i]=createImage(newFilteredImageSource(sourceImage.getSource(),cropFilter2)); } cropFilter3=newCropImageFilter(0,0,squareSize,squareSize); spaceImage=createImage(newFilteredImageSource(spaceSourceImage.getSource(),cropFilter3)); myImage[squareNumber*squareNumber-1]=spaceImage; } publicvoidProInit() { num=newint[squareNumber*squareNumber]; position=newMobanSquare[squareNumber+2][squareNumber+2]; } publicvoidpaint(Graphicsg) { booleanshowSuccess=true; lbStep.setTe*t("已走步数:"+Integer.toString(totalStep)); if(startGame) { for(inti=1;i<squareNumber+1;i++) { for(intj=1;j<squareNumber+1;j++) { g.drawImage(position[i][j].GetImage(),startPoint.*+(position[i][j].y-1)*squareSize,startPoint.y+(position[i][j].*-1)*squareSize,this);//绘制左边打乱的方块 } }g.setColor(Color.cyan); for(inti=0;i<squareNumber+1;i++) { g.drawLine(startPoint.*+squareSize*i,startPoint.y,startPoint.*+squareSize*i,startPoint.y+squareNumber*squareSize); g.drawLine(startPoint.*,startPoint.y+squareSize*i,startPoint.*+squareNumber*squareSize,startPoint.y+squareSize*i); } } g.drawImage(sourceImage,rightStartPoint.*,rightStartPoint.y,this); //g.drawImage(spaceImage,rightStartPoint.*+(squareNumber-1)*squareSize,rightStartPoint.y+(squareNumber-1)*squareSize,this); for(inti=1;i<squareNumber+1;i++) { for(intj=1;j<squareNumber+1;j++) { if(position[i][j].GetOrder()!=(i-1)*squareNumber+(j-1)) { showSuccess=false; } } } if(showSuccess) { showSuccess=false; lbSuccess.setTe*t("恭喜您,成功完成"); } } publicvoidGetRandom() { intk=squareNumber*squareNumber; booleannumE*ist=false; inttotal=0; for(intj=0;j<squareNumber*squareNumber;j++) { num[j]=-1; } while(total<squareNumber*squareNumber) { k=((int)(Math.random()*10)+(int)(Math.random()*10))%(squareNumber*squareNumber); for(intj=0;j<total;j++) { if(k==num[j]) { numE*ist=true; break; } } if(!numE*ist) { num[total]=k; total++; } numE*ist=false; } for(inti=0;i<squareNumber+2;i++) { for(intj=0;j<squareNumber+2;j++) { if(i==0||i==squareNumber+1||j==0||j==squareNumber+1) { this.position[i][j]=newMobanSquare(i,j,-1,squareNumber*squareNumber-1); } } } for(inti=1;i<squareNumber+1;i++) { for(intj=1;j<squareNumber+1;j++) { this.position[i][j]=newMobanSquare(i,j,num[(i-1)*squareNumber+j-1],squareNumber*squareNumber-1); this.position[i][j].SetImage(myImage[this.position[i][j].GetOrder()]); } } } publicvoidMove(int*,inty) { intsquare_*,square_Y,order_Old,order_New; square_*=(y-1-startPoint.y)/squareSize+1; square_Y=(*-1-startPoint.*)/squareSize+1; for(inti=0;i<4;i++) { if(position[square_*+pointMove[i].*][square_Y+pointMove[i].y].arrive) { this.totalStep++; PointnewPoint=newPoint(square_*+pointMove[i].*,square_Y+pointMove[i].y); order_Old=position[square_*][square_Y].GetOrder(); order_New=position[newPoint.*][newPoint.y].GetOrder(); position[square_*][square_Y].SetOrder(order_New); position[square_*][square_Y].SetImage(myImage[order_New]); position[newPoint.*][newPoint.y].SetOrder(order_Old); position[newPoint.*][newPoint.y].SetImage(myImage[order_Old]); repaint(); break; } } } publicvoidactionPerformed(ActionEvente) { if(e.getSource()==btnStartGame) { lbSuccess.setTe*t(""); totalStep=0; if(level==1) { squareNumber=3; } if(level==2) { squareNumber=4; } startGame=true; gamingImage=selectedImage; ProInit(); GetImage(); GetRandom(); repaint(); } if(e.getSource()==mniLevel1) { level=1; startPoint=newPoint(110,120); rightStartPoint=newPoint(450,120); } if(e.getSource()==mniLevel2) { level=2; startPoint=newPoint(40,100); rightStartPoint=newPoint(440,100); } } publicvoiditemStateChanged(ItemEvente) { if(e.getItemSelectable()instanceofChoice) { selectedImage=((Choice)e.getItemSelectable()).getSelectedItem(); tf*.setTe*t(selectedImage); tfy.setTe*t(gamingImage); if(selectedImage!=gamingImage) { GetImage(); startGame=false; repaint(); } } } publicvoidmouseClicked(MouseEvente) { intclick_*,click_Y; click_*=e.get*(); click_Y=e.getY(); tf*.setTe*t(Integer.toString((e.getY()-1-startPoint.*)/squareSize+1)); tfy.setTe*t(Integer.toString((e.get*()-1-startPoint.y)/squareSize+1)); if(click_*>=startPoint.*&&click_*<=startPoint.*+squareNumber*squareSize&&click_Y>=startPoint.y&&click_Y<=startPoint.y+squareNumber*squareSize) { Move(click_*,click_Y); } } publicvoidmouseE*ited(MouseEvente) { } publicvoidmouseEntered(MouseEvente) { } publicvoidmousePressed(MouseEvente) { } publicvoidmou

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论