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

📄 gamescreen.java

📁 一个小程序,提供参考
💻 JAVA
字号:
import java.io.IOException;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class GameScreen extends Canvas implements Runnable {

	/** 图片资源 */
	Image imgMap, imgRope;

	/** 人物的坐标 */
	int ropeX, ropeY;

	/** 人物当前状态 */
	int ropeCurState;

	/** 人物当前帧 */
	int ropeCurFrame;

	/** 人物帧的宽度和高度 */
	int ropeFrameWidth, ropeFrameHeight;

	/** 地图的坐标 */
	int mapX, mapY;

	/** 人物上下左右移动的四种状态********************** */
	final static int ROPE_UP_MOVE = 0;

	final static int ROPE_DOWN_MOVE = 1;

	final static int ROPE_LEFT_MOVE = 2;

	final static int ROPE_RIGHT_MOVE = 3;

	// ************************************************/
	/** 人物图片列数 */
	final static int ROPE_IMG_COL = 3;

	/** 人物图片行数 */
	final static int ROPE_IMG_ROW = 4;

	/** 地图图片列数 */
	final static int MAP_IMG_COL = 192 / 32;

	/** 地图图片行数 */
	final static int MAP_IMG_ROW = 96 / 32;

	/** 地图图块宽度 */
	final static int MAP_TILE_WIDTH = 32;

	/** 地图图块高度 */
	final static int MAP_TILE_HEIGHT = 32;

	/** 地图数组 */
	short gameMap[][] = { { 4, 5, 6, 4, 5, 6, 4, 5, 6, 4 },
			{ 12, 9, 8, 9, 12, 11, 9, 12, 11, 11 },
			{ 14, 15, 14, 15, 14, 15, 14, 15, 14, 15 },
			{ 13, 13, 13, 13, 13, 13, 13, 13, 13, 13 },
			{ 13, 13, 13, 13, 13, 13, 13, 13, 13, 13 },
			{ 13, 13, 13, 13, 13, 13, 13, 13, 13, 13 },
			{ 13, 13, 13, 13, 13, 13, 13, 13, 13, 13 },
			{ 13, 13, 13, 13, 13, 13, 13, 13, 13, 13 },
			{ 13, 13, 13, 13, 13, 13, 13, 13, 13, 13 },
			{ 13, 13, 13, 13, 13, 13, 13, 13, 13, 13 } };

	public GameScreen() {
		try {
			imgRope = Image.createImage("/ss.png");
			imgMap = Image.createImage("/map_title1.png");
		} catch (IOException e) {
		}
		ropeCurState = -1;
		ropeFrameWidth = imgRope.getWidth() / ROPE_IMG_COL;
		ropeFrameHeight = imgRope.getHeight() / ROPE_IMG_ROW;
		new Thread(this).start();
	}

	protected void paint(Graphics g) {
		drawMap(g);
		drawRope(g);
		ropeMove();
	}

	public void run() {
		while (true) {
			repaint();
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
			}
		}

	}

	/** 渲染地图 */
	public void drawMap(Graphics g) {
		for (int row = 0; row < gameMap.length; row++) {
			for (int col = 0; col < gameMap[row].length; col++) {
				g.setClip(mapX + col * MAP_TILE_WIDTH, mapY + row
						* MAP_TILE_HEIGHT, MAP_TILE_WIDTH, MAP_TILE_HEIGHT);
				if (gameMap[row][col] != 0) {
					int tx, ty;
					tx = (gameMap[row][col] - 1) % MAP_IMG_COL * MAP_TILE_WIDTH;
					ty = (gameMap[row][col] - 1) / MAP_IMG_COL
							* MAP_TILE_HEIGHT;
					g.drawImage(imgMap, -tx + mapX + col * MAP_TILE_WIDTH, -ty
							+ mapY + row * MAP_TILE_HEIGHT, 20);
				}
			}
		}
	}

	/** 渲染人物 */
	public void drawRope(Graphics g) {
		g.setClip(ropeX, ropeY, ropeFrameWidth, ropeFrameHeight);
		int tx = ropeCurFrame % ROPE_IMG_COL * ropeFrameWidth;
		int ty = ropeCurFrame / ROPE_IMG_COL * ropeFrameHeight;
		g.drawImage(imgRope, -tx + ropeX, -ty + ropeY, 20);
	}

	/** 人物移动逻辑 */
	public void ropeMove() {
		if (ropeCurState != -1)
			ropeCurFrame++;
		switch (ropeCurState) {
		case ROPE_UP_MOVE:
			if(ropeY>getHeight()/2){
				ropeY-=3;
			}
			else{
				if(mapY>=0){
					if(ropeY>0)
						ropeY-=3;
				}
				else
					mapY+=3;
			}
			if (ropeCurFrame < 0 || ropeCurFrame > 2)
				ropeCurFrame = 0;
			break;
		case ROPE_DOWN_MOVE:
			if(ropeY<getHeight()/2)
				ropeY+=3;
			else{
				if(mapY<=-(gameMap.length*MAP_TILE_HEIGHT-getHeight())){
					if(ropeY<getHeight()-ropeFrameHeight)
						ropeY+=3;
				}else
					mapY-=3;
			}
			if (ropeCurFrame < 6 || ropeCurFrame > 8)
				ropeCurFrame = 6;
			break;
		case ROPE_LEFT_MOVE:
			
			if (ropeCurFrame < 9 || ropeCurFrame > 11)
				ropeCurFrame = 9;
			break;
		case ROPE_RIGHT_MOVE:
			if (ropeCurFrame < 3 || ropeCurFrame > 5)
				ropeCurFrame = 3;
			break;
		default:
		}
	}

	protected void keyPressed(int keyCode) {
		int t = getGameAction(keyCode);
		switch (t) {
		case UP:
			ropeCurState = ROPE_UP_MOVE;
			break;
		case DOWN:
			ropeCurState = ROPE_DOWN_MOVE;
			break;
		case LEFT:
			ropeCurState = ROPE_LEFT_MOVE;
			break;
		case RIGHT:
			ropeCurState = ROPE_RIGHT_MOVE;
			break;
		default:
		}
	}

	protected void keyReleased(int keyCode) {
		ropeCurState = -1;
	}
	
	boolean isUpColl(){
		int mx=Math.abs(mapX)+ropeX;
		int my=Math.abs(mapY)+ropeY;
		int row=my/MAP_TILE_HEIGHT;
		int col=mx/MAP_TILE_WIDTH;
		System.out.println(gameMap[row][col]);
		if(gameMap[row-1][col]>=13&&gameMap[row-1][col]<=18)
			return false;
		else 
			return true;
	}

}








⌨️ 快捷键说明

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