java編寫俄羅斯方塊_第1頁
java編寫俄羅斯方塊_第2頁
java編寫俄羅斯方塊_第3頁
java編寫俄羅斯方塊_第4頁
java編寫俄羅斯方塊_第5頁
已閱讀5頁,還剩40頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

java編寫俄羅斯方塊Game_Box.java//方塊類publicclassGame_Box{staticint[][]pattern={{0x0f00,0x4444,0x0f00,0x4444},//長條形{0x04e0,0x0464,0x00e4,0x04c4},//T型{0x2640,0xc600,0x2640,0xc600},//Z型{0x6220,0x1700,0x2230,0x0740},//L型{0x0660,0x0660,0x0660,0x0660}//田字形intblockType;//塊的模式號(hào)(0-6)intturnState;//塊的翻轉(zhuǎn)狀態(tài)(0-3)intblockState;//塊的下落狀態(tài)introw;//行intcol;//列Game_Drawscr;//聲明類型//塊類的構(gòu)造方法Game_Box(Game_Drawgame_scr){this.scr=game_scr;blockType=(int)(Math.random()7);//turnState=(int)Math.random()3;blockState=1;row=game_scr.getInitRow();col=game_scr.getInitCol();}//重新初始化塊,并顯示新塊publicvoidreset(){blockType=(int)(Math.random()7);//隨機(jī)從7中生成一中方塊//turnState=(int)(Math.random()3);blockState=1;row=scr.getInitRow();col=scr.getInitCol();dispBlock(1);}//實(shí)現(xiàn)“塊”翻轉(zhuǎn)的方法publicvoidleftTurn(){if(assertValid(blockType,(turnState+1)%4,row,col)){dispBlock(0);turnState=(turnState+1)%4;dispBlock(1);}}//實(shí)現(xiàn)“塊”的左移的方法publicvoidleftMove(){if(assertValid(blockType,turnState,row,col-1)){dispBlock(0);col--;dispBlock(1);}}//實(shí)現(xiàn)塊的右移publicvoidrightMove(){if(assertValid(blockType,turnState,row,col+1)){dispBlock(0);dispBlock(1);}}//實(shí)現(xiàn)塊落下的操作的方法publicbooleanfallDown(){if(blockState==2)returnfalse;if(assertValid(blockType,turnState,row-1,col)){dispBlock(0);row--;dispBlock(1);return(true);}else{blockState=2;dispBlock(2);returnfalse;}}//判斷是否正確的方法booleanassertValid(intt,ints,introw,intcol){intk=0x8000;for(inti=0;i<4;i++){for(intj=0;j<4;j++){if((int)(pattern[t][s]&k)!=0){inttemp=scr.getScrArrXY(row-i,col+j);if(temp<0||temp==2)returnfalse;}kk2;}}returntrue}//同步顯示的方法publicsynchronizedvoiddispBlock(ints){intk=0x8000;System.out.println((int)pattern[blockType][turnState]);for(inti=0;i<4;i++){for(intj=0;j<4;j++){if(((int)pattern[blockType][turnState]&k)!=0){scr.drawUnit(row-i,col+j,s);}kk/2;}}}}Game_Command.javaimportjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;publicclassGame_CommandimplementsActionListener{staticfinalintbutton_play=1;//給按鈕分配編號(hào)staticfinalintbutton_levelup=2;staticfinalintbutton_leveldown=3;staticfinalintbutton_quit=4;staticfinalintbutton_pause=5;staticbooleanpause_resume=true;intcurButton;//當(dāng)前按鈕Game_Drawscr;//控制按鈕類的構(gòu)造方法Game_Command(intbutton,Game_Drawscr){curButton=button;this.scr=scr;}//按鈕執(zhí)行方法publicvoidactionPerformed(ActionEvente){switch(curButton){case1:if(!Game_Layout.isPlay){scr.initScr();Game_Layout.isPlay=true;Game_Layout.score=0;Game_Layout.scoreField.setText("0");Game_Layout.timer.resume();}scr.requestFocus();case2:if(Game_Layout.level<10){Game_Layout.level++;Game_Layout.levelField.setText(""+Game_Layout.level);Game_Layout.score=0;Game_Layout.scoreField.setText(""+Game_Layout.score);}scr.requestFocus();case3:if(Game_Layout.level>1){Game_Layout.level--;Game_Layout.levelField.setText(""+Game_Layout.level);Game_Layout.score=0;Game_Layout.scoreField.setText(""+Game_Layout.score);}scr.requestFocus();eSystem.exit(0);}}}Game_Draw.javaimportjava.awt.Canvas;importjava.awt.Color;importjava.awt.Graphics;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;publicclassGame_DrawextendsCanvasimplementsKeyListener{finalintunitSize=30;//小方塊邊長introwNum;//正方格的行數(shù)intcolumnNum;//正方格的列數(shù)intmaxAllowRowNum;//允許有多少行未削intblockInitRow;//新出現(xiàn)塊的起始行坐標(biāo)intblockInitCol;//新出現(xiàn)塊的起始列坐標(biāo)int[][]scrArr;//屏幕數(shù)組Game_Boxb=newGame_Box(this);//對(duì)方快的引用Game_MyTimertime;//畫布類的構(gòu)造方法Game_Draw(){rowNum=15;columnNum=10;maxAllowRowNum=rowNum-2;blockInitRow=rowNum;blockInitCol=columnNum-7;scrArr=newint[32][32];}//初始化屏幕,并將屏幕數(shù)組清零的方法publicvoidinitScr(){for(inti=0;i<rowNum;i++)for(intj=0;j<columnNum;j++)scrArr[i][j]=0;b.reset();aint}//重新刷新畫布方法publicvoidpaint(Graphicsg){for(inti=0;i<rowNum;i++)for(intj=0;j<columnNum;j++)drawUnit(i,j,scrArr[i][j]);}//畫方塊的方法publicvoiddrawUnit(introw,intcol,inttype){System.out.println(getSize().height);scrArr[row][col]=type;Graphicsg=getGraphics();switch(type){//表示畫方快的方法case:g.setColor(Color.BLACK);break;//以背景為顏色畫case1:g.setColor(Color.blue);break;//畫正在下落的方塊case2:g.setColor(Color.GRAY);break;//畫已經(jīng)落下的方法}g.fill3DRect(colunitSize,getSize().height-(row+1)unitSize,unitSize,unitSize,true);g.dispose();}publicGame_BoxgetBlock(){}//返回屏幕數(shù)組中(row,col)位置的屬性值publicintgetScrArrXY(introw,intcol){if(row<0||row>=rowNum||col<0||col>=columnNum)return(-1);return(scrArr[row][col]);}//返回新塊的初始行坐標(biāo)方法publicintgetInitRow(){return(blockInitRow);//返回新塊的初始行坐標(biāo)}//返回新塊的初始列坐標(biāo)方法publicintgetInitCol(){return(blockInitCol);//返回新塊的初始列坐標(biāo)}//滿行刪除方法voiddeleteFullLine(){intfull_line_num=0;intk=0;for(inti=0;i<rowNum;i++){booleanisfull=true;for(intj=0;j<columnNum;j++)if(scrArr[i][j]==0){isfull=false;break;}if(isfull)full_line_num+=100;if(k!=0&&k-1!=i&&!isfull)for(intj=0;j<columnNum;j++){if(scrArr[i][j]==0)drawUnit(k-1,j,0);drawUnit(k-1,j,2);scrArr[k-1][j]=scrArr[i][j];}}for(inti=k-1;i<rowNum;i++){for(intj=0;j<columnNum;j++){drawUnit(i,j,0);scrArr[i][j]=0;}}Game_Layout.score+=full_line_num;Game_Layout.scoreField.setText(""+Game_Layout.score);}//判斷游戲是否結(jié)束方法booleanisGameEnd(){for(intcol=0;col<columnNum;col++){if(scrArr[maxAllowRowNum][col]!=0)returntrue}returnfalse;}publicvoidkeyTyped(KeyEvente){}publicvoidkeyReleased(KeyEvente){}//處理鍵盤輸入的方法publicvoidkeyPressed(KeyEvente){}//booleanT=true;if(!Game_Layout.isPlay)switch(e.getKeyCode()){caseKeyEvent.VK_DOWN:b.fallDown();caseKeyEvent.VK_LEFT:b.leftMove();caseKeyEvent.VK_RIGHT:b.rightMove();caseKeyEvent.VK_SPACE:b.leftTurn();}}Game_Layout.javaimportjava.awt.Button;importjava.awt.Dimension;importjava.awt.GridLayout;importjava.awt.Label;importjava.awt.Panel;importjava.awt.TextField;importjava.awt.event.WindowListener;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;publicclassGame_LayoutextendsJFrame{publicstaticintlevel=1;publicstaticintscore=0;publicstaticTextFieldscoreField;publicstaticTextFieldlevelField;publicstaticbooleanisPlay=false;publicstaticGame_MyTimertimer;Game_DrawgameScr=newGame_Draw();//實(shí)例主屏//俄羅斯方塊類的構(gòu)造方法Game_Layout(){setTitle("俄羅斯方塊");setSize(620,480);setLayout(newGridLayout(1,2));//整體分為兩個(gè)部分gameScr.addKeyListener(gameScr);//就收鍵盤監(jiān)聽,監(jiān)聽的內(nèi)容是游戲主界面timer=newGame_MyTimer(gameScr);timer.setDaemon(true);timer.start();timer.suspend();add(gameScr);JPanelrightScr=newJPanel();rightScr.setLayout(newGridLayout(2,1,0,0));//rightScr.setSize(120,480);//LeftScr.setSize(1000,40);add(rightScr);//add(LeftScr);//右邊信息窗體的布局Game_MyPanelinfoScr=newGame_MyPanel();//infoScr.setSize(120,300);rightScr.add(infoScr);//定義標(biāo)簽和初始值JLabelscorep=newJLabel("分?jǐn)?shù):");JLabellevelp=newJLabel("級(jí)數(shù):");scoreField=newTextField(8);//定義文本長度levelField=newTextField(8);scoreField.setEditable(false);//文本不可以編輯levelField.setEditable(false);infoScr.add(scorep);infoScr.add(scoreField);infoScr.add(levelp);infoScr.add(levelField);//scorep.setSize(10,10);//scoreField.setSize(newDimension(20,60));//levelp.setSize(newDimension(20,60));//levelField.setSize(newDimension(20,60));scoreField.setText("0");levelField.setText("1");//右邊控制按鈕窗體的布局Game_MyPanelcontrolScr=newGame_MyPanel();//控制面板直為5rightScr.add(controlScr);JButtonplay_b=newJButton("開始游戲");//play_b.setSize(newDimension(50,20));play_b.addActionListener(newGame_Command(1,gameScr));JButtonlevel_up_b=newJButton("提高級(jí)數(shù)");level_up_b.addActionListener(newGame_Command(2,gameScr));JButtonlevel_down_b=newJButton("降低級(jí)數(shù)");level_down_b.addActionListener(newGame_Command(3,gameScr));JButtonquit_b=newJButto

溫馨提示

  • 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)論