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

📄 mazegamecanvas.java

📁 是基于J2ME的一个模拟手机的界面可以实现手机收发短信基于客户端和服务器端的功能
💻 JAVA
字号:
package phone.mediaSound.game.migong;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.TiledLayer;

public class MazeGameCanvas extends GameCanvas implements Runnable {

	private boolean isRunning = true;

	private Display display;

	private MazeMain mm;

	// 定义一个二维数组,用来放地图
	int[][] cells = { { 0, 1, 1, 1, 1, 1, 1, 1 }, { 0, 0, 0, 1, 0, 1, 1, 1 },
			{ 0, 0, 1, 1, 1, 0, 1, 0 }, { 0, 0, 1, 1, 1, 1, 1, 1 },
			{ 0, 1, 0, 0, 0, 1, 1, 0 }, { 0, 0, 0, 0, 1, 1, 1, 1 },
			{ 0, 1, 0, 0, 0, 0, 0, 1 }, { 1, 0, 0, 0, 0, 1, 0, 2 },
			{ 0, 1, 0, 0, 0, 0, 0, 1 }, { 1, 0, 0, 0, 0, 1, 0, 2 } };

	private int viewWindowW;// 定义窗口的宽度

	private int viewWindowH;// 定义窗口的高度

	private int direction = RIGHT;// 定义乌龟爬行方向

	private int wgX;// 定义乌龟x轴坐标方向

	private int wgY;// 定义乌龟x轴坐标方向

	private Sprite wg;

	private TiledLayer walls;

	private TiledLayer bg, zhdian;

	private LayerManager layers;

	private Image img1;// 游戏地图图片

	private Image img2;// 游戏精灵图片

	public MazeGameCanvas(MazeMain mm, Display display) {
		super(false);
		// this.setFullScreenMode(true);
		this.mm = mm;
		this.display = display;
		viewWindowW = this.getWidth();
		viewWindowH = this.getHeight();
//		initialize();
//	}
//
//	public void initialize() {

		wgX = 1;
		wgY = 1;
		try {
			img1 = Image
					.createImage("/phone/mediaSound/game/migong/pictures/map.png");
			img2= Image
					.createImage("/phone/mediaSound/game/migong/pictures/monkey.png");

		} catch (Exception e) {
			System.out.println("装载图像错误" + e.getMessage());
			e.printStackTrace();
		}
		bg = new TiledLayer( cells[0].length,cells.length, img1, img1
				.getWidth() / 3, img1.getHeight());
		walls = new TiledLayer(cells[0].length,cells.length, img1, img1
				.getWidth() / 3, img1.getHeight());
		zhdian = new TiledLayer(cells[0].length,cells.length, img1, img1
				.getWidth() / 3, img1.getHeight());

		for (int y = 0; y < cells.length; y++) {
			for (int x = 0; x < cells[0].length; x++) {
				bg.setCell(x, y, 0);
				walls.setCell(x, y, 0);
				zhdian.setCell(x, y, 0);
				if (cells[y][x] == 1) {
					walls.setCell(x, y, 2);
				} else if (cells[y][x] == 2) {
					zhdian.setCell(x, y, 3);
				} else {
					bg.setCell(x, y, 1);
				}
			}
		}
		wg = new Sprite(img2);
		wg.setTransform(Sprite.TRANS_MIRROR);
		wg.setPosition(wgX, wgY);
		layers = new LayerManager();
		layers.append(wg);
		layers.append(walls);
		layers.append(bg);
		layers.append(zhdian);
		layers.setViewWindow(0, 0, viewWindowW, viewWindowH);
		Thread th = new Thread(this);
		th.start();

	}

	public void run() {
		Graphics g = this.getGraphics();
		while (isRunning) {
			int key = this.getKeyStates();
			if ((key & GameCanvas.LEFT_PRESSED) != 0) {
				move(LEFT);
			} else if ((key & GameCanvas.RIGHT_PRESSED) != 0) {
				move(RIGHT);
			} else if ((key & GameCanvas.UP_PRESSED) != 0) {
				move(UP);
			} else if ((key & GameCanvas.DOWN_PRESSED) != 0) {
				move(DOWN);
			}
			g.setColor(255,255,255);
			g.fillRect(0, 0, viewWindowW, viewWindowH);
			layers.paint(g, 0, 0);
			this.flushGraphics();
			try {
				Thread.currentThread().sleep(200);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	public void move(int dir) {
		direction = dir;
		switch (direction) {
		case LEFT:
			wg.setTransform(Sprite.TRANS_NONE);
			break;
		case RIGHT:
			wg.setTransform(Sprite.TRANS_MIRROR);
			break;
		case UP:
			wg.setTransform(Sprite.TRANS_MIRROR);
			break;
		case DOWN:
			wg.setTransform(Sprite.TRANS_MIRROR);
			break;
		}
		// wg.setPosition(wgX * 30, wgY * 30);
		int x = wgX;
		int y = wgY;
		switch (direction) {
		case LEFT:
			x--;
			break;
		case RIGHT:
			x++;
			break;
		case UP:
			y--;
			break;
		case DOWN:
			y++;
			break;
		}
		move(x, y);
	}

	public void move(int x, int y) {
		wg.setPosition(x * 30, y * 30);
		if (wg.collidesWith(walls, false)) {
			wg.setPosition(wgX * 30, wgY * 30);
			return;
		}
		wgX = x;
		wgY = y;
		if (wg.collidesWith(zhdian, false)) {
			Alert alert = new Alert("恭喜,走出了迷宫!");
			alert.setString("游戏结束!");
			alert.setTimeout(Alert.FOREVER);
			display.setCurrent(alert, this);
		}

	}

}

⌨️ 快捷键说明

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