j2me中的A*算法


import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.lang.Thread;

public class MainMid extends MIDlet {
	MainPit myScreen;
	boolean first = true;

	public MainMid() {
		myScreen = new MainPit(this);
	}

	protected void destroyApp(boolean unconditional) {
	}

	protected void pauseApp() {
	}

	protected void startApp()// J2ME程序的执行线程,自动执行响应按键。
	{
		Display.getDisplay(this).setCurrent(myScreen);// myScreen获得当前屏幕的显示权
		if (first)// 限定执行线程只执行一次
		{
			try {
				Thread myThread = new Thread(myScreen);// 开启一个线程用来实现游戏的逻辑与绘制
				myThread.start();// 打开线程
			} catch (Error e) {
				destroyApp(false);
				notifyDestroyed();
			}
			first = false;
		}
	}

	// 游戏结束
	public void exit()// 关闭执行线程,整体程序退出
	{
		destroyApp(true);
		notifyDestroyed();
	}

}



import java.lang.*;
import javax.microedition.lcdui.*;
import java.util.Random;
import javax.microedition.rms.*;
import java.io.*;

class MainPit extends Canvas implements Runnable {
	MainMid myMid;
	// 按键表
	private static final byte KEY_NONE = 0;
	private static final byte KEY_UP = -1;
	private static final byte KEY_DOWN = -2;
	private static final byte KEY_LEFT = -3;
	private static final byte KEY_RIGHT = -4;
	private static final byte KEY_FIRE = -5;
	private static final byte KEY_GAM_LEFT = -6;
	private static final byte KEY_GAM_RIGHT = -7;
	private static final byte KEY_NUM5 = 53;
	private int hangfire = 300;// 延时大小

	Graphics gb;
	private Image bufImg;// 缓存
	// 屏幕大小
	private int nWidth;
	private int nHeight;

	public MainPit(MainMid mid) {
		myMid = mid;
		nWidth = getWidth();// 屏幕大小
		nHeight = nWidth;// getHeight();
		cw = nWidth / mWidth;//列
		ch = nHeight / mHeight;//行
		try {
			bufImg = Image.createImage(nWidth, nHeight);// 申请缓存空间
			gb = bufImg.getGraphics();
		} catch (Exception e) {
		}
	}

	public void paint(Graphics g) {
		g.setColor(0);
		g.fillRect(0, 0, nWidth, getHeight());
		g.drawImage(bufImg, 0, 0, 0);
		g.setColor(0xff00);
		g.drawString("" + ttime, 0, getHeight(), 36);
	}

	private void showBegin() {
		gb.setColor(0x0000ff00);
		gb.fillArc(begin_x * cw, begin_y * ch, cw, ch, 0, 360);
	}

	int state = 0;

	public void run() {
		while (true) {
			switch (state) {
			case 0:
				showMap();
				showCursor();
				break;
			case 1:
				showMap();
				showBegin();
				showCursor();
				break;
			case 2:
				showMap();
				showBegin();
				showfather(end_x, end_y);
				showCursor();
				break;
			}
			repaint();
			serviceRepaints();
			try {
				Thread.sleep(hangfire);
				System.gc();
				Thread.yield();
			} catch (Exception e) {
			}
		}
	}

	private int cx=10, cy=0;

	private void showCursor() {
		gb.setColor(0x000000ff);
		gb.drawRect(cx * cw + 2, cy * ch + 2, cw - 4, ch - 4);
	}

	private int cw, ch;

	private void showMap() {
		clearScreen(0x00ffffff);
		for (int i = 0; i < mHeight; i++) {
			for (int j =
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值