




已閱讀5頁(yè),還剩20頁(yè)未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
項(xiàng)目實(shí)戰(zhàn) 俄羅斯方塊,主講:賈宗維,程序演示,游戲01_功能演示與說(shuō)明 游戲02_面向?qū)ο笤O(shè)計(jì) 游戲03_使用API類組裝游戲 游戲04_編寫各個(gè)類主體框架 游戲05_編寫Controler類實(shí)現(xiàn)事件監(jiān)聽(tīng) 游戲06_編寫類測(cè)試代碼 游戲07_圖形設(shè)計(jì)與創(chuàng)建 游戲08_圖形移動(dòng)與顯示 游戲09_處理游戲邊界問(wèn)題 游戲10_障礙物生成與顯示 游戲11_消除滿行的障礙物 游戲12_增加游戲結(jié)束 游戲13_定時(shí)下落,編寫各個(gè)類主體框架-Shape類,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 moveDown“); 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.sleep(1000); catch (InterruptedException e) / TODO Auto-generated catch block e.printStackTrace();,編寫各個(gè)類主體框架-ShapeFactory類,public class ShapeFactory public Shape getShape(ShapeListener listener) System.out.println(“ShapeFactorys getShape“); Shape shape=new Shape(); return shape; ,編寫各個(gè)類主體框架-Ground類,package cn.tetris.entities; public class Ground public void accept() System.out.println(“Grounds accept“); public void drawMe() System.out.println(“Grounds drawMe“); ,編寫各個(gè)類主體框架-GamePanel類,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 void paintComponent(Graphics arg0) / TODO Auto-generated method stub /重新顯示 if (ground!=null ,編寫各個(gè)類主體框架-Controller類,public class Controller extends KeyAdapter private Ground ground; private Shape shape; private ShapeFactory shapeFactory; private GamePanel gamePanel; public void keyPressed(KeyEvent 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); ,編寫各個(gè)類主體框架-ShapeListener接口,public interface ShapeListener void shapeMoveDown(Shape shape); ,Shape類增加監(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 moveDown“); 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.sleep(1000); catch (InterruptedException e) / TODO Auto-generated catch block e.printStackTrace();,Shape類中增加注冊(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類實(shí)現(xiàn)ShapeListener接口,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 getShape“); Shape shape=new Shape(); shape.addShapeListener(listener); return shape; ,GamePanel類設(shè)置大小,public GamePanel() this.setSize(300,300); ,Controller類中增加開(kāi)始新游戲方法,public void newGame() shape=shapeFactory.getShape(this); ,Controller類中如何接收外部控制的對(duì)象,public Controller(ShapeFactory shapeFactory,Ground ground, GamePanel gamePanel) this.shapeFactory=shapeFactory; this.ground=ground; this.gamePanel=gamePanel; ,測(cè)試類Game,public class Game public static void main(String args) / TODO Auto-generated method stub ShapeFactory shapeFactory=new ShapeFactory(); Ground ground=new Ground(); GamePanel gamePanel=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.addKeyListener(controller); frame.setVisible(true); controller.newGame(); ,程序步驟,游戲01_功能演示與說(shuō)明 游戲02_面向?qū)ο笤O(shè)計(jì) 游戲03_使用API類組裝游戲 游戲04_編寫各個(gè)類主體框架 游戲05_編寫Controler類實(shí)現(xiàn)事件監(jiān)聽(tīng) 游戲06_編寫類測(cè)試代碼 游戲07_圖形設(shè)計(jì)與創(chuàng)建 游戲08_圖形移動(dòng)與顯示 游戲09_處理游戲邊界問(wèn)題 游戲10_障礙物生成與顯示 游戲11_消除滿行的障礙物 游戲12_增加游戲結(jié)束 游戲13_定時(shí)下落,Shape類,增加圖形的描述,/二維變量用于保存圖形的所有狀態(tài) private 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類增加生產(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, 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類中增加圖形的位置信息,/表示圖形距離左側(cè)的距離 private 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類中增加圖形的繪制方法,public void drawMe(Graphics g) System.out.println(“shapes drawme“); g.setColor(Color.RED); /循環(huán)訪問(wèn)代表方正的數(shù)組 for(int x=0;x4;x+) for(int y=0;y4;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 getFlagByPoint(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; pu
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 造船作業(yè)現(xiàn)場(chǎng)管理辦法
- 襄陽(yáng)水路維修管理辦法
- 福州單位餐飲管理辦法
- 廣西交通住宿管理辦法
- 2025年茶藝師(茶葉行業(yè)市場(chǎng)拓展)職業(yè)技能鑒定試卷
- 2025年電子商務(wù)師(職業(yè)資格專業(yè)專業(yè)專業(yè)專業(yè)級(jí))考試試卷
- 科研資金項(xiàng)目管理辦法
- 小學(xué)勞動(dòng)基地管理辦法
- 評(píng)估機(jī)構(gòu)日常管理辦法
- 藥品運(yùn)輸貨源管理辦法
- 腫瘤免疫治療及護(hù)理講課件
- 浙江2025年6月高二學(xué)考模擬-數(shù)學(xué)試題及答案
- 臺(tái)胞臺(tái)屬活動(dòng)方案
- 百師聯(lián)盟2023-2024學(xué)年高一年級(jí)下學(xué)期6月期末聯(lián)考考試卷 生物及答案
- 新建3000P(Flops)智算超算中心項(xiàng)目可行性研究報(bào)告寫作模板-備案審批
- 八年級(jí)數(shù)學(xué)下學(xué)期《平行四邊形》的教學(xué)反思
- 2025-2030中國(guó)交流伺服控制器行業(yè)應(yīng)用動(dòng)態(tài)及投資前景分析報(bào)告
- 林業(yè)碳匯項(xiàng)目開(kāi)發(fā)流程與審核要點(diǎn)
- 堅(jiān)持嚴(yán)格陣地管理制度
- 2025-2030全球及中國(guó)實(shí)驗(yàn)室信息管理系統(tǒng)和和LIMS行業(yè)市場(chǎng)現(xiàn)狀供需分析及投資評(píng)估規(guī)劃分析研究報(bào)告
- T/BECC 002-2024智算中心技術(shù)要求和評(píng)估方法
評(píng)論
0/150
提交評(píng)論