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

📄 missilehitplane.java

📁 一个用j2me写的导弹打飞机演示程序,画面很好,算法很经典
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.lang.Math.*;

public class MissileHitPlane extends MIDlet 
{
  private Display display;//Display是MIDlet里的一个类
  public void startApp()//入口的函数
	  {
    display = Display.getDisplay(this);
    MissileHitPlaneCanvas planeCanvas = new MissileHitPlaneCanvas();
    planeCanvas.start();//调用start()函数
    display.setCurrent(planeCanvas);
      }
  
  public Display getDisplay() 
	  {
    return display;  
      }
  
  public void pauseApp()//暂停应用程序
	  {
      }

  public void destroyApp(boolean unconditional)//销毁程序,中断程序
	  {
  	System.gc();
	destroyApp(false);
	notifyDestroyed();
      }  
}
class MissileHitPlaneCanvas extends GameCanvas implements Runnable {
  private boolean isPlay;
  private boolean isPlaying;   
  private int width;        
  private int height;       
  
  int lineLength;
  int planeLength;
  int NCounts ;
  static final int RANGE = 120;//定义画布的范围

  public MissileHitPlaneCanvas()//构造函数
  {
    super(true);
    width = getWidth();
    height = getHeight();
    if (width > height) lineLength = height;
    else lineLength = width;
    planeLength = lineLength / 10;
     NCounts = 0;
  }

  // Automatically start thread for game loop
  public void start()
  {
    isPlay = true;
    isPlaying = true;
    Thread t = new Thread(this);//开始一个线程
    t.start();
  }

  public void stop()
	{
	  isPlaying = false;
	}

  // Main Game Loop
  public void run() //开始运行程序
	{
    Graphics g = getGraphics();
    while (isPlaying == true) 
	{
      ShootPlane(g);
      try 
	  { 
      	Thread.sleep(10000); 
      }
      catch (InterruptedException ie) 
	 {
      isPlay = false;
	 }
    }
  }
void ShootPlane(Graphics g)
{
	g.setColor(0,255,0);
    g.fillRect(0, 0, getWidth(), getHeight());
	g.setColor(23,23,23);
	g.drawString("导弹打飞机程序",0,0,0);
	g.setColor(255,0,0);
	g.fillArc(95,95,15,15,0,360);
	flushGraphics();
	double bx=100;
	double by=100;
	double b1x=100;
	double b1y=100;
	
	double px=200;
	double py=100;
	double p1x=200;
	double p1y=100;
	
	int r=100;
	int Vb=8;
	int Vp=5;
	int min_s=1;
	int i=0;
	double dt=0.05;
	double A=Vp*dt/r;
	double distance=r;
	double ox=100;
	double oy=100;
    while(distance>=min_s)
	{
		++i;
		px=(ox+Math.cos(-i*A)*r);
		py=(oy+Math.sin(-i*A)*r);
		g.setColor(255,0,0);
		g.drawLine((int)p1x,(int)p1y,(int)px,(int)py);
		flushGraphics();
		p1x=px;
		p1y=py;
		
		
		bx=(b1x+Vb*dt*(p1x-b1x)/distance);
		by=(b1y+Vb*dt*(p1y-b1y)/distance);
		g.setColor(0,0,255);
		g.drawLine((int)b1x,(int)b1y,(int)bx,(int)by);
		flushGraphics();
		b1x=bx;
		b1y=by;
			
		distance=Math.sqrt((bx-px)*(bx-px)+(by-py)*(by-py));
	}
    isPlaying=false;
	
}

}

⌨️ 快捷键说明

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