Java模板游戲代碼_第1頁
Java模板游戲代碼_第2頁
Java模板游戲代碼_第3頁
Java模板游戲代碼_第4頁
Java模板游戲代碼_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

-.z.魔板游戲代碼publicclassPuzzleFramee*tendsJFrameimplementsActionListener{PuzzlePadpuzzlePad;utton開始;JMenuBarbar;JMenufileMenu;JMenuItem初級(jí),高級(jí);JRadioButton數(shù)字玩法,圖像玩法;ButtonGroupgroup=null;Containercon=null;publicPuzzleFrame(){bar=newJMenuBar();fileMenu=newJMenu("魔板游戲");初級(jí)=newJMenuItem("初級(jí)");高級(jí)=newJMenuItem("高級(jí)");fileMenu.add(初級(jí));fileMenu.add(高級(jí));bar.add(fileMenu);setJMenuBar(bar);初級(jí).addActionListener(this);高級(jí).addActionListener(this);開始=newutton("開始玩");開始.addActionListener(this);group=newButtonGroup();數(shù)字玩法=newJRadioButton("數(shù)字玩法",true);圖像玩法=newJRadioButton("圖像玩法",false);group.add(數(shù)字玩法);group.add(圖像玩法);puzzlePad=newPuzzlePad(3,3,50,50);con=getContentPane();con.add(puzzlePad,BorderLayout.CENTER);JPanelpNorth=newJPanel();pNorth.add(數(shù)字玩法);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(數(shù)字玩法.isSelected()){puzzlePad.隨機(jī)排列數(shù)字();}elseif(圖像玩法.isSelected()){puzzlePad.隨機(jī)排列圖像();}}elseif(e.getSource()==初級(jí)){con.remove(puzzlePad);puzzlePad=newPuzzlePad(3,3,50,50);con.add(puzzlePad,BorderLayout.CENTER);con.validate();this.validate();}elseif(e.getSource()==高級(jí)){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("長(zhǎng)頸鹿"); chChoiceImage.add("牛"); chChoiceImage.add("小貓"); chChoiceImage.add("小貓2"); chChoiceImage.add("羊"); chChoiceImage.add("猴"); chChoiceImage.add("數(shù)字"); mniLevel1=newMenuItem("簡(jiǎn)單"); 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("已走步數(shù):"+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("已走步數(shù):"+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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論