路是爬出来的

#

游戏中动画的基础

        这篇文章是纯粹的个人看法。

        游戏的基础是动画,想来大家都知道。这几天公司的项目都忙完了。很是无聊,所以就上网找了些资源,并写两个动画的例子。在此贴出来,让大家把砖头砸我吧。^_^    

        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 路是爬出来的 阅读(152) | 评论 (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 路是爬出来的 阅读(274) | 评论 (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 路是爬出来的 阅读(371) | 评论 (0)编辑 收藏

我设计的入门游戏-弱智雷电

      这几天公司比较有空闲。我未曾设计过游戏。不过出于学习的原因也搞了些玩玩。现在我设计了一个手机雷电弱智版本。

游戏只是实现了最基本的功能。比如飞机移动。产生飞机。发射子弹。击中敌方,爆炸等功能。还有很多的地方不好。不过话又说回来,只是入门的。所以大家做个参考,学习学习就ok了。里面的注释也写的比较详细。希望大家多多指教

posted @ 2006-12-30 09:24 路是爬出来的 阅读(108) | 评论 (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 路是爬出来的 阅读(1287) | 评论 (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 路是爬出来的 阅读(175) | 评论 (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 路是爬出来的 阅读(149) | 评论 (0)编辑 收藏

坏心情

        好久没写自己的内心感受了。这段时间狂发j2me的文章。纯粹是个人爱好。而非工作需要。说来自从结束上个IKan项目后。现在的工作基本上是维护一些老应用程序。有时候甚是觉得无聊。同学们都说我变了。变的沉默,而我自己到不察觉,或许有些事情真的是需要外人来提醒啊。前阵子也曾经跟公司提过,我想做技术之余兼任市场方面的工作。提是提了。后来就没下文了。上次主管提出说要修炼修炼我们的内功。也提出了些具体方案,比如从测试入手。艾,最近心情不怎么顺。星期6,7都睡觉。要么就跟朋友出去逛逛。不太喜欢编码了。或许我们在适当的时候,适当的地点,做自己想做的事情。

         都说程序员很木讷。我却不这样认为。一开始全心投入去编码的时候也许是这样,等他发现自己需要换种活法的时候,却往往很迷失。

        昨天遇到一个很久很久未联系的朋友的QQ,跟他寒暄了几句,发现居然不是他本人,郁闷。那个人说,对方去上海读书了。想想认识了这么久又怎么样,跑了。连个屁也不放。真实物是人非。或许对方想抛弃以前所有的东西,自己过新的生活。那样是多么的洒脱。自己何尝不想这样呢?  。。。。。。。。。

         当你写程序写久了。会不会有种,以抽象的机器观念来观察现实的肉体观了

        哈哈哈哈哈哈哈。

   

posted @ 2006-12-30 09:24 路是爬出来的 阅读(128) | 评论 (0)编辑 收藏

j2me圈子的朋友有没兴趣大家一起做个冒险岛的游戏

         本想不应该写称blog。应该发表到j2me圈子的论坛里面。但是论坛只是很简单的文本,并没有上传图片。所以就贴到这里了

前几天闲逛了一些素材网站。发现有个用户设计了一些资源,所以下载下来,自己对人物的动作做了些修改。上传出来,大家看看有几乎就一起搞个简单的冒险岛游戏。

       怎么样?





我专门开这个帖子,然后希望有时间,并且爱好j2me game开放的朋友跟贴。并提出自己的想法。

呵呵

posted @ 2006-12-30 09:24 路是爬出来的 阅读(130) | 评论 (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 路是爬出来的 阅读(184) | 评论 (0)编辑 收藏

仅列出标题
共5页: 上一页 1 2 3 4 5 下一页