《Java俄羅斯方塊實(shí)現(xiàn)步驟.ppt》由會(huì)員分享,可在線閱讀,更多相關(guān)《Java俄羅斯方塊實(shí)現(xiàn)步驟.ppt(25頁(yè)珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。
1、項(xiàng)目實(shí)戰(zhàn)俄羅斯方塊,主講:賈宗維,程序演示,游戲01_功能演示與說(shuō)明 游戲02_面向?qū)ο笤O(shè)計(jì) 游戲03_使用API類(lèi)組裝游戲 游戲04_編寫(xiě)各個(gè)類(lèi)主體框架 游戲05_編寫(xiě)Controler類(lèi)實(shí)現(xiàn)事件監(jiān)聽(tīng) 游戲06_編寫(xiě)類(lèi)測(cè)試代碼 游戲07_圖形設(shè)計(jì)與創(chuàng)建 游戲08_圖形移動(dòng)與顯示 游戲09_處理游戲邊界問(wèn)題 游戲10_障礙物生成與顯示 游戲11_消除滿(mǎn)行的障礙物 游戲12_增加游戲結(jié)束 游戲13_定時(shí)下落,編寫(xiě)各個(gè)類(lèi)主體框架-Shape類(lèi),public class Shape //private ShapeListener listener; public void moveLeft()
2、System.out.println(shapes moveLeft); public void moveRight() System.out.println(shapes moveright); public void moveDown() System.out.println(shapes moveDown); public void rotate() System.out.println(shapes rotate); public void drawMe() System.out.println(shapes drawme); ,private class S
3、hapeDriver implements Runnable public void run() // TODO Auto-generated method stub while(true) moveDown(); //listener.shapeMoveDown(Shape.this); try Thread.sleep(1000); catch (InterruptedException e) // TODO Auto-generated catch block e.printStackTrace();,編寫(xiě)各個(gè)類(lèi)主體框架-ShapeFactory類(lèi),public class ShapeF
4、actory public Shape getShape(ShapeListener listener) System.out.println(ShapeFactorys getShape); Shape shape=new Shape(); return shape; ,編寫(xiě)各個(gè)類(lèi)主體框架-Ground類(lèi),package cn.tetris.entities; public class Ground public void accept() System.out.println(Grounds accept); public void drawMe() System.out
5、.println(Grounds drawMe); ,編寫(xiě)各個(gè)類(lèi)主體框架-GamePanel類(lèi),public class GamePanel extends JPanel private Ground ground; private Shape shape; public void display(Ground ground,Shape shape) System.out.println(GamePanels display); this.ground=ground; this.shape=shape; this.repaint(); Override protected voi
6、d paintComponent(Graphics arg0) // TODO Auto-generated method stub //重新顯示 if (ground!=null ,編寫(xiě)各個(gè)類(lèi)主體框架-Controller類(lèi),public class Controller extends KeyAdapter private Ground ground; private Shape shape; private ShapeFactory shapeFactory; private GamePanel gamePanel; public void keyPressed(KeyEvent
7、e) switch(e.getKeyCode()) case KeyEvent.VK_UP: shape.rotate(); break; case KeyEvent.VK_LEFT: shape.moveLeft(); break; case KeyEvent.VK_RIGHT: shape.moveRight(); break; case KeyEvent.VK_DOWN: shape.moveDown(); break; gamePanel.display(ground, shape); ,編寫(xiě)各個(gè)類(lèi)主體框架-ShapeListener接口,public interface ShapeL
8、istener void shapeMoveDown(Shape shape); ,Shape類(lèi)增加監(jiān)聽(tīng)器對(duì)象及下落后調(diào)用,public class Shape private ShapeListener listener; public void moveLeft() System.out.println(shapes moveLeft); public void moveRight() System.out.println(shapes moveright); public void moveDown() System.out.println(shapes moveD
9、own); public void rotate() System.out.println(shapes rotate); public void drawMe() System.out.println(shapes drawme); ,private class ShapeDriver implements Runnable public void run() // TODO Auto-generated method stub while(true) moveDown(); listener.shapeMoveDown(Shape.this); try Thread.slee
10、p(1000); catch (InterruptedException e) // TODO Auto-generated catch block e.printStackTrace();,Shape類(lèi)中增加注冊(cè)監(jiān)聽(tīng)器的方法,public void addShapeListener(ShapeListener l) if(l!=null) this.listener =l; ,Shape構(gòu)造方法中啟動(dòng)下落線程,public Shape() new Thread(new ShapeDriver()).start(); ,Controller類(lèi)實(shí)現(xiàn)ShapeListener接口,
11、public class Controller extends KeyAdapter implements ShapeListener public void shapeMoveDown(Shape shape) // TODO Auto-generated method stub gamePanel.display(ground, shape); ,生產(chǎn)圖形時(shí)同時(shí)注冊(cè)監(jiān)聽(tīng)器,public class ShapeFactory public Shape getShape(ShapeListener listener) System.out.println(ShapeFactorys get
12、Shape); Shape shape=new Shape(); shape.addShapeListener(listener); return shape; ,GamePanel類(lèi)設(shè)置大小,public GamePanel() this.setSize(300,300); ,Controller類(lèi)中增加開(kāi)始新游戲方法,public void newGame() shape=shapeFactory.getShape(this); ,Controller類(lèi)中如何接收外部控制的對(duì)象,public Controller(ShapeFactory shapeFactory,Ground g
13、round, GamePanel gamePanel) this.shapeFactory=shapeFactory; this.ground=ground; this.gamePanel=gamePanel; ,測(cè)試類(lèi)Game,public class Game public static void main(String args) // TODO Auto-generated method stub ShapeFactory shapeFactory=new ShapeFactory(); Ground ground=new Ground(); GamePanel gameP
14、anel=new GamePanel(); Controller controller=new Controller(shapeFactory,ground,gamePanel); JFrame frame=new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(gamePanel.getSize().width+10, gamePanel.getSize().height+35); frame.add(gamePanel); gamePanel.addKey
15、Listener(controller); frame.setVisible(true); controller.newGame(); ,程序步驟,游戲01_功能演示與說(shuō)明 游戲02_面向?qū)ο笤O(shè)計(jì) 游戲03_使用API類(lèi)組裝游戲 游戲04_編寫(xiě)各個(gè)類(lèi)主體框架 游戲05_編寫(xiě)Controler類(lèi)實(shí)現(xiàn)事件監(jiān)聽(tīng) 游戲06_編寫(xiě)類(lèi)測(cè)試代碼 游戲07_圖形設(shè)計(jì)與創(chuàng)建 游戲08_圖形移動(dòng)與顯示 游戲09_處理游戲邊界問(wèn)題 游戲10_障礙物生成與顯示 游戲11_消除滿(mǎn)行的障礙物 游戲12_增加游戲結(jié)束 游戲13_定時(shí)下落,Shape類(lèi),增加圖形的描述,//二維變量用于保存圖形的所有狀態(tài) privat
16、e int body; //用于保存圖形當(dāng)前的狀態(tài) private int status; //設(shè)置狀態(tài)的方法 public void setBody(int body) this.body=body; //設(shè)置當(dāng)前是第幾種狀態(tài) public void setStatus(int status) this.status=status; ,ShapeFactory類(lèi)增加生產(chǎn)各種圖形,//三維數(shù)組用于表示一種圖形的多種形狀 private int shapes=new int 1,0,0,0, 1,1,1,0, 0,0,0,0, 0,0,0,0, 1,1,0,0, 1,0,0,0
17、, 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 ;,public Shape //生產(chǎn)一個(gè)隨機(jī)數(shù),用于表示圖形的狀態(tài) int type=new Random().nextInt(shapes.length); //設(shè)置圖形有幾種狀態(tài) shape.setBody(shapestype); //設(shè)置默認(rèn)狀態(tài) shape.setStatus(0); return shape; ,Shape類(lèi)中增加圖形的位置信息,//表示圖形距離左側(cè)的距離 privat
18、e int left; //表示圖形距離上邊界的距離 private int top; public void moveLeft() left--; public void moveRight() left++; public void moveDown() top++; public void rotate() //顯示下一個(gè)狀態(tài),但得保證狀態(tài)值不超過(guò)4,所以需處理 status=(status+1)%body.length; ,Shape類(lèi)中增加圖形的繪制方法,public void drawMe(Graphics g) System.out.println(s
19、hapes drawme); g.setColor(Color.RED); //循環(huán)訪問(wèn)代表方正的數(shù)組 for(int x=0;x<4;x++) for(int y=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表示不繪圖 private boolean getFlagByPoin
20、t(int x,int y) return bodystatusy*4+x==1; ,游戲常量的存放Glaobal,public class Global //表示每個(gè)方格的像素值 public static final int CELL_SIZE=20; //表示圖形面板有多少個(gè)格子寬和高 public static final int WIDTH=15; public static final int HEIGHT=15; ,測(cè)試游戲,下落方塊出現(xiàn)拖影,在GamePanel類(lèi)的paintComponent方法中增加如下代碼:在填充一個(gè)與背景同色的區(qū)域 g.setColor(new C
21、olor(0 xcfcfcf)); g.fillRect(0, 0, Global.CELL_SIZE*Global.WIDTH, Global.CELL_SIZE*Global.HEIGHT); //填充一個(gè)灰色顯示區(qū)域,避免圖形拖影 修改此處的同時(shí)還需修改Shape的drawme方法中設(shè)置圖形的顏色,以及給frame也注冊(cè)監(jiān)聽(tīng)器。,圖形如何避免移出邊界,public boolean isMoveable(Shape shape,int action) //得到圖像當(dāng)前的位置信息 int left=shape.getLeft(); int top=shape.getTop(); //根據(jù)圖形的動(dòng)作,得出圖形最新的位置信息 switch(action) case Shape.LEFT: left--; break; case Shape.RIGHT: left++; break; case Shape.DOWN: top++; break; ,//依次取出圖形中的每一個(gè)點(diǎn),判斷是否超出顯示區(qū)域 for(int x=0;x=Global.HEIGHT|| left+x=Global.WIDTH) return false; return true; ,