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

📄 mandownscreen.java

📁 男人下一百层手机游戏
💻 JAVA
字号:
package src;

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

public class ManDownScreen extends GameCanvas implements Runnable,
		CommandListener {
	private static final int MILLIS_PER_TICK = 50;

	private int x, y;

	private ManLayerDownMidlet midlet; // Hold the Main Midlet

	private Command backCommand = new Command("Back", Command.BACK, 1);

	private ManMap manMap;

	private boolean isPlay; // Game Loop runs when isPlay is true

	private int width; // To hold screen width

	private int height; // To hold screen height

	private int scnViewWidth; // Hold Width Screen View Port

	private int scnViewHeight; // Hold Height Screen View Port

	private Thread gameThread = null;

	// Layer Manager to manager background (terrain)
	private LayerManager layerManager;

	// TiledLayer - Terrain
	private TiledLayer movemap;

	private int terrainScroll; // Hold Y position for scrolling

	// Sprites
	private PlayerSprite player;

	public static boolean gameover = false;

	public static boolean gamecross = false;

	public static int w;

	public static int h;

	// Constructor and initialization
	public ManDownScreen(ManLayerDownMidlet midlet) throws Exception {
		super(true);
		this.midlet = midlet;
		addCommand(backCommand);
		setCommandListener(this);

		width = getWidth(); // get screen width
		height = getHeight(); // get screen height
		scnViewWidth = width; // Set View Port width to screen width
		scnViewHeight = height; // Set View Port height to screen height

		isPlay = true;

		// setup map
		manMap = new ManMap(scnViewHeight);
		movemap = manMap.getMap();

		// setup player sprite
		Image image = Image.createImage("/res/player.png");
		player = new PlayerSprite(image, 24, 18, width, height); // 24 =
																	// width of
																	// sprite in
																	// pixels,
																	// 18 is
																	// height of
																	// sprite in
																	// pixels
		player.startPosition();

		// init bullets

		layerManager = new LayerManager();
		layerManager.append(player);
		layerManager.append(movemap);

	}

	// Start thread for game loop
	public void start() {
		gameThread = new Thread(this);
		gameThread.start();
	}

	// Stop thread for game loop
	public void stop() {
		gameThread = null;
	}

	// Main Game Loop
	public void run() {
		Graphics g = getGraphics();

		Thread currentThread = Thread.currentThread();

		try {
			while (currentThread == gameThread) {
				long startTime = System.currentTimeMillis();
				if (isShown()) {
					if (isPlay) {

						tick();
						// player.undo();
					}
					render(g);
				}
				long timeTake = System.currentTimeMillis() - startTime;
				if (timeTake < MILLIS_PER_TICK) {
					synchronized (this) {
						wait(MILLIS_PER_TICK - timeTake);
					}
				} else {
					currentThread.yield();
				}
			}
		} catch (InterruptedException ex) {
			// won't be thrown
		}

	}

	// Handle dynamic changes to game including user input
	public void tick() {
		// Scroll Terrain
		manMap.scrollMap();

		// Player Actions
		int keyStates = getKeyStates();

		// Player Moves
		if ((keyStates & LEFT_PRESSED) != 0) {
			player.moveLeft();
		}
		if ((keyStates & RIGHT_PRESSED) != 0) {
			player.moveRight();
		}
		if ((keyStates & UP_PRESSED) != 0) {
			player.moveUp();
		}
		// if ((keyStates & DOWN_PRESSED) != 0) {
		// player.moveDown();
		// }

		if (!(player.collidesWith(manMap.getMap(), true))) {
			player.moveDown();
		} else {
			player.moveUp();
		}

	}

	public void commandAction(Command c, Displayable d) {
		if (c == backCommand) {

		}
	}

	// Method to Display Graphics
	private void render(Graphics g) {
		if (!gameover) {
			// Set Background color to beige
			// g.setColor(0xF8DDBE);
			g.setColor(manMap.getGroundColor());
			g.fillRect(0, 0, width, height);
			g.setColor(0x0000ff);

			// Get Current Map
			movemap = manMap.getMap();

			// LayerManager Paint Graphics
			layerManager.paint(g, 0, 0);

			flushGraphics();
		}
		if (gameover) {
			if (!gamecross) {
				isPlay = false;
				w = getWidth();
				h = getHeight();
				g.setColor(0x000000);
				g.fillRect(0, 0, w, h);
				g.setColor(0xffffff);
				g.drawString("大侠请重来过吧!", w / 2 - 80, h / 2, g.LEFT | g.TOP);
				g.drawString("男人下一百层 1.0 版", w / 2 - 80, h / 2 + 15, g.LEFT
						| g.TOP);
				g.drawString(" 无锡天智 板权所有", w / 2 - 80, h / 2 + 30, g.LEFT
						| g.TOP);
			} else {
				w = getWidth();
				h = getHeight();
				g.setColor(0x00fff0);
				g.fillRect(0, 0, w, h);
				g.setColor(0x000000);
				g.drawString("真有男人味!", w / 2 - 80, h / 2, g.LEFT | g.TOP);
				g.drawString("男人下一百层 1.0 版", w / 2 - 80, h / 2 + 15, g.LEFT
						| g.TOP);
				g.drawString(" 无锡天智 板权所有", w / 2 - 80, h / 2 + 30, g.LEFT
						| g.TOP);
			}
			flushGraphics();
		}
	}
}

⌨️ 快捷键说明

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