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

📄 gamecenter.java

📁 利用j2me所开发的一个角色扮演类游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				bgMap[row][col] = 0;
			}
		}
		while (HolebackNum > 0) {
			long x;
			x = (long) Math.abs(rand.nextFloat() * mapNum); // X保存一维数组的下标
			int col = (int) (x % width);// 通过一维数组X算出其在二维数组的下标
			int row = (int) ((x - col) / width);
			if (bgMap[row][col] == 0) {
				bgMap[row][col] = (byte) (rand.nextInt(3) + 1);
				HolebackNum--;
			}
		}
	}

	private void loadNextMap(int tollGateNum) {
		nextTallGateMap(this.tollGateNum, this.mapWidth, this.mapHeight);
		for (int col = 0; col < 8; col++) {
			for (int row = 0; row < 16; row++) {
				zaLayer.setCell(col, row, bgMap[row][col]);
			}
		}
	}

	private void loadMap(int tollGateNum) {
		// 得到本关最多的能用的时间
		currentGateTimes = (21 - (tollGateNum - 2) * 5) * 6 * 1000;
		startTimes = System.currentTimeMillis(); // 当前关开始时间
		nextTallGateMap(this.tollGateNum, this.mapWidth, this.mapHeight);
		try {
			img = Image.createImage("/bgmap1.png");
		} catch (IOException e) {
			e.printStackTrace();
		}
		bgLayer = new TiledLayer(8, 16, img, 60, 40);
		for (int col = 0; col < 8; col++) {
			for (int row = 0; row < 16; row++) {
				bgLayer.setCell(col, row, 4);
			}
		}
		zaLayer = new TiledLayer(8, 16, img, 60, 40);
		for (int col = 0; col < 8; col++) {
			for (int row = 0; row < 16; row++) {
				zaLayer.setCell(col, row, bgMap[row][col]);
			}
		}
		layerManager.append(zaLayer);
		layerManager.append(bgLayer);
	}

	private void keyControl() {
		// 处理用户按键输入

		int keyCode = this.getKeyStates();
		boolean iscollidesWithZa = false;
		int xMove = 0, yMove = 0;
		if ((keyCode & this.UP_PRESSED) != 0) {
			yMove = -dDistance;
			dDirection = this.UP_PRESSED;
			dongSprite.forward(dDirection, dDistance);
		} else if ((keyCode & this.DOWN_PRESSED) != 0) {
			yMove = dDistance;
			dDirection = this.DOWN_PRESSED;
			dongSprite.forward(dDirection, dDistance);

		} else if ((keyCode & this.RIGHT_PRESSED) != 0) {
			xMove = dDistance;
			dDirection = this.RIGHT_PRESSED;
			dongSprite.forward(dDirection, dDistance);
		} else if ((keyCode & this.LEFT_PRESSED) != 0) {
			xMove = -dDistance;
			dDirection = this.LEFT_PRESSED;
			dongSprite.forward(dDirection, dDistance);
		} else if ((keyCode & this.FIRE_PRESSED) != 0) {
		}
		// 处理精灵与障碍层碰撞的问题
		if (dongSprite.meetSprite.collidesWith(zaLayer, true)) {
			iscollidesWithZa = true;
			dDirection = dongSprite.lastdirection;
			if (dDirection == this.DOWN_PRESSED) {
				dDirection = this.UP_PRESSED;
				dongSprite.forward(dDirection, dDistance);
				dongSprite.setFrameSequence(down);
			} else if (dDirection == this.UP_PRESSED) {
				dDirection = this.DOWN_PRESSED;
				dongSprite.forward(dDirection, dDistance);
				dongSprite.setFrameSequence(up);
			} else if (dDirection == this.LEFT_PRESSED) {
				dDirection = this.RIGHT_PRESSED;
				dongSprite.forward(dDirection, dDistance);
			} else if (dDirection == this.RIGHT_PRESSED) {
				dDirection = this.LEFT_PRESSED;
				dongSprite.forward(dDirection, dDistance);
			}
		}
		// 检测与炸弹相碰撞
		for (int i = 0; i < 2 * this.tollGateNum; i++) {
			if (dongSprite.collidesWith(bomb[i], true)) {
				dongSprite.score += 50;
				dongSprite.collidesWithBomb();
				bomb[i].setVisible(false);
				try {
					if (gameMain.gameSound == 1) {
						jBombMusicPlayer.start();
					}
				} catch (MediaException e) {

					e.printStackTrace();
				}
			}
		}
		if ((xMove != 0 || yMove != 0) && !iscollidesWithZa) {
			xView += xMove;
			yView += yMove;
			if (xView < 0) {
				xView -= xMove;
			} else if (bgLayer.getWidth() - xView < screenW) {
				xView -= xMove;
			} else if (((dongSprite.getX() - xView) < (screenW / 2 - 20))
					|| ((bgLayer.getWidth() - dongSprite.getX()) < (screenW / 2 - 20))) {
				xView -= xMove;
			}
			if (yView < 0) {
				yView -= yMove;
			} else if (bgLayer.getHeight() - yView < screenH) {
				yView -= yMove;
			}
			layerManager.setViewWindow(xView, yView, screenW, screenH - 30);
		}
		if (dongSprite.collidesWith(mb, true)) {
			dongSprite.score = dongSprite.score + 1000 + 500
					* (this.tollGateNum - 1);
			if (this.tollGateNum < 8) {
				nextTallGate(this.tollGateNum);
			} else {
				try {
					score.loadScores();
					if (score.isHighScore(dongSprite.score)) {// 如果是高分,则让用户输入姓名,否则直接退出游戏
						isRunPlayGame = false;
						if (myTextBox == null) {
							myTextBox = new MyTextBox(62, 107, gameMain, score,
									dongSprite.score);
						} else {
							myTextBox.setStart(dongSprite.score);
						}
						stop();
						gameMain.display.setCurrent(myTextBox);
					} else {
						isRunPlayGame = false;
						this.removeCommand(usebomb1);
						this.removeCommand(returnGameMenu);
						this.removeCommand(cancelMenu);
						stop();
						if (GameLogo.gameMenu != null) {
							GameLogo.gameMenu.setMenuSelect(0);
						}
						gameMain.display.setCurrent(GameLogo.gameMenu);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	private void showGame(Graphics g) {
		// 显示游戏画面
		g.setColor(0, 0, 0);
		g.fillRect(0, 0, screenW, screenH);
		g.setColor(255, 255, 255);
		layerManager.paint(g, 0, 15);
		int bombX = 10;
		int i = 0;
		while (dongSprite.bombNum > i) {
			// 如果主角拥有炸弹,则显示出来
			try {
				img = Image.createImage("/bomb.png");
			} catch (IOException e) {
				e.printStackTrace();
			}
			g.drawImage(img, bombX, 0, Graphics.TOP | Graphics.LEFT);
			bombX = bombX + 13;
			i++;
		}
		g.drawString("SCORE "
				+ this.asLongString(String.valueOf(dongSprite.score), 5),
				screenW / 2 + 45, 0, 0);
		g.drawString("LEVEL "
				+ this.asLongString(String.valueOf(tollGateNum), 2),
				screenW / 2 - 24, screenH - 15, 0);
		long showTimes = currentGateTimes
				- (System.currentTimeMillis() - startTimes);
		if (showTimes < 0 && this.tollGateNum > 1) {
			showTimes = 0;
			try {
				score.loadScores();
				if (score.isHighScore(dongSprite.score)) {// 如果是高分,则让用户输入姓名,否则直接退出游戏
					isRunPlayGame = false;
					if (myTextBox == null) {
						myTextBox = new MyTextBox(62, 107, gameMain, score,
								dongSprite.score);
					} else {
						myTextBox.setStart(dongSprite.score);
					}
					stop();
					gameMain.display.setCurrent(myTextBox);
				} else {
					isRunPlayGame = false;
					this.removeCommand(usebomb1);
					this.removeCommand(returnGameMenu);
					this.removeCommand(cancelMenu);
					stop();
					if (GameLogo.gameMenu != null) {
						GameLogo.gameMenu.setMenuSelect(0);
					}
					gameMain.display.setCurrent(GameLogo.gameMenu);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if (this.tollGateNum > 1) {
			g.drawString(Long.toString((showTimes / 1000) / 60) + "分"
					+ Long.toString((showTimes / 1000) % 60) + "秒", 5,
					screenH - 15, 0);
		}

		this.flushGraphics();
	}

	public void run() {
		Graphics g = this.getGraphics();
		while (isRunPlayGame) {
			keyControl();
			showGame(g);
			try {
				Thread.sleep(150);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	private void stop() {
		// 停止游戏
		gameBgMusicPlayer.close();
		jBombMusicPlayer.close();
		uBombMusicPlayer.close();
	}

	public void commandAction(Command c, Displayable d) {
		if (c.getCommandType() == Command.OK) {
			if (dongSprite.bombNum > 0) {
				dongSprite.useBomb(dongSprite.lastdirection, 40);
				try {
					if (gameMain.gameSound == 1) {
						uBombMusicPlayer.start();
					}
				} catch (MediaException e) {

					e.printStackTrace();
				}
			}
		} else if (c.getCommandType() == Command.EXIT) {
			isRunPlayGame = false;
			this.removeCommand(usebomb1);
			this.removeCommand(returnGameMenu);
			this.removeCommand(cancelMenu);
			stop();
			
			if (GameLogo.gameMenu != null) {
				GameLogo.gameMenu.setMenuSelect(0);
			}
			gameMain.display.setCurrent(GameLogo.gameMenu);
		}
	}

	public static String asLongString(String str, int len1) {
		String temps = "0000000000";
		if (str.length() >= len1) {
			str.substring(0, len1);
		} else {
			str = temps.substring(0, len1 - str.length()) + str;
		}
		return str;
	}

	public static final Image createImage(String filename) {
		Image image = null;
		try {
			image = Image.createImage(filename);
		} catch (Exception e) {
		}
		return image;
	}
}

⌨️ 快捷键说明

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