Java俄羅斯方塊實現(xiàn)步驟.ppt
《Java俄羅斯方塊實現(xiàn)步驟.ppt》由會員分享,可在線閱讀,更多相關(guān)《Java俄羅斯方塊實現(xiàn)步驟.ppt(25頁珍藏版)》請在裝配圖網(wǎng)上搜索。
項目實戰(zhàn)--俄羅斯方塊,主講:賈宗維,程序演示,游戲01_功能演示與說明游戲02_面向?qū)ο笤O(shè)計游戲03_使用API類組裝游戲游戲04_編寫各個類主體框架游戲05_編寫Controler類實現(xiàn)事件監(jiān)聽游戲06_編寫類測試代碼游戲07_圖形設(shè)計與創(chuàng)建游戲08_圖形移動與顯示游戲09_處理游戲邊界問題游戲10_障礙物生成與顯示游戲11_消除滿行的障礙物游戲12_增加游戲結(jié)束游戲13_定時下落,編寫各個類主體框架-Shape類,publicclassShape{//privateShapeListenerlistener;publicvoidmoveLeft(){System.out.println("shapesmoveLeft");}publicvoidmoveRight(){System.out.println("shapesmoveright");}publicvoidmoveDown(){System.out.println("shapesmoveDown");}publicvoidrotate(){System.out.println("shapesrotate");}publicvoiddrawMe(){System.out.println("shapesdrawme");},privateclassShapeDriverimplementsRunnable{publicvoidrun(){//TODOAuto-generatedmethodstubwhile(true){moveDown();//listener.shapeMoveDown(Shape.this);try{Thread.sleep(1000);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();,編寫各個類主體框架-ShapeFactory類,publicclassShapeFactory{publicShapegetShape(ShapeListenerlistener){System.out.println("ShapeFactorysgetShape");Shapeshape=newShape();returnshape;}},編寫各個類主體框架-Ground類,packagecn.tetris.entities;publicclassGround{publicvoidaccept(){System.out.println("Groundsaccept");}publicvoiddrawMe(){System.out.println("GroundsdrawMe");}},編寫各個類主體框架-GamePanel類,publicclassGamePanelextendsJPanel{privateGroundground;privateShapeshape;publicvoiddisplay(Groundground,Shapeshape){System.out.println("GamePanelsdisplay");this.ground=ground;this.shape=shape;this.repaint();}@OverrideprotectedvoidpaintComponent(Graphicsarg0){//TODOAuto-generatedmethodstub//重新顯示if(ground!=null}},編寫各個類主體框架-Controller類,publicclassControllerextendsKeyAdapter{privateGroundground;privateShapeshape;privateShapeFactoryshapeFactory;privateGamePanelgamePanel;publicvoidkeyPressed(KeyEvente){switch(e.getKeyCode()){caseKeyEvent.VK_UP:shape.rotate();break;caseKeyEvent.VK_LEFT:shape.moveLeft();break;caseKeyEvent.VK_RIGHT:shape.moveRight();break;caseKeyEvent.VK_DOWN:shape.moveDown();break;}gamePanel.display(ground,shape);},編寫各個類主體框架-ShapeListener接口,publicinterfaceShapeListener{voidshapeMoveDown(Shapeshape);},Shape類增加監(jiān)聽器對象及下落后調(diào)用,publicclassShape{privateShapeListenerlistener;publicvoidmoveLeft(){System.out.println("shapesmoveLeft");}publicvoidmoveRight(){System.out.println("shapesmoveright");}publicvoidmoveDown(){System.out.println("shapesmoveDown");}publicvoidrotate(){System.out.println("shapesrotate");}publicvoiddrawMe(){System.out.println("shapesdrawme");},privateclassShapeDriverimplementsRunnable{publicvoidrun(){//TODOAuto-generatedmethodstubwhile(true){moveDown();listener.shapeMoveDown(Shape.this);try{Thread.sleep(1000);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();,Shape類中增加注冊監(jiān)聽器的方法,publicvoidaddShapeListener(ShapeListenerl){if(l!=null){this.listener=l;}},Shape構(gòu)造方法中啟動下落線程,publicShape(){newThread(newShapeDriver()).start();},Controller類實現(xiàn)ShapeListener接口,publicclassControllerextendsKeyAdapterimplementsShapeListener{publicvoidshapeMoveDown(Shapeshape){//TODOAuto-generatedmethodstubgamePanel.display(ground,shape);},生產(chǎn)圖形時同時注冊監(jiān)聽器,publicclassShapeFactory{publicShapegetShape(ShapeListenerlistener){System.out.println("ShapeFactorysgetShape");Shapeshape=newShape();shape.addShapeListener(listener);returnshape;}},GamePanel類設(shè)置大小,publicGamePanel(){this.setSize(300,300);},Controller類中增加開始新游戲方法,publicvoidnewGame(){shape=shapeFactory.getShape(this);},Controller類中如何接收外部控制的對象,publicController(ShapeFactoryshapeFactory,Groundground,GamePanelgamePanel){this.shapeFactory=shapeFactory;this.ground=ground;this.gamePanel=gamePanel;},測試類Game,publicclassGame{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubShapeFactoryshapeFactory=newShapeFactory();Groundground=newGround();GamePanelgamePanel=newGamePanel();Controllercontroller=newController(shapeFactory,ground,gamePanel);JFrameframe=newJFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(gamePanel.getSize().width+10,gamePanel.getSize().height+35);frame.add(gamePanel);gamePanel.addKeyListener(controller);frame.setVisible(true);controller.newGame();}},程序步驟,游戲01_功能演示與說明游戲02_面向?qū)ο笤O(shè)計游戲03_使用API類組裝游戲游戲04_編寫各個類主體框架游戲05_編寫Controler類實現(xiàn)事件監(jiān)聽游戲06_編寫類測試代碼游戲07_圖形設(shè)計與創(chuàng)建游戲08_圖形移動與顯示游戲09_處理游戲邊界問題游戲10_障礙物生成與顯示游戲11_消除滿行的障礙物游戲12_增加游戲結(jié)束游戲13_定時下落,Shape類,增加圖形的描述,//二維變量用于保存圖形的所有狀態(tài)privateint[][]body;//用于保存圖形當(dāng)前的狀態(tài)privateintstatus;//設(shè)置狀態(tài)的方法publicvoidsetBody(intbody[][]){this.body=body;}//設(shè)置當(dāng)前是第幾種狀態(tài)publicvoidsetStatus(intstatus){this.status=status;},ShapeFactory類增加生產(chǎn)各種圖形,//三維數(shù)組用于表示一種圖形的多種形狀privateintshapes[][][]=newint[][][]{{{1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0},{1,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0},{1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0},{0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0}}};,publicShape//生產(chǎn)一個隨機(jī)數(shù),用于表示圖形的狀態(tài)inttype=newRandom().nextInt(shapes.length);//設(shè)置圖形有幾種狀態(tài)shape.setBody(shapes[type]);//設(shè)置默認(rèn)狀態(tài)shape.setStatus(0);returnshape;}},Shape類中增加圖形的位置信息,//表示圖形距離左側(cè)的距離privateintleft;//表示圖形距離上邊界的距離privateinttop;publicvoidmoveLeft(){left--;}publicvoidmoveRight(){left++;}publicvoidmoveDown(){top++;}publicvoidrotate(){//顯示下一個狀態(tài),但得保證狀態(tài)值不超過4,所以需處理status=(status+1)%body.length;},Shape類中增加圖形的繪制方法,publicvoiddrawMe(Graphicsg){System.out.println("shapesdrawme");g.setColor(Color.RED);//循環(huán)訪問代表方正的數(shù)組for(intx=0;x<4;x++){for(inty=0;y<4;y++){if(getFlagByPoint(x,y)){g.fill3DRect((left+x)*Global.CELL_SIZE,(top+y)*Global.CELL_SIZE,Global.CELL_SIZE,Global.CELL_SIZE,true);}}}}//獲取方正中標(biāo)志是1還是0,1表示要繪圖0表示不繪圖privatebooleangetFlagByPoint(intx,inty){returnbody[status][y*4+x]==1;},游戲常量的存放——Glaobal,publicclassGlobal{//表示每個方格的像素值publicstaticfinalintCELL_SIZE=20;//表示圖形面板有多少個格子寬和高publicstaticfinalintWIDTH=15;publicstaticfinalintHEIGHT=15;},測試游戲,下落方塊出現(xiàn)拖影,在GamePanel類的paintComponent方法中增加如下代碼:在填充一個與背景同色的區(qū)域g.setColor(newColor(0 xcfcfcf));g.fillRect(0,0,Global.CELL_SIZE*Global.WIDTH,Global.CELL_SIZE*Global.HEIGHT);//填充一個灰色顯示區(qū)域,避免圖形拖影修改此處的同時還需修改Shape的drawme方法中設(shè)置圖形的顏色,以及給frame也注冊監(jiān)聽器。,圖形如何避免移出邊界,publicbooleanisMoveable(Shapeshape,intaction){//得到圖像當(dāng)前的位置信息intleft=shape.getLeft();inttop=shape.getTop();//根據(jù)圖形的動作,得出圖形最新的位置信息switch(action){caseShape.LEFT:left--;break;caseShape.RIGHT:left++;break;caseShape.DOWN:top++;break;},//依次取出圖形中的每一個點,判斷是否超出顯示區(qū)域for(intx=0;x=Global.HEIGHT||left+x=Global.WIDTH)returnfalse;}}}returntrue;},- 1.請仔細(xì)閱讀文檔,確保文檔完整性,對于不預(yù)覽、不比對內(nèi)容而直接下載帶來的問題本站不予受理。
- 2.下載的文檔,不會出現(xiàn)我們的網(wǎng)址水印。
- 3、該文檔所得收入(下載+內(nèi)容+預(yù)覽)歸上傳者、原創(chuàng)作者;如果您是本文檔原作者,請點此認(rèn)領(lǐng)!既往收益都?xì)w您。
下載文檔到電腦,查找使用更方便
9.9 積分
下載 |
- 配套講稿:
如PPT文件的首頁顯示word圖標(biāo),表示該P(yáng)PT已包含配套word講稿。雙擊word圖標(biāo)可打開word文檔。
- 特殊限制:
部分文檔作品中含有的國旗、國徽等圖片,僅作為作品整體效果示例展示,禁止商用。設(shè)計者僅對作品中獨(dú)創(chuàng)性部分享有著作權(quán)。
- 關(guān) 鍵 詞:
- Java 俄羅斯方塊 實現(xiàn) 步驟
鏈接地址:http://www.820124.com/p-13189693.html