路是爬出來的

          #

          游戲中動畫的基礎

                  這篇文章是純粹的個人看法。

                  游戲的基礎是動畫,想來大家都知道。這幾天公司的項目都忙完了。很是無聊,所以就上網找了些資源,并寫兩個動畫的例子。在此貼出來,讓大家把磚頭砸我吧。^_^    

                  j2me midp2.0有個game的包是用來設計有游戲用的。它提供了游戲設計的基礎控件,比如雙緩沖,精靈,圖層控制器等基礎設施,這些設施可以方便我們的設計,比如雙緩沖可以讓游戲執行流暢,精靈等,可以更好的控制角色。

                 說白了。動畫的效果其實就是一幅幅圖片按照指定的時間一幅幅的換圖片而已。

                 好了。看代碼吧。

          java 代碼


           


          1.    

          2. package org.wuhua.game.timer;  

          3.   

          4. import java.util.Timer;  

          5. import java.util.TimerTask;  

          6.   

          7. /** 

          8.  *  對Timer的包裝 

          9.  * @author wuhua 

             
             

          10.  */  

          11. public class TimerTaskManager {  

          12.     private Timer _timer;  

          13.   

          14.     static TimerTaskManager instace;  

          15.   

          16.     public static TimerTaskManager getInstace() {  

          17.         if (instace == null)  

          18.             instace = new TimerTaskManager();  

          19.         return instace;  

          20.     }  

          21.   

          22.     public TimerTask add(Runnable runnable, long period) {  

          23.         TimerTask task = new RunnableTimerTask(runnable);  

          24.         long delay = period;  

          25.         getTimer().schedule(task, delay, period);  

          26.         return task;  

          27.     }  

          28.   

          29.     void close() {  

          30.         if (_timer != null) {  

          31.             _timer.cancel();  

          32.             _timer = null;  

          33.         }  

          34.     }  

          35.   

          36.     private Timer getTimer() {  

          37.         if (_timer == null)  

          38.             _timer = new Timer();  

          39.         return _timer;  

          40.     }  

          41.   

          42.     static class RunnableTimerTask extends TimerTask {  

          43.         private Runnable _runnable;  

          44.   

          45.         RunnableTimerTask(Runnable runnable) {  

          46.             _runnable = runnable;  

          47.         }  

          48.   

          49.         public void run() {  

          50.             _runnable.run();  

          51.         }  

          52.     }  

          53. }  



          java 代碼


           


          1.    

          2. package org.wuhua.game;  

          3.   

          4. import java.io.IOException;  

          5. import java.util.TimerTask;  

          6.   

          7. import javax.microedition.lcdui.Canvas;  

          8. import javax.microedition.lcdui.Graphics;  

          9. import javax.microedition.lcdui.Image;  

          10.   

          11. import org.wuhua.game.timer.TimerTaskManager;  

          12.   

          13.    

          14.   

          15. /** 

          16.  * 動畫的主類 

          17.  * @author wuhua 

             
             

          18.  */  

          19. public class Game extends Canvas implements Runnable{  

          20.   

          21.     private Image source;  

          22.     private Image action[] = new Image[10];  

          23.     private int bgcolor = 0x209C00;  

          24.     private TimerTask task;  

          25.     private static int next;  

          26.     Game(){  

          27.         try {  

          28.             source = Image.createImage("/action.png");  

          29.         } catch (IOException e) {  

          30.                

          31.             e.printStackTrace();  

          32.         }  

          33.         //切割圖片  

          34.         for(int i=0; i<5; i++){  

          35.             action[i] = Image.createImage(source, 96*i, 096600);  

          36.         }  

          37.           

          38.         for(int j=5; j<10; j++){  

          39.             action[j] = Image.createImage(source, 96*(j-5), 10296800);  

          40.         }  

          41.           

          42.         //這個是用來執行動作的計時器。原理是要求經過0.2毫秒動一次  

          43.         task = TimerTaskManager.getInstace().add(this150);   

          44.     }  

          45.     protected void paint(Graphics g) {  

          46.         fillScreen(g);  

          47.          paintAction(g);  

          48.   

          49.     }  

          50.     private void fillScreen(Graphics g) {  

          51.         g.setColor(0xFFFFFF);  

          52.         g.fillRect(00this.getWidth(), this.getHeight());  

          53.           

          54.     }  

          55.     private void paintAction(Graphics g) {  

          56.         if(next == 10)  

          57.             next =0;  

          58.         //如果繪制的圖片是出雷電的時候,讓人物停留在那里。這樣的效果會好點  

          59.         if(next>=5){  

          60.             g.drawImage(action[4], 10*40, Graphics.LEFT|Graphics.TOP);  

          61.         }  

          62.         g.drawImage(action[next], 10*next, 0, Graphics.LEFT|Graphics.TOP);  

          63.           

          64.            

          65.         next++;  

          66.           

          67.     }  

          68.     public void run() {  

          69.         repaint();  

          70.           

          71.     }  

          72.       

          73.       

          74.   

          75. }  


          posted @ 2006-12-30 09:24 路是爬出來的 閱讀(161) | 評論 (0)編輯 收藏

          關于j2me game雙緩沖實現探討

                雙緩沖技術的應用很廣泛,設計游戲的時候更是需要它,

               在midp1.0中,api中并沒有game這個包,看到網上很多人在討論設計游戲的時候會出現圖片斷裂,屏幕閃爍等問題。

               我經過這幾天的學習整理下自己的學習心得,用來拋磚,希望對此有研究高手們相互討論。讓我也學習學習。

              

               雙緩沖的原理可以這樣形象的理解:把電腦屏幕看作一塊黑板。首先我們在內存環境中建立一個“虛擬“的黑板,然后在這塊黑板上繪制復雜的圖形,等圖形全部繪 制完畢的時候,再一次性的把內存中繪制好的圖形“拷貝”到另一塊黑板(屏幕)上。采取這種方法可以提高繪圖速度,極大的改善繪圖效果。

              對于手機來說。具體的過程就是通過extends Canvas。然后獲取bufferImage。再然后就getGraphics。最后就是在這個graphics中繪制圖片等,再最后就是把這個繪制好的bufferImage繪制的屏幕上。

               說歸說。具體還是要看代碼的。里面的代碼參照了一些開源的代碼。

          java 代碼


           


          1. /******************************************************************** 

          2.  * 項目名稱             :足球項目j2me客戶端         

             

          3.  *  

          4.  * Copyright 2005-2006 Teesoo. All rights reserved 

          5.  ********************************************************************/  

          6. package org.wuhua.game;  

          7.   

          8. import javax.microedition.lcdui.Canvas;  

          9. import javax.microedition.lcdui.Graphics;  

          10. import javax.microedition.lcdui.Image;  

          11.   



          12.  

          13.   

          14. /** 

          15.  * 類名:GameCanvas.java 

             編寫日期: 2006-11-29 

             程序功能描述:
             

          16.  * 實現雙緩沖的Game畫布。實現原理是創建一個BufferImage。然后繪制,最后顯示出來。就這么簡單。

             Demo: 

             Bug:
             

          17.  * 

             

          18.  *  

          19.  * 程序變更日期 :

             變更作者 :

             變更說明 :

             

          20.  *  

          21.  * @author wuhua 

             
             

          22.  */  

          23. public abstract class GameCanvas extends Canvas {  

          24.   

          25.     /** 

          26.      * 繪制緩沖的圖片。用戶繪制資源的時候都是操作這個圖片來進行的 

          27.      */  

          28.     private Image bufferImage;  

          29.   

          30.     private int height;  

          31.   

          32.     private int width;  

          33.   

          34.     private int clipX, clipY, clipWidth, clipHeight;  

          35.   

          36.     private boolean setClip;  

          37.   

          38.     protected GameCanvas() {  

          39.   

          40.         super();  

          41.   

          42.         width = getWidth();  

          43.         height = getHeight();  

          44.   

          45.         this.bufferImage = Image.createImage(width, height);  

          46.   

          47.     }  

          48.   

          49.     protected void paint(Graphics g) {  

          50.         //如果要求繪制指定區域的話就需要這樣了  

          51.         if (this.setClip) {  

          52.             g.clipRect(this.clipX, this.clipY, this.clipWidth, this.clipHeight);  

          53.             this.setClip = false;  

          54.         }  

          55.         g.drawImage(this.bufferImage, 00, Graphics.TOP | Graphics.LEFT);  

          56.   

          57.     }  

          58.   

          59.     public void flushGraphics(int x, int y, int width, int height) {  

          60.         this.setClip = true;  

          61.         this.clipX = x;  

          62.         this.clipY = y;  

          63.         this.clipWidth = width;  

          64.         this.clipHeight = height;  

          65.   

          66.         repaint();  

          67.         serviceRepaints();  

          68.     }  

          69.   

          70.     public void flushGraphics() {  

          71.         repaint();  

          72.         serviceRepaints();  

          73.     }  

          74.   

          75.     /** 

          76.      * 設計者主要是通過調用這個方法獲取圖片。然后就可以繪制了 

          77.      * @return 

          78.      */  

          79.     protected Graphics getGraphics() {  

          80.         return this.bufferImage.getGraphics();  

          81.     }  

          82.   

          83.     /** 

          84.      * 這個方法主要是處理Nokia平臺,用戶調用setFullScreenMode(boolean enable) 時重新按照新的w & h創建緩沖圖片 

          85.      */  

          86.     protected final void sizeChanged(int w, int h) {  

          87.         if (h > height) {  

          88.             this.bufferImage = Image.createImage(w, h);  

          89.         }  

          90.     }  

          91. }  


          posted @ 2006-12-30 09:24 路是爬出來的 閱讀(301) | 評論 (0)編輯 收藏

          設計可組裝的j2me UI(七) Choice

             艾,不想解釋了。貼代碼算了

          java 代碼


           


          1. /******************************************************************** 

          2.  *  

          3.  * 版權說明,此程序僅供學習參考。不能用于商業 

          4.  *  

          5.  ********************************************************************/  

          6. package org.pook.ui;  

          7.   

          8. import javax.microedition.lcdui.Graphics;  

          9. import javax.microedition.lcdui.Image;  

          10.   

          11. import org.pook.ui.core.Platform;  

          12.    

          13.    

          14.   

          15. /** 

          16.  * <b>類名:ChoiceGroup.java</b> </br>  

          17.  * 編寫日期: 2006-9-19 <br/> 

          18.  * 程序功能描述?? 本Class是一個多選擇列表,因為項目的原??,此列表是??個固定形式的<br/> 

          19.  * 可以在Form上進行添加刪??,只不過表現形式固??<br/> 

          20.  * Demo: <br/> 

          21.  * Bug: <br/> 

          22.  *  

          23.  * 程序變更日期 ??<br/>  

          24.  * 變更作??? ??<br/>  

          25.  * 變更說明 ??<br/> 

          26.  *  

          27.  * @author wuhua </br> <a href="mailto:rrq12345@163.com">rrq12345@163.com</a> 

          28.  */  

          29. public class Choice extends Part {  

          30.       

          31.        

          32.     private boolean  select;  

          33.       

          34.     /** 顯示選擇狀??? */  

          35.     private Image[] imageElements;  

          36.    

          37.    

          38.       

          39.     public Choice(String label,  

          40.             Image[] imageElements, boolean  select) {  

          41.         super(0000);  

          42.         if(label == null)  

          43.             label = "";  

          44.         this.label = label;  

          45.         this.select = select;  

          46.         this.imageElements = imageElements;  

          47.        

          48.     }  

          49.   

          50.        

          51.     public void paint(Graphics g) {  

          52.         paintChoiceImpl(g);  

          53.   

          54.     }  

          55.   

          56.     private void paintChoiceImpl(Graphics g) {  

          57.        

          58.         paintChoice(g);  

          59.           

          60.     }  

          61.   

          62.       

          63.       

          64.     private void paintChoice(Graphics g) {  

          65.         g.drawImage(select?imageElements[0]:imageElements[1],view[X], view[Y],  

          66.                     Graphics.TOP|Graphics.LEFT);  

          67.         paintLabel(g);  

          68.           

          69.     }  

          70.     private void paintLabel(Graphics g) {  

          71.         if(label.equals(""))  

          72.             return;  

          73.         g.setColor(fontColor);  

          74.         g.drawString(label,view[X] + imageElements[0].getWidth() + 4, view[Y]-2, Graphics.TOP | Graphics.LEFT);  

          75.           

          76.         if(hasFocus)  

          77.             paintSelect(g);  

          78.     }  

          79.       

          80.        

          81.   

          82.     /** 繪制選擇?? **/  

          83.     private void paintSelect(Graphics g) {  

          84.         g.drawRect(view[X] - 1, view[Y]  - 1,   

          85.                    imageElements[0].getWidth()  + 1,  

          86.                   imageElements[0].getHeight() + 1);  

          87.                    

          88.           

          89.     }  

          90.   

          91.   

          92.     public void onClick(int keyCode) {  

          93.         if(keyCode == Platform.KEY_ENTER   

          94.                 && hasFocus)  

          95.             select = !select;  

          96.   

          97.     }  

          98.   

          99.   

          100.     public boolean isSelect() {  

          101.         return select;  

          102.     }  

          103.   

          104.   

          105.     public void setSelect(boolean select) {  

          106.         this.select = select;  

          107.     }  

          108.   

          109. }  


          posted @ 2006-12-30 09:24 路是爬出來的 閱讀(382) | 評論 (0)編輯 收藏

          我設計的入門游戲-弱智雷電

                這幾天公司比較有空閑。我未曾設計過游戲。不過出于學習的原因也搞了些玩玩。現在我設計了一個手機雷電弱智版本。

          游戲只是實現了最基本的功能。比如飛機移動。產生飛機。發射子彈。擊中敵方,爆炸等功能。還有很多的地方不好。不過話又說回來,只是入門的。所以大家做個參考,學習學習就ok了。里面的注釋也寫的比較詳細。希望大家多多指教

          posted @ 2006-12-30 09:24 路是爬出來的 閱讀(117) | 評論 (0)編輯 收藏

          游戲入門之一 雷電 精靈模型

                設計游戲我個人認為一個好的角色操作將事半工倍。所以我現在把雷電的所有角色抽象成一個Fairy。里面有實現繪制的方法以及移動,碰撞算法。

                在這里我強調下,我的碰撞算法是簡單的實現。就是一個物體為參照物體。在10像素范圍內x,y軸如果發現另外一個物體侵入則判斷為true,發之為false

          java 代碼


           


          1.    

          2. package org.wuhua.game.model;  

          3.   

          4. import javax.microedition.lcdui.Graphics;  

          5. import javax.microedition.lcdui.Image;  

          6.   

          7. import org.wuhua.game.util.Log;  

          8.    

          9. /** 

          10.  * 類名:Sprite.java 

             
             

          11.  * 編寫日期: 2006-11-29 

             

          12.  * 程序功能描述:建立精靈物體模型 

             

          13.  * Demo: 

             

          14.  * Bug: 

             

          15.  *  

          16.  * 程序變更日期 :

             
             

          17.  * 變更作者 :

             
             

          18.  * 變更說明 :

             

          19.  *  

          20.  * @author wuhua 

             
             

          21.  */  

          22. public class Fairy {  

          23.     static Log log = Log.getLog("Fairy");  

          24.     /** 

          25.      * position of Fairy in x offset  

          26.      */  

          27.     int x; // = 0;  

          28.   

          29.     /** 

          30.      * position of Fairy in y offset  

          31.      */  

          32.     int y; // = 0;  

          33.   

          34.     /** 

          35.      * width of layer  

          36.      */  

          37.     int width; // = 0;  

          38.   

          39.     /** 

          40.      * height of layer 

          41.      */  

          42.     int height; // = 0;  

          43.   

          44.     /**  

          45.      * If the Layer is visible it will be drawn when paint 

          46.      * is called. 

          47.      */  

          48.     boolean visible = true;  

          49.       

          50.     /** 

          51.      * 圖片資源 

          52.      *   

          53.      */  

          54.       

          55.     Image fairy;   

          56.       

          57.     public Fairy(Image fairy,int x, int y){  

          58.         this.fairy = fairy;  

          59.         this.x = x;  

          60.         this.y = y;  

          61.     }  

          62.       

          63.     public void setPosition(int x, int y) {  

          64.         this.x = x;  

          65.         this.y = y;  

          66.     }  

          67.       

          68.     public void move(int dx, int dy) {    

          69.        

          70.         x += dx;  

          71.         y += dy;  

          72.     }  

          73.       

          74.     public void setVisible(boolean visible) {  

          75.         this.visible = visible;  

          76.     }  

          77.   

          78.      

          79.     public final boolean isVisible() {  

          80.         return visible;  

          81.     }  

          82.   

          83.     public final int getHeight() {  

          84.         return height;  

          85.     }  

          86.   

          87.     public final int getWidth() {  

          88.         return width;  

          89.     }  

          90.   

          91.     public final int getX() {  

          92.         return x;  

          93.     }  

          94.   

          95.     public final int getY() {  

          96.         return y;  

          97.     }  

          98.       

          99.     public void paint(Graphics g){  

          100.         if (g == null) {  

          101.             throw new NullPointerException("Graphics 不存在");  

          102.         }  

          103.         if(this.visible){  

          104.             //log.debug("x=" + x + " y=" + y);   

          105.             g.drawImage(fairy, x, y,  Graphics.TOP | Graphics.HCENTER);  

          106.         }  

          107.     }  

          108.   

          109.     /** 

          110.      * 進行簡單的碰撞算法, 希望高手可以給個建議。 

          111.      * @param f 

          112.      * @return 

          113.      */  

          114.     public final boolean collidesWith(Fairy f){  

          115.           

          116.        

          117.         if((f.getX() >= this.getX() - 20 && f.getX() <= this.getX() + 20)  

          118.                 &&  (f.getY() >= this.getY() - 10  && f.getY() <= this.getY()+10 )){  

          119.             //log.debug("this.getY=" + this.getY());  

          120.             //log.debug("f.getY=" + f.getY());  

          121.                

          122.                

          123.             return true;  

          124.         }  

          125.               

          126.         return false;  

          127.     }  

          128.    

          129.   

          130. }  


          posted @ 2006-12-30 09:24 路是爬出來的 閱讀(1316) | 評論 (0)編輯 收藏

          游戲入門之二 雷電 Hero(自己飛機)模型的創建

                    這里我將要講述我游戲的主角,Hero。這里的游戲的主角相對比較簡單。 只需實現,飛機的移動以及創建。

          唯一的兩點是飛機移動的時候,不能把飛機都給移動到屏幕外面去了。只有的話,那我們這些設計游戲的人就太不專業了。呵呵。

                   至于發射子彈,本來我也想把Ball跟發射子彈的創建以及管理都放在此的,后來想想,還是分開點好。這些就是高手們常常說的。要解耦啊,每個類應該盡量簡單啊。不應該負責額外的工作啊。

                 我聽這些都煩惱死了。不過煩惱歸煩。高手的話還是要聽的。^_^

               同理可以實現Foe, Ball等角色類,以及創建FoeManager, BallManager等.這里就不詳細闡述他們了

          java 代碼


           


          1. /******************************************************************** 

          2.  * 項目名稱             :j2me學習          

             

          3.  *  

          4.  * Copyright 2005-2006 Wuhua. All rights reserved 

          5.  ********************************************************************/  

          6. package org.wuhua.battleplan;  

          7.   

          8. import org.wuhua.game.model.Fairy;  

          9. import org.wuhua.game.util.Log;  

          10.   

          11. /** 

          12.  * 類名:Hero.java 

             
             

          13.  * 編寫日期: 2006-11-30 

             

          14.  * 程序功能描述:玩家飛機控制類 

             

          15.  * Demo: 

             

          16.  * Bug: 

             

          17.  *  

          18.  * 程序變更日期 :

             
             

          19.  * 變更作者 :

             
             

          20.  * 變更說明 :

             

          21.  *  

          22.  * @author wuhua 

             
             

          23.  */  

          24. public class Hero extends Fairy {  

          25.       

          26.   

          27.     static Log log = Log.getLog("Hero");  

          28.     static Hero hero;  

          29.     private Hero(int x, int y){  

          30.         super(Resources.FLY, x, y);  

          31.     }  

          32.     /** 

          33.      * 在選中的位置的位置創建一個Hero 

          34.      */  

          35.     public final static Hero createHero(int x, int y){  

          36.         if(hero == null){  

          37.             hero = new Hero(x,y);  

          38.         }  

          39.         return hero;  

          40.     }  

          41.       

          42.       

          43.       

          44.       

          45.     public final void moveLeft(){  

          46.         if(this.getX()>=16)  

          47.             this.move(-80);  

          48.     }  

          49.       

          50.     public final void moveRight(){  

          51.         if(this.getX()<=Platform.WIDTH-16)  

          52.             this.move(80);  

          53.     }  

          54.       

          55.     public final void moveUp(){  

          56.         if(this.getY()>=8)  

          57.             this.move(0, -8);  

          58.     }  

          59.     public final void moveDown(){  

          60.         if(this.getY()<=Platform.HEIGHT-38)  

          61.             this.move(08);  

          62.     }  


          63. }  


          posted @ 2006-12-30 09:24 路是爬出來的 閱讀(182) | 評論 (0)編輯 收藏

          游戲入門之三 雷電 Game

                 這里介紹的是Game的邏輯類,主要控制游戲的動作,以及繪制。

                 詳細里面代碼有注釋

          java 代碼


           


          1. /******************************************************************** 

          2.  * 項目名稱             :j2me學習          

             

          3.  *  

          4.  * Copyright 2005-2006 Wuhua. All rights reserved 

          5.  ********************************************************************/  

          6. package org.wuhua.battleplan;  

          7.   

          8. import java.util.Stack;  

          9.   

          10. import javax.microedition.lcdui.Graphics;  

          11. import javax.microedition.lcdui.Image;  

          12.   

          13. import org.wuhua.game.GameCanvas;  

          14. import org.wuhua.game.model.Fairy;  

          15. import org.wuhua.game.util.Log;  

          16.   

          17.    

          18. /** 

          19.  * 類名:Game.java 

             
             

          20.  * 編寫日期: 2006-11-30 

             

          21.  * 程序功能描述:游戲的主體類。游戲的繪制,狀態的改變都在這邊。 

             

          22.  * Demo: 

             

          23.  * Bug: 

             

          24.  *  

          25.  * 程序變更日期 :

             
             

          26.  * 變更作者 :

             
             

          27.  * 變更說明 :

             

          28.  *  

          29.  * @author wuhua 

             
             

          30.  */  

          31. public class Game extends GameCanvas {  

          32.     static Log log = Log.getLog("Game");  

          33.     private Hero hero;  

          34.       

          35.     private Stack balls;  

          36.     private Stack foes;  

          37.       

          38.     private Stack balst;  

          39.     /** 

          40.      * 爆炸效果索引 

          41.      */  

          42.     private int balstIndex;  

          43.       

          44.     /** 

          45.      * if time = 3 的時候建立一個 

          46.      */  

          47.     private int genaratBallTime;  

          48.       

          49.     private int genaratFoeTime;  

          50.       

          51.       

          52.     Game(){  

          53.         super();  

          54.         this.setFullScreenMode(true);  

          55.     }  

          56.       

          57.     void init(){  

          58.           

          59.         WIDTH = getWidth();  

          60.         HEIGHT = getHeight();  

          61.         log.debug("WIDTH=" + WIDTH);  

          62.         log.debug("hegiht=" + HEIGHT);  

          63.         this.bufferImage = Image.createImage(WIDTH, HEIGHT);  

          64.            

          65.         Platform.WIDTH = this.getWidth();  

          66.         Platform.HEIGHT = this.getHeight();  

          67.           

          68.           

          69.         hero = Hero.createHero(Platform.WIDTH/2, Platform.HEIGHT -30);  

          70.        

          71.         balst = new Stack();  

          72.            

          73.     }  

          74.       

          75.     /** 

          76.      * 產生爆炸效果 

          77.      * @param x 

          78.      * @param y 

          79.      */  

          80.     void genaratorBalst(int x, int y){  

          81.         balst.addElement(new Fairy(Resources.BLAST[0], x, y));  

          82.         balst.addElement(new Fairy(Resources.BLAST[1], x, y));  

          83.         balst.addElement(new Fairy(Resources.BLAST[2], x, y));  

          84.         balst.addElement(new Fairy(Resources.BLAST[3], x, y));  

          85.         balst.addElement(new Fairy(Resources.BLAST[4], x, y));  

          86.     }  

          87.       

          88.     /** 

          89.      * 碰撞。實在沒有好的實現。 我想不出來了. 

          90.      * 邏輯是遍歷所有子彈,然后再遍歷所有敵機,再判斷是否碰撞,如果碰撞,則產生一個爆炸實例. 

          91.      * 最后刪除子彈跟飛機. 

          92.      * 

          93.      */  

          94.     void collides(){  

          95.         if(balls == null   

          96.                 || foes == null)  

          97.             return ;  

          98.         for(int i = 0; i < balls.size(); i ++){  

          99.             Ball b = (Ball) balls.elementAt(i);  

          100.             for(int j =0; j < foes.size(); j ++){  

          101.                 Foe f = (Foe) foes.elementAt(j);  

          102.                 if(b.collidesWith(f)){  

          103.                     this.genaratorBalst(f.getX(), f.getY());  

          104.                     balls.removeElement(b);  

          105.                     foes.removeElement(f);  

          106.                     return;  

          107.                 }  

          108.                

          109.             }  

          110.         }  

          111.     }  

          112.       

          113.     /** 

          114.      * 繪制游戲場景跟Hero 

          115.      * 

          116.      */  

          117.     void drawGame(){  

          118.         if(Platform.HEIGHT < this.getHEIGHT()){  

          119.             Platform.HEIGHT = this.getHEIGHT();  

          120.         }  

          121.           

          122.         Graphics g = this.getGraphics();  

          123.         if(g == null)  

          124.             return;  

          125.         fillFullScreen(g,0x349293);  

          126.         paintHeroAndBall(g);  

          127.           

          128.         paintFoe(g);  

          129.           

          130.         paintBalst(g);  

          131.         this.flushGraphics();  

          132.     }  

          133.   

          134.     /** 

          135.      * 繪制爆炸效果 

          136.      * @param g 

          137.      */  

          138.     private void paintBalst(Graphics g) {  

          139.            

          140.         if(balst == null   

          141.                 || balst.size() == 0)  

          142.             return;  

          143.           

          144.         Fairy bf = (Fairy) balst.elementAt(balstIndex);  

          145.         bf.paint(g);  

          146.         if(balstIndex >= 4){  

          147.             balstIndex = 0;  

          148.             balst.removeAllElements();  

          149.         }  

          150.               

          151.         balstIndex++;  

          152.     }  

          153.   

          154.     /** 

          155.      * 繪制敵機 

          156.      * @param g 

          157.      */  

          158.     private void paintFoe(Graphics g) {  

          159.         if(foes == null)  

          160.             return ;  

          161.         for(int i=0; i < foes.size(); i++){  

          162.             Foe foe = (Foe) foes.elementAt(i);  

          163.             foe.paint(g);  

          164.         }  

          165.           

          166.     }  

          167.       

          168.     /** 

          169.      * 制造敵飛機 

          170.      * 

          171.      */  

          172.     public void genaratorFoe(){  

          173.         if(this.genaratFoeTime == 5){             

          174.             FoeManager.addFoe(FoeManager.genarator());  

          175.             FoeManager.clearFoesIsOut();  

          176.             foes = FoeManager.getFoes();  

          177.             genaratFoeTime = 0;  

          178.         }  

          179.           

          180.         genaratFoeTime++;  

          181.     }  

          182.       

          183.     /** 

          184.      * 敵機飛行 

          185.      * 

          186.      */  

          187.     public void foeFly(){  

          188.         if(foes == null)  

          189.             return ;  

          190.         for(int i = 0; i < foes.size(); i++){  

          191.             Foe foe = (Foe) foes.elementAt(i);  

          192.             foe.fly();  

          193.         }  

          194.     }  

          195.   

          196.     private void paintHeroAndBall(Graphics g) {  

          197.         hero.paint(g);  

          198.         paintBalls(g);  

          199.     }  

          200.   

          201.     /** 

          202.      * 繪制子彈 

          203.      * @param g 

          204.      */  

          205.     private void paintBalls(Graphics g) {  

          206.         if(balls == null)  

          207.             return ;  

          208.         for(int i = 0; i < balls.size(); i++){  

          209.             Ball ball = (Ball) balls.elementAt(i);  

          210.             ball.paint(g);  

          211.         }  

          212.           

          213.     }  

          214.       

          215.     /** 

          216.      * 子彈的飛行 

          217.      * 

          218.      */  

          219.     public void ballFly(){  

          220.         if(balls == null)  

          221.             return ;  

          222.         for(int i = 0; i < balls.size(); i++){  

          223.             Ball ball = (Ball) balls.elementAt(i);  

          224.             ball.fly();  

          225.         }  

          226.     }  

          227.       

          228.     /** 

          229.      * 飛機的動作 

          230.      * 

          231.      */  

          232.     public void heroAction(){  

          233.         checkHeroIsExists();  

          234.         int keyCode = this.getKeyStates();  

          235.            

          236.         switch(keyCode){  

          237.         case Platform.KEY_LEFT: hero.moveLeft(); break;  

          238.         case Platform.KEY_RIGHT: hero.moveRight(); break;  

          239.         case Platform.KEY_UP: hero.moveUp(); break;  

          240.         case Platform.KEY_DOWN: hero.moveDown(); break;  

          241.         case Platform.KEY_FIRE: genaratorBall(); break;  

          242.         }  

          243.     }  

          244.   

          245.     /** 

          246.      * 創建子彈 

          247.      * 

          248.      */  

          249.     private void genaratorBall() {  

          250.        

          251.         if(this.genaratBallTime == 3){  

          252.             checkHeroIsExists();  

          253.               

          254.             BallManager.addBall(BallManager.genarator(hero.getX(), hero.getY()));  

          255.             BallManager.clearBallsIsOut();  

          256.             balls = BallManager.getBalls();  

          257.             genaratBallTime = 0;  

          258.         }  

          259.           

          260.         genaratBallTime++;  

          261.           

          262.           

          263.     }  

          264.   

          265.     private void checkHeroIsExists() {  

          266.         if(hero == null){  

          267.             throw new java.lang.NullPointerException("Hero is Null");  

          268.         }  

          269.     }  

          270.   

          271.     /** 

          272.      * 游戲的run。控制游戲個各個方面 

          273.      * 

          274.      */  

          275.     public void run(){  

          276.         this.collides();  

          277.         this.heroAction();  

          278.         this.ballFly();  

          279.         this.genaratorFoe();  

          280.         this.foeFly();  

          281.           

          282.         this.drawGame();  

          283.         this.setKeyStates(1000);  

          284.     }  

          285. }  







          代碼就是上面的,如果有什么好的建議,請評論。下面的一課,我將介紹GameThread。

          posted @ 2006-12-30 09:24 路是爬出來的 閱讀(153) | 評論 (0)編輯 收藏

          壞心情

                  好久沒寫自己的內心感受了。這段時間狂發j2me的文章。純粹是個人愛好。而非工作需要。說來自從結束上個IKan項目后。現在的工作基本上是維護一些老應用程序。有時候甚是覺得無聊。同學們都說我變了。變的沉默,而我自己到不察覺,或許有些事情真的是需要外人來提醒啊。前陣子也曾經跟公司提過,我想做技術之余兼任市場方面的工作。提是提了。后來就沒下文了。上次主管提出說要修煉修煉我們的內功。也提出了些具體方案,比如從測試入手。艾,最近心情不怎么順。星期6,7都睡覺。要么就跟朋友出去逛逛。不太喜歡編碼了。或許我們在適當的時候,適當的地點,做自己想做的事情。

                   都說程序員很木訥。我卻不這樣認為。一開始全心投入去編碼的時候也許是這樣,等他發現自己需要換種活法的時候,卻往往很迷失。

                  昨天遇到一個很久很久未聯系的朋友的QQ,跟他寒暄了幾句,發現居然不是他本人,郁悶。那個人說,對方去上海讀書了。想想認識了這么久又怎么樣,跑了。連個屁也不放。真實物是人非。或許對方想拋棄以前所有的東西,自己過新的生活。那樣是多么的灑脫。自己何嘗不想這樣呢?  。。。。。。。。。

                   當你寫程序寫久了。會不會有種,以抽象的機器觀念來觀察現實的肉體觀了

                  哈哈哈哈哈哈哈。

             

          posted @ 2006-12-30 09:24 路是爬出來的 閱讀(139) | 評論 (0)編輯 收藏

          j2me圈子的朋友有沒興趣大家一起做個冒險島的游戲

                   本想不應該寫稱blog。應該發表到j2me圈子的論壇里面。但是論壇只是很簡單的文本,并沒有上傳圖片。所以就貼到這里了

          前幾天閑逛了一些素材網站。發現有個用戶設計了一些資源,所以下載下來,自己對人物的動作做了些修改。上傳出來,大家看看有幾乎就一起搞個簡單的冒險島游戲。

                 怎么樣?





          我專門開這個帖子,然后希望有時間,并且愛好j2me game開放的朋友跟貼。并提出自己的想法。

          呵呵

          posted @ 2006-12-30 09:24 路是爬出來的 閱讀(139) | 評論 (0)編輯 收藏

          我的第一個真正意義上的測試

                 前段日子很無聊,也是很無奈的。經過了這段日子的,我想了很多事情,雖然全部想通,但卻努力的去想了。我想這樣就Ok了

          對嗎?

                 好了。前陣子,老板請喝早茶,順便跟我們這些技術人員討論了怎么樣可以提高我們內功的問題。我老板也是技術出身的。有相當多的經驗,當然我最初的想法并不是去研究測試,我直接提出:“我覺得我們應該提高我們對設計模式的理解”。他卻不怎么同意,說測試才是我們現在的當務之急,然后很肯定的說。TDD,重構都是建立再單元測試基礎上的。并推翻了我的建議:”有空讓對設計模式有深厚理解的員工給我們上課“。

                 開始由于我個人原因,或許我是個很情緒話的員工,也許在另外一家公司我可能早就被T。也許老板對我太好了。^_^

          我當時不怎么響應,總是做自己喜歡做的事情,后來自己慢慢覺得自己的工作態度不對,所以昨天試著努力糾正自己的錯誤,真是在這樣的環境下,自己寫出了自己第一個我認為真正意義上的測試,把它記錄下來。

                 測試的要求:

                 測試對一個Account的Dao操作以及Service。

          java 代碼


           


          1. //先來測試最基本的dao吧  

          2. package org.wuhua.dao;  

          3.   

          4. import java.util.Collection;  

          5.   

          6. public interface IBaseDao {  

          7.     Object save(Object o);  

          8.     void delete(Object o);  

          9.     Object update(Object o);  

          10.     Collection list();  

          11. }  



          根據我的理解,測試的對方要跟mock的對象分開,(開始我一直認為你要mock的對象就是你要測試的東西,搞著搞著,我就很迷茫了。)。現在要做的就是看你IBaseDao的實現是什么了。如果實現是采用SpringHibernateTemplate的話你就去mock一個這樣對象,不過此對象并不是接口,所以你要用到easymock的擴展包,以對它的支持。如果你實現的采用純Hibernate的話。那你就去mock一個SessionFactory吧。很簡單吧,難道這就是所謂的解耦嗎?我想是的,這正是解耦。 哈哈



          看下我的實現吧,采用Spring實現。

          java 代碼


           


          1. package org.wuhua.dao.impl;  

          2.   

          3. import java.util.Collection;  

          4.   

          5. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  

          6. import org.wuhua.dao.IBaseDao;  

          7.   

          8. public class BaseDao extends  HibernateDaoSupport  

          9. implements IBaseDao {  

          10.   

          11.     public void delete(Object o) {  

          12.          this.getHibernateTemplate().delete(o);        

          13.     }  

          14.   

          15.     public Collection list() {  

          16.        

          17.         return null;  

          18.     }  

          19.   

          20.     public Object save(Object o) {  

          21.         return this.getHibernateTemplate().save(o);  

          22.             

          23.     }  

          24.   

          25.     public Object update(Object o) {  

          26.         this.getHibernateTemplate().update(o);  

          27.         return o;  

          28.     }  

          29.   

          30. }  



          測試代碼

          java 代碼


           


          1. package org.wuhua.dao;  

          2.   

          3. import java.io.Serializable;  

          4.   

          5. import junit.framework.TestCase;  

          6.   

          7. import org.easymock.MockControl;  

          8. import org.easymock.classextension.MockClassControl;  

          9. import org.springframework.orm.hibernate3.HibernateTemplate;  

          10. import org.wuhua.dao.impl.BaseDao;  

          11.   

          12. public class BaseDaoTest extends TestCase {  

          13.   

          14.     MockControl control;  

          15.   

          16.     private HibernateTemplate ht;  

          17.   

          18.     private BaseDao baseDao;  

          19.   

          20.     protected void setUp() throws Exception {  

          21.         control = MockClassControl.createControl(HibernateTemplate.class);  

          22.         ht = (HibernateTemplate) control.getMock();  

          23.         baseDao = new BaseDao();  

          24.         baseDao.setHibernateTemplate(ht);  

          25.     }  

          26.   

          27.     public void testSave() {  

          28.         Object o = new Object();  

          29.         ht.save(o); 


          30.         //這里我是有疑問的。

          31.         //1,為什么HibernateTemplate返回的是Serializable。

          32.         //2,設置的返回植為什么一定要跟調用ht.save(o)一致呢?


          33.         control.setReturnValue(new Wuhua());  

          34.         control.replay();  

          35.         baseDao.save(o);  

          36.         control.verify();  

          37.     }  

          38.       

          39.     public void testUpdate() {  

          40.         Object a = new Object();  

          41.         ht.update(a);  

          42.        

          43.         control.replay();  

          44.         try {  

          45.             baseDao.update(a);  

          46.             fail("Not catch exception!");  

          47.         } catch(Exception e) {  

          48.                

          49.         }  

          50.         control.verify();  

          51.     }  

          52.       

          53.     class Wuhua implements Serializable {}  

          54.   

          55. }  



          上面就是我第一次很認真的測試,有很多不明白的地方

          posted @ 2006-12-30 09:24 路是爬出來的 閱讀(196) | 評論 (0)編輯 收藏

          僅列出標題
          共5頁: 上一頁 1 2 3 4 5 下一頁 
          主站蜘蛛池模板: 旬阳县| 瓦房店市| 绥棱县| 银川市| 镇江市| 土默特右旗| 杭锦旗| 乐山市| 清流县| 明光市| 读书| 鸡东县| 济源市| 德州市| 台北市| 台东县| 阿勒泰市| 沭阳县| 澜沧| 县级市| 舞阳县| 利辛县| 迁安市| 九龙县| 进贤县| 金平| 樟树市| 新竹县| 襄樊市| 滁州市| 湄潭县| 新昌县| 安乡县| 乌什县| 绥滨县| 成武县| 沙雅县| 北川| 三河市| 巴林左旗| 德兴市|