rpgcanvas.java

来自「一个J2ME的RPG游戏」· Java 代码 · 共 1,841 行 · 第 1/4 页

JAVA
1,841 行
字号
						Image tempimage = npc.img;
						vectorpicture.addElement(tempimage);
						npc.pause = true;
					}
				}
			}
			//与仓库入口碰撞,进入场景二
			if (mrole.collidesWith(load.imgmap3, true)) {
				scene = 2;
				inputmode = 2;
				mrole.roomx = 80;
				mrole.roomy = 153;
				removeCommand(mainMenu);
				// load.maproom1layer2.setVisible(true);
				// load.maproom1layer1.setVisible(true);
				// load.maproom1ground.setVisible(true);
			}
		}
	}
	
	//与老人的对话
	public void drawlist() {
		g.setColor(0, 0, 100);
		g.fillRect(0, getHeight() - 40, getWidth(), 40);
		g.setColor(255, 255, 128);

		g.drawString(choose1, 50, getHeight() - 30, 0);
		g.drawString(choose2, 50, getHeight() - 20, 0);

		g.setColor(255, 255, 0);
		g.fillArc(45, selecty, 5, 5, 0, 360);
		Font ff = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
				Font.SIZE_SMALL);
		g.setFont(ff);
		g.setColor(255, 255, 255);
		g
				.drawString("Push FIRE", screenwidth, screenheight, g.BOTTOM
						| g.RIGHT);
		// if((key&FIRE_PRESSED)!=0)showyesorno=false;
	}
	
	//在场景一加载游戏画面
	public void append() {
		lmscene1.append(mrole);
		lmscene1.append(load.npc[0]);
		lmscene1.append(load.npc[1]);
		lmscene1.append(load.npc[2]);
		lmscene1.append(load.npc[3]);
		lmscene1.append(load.npc[4]);
		lmscene1.append(load.npc[5]);
		lmscene1.append(load.property[0]);
		lmscene1.append(load.property[1]);
		lmscene1.append(load.property[2]);
		lmscene1.append(load.property[3]);
		lmscene1.append(load.property[5]);
		lmscene1.append(load.imgmap1);
		lmscene1.append(load.imgmap2);
		lmscene1.append(load.imgmap3);
		lmscene1.append(load.hourse);
		lmscene1.append(load.mapbackground);
	}

	//场景二加载游戏画面
	public void append2() {
		lmscene2.append(mrole);
		if (isBossDead && (mroleProperty[0] >= 2))
			lmscene2.append(load.property[4]);
		// lmscene2.append(load.flagLayer);
		lmscene2.append(load.maproom1layer2);
		lmscene2.append(load.maproom1layer1);
		lmscene2.append(load.maproom1ground);
	}

	// 输入函数,负责主角和屏幕的移动

	// 主线成,
	int i = 0;
	public void run() {
		// long startTime = System.currentTimeMillis();
		Thread currentThread = Thread.currentThread();

		while (currentThread == t) {
			input();          //获取输入
			switch (gameState) {
			case GAME_START:
				addCommand(mainMenu);
				nextmessage();
				collidetest();  //碰撞检测
				draw();       //绘制画面
				break;
			case GAME_PAUSE:
				drawPause();   //游戏暂停
				break;
			case GAME_MENU:
				drawMenu();    //游戏菜单
				break;
			case GAME_ABOUT:
				drawMenuAbout();   //游戏介绍
				break;
			}
			
			this.flushGraphics();

			try {
				Thread.sleep(150);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			// long time2 = System.currentTimeMillis() - startTime;
			// if (time2 < interval) {
			// try {
			// // t.sleep(interval - time2);            //刷新太快,游戏人物滑步
			// Thread.sleep(500);
			// } catch (InterruptedException e) {
			// e.printStackTrace();
			// }
			// } else
			// Thread.yield();
		}
	}
	
	//绘制菜单
	public void drawMenu() {
		g.setColor(255, 255, 255); // 白色, g.setColor(255);代表蓝色,相当于(0,0,255) 与1.0不同
		g.fillRect(0, 0, screenwidth, screenheight);
		g.setColor(0);

		int menuOffset = (screenheight - font.getHeight() * 6) / 2;
		g.setFont(font);
		g.setClip(0, 0, screenwidth, screenheight);
		// g.drawImage(image, 0, 0, g.LEFT | g.TOP);
		int item0Color = 0;
		int item1Color = 0;
		int item2Color = 0;
		int item3Color = 0;
		int item4Color = 0;
		int item5Color = 0;

		switch (menuState) {        //使相应菜单状态颜色变红
		case MENU_START:
			item0Color = 255 << 16;
			break;
		case MENU_LOAD:
			item1Color = 255 << 16;
			break;
		case MENU_VOL:
			item2Color = 255 << 16;
			break;
		case MENU_QUIT:
			item3Color = 255 << 16;
			break;
		case MENU_ABOUT:
			item4Color = 255 << 16;
			break;
		case MENU_SAVE:
			item5Color = 255 << 16;
			break;
		}
		g.setColor(item0Color);
		g.drawString("新游戏", (screenwidth - font.stringWidth("新游戏")) / 2,
				menuOffset + font.getHeight() * 0, g.LEFT | g.TOP);
		g.setColor(item1Color);
		g.drawString("载入进度", (screenwidth - font.stringWidth("载入进度")) / 2,
				menuOffset + font.getHeight() * 1, g.LEFT | g.TOP);
		g.setColor(item2Color);
		if (isVolOn) {
			g.drawString("关闭声音", (screenwidth - font.stringWidth("关闭声音")) / 2,
					menuOffset + font.getHeight() * 2, g.LEFT | g.TOP);
		} else {
			g.drawString("打开声音", (screenwidth - font.stringWidth("打开声音")) / 2,
					menuOffset + font.getHeight() * 2, g.LEFT | g.TOP);
		}
		g.setColor(item3Color);
		g.drawString("退出", (screenwidth - font.stringWidth("退出")) / 2,
				menuOffset + font.getHeight() * 3, g.LEFT | g.TOP);
		g.setColor(item4Color);
		g.drawString("关于游戏", (screenwidth - font.stringWidth("关于游戏")) / 2,
				menuOffset + font.getHeight() * 4, g.LEFT | g.TOP);
		g.setColor(item5Color);
		g.drawString("保存游戏", (screenwidth - font.stringWidth("保存游戏")) / 2,
				menuOffset + font.getHeight() * 5, g.LEFT | g.TOP);
	}

	public void drawPause() {

	}

	//根据设定大小,自动换行输出的字符串
	final int CharacterNumber = 8;
	public Vector getSubsection(String str) {
		Vector vector = new Vector();
		while (!str.equals("")) {
			if (str.length() > CharacterNumber) {
				vector.addElement(str.substring(0, CharacterNumber));
				str = str.substring(CharacterNumber);
			} else {
				vector.addElement(str);
				str = "";
			}
		}
		return vector;
	}

	//游戏介绍
	public void drawMenuAbout() {
		int i = 0;
		tempH = 0;
		g.setColor(255,255,255);
		g.fillRect(0, 0, screenwidth, screenheight);
		g.setColor(0);

		String gameAbout = "游戏背景:人生百态,事竟千相,"
			+ "在这个世界里,跟我们一起并行存在着各种各样的人,"
				+ "发生着各种各样的事," + "如果不去发现,你永远不知道这个世界有多大," 
				+ "不去体会,永远不懂得什么是酸甜苦辣。"
				+ "就让一切从现在开始探索," + "一切从现在开始展现……\n";

		String gameOperation="游戏操作:上、下、左、右按键分别控制人物的"
			+"上、下、左、右运动或者上、下、左、右选择\n";
		
		String gameFinish="游戏结束:当游戏人物血小于等于0时游戏失败结束,"
			+"当游戏人物杀死boss并达到等级2后"
			+"农场仓库刷新出一面旗子,找到旗子,游戏胜利结束。\n";
		
		g.setFont(font);
		int hWidth = screenwidth / 2;

		Vector vector = getSubsection(gameAbout);
		for ( i = 0; i < vector.size(); i++) {
			g.drawString((String) vector.elementAt(i), hWidth, gameAboutY + 5 + fHeight * i,
					Graphics.TOP | Graphics.HCENTER);
		}
		
		tempH = 5 + fHeight * i;
		vector = getSubsection(gameOperation);
		for(i = 0; i < vector.size(); i++){
			g.drawString((String) vector.elementAt(i), hWidth, gameAboutY + 5 + tempH + fHeight * i,
					Graphics.TOP | Graphics.HCENTER);
		}
		
		tempH = 5 + tempH + fHeight * i;
		vector = getSubsection(gameFinish);
		for(i = 0; i < vector.size(); i++){
			g.drawString((String) vector.elementAt(i), hWidth, gameAboutY + 5 + tempH + fHeight * i,
					Graphics.TOP | Graphics.HCENTER);
		}
		
		tempH = 5 + tempH + fHeight * i;
		vector = null;
		i = 0;

		removeCommand(backGame);
		this.addCommand(backMenu);
	}

	//声音播放
	public void playVol() {
		if (isVolOn) {
			if (bgPlayer == null) {
				bgPlayer = new AudioPlayer("bg.mid", "audio/midi", true);
				bgPlayer.setLoop();
				bgPlayer.play();
			}
		} else {
			if (bgPlayer != null) {
				bgPlayer.stop();
				bgPlayer.close();

				bgPlayer = null;
				System.gc();
			}
		}
	}

	//加载游戏进度
	public void loadGame() {
		// System.out.println("enter loadGame()");
		removeCommand(backGame);
		addCommand(mainMenu);
		byte[] rec = record.getRecord(1);
		// System.out.println(rec[1]);
		ByteArrayInputStream bais = new ByteArrayInputStream(rec);
		DataInputStream dis = new DataInputStream(bais);
		try {
			// isVolOn = dis.readBoolean();
			// missionmessage=dis.readUTF();
			// curmessage=dis.readUTF();
			catchcat = dis.readBoolean();
			catchcat2 = dis.readBoolean();
			showyesorno = dis.readBoolean();
			mainroleanswer = dis.readBoolean();
			inputmode = dis.readInt();
			iscollide = dis.readBoolean();
			screenx = dis.readInt();
			screeny = dis.readInt();
			tempx = dis.readInt();
			tempy = dis.readInt();
			lastscreenx = dis.readInt();
			lastscreeny = dis.readInt();
			selecty = dis.readInt();
			for (int i = 0; i < 5; i++)
				mroleProperty[i] = dis.readInt();
			roleBloodHeight = dis.readInt();
			ismessage = dis.readBoolean();
			firstTouch = dis.readBoolean();
			isbeat = dis.readBoolean();
			isFirstStart = dis.readBoolean();
			isBossDead = dis.readBoolean();
			mrole.x = dis.readInt();
			mrole.y = dis.readInt();
			load.property[0].setVisible(dis.readBoolean());
			load.property[1].setVisible(dis.readBoolean());
			load.npc[2].setVisible(dis.readBoolean());

		} catch (IOException e) {
			System.out.println("Error in loadGame() methon");
			e.printStackTrace();
		}
	}

	//保存游戏状态
	public void saveGameState() {
		removeCommand(backGame);
		addCommand(mainMenu);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		try {
			// dos.writeBoolean(isVolOn);
			// dos.writeUTF(missionmessage);
			// dos.writeUTF(curmessage);
			dos.writeBoolean(catchcat);
			dos.writeBoolean(catchcat2);
			dos.writeBoolean(showyesorno);
			dos.writeBoolean(mainroleanswer);
			dos.writeInt(inputmode);
			dos.writeBoolean(iscollide);
			dos.writeInt(screenx);
			dos.writeInt(screeny);
			dos.writeInt(tempx);
			dos.writeInt(tempy);
			dos.writeInt(lastscreenx);
			dos.writeInt(lastscreeny);
			dos.writeInt(selecty);
			for (int i = 0; i < 5; i++)
				dos.writeInt(mroleProperty[i]);
			dos.writeInt(roleBloodHeight);
			dos.writeBoolean(ismessage);
			dos.writeBoolean(firstTouch);
			dos.writeBoolean(isbeat);
			dos.writeBoolean(isFirstStart);
			dos.writeBoolean(isBossDead);
			dos.writeInt(mrole.x);
			dos.writeInt(mrole.y);
			dos.writeBoolean(load.property[0].isVisible());
			dos.writeBoolean(load.property[1].isVisible());
			dos.writeBoolean(load.npc[2].isVisible());

			if(record.IsEmpty())this.record.addRecord(baos.toByteArray());
			else 
				this.record.SetRecord(1, baos.toByteArray());
			
			dos.close();
			baos.close();
			// System.out.println("saveGameState !!");
		} catch (IOException e) {
			System.out.println("Error in saveGameState().");
			e.printStackTrace();
		}
	}

	//重新初始化游戏
	private void reinit() {
		catchcat = false;
		catchcat2 = false;
		showyesorno = false;
		mainroleanswer = false;
		scene = 1;
		inputmode = 0;
		iscollide = false;
		selecty = 152;
		isVolOn = false;
		curmessage = null;
		missionmessage = null;
		curpicture = null;
		mrole.setFrame(1);
		mrole.x = 50;
		mrole.y = 50; // 为什么 此处用mrole.setPosition(50,50)
		// 不能重设英雄位置,是否此时设的是英雄父类的x和y坐标
		mroleProperty[0] = 0;
		mroleProperty[1] = 0;
		mroleProperty[2] = 0;
		roleBloodHeight = 40;
		attackSelecty = screenwidth - 35;
		bossBlood = 30;
		warEnd = false; 
		menuState = MENU_START;
		isGold = false;
		isSeller = false;
		isBuy = false; 
		isSell = false; 
		forBuy = false;
		forSell = false;
		sellerAsk = false; 
		roleChoose = false; 
	}
	
	//游戏输入
	void input() {
		key = getKeyStates();

		if (gameState == GAME_PAUSE)
			gameState = GAME_START;
		if (gameState == GAME_MENU) {  //当游戏在菜单状态下
			switch (key) {
			case UP_PRESSED:
				switch (menuState) {
				case MENU_START:
					if (isFirstStart)
						menuState = MENU_ABOUT;
					else
						menuState = MENU_SAVE;
					break;
				case MENU_LOAD:
					menuState = MENU_START;
					break;
				case MENU_VOL:
					menuState = MENU_LOAD;
					break;
				case MENU_QUIT:
					menuState = MENU_VOL;
					break;
				case MENU_ABOUT:
					menuState = MENU_QUIT;
					break;
				case MENU_SAVE:
					menuState = MENU_ABOUT;
					break;
				}
				break;
			case DOWN_PRESSED:
				switch (menuState) {
				case MENU_START:
					menuState = MENU_LOAD;
					break;
				case MENU_LOAD:
					menuState = MENU_VOL;
					break;
				case MENU_VOL:
					menuState = MENU_QUIT;
					break;
				case MENU_QUIT:
					menuState = MENU_ABOUT;
					break;
				case MENU_ABOUT:
					if (isFirstStart)
						menuState = MENU_START;
					else
						menuState = MENU_SAVE;
					break;
				case MENU_SAVE:

⌨️ 快捷键说明

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