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

📄 gamecanvas.java

📁 j2me手机游戏《都市历险记》的源代码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
		// nowFrame = 0;
		// index = 0;
		// direction = 0;
		// }
		// if ( (n == Canvas.KEY_NUM6) || (n == -5)) {
		// nowFrame = 0;
		// index = 0;
		// direction = 0;
		// }
		// if ( (n == Canvas.KEY_NUM5) || (n == -20)) {
		// nowFrame = 0;
		// index = 0;
		// direction = 0;
		// chechScore();
		// }
		if (this.getGameAction(n) == LEFT) {
			nowFrame = 0;
			index = 0;
			direction = 0;
		}
		if (this.getGameAction(n) == RIGHT) {
			nowFrame = 0;
			index = 0;
			direction = 0;
		}
		if (this.getGameAction(n) == 8) {
			nowFrame = 0;
			index = 0;
			direction = 0;
			chechScore();
		}
	}

	/***************************************************************************
	 * 初始化地图
	 **************************************************************************/
	public void initMap(int mode) {
		loadLevelImage(mode);
		// loadMoveMap(mode);
	}

	/***************************************************************************
	 * 画地图
	 **************************************************************************/
	public void drawMap() {
		int rowI = Math.abs(map_CurY / 24);
		// if (rowI >= 16)
		// rowI = 16;
		int colJ = Math.abs(map_CurX / 24);
		// if (colJ >= 76)
		// colJ = 76;
		for (int i = rowI; i < 9 + rowI; i++) {
			for (int j = colJ; j < 9 + colJ; j++) {
				// System.out.println(" "+i+" "+j);
				int tmp = map[i][j];

				// System.out.println(" "+tmp);
				if (tmp >= 0) {
					offG.setClip(j * 24 - map_CurX, i * 24 - map_CurY, 24, 24);

					int tmpr = tmp / nowMapCol[this.level_Mode];
					int tmpc = tmp % nowMapCol[this.level_Mode];
					offG.drawImage(level_Image, (j - tmpc) * 24 - map_CurX,
							(i - tmpr) * 24 - map_CurY, 0);
				}
			}
		}
	}

	// drawPontoon(g);
	// drawBottle(g);

	/***************************************************************************
	 * 初始化地图
	 **************************************************************************/
	public void loadLevelImage(int flag) {
		level_Image = null;
		System.gc();
		int imfile;
		if (flag >= 2)
			imfile = 2;
		else
			imfile = 1;
		try {

			level_Image = Image.createImage("/map" + imfile + ".png");
		} catch (Exception e) {
			System.out.println("no map1");
		}

		DataInputStream in = new DataInputStream(getClass()
				.getResourceAsStream("/stage_" + flag + ".dat"));
		map = new byte[mapSize[level_Mode - 1][0]][mapSize[level_Mode - 1][1]];
		mapW = mapSize[level_Mode - 1][1] * 24 - 9 * 24;
		mapH = mapSize[level_Mode - 1][0] * 24 - SCREENHEIGHT;
		System.out.println("  len" + map.length + " len2" + map[0].length
				+ "map file num" + imfile);
		int k = 0;
		try {
			Thread.yield();
			for (int i = 0; i < mapSize[level_Mode - 1][0]; i++) {
				for (int j = 0; j < mapSize[level_Mode - 1][1]; j++) {
					map[i][j] = in.readByte();
					System.out.print(" " + map[i][j]);
					k++;
					this.repaint();
					// this.serviceRepaints();
				}
				System.out.println();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
			System.out.println("map err");
		}

	}

	/***************************************************************************
	 * 判断上下左右是否可以通过
	 **************************************************************************/
	/***************************************************************************
	 * 判断左是否可以通过
	 **************************************************************************/
	public boolean left_CanCross() {
		int row = (py + 45 + map_CurY) / 24;
		int col = (px + 10 + map_CurX) / 24;
		// System.out.println("map= " + map[row][col]);
		if (map[row][col] == 19 || map[row][col] == 20 || map[row][col] == 21) {
			return false;
		} else {
			return true;
		}
	}

	/***************************************************************************
	 * 判断右是否可以通过
	 **************************************************************************/
	public boolean right_CanCross() {
		int row = (py + 45 + map_CurY) / 24;
		int col = (px + 40 + map_CurX) / 24;
		// System.out.println("row="+row+"col="+col);
		// System.out.println("map= " + map[row][col]);
		if (col > map[0].length) {
			return false;
		}
		if (map[row][col] == 19 || map[row][col] == 20 || map[row][col] == 21) {
			return false;
		} else {
			return true;
		}
	}

	/***************************************************************************
	 * 判断下是否可以通过
	 **************************************************************************/
	public boolean down_CanCross() {
		int row = (py + 50 + map_CurY) / 24;
		int col = (px + 20 + map_CurX) / 24;
		// System.out.println("row="+row+"col="+col);
		// System.out.println("map= " + map[row][col]);

		// boolean b1= (map[row][col] == 0 || map[row][col] == 19 ||
		// map[row][col] == 20 ||
		// map[row][col] == 21);
		// boolean b2=(row==9);
		// if((this.level_Mode==1&&b1)||(level_Mode>1&&b2))
		if (row == 7) {
			// System.out.println("py= "+py);
			py = row * 24 - 50 - map_CurY;
			// unCross = row * 16 - 24 - map_CurY;
			dir2_Flag = true;
			if (direction == 0) {
				nowFrame = 0;
				index = 0;
			}
			return false;
		} else {
			return true;
		}
	}

	// public boolean up_CanCross() {
	// return false;
	// }

	/***************************************************************************
	 * 踩到钉子死亡
	 **************************************************************************/
	public boolean deadDown() {
		int row = (py + 24 + map_CurY) / 24;
		int col = (px + 10 + map_CurX) / 24;
		// if (map[row][col] == ) {
		// return true;
		// } else {
		return false;
		// }
	}

	/***************************************************************************
	 * 初始化敌人子弹 //
	 **************************************************************************/
	// public void createMonBullet() {
	// for (int i = 0; i < mon.size(); i++) {
	// Monster m1;
	// m1 = (Monster) mon.elementAt(i);
	// if (Math.abs(m1.mx - px - map_CurX) < 100
	// && Math.abs(m1.my - py - map_CurY) < 208) {
	// if (direction == DIR_RIGHT
	// && (py + 12) > Math.abs(m1.mx - py - map_CurY)
	// && (py + 12) > Math.abs(m1.my - py - map_CurY) + 24) {
	// // if (Math.abs(r1.nextInt()) % 10 == 5) {
	// mon_Bullet.fire_Flag = mon_Bullet.FIER_LEFT;
	// mon_Bullet.createBullet((m1.mx - py - map_CurY),
	// (m1.my - py - map_CurY));
	// // }
	// }
	//
	// }
	// }
	// }
	/***************************************************************************
	 * 加载下一关
	 **************************************************************************/
	public boolean nextLevel() {
		int row = (py + 12 + map_CurY) / 24;
		int col = (px + 20 + map_CurX) / 24;
		// System.out.println(map_Move[row][col]);
		// if (map_Move[row][col] == 97) {
		// return true;
		// } else {
		return false;
		// }
	}

	/***************************************************************************
	 * 游戏主循环
	 **************************************************************************/
	public void run() {
		while (ISRUN) {
			// long l1= Runtime.getRuntime().totalMemory();
			// long l2= Runtime.getRuntime().freeMemory();
			// System.out.println("total "+l1);
			// System.out.println("free "+l2);
			if (flag) {
				this.repaint();
				try {
					Thread.sleep(this.b_time);
				} catch (Exception E) {
				}

				if (this.menuState < 3)
					this.menuState++;
				else {// b_im=2;
					// isok=false;
					flag = false;

				}
				continue;

			}
			switch (this.gameState) {
			case LOAD:
				// System.out.println("loading");
				Thread.yield();
				System.gc();
				this.proess += 5;
				if (this.proess >= 100) {
					this.gameState = GAMEING;

				}
				this.repaint(); // 重绘
				this.serviceRepaints();
				break;
			case GAMEOVER:
			case GAMEING:

				if (life <= 0) {
					this.gameState = GAMEOVER;
					index = 2;
				}
				//
				// if (nextLevel()) {
				// this.initLevel(2);
				// }
				// if (monFrame < 1) {
				// monFrame++;
				// } else {
				// monFrame = 0;
				// }
				// monPx += monMove;
				// monMax = monMax + monMove;
				// if (monMax == 0) {
				// monMove = 4;
				// monLorR = 0;
				// }
				// if (monMax == 48) {
				// monMove = -4;
				// monLorR = 1;
				// }
				// // System.out.println(direction);

				if (direction == DIR_LEFT) { // 方向向左时人物的移动
					// if ((nowFrame == 2 || nowFrame == 2) && upFlag == 18) {
					// dir2 = DIR_DOWN;
					// }
					if (down_CanCross()) {
						dir2 = DIR_DOWN;
					}

					// if (deadDown()) {// 碰到钉子死
					// py += 24;
					// deadFlag = true;
					// life--;
					// }

					if (left_CanCross()) {
						if (px > SCREENWIDTH / 2 - 20) {
							px -= 8;
						} else {
							if (map_CurX > 0) {
								map_CurX -= 8;
								this.bottle[0] += 8;
								backMove(); // 背景移动
							} else {
								if (px > 0) {
									px -= 8;
								}
							}
						}

					}
				}

				// // System.out.println(upFlag);
				if (direction == DIR_RIGHT) { // 方向向右时人物的移动
					// if ((nowFrame == 2 || nowFrame == 3) && upFlag == 18) {
					// dir2 = DIR_DOWN;
					//
					// }
					if (down_CanCross()) {
						dir2 = DIR_DOWN;
					}

					if (deadDown()) {
						py += 24;
						life--;
						deadFlag = true;
					}
					if (right_CanCross()) {
						if (px <= SCREENWIDTH / 2 - 20) {
							px += 8;
						} else {
							if (map_CurX < mapW) {
								map_CurX += 8;
								this.bottle[0] -= 8;
								backMove(); // 背景移动
							} else {
								if (px < 156) {
									px += 8;
								}
							}
						}

					}
				}

				// System.out.println("mapX= " + map_CurX + " mapY= " +
				// map_CurY);
				// System.out.println(" px= " + px + " py= " + py);

				if (dir2 == DIR_UP) { // 方向向上时人物的跳

					if (py < 100) {
						System.out.println(map_CurY);
						System.out.println(upFlag);
						map_CurY -= upFlag;
						if (map_CurY < 0) {
							map_CurY = 0;
							py -= upFlag;
						}
					} else {
						py -= upFlag;
					}

					if (upFlag > 0) {
						upFlag -= 2;
					} else {
						dir2 = DIR_DOWN;
						dir3 = 0;
						index = 5;
					}
				}
				if (dir3 == 18) {
					if (px > SCREENWIDTH / 2 - 20) {
						px -= 8;
						if (px < 0)
							px = 0;

					} else {
						if (map_CurX > 0) {
							map_CurX -= 8;
							this.bottle[0] += 8;
							backMove(); // 背景移动
						} else {
							if (px > 0) {
								px -= 8;
								if (px < 0)
									px = 0;
							}
						}
					}

				}
				if (dir3 == 20) {
					if (px <= SCREENWIDTH / 2 - 20) {
						px += 8;
						if (px > map[0].length * 24 - 24) {
							px = map[0].length * 24 - 24;
						}
					} else {
						if (map_CurX < mapW) {
							map_CurX += 8;
							this.bottle[0] -= 8;
							backMove(); // 背景移动
						} else {
							if (px < SCREENWIDTH - 20) {
								px += 8;

								if (px > map[0].length * 24 - 24) {
									px = map[0].length * 24 - 24;
								}
							}
						}
					}

				}

				if (dir2 == DIR_DOWN) { // 方向向下时人物的跳
					// System.out.println("down= " + upFlag);
					if (deadDown()) {
						// py += 24;
						// life--;
						// deadFlag = true;
					}

					// if (!fire_Flag) {
					// nowFrame = 6;
					// }
					if (down_CanCross()) {
						int tmp = py;
						if (py < 98) {
							System.out.println("撒"+map_CurY);
							System.out.println("撒瑁"+upFlag);
							map_CurY += upFlag;
							if (map_CurY >= mapH) {

								map_CurY = mapH;
								py += upFlag;
							}
						} else {
							py += upFlag;
						}

						if (upFlag < 18) {
							upFlag += 2;
						}
						if (!down_CanCross()) {
							dir2 = 0;
							dir2_Flag = true;
							upFlag = 18;
							if (direction == 0) {
								nowFrame = 0;
								index = 0;
							}
						}
					}
				}
				try {
					if (gameState == GAMEING) { // //过关
						if (enemy[0][3] == 0 && (px >= SCREENWIDTH - 30)) {
							// System.out.println("now levetl"+level_Mode);

							level_Mode++;
							this.bossCount = 0;
							score += 100;
							proess = 0;
							level_Image = null;
							enemy[0][3] = 1;
							px = 0;
							dir2 = DIR_DOWN;
							dir3 = 0;
							index = 0;
							if (level_Mode > 3) {// this.saveRecord();
								life = 0;
								index = 2;
								gameState = GameCanvas.GAMEOVER;

							} else {
								gameState = LOAD;
							}
						}
					}
				} catch (Exception e) {
				}
				this.repaint(); // 重绘

				// this.serviceRepaints();
				nowFrame++; // 变换动作侦

				if (index == 0) { // 站立
					if (nowFrame > playerStop.length - 1) {
						nowFrame = 0;
					}
				}
				if (index == 1) {
					if (nowFrame > RunMotion.length - 1) {
						nowFrame = 0;
					}
				}
				if (index == 2) {
					if (nowFrame > playerDead.length - 1) {
						nowFrame = 0;

					}
				}
				break;
			case PAUSE:
				break;
			}

			try {
				Thread.sleep(50);
			} catch (Exception e) {
			}
		}

	}
}

⌨️ 快捷键说明

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