⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamethread.java

📁 一个Java小程序
💻 JAVA
字号:
/**
 * @(#)GameThread.java	
 * Copyright (c) 2004-2005 wuhua of workroom Inc. All Rights Reserved.
 * @version 	1.0, 10/05/2004
 * @author 	饶荣庆
 * @author 	余煜辉
 */
package com.wuhua.mali;

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;

/**
 *此类是用来描述游戏线程,用来启动游戏
  */
public class GameThread extends Thread 
{
  //---------------------------------------------------------
  //   fields

  /**
   * The Thread delay time
   */
  public  int delay = 80;
  /**
   * Whether or not the main thread would like this thread 
   * to pause.
   */
  private boolean myShouldPause;

  /**
   * Whether or not the main thread would like this thread 
   * to stop.
   */
  public  boolean myShouldStop;

  /**
   * Whether or not this thread has been started.
   */
  private boolean myAlreadyStarted;

 

  /**
   * A handle back to the graphical components.
   */
  public MaliCanvas maliCanvas;

  //----------------------------------------------------------
  //   initialization

  /**
   * standard constructor.
   */
  GameThread(MaliCanvas canvas) 
  {
	  //this.display = display;
	  maliCanvas = canvas;
	  /*new一个时间对象开始记时*/
  }

  //----------------------------------------------------------
  //   actions

  /**
   * start or pause or unpause the game.
   */
  public void go()
  {
	  if(!myAlreadyStarted) 	
	  {
		  /*开始*/
		  myAlreadyStarted = true;
		  maliCanvas.clock();
		  this.start();
	  } 
	  else 
	  {
		  /*暂停*/
		  myShouldPause = !myShouldPause;
	  }
  }

  /**
   * pause the game.
   */
  public void pause()
  {
	  myShouldPause = true;
  }


  /**
   * stops the game.
   */
  public  void requestStop() 
  {
	  myShouldStop = true;
  }

  public boolean isStop()
  {
	  return  myShouldStop;
  }
  /**
   * start the game..
   */
  public void run() 
  {
	  myShouldStop = false;
	  myShouldPause = false;
	  while(true) 
	  {
		  Graphics g = maliCanvas.getGraph();
		  if(myShouldStop) 
		  {
			  break;  //退出
		  }
		  if(!myShouldPause) 
		  {
			  maliCanvas.input();
			  maliCanvas.drawScreen(g);
			  try 
			  {
				  Thread.sleep(delay);  //线程延迟
			  }
              catch(InterruptedException ie) 
			  {}
          }
      }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -