路是爬出来的

游戏入门之四 雷电 GameThread

        个人认为游戏最难控制的就是这些线程了。如果游戏复杂的话。为了避免资源冲突,死锁等。这方面对程序要求是很高的。还好。我那个游戏很简单,所以就不用考虑到这些。

      只要一个最重要的东西就行了。

java 代码


 


  1. //无非是要程序不停的运行,直到游戏结束为之

  2. while (true) {  

  3.             game.run();  

  4.             try {  

  5.                 Thread.sleep(80); // 线程延迟  

  6.             } catch (InterruptedException ie) {  

  7.             }  

  8. }  



   

java 代码


 


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

  2.  * 项目名称             :<b>j2me学习</b>          <br/> 

  3.  *  

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

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

  6. package org.wuhua.battleplan;  

  7.   

  8. import javax.microedition.lcdui.Display;  

  9.   

  10. /** 

  11.  * <b>类名:GameThread.java</b> </br> 编写日期: 2006-11-30 <br/> 

  12.  * 程序功能描述:游戏线程控制类,主要负责按照一定的时间刷新屏幕,来控制画面 . 

  13.  * 因为程序是每80毫秒刷新一次,所以在控制子弹跟飞机的时候,建议设计着应该在那边控制 

  14.  * <br/> Demo: <br/> Bug: <br/> 

  15.  *  

  16.  * 程序变更日期 :<br/> 变更作者 :<br/> 变更说明 :<br/> 

  17.  *  

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

  19.  */  

  20. public class GameThread implements Runnable {  

  21.   

  22.     private Game game;  

  23.     GameThread(){  

  24.         game = new Game();  

  25.     }  

  26.     public void run() {  

  27.         gameRun();  

  28.   

  29.     }  

  30.       

  31.     public void init(){  

  32.         game.init();  

  33.     }  

  34.     public void open(Display d){  

  35.         d.setCurrent(game);  

  36.     }  

  37.   

  38.     private void gameRun() {  

  39.         while (true) {  

  40.             game.run();  

  41.             try {  

  42.                 Thread.sleep(80); // 线程延迟  

  43.             } catch (InterruptedException ie) {  

  44.             }  

  45.         }  

  46.   

  47.     }  

  48.   

  49. }  


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


只有注册用户登录后才能发表评论。


网站导航: