rpgcanvas.java

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

JAVA
1,841 行
字号
					menuState = MENU_START;
					break;
				}
				break;
			case FIRE_PRESSED:
				switch (menuState) {
				case MENU_START:
					if(!isFirstStart)reinit();
					gameState = GAME_START;
					isFirstStart = false;
					removeCommand(backGame);
					this.addCommand(mainMenu);
					break;
				case MENU_LOAD:
					loadGame();
					isFirstStart = false;
					gameState = GAME_START;
					removeCommand(backGame);
					this.addCommand(mainMenu);
					break;
				case MENU_VOL:
					isVolOn = !isVolOn;
					// isFirstStart = false;
					playVol();
					break;
				case MENU_QUIT:
					this.midlet.exitMIDlet();
					break;
				case MENU_ABOUT:
					gameState = GAME_ABOUT;
					gameAboutY = 0;
					break;
				case MENU_SAVE:
					saveGameState();
					gameState = GAME_START;
					break;
				}
				break;
			}
		}
		
		//进入游戏介绍状态
		else if(gameState == GAME_ABOUT){
			switch(key){
			case UP_PRESSED:
				gameAboutY += fHeight;
				if(gameAboutY >= 0)gameAboutY = 0;
				break;
			case DOWN_PRESSED:
				gameAboutY -= fHeight;
				if(gameAboutY <= screenheight - tempH)gameAboutY = screenheight -tempH;				
				break;
			default:
				break;
			
			}
		}
		
		//进入游戏状态
		else if (gameState == GAME_START) {  
			//输入模式为0时
			if (inputmode == 0) {
				if ((key & FIRE_PRESSED) != 0) {
					System.out.println("@@@@@@@@");
					ismessage = false;
					load.npc[0].pause = false;
					if (isSoldier) {
						// isAttack = true;
						isChoose = true;
						inputmode = 1;
						choose1 = "攻击";
						choose2 = "放弃";
					}
					
					else if (isProperty) {
						isProperty = false;
						if (isBoss) {
							isChoose = true;
							inputmode = 1;
							choose1 = "攻击";
							choose2 = "放弃";
						} else if (isFood) {
							load.property[2].setVisible(false);
							randX = getrand(480);
							randY = getrand(450);
							roleBloodHeight += 3;
							if (roleBloodHeight > 40)
								roleBloodHeight = 40;
							load.property[2] = new Property(load.food, randX,
									randY);
							isFood = false;
						} else if (isSword) {
							load.property[3].setVisible(false);
							mroleProperty[1]++;
							isSword = false;
						}

						else if (isGold) {
							load.property[5].setVisible(false);
							randX = getrand(480);
							randY = getrand(450);
							load.property[5] = new Property(load.gold, randX,
									randY);
							isGold = false;
							mroleProperty[4]++;
						} 
						else if (catchcat) {
							load.property[0].setVisible(false);
							catchcat2 = true;
							catchcat = false;
						}
					}
					 else if (iscollide == true) {
							showyesorno = true;
							inputmode = 1;
							choose1 = "好的";
							choose2 = "不了";
					}
					else if (isSeller) {
						inputmode = 3;
						choose1 = "买";
						choose2 = "卖";
						choose3 = "放弃";
						sellerAsk = true;
						isSeller = false;
					}					
				}
//				
//				else if((key & GAME_A_PRESSED) != 0){
//					isJump = true;
//				}
				else if ((key & UP_PRESSED) != 0) {
					// System.out.println(key);
					mrole.setFrame(mrole.directionup++);
					if (mrole.directionup > 15)
						mrole.directionup = 12;

					if (mrole.y - 4 <= 0)
						mrole.y = 0;
					else
						mrole.move(0, -4);

					if (screeny - 4 > 0)
						screenmove(0, -4);
					else
						screeny = 0;
				}
				
				else if ((key & DOWN_PRESSED) != 0) {

					mrole.setFrame(mrole.directiondown++);
					if (mrole.directiondown > 3)
						mrole.directiondown = 0;

					if (mrole.y + 24 + 4 >= imgbackgroundheight)
						mrole.y = imgbackgroundheight - 24;
					else
						mrole.move(0, 4);

					if (screeny + 4 + screenheight < imgbackgroundheight)
						screenmove(0, 4);
					else
						screeny = imgbackgroundheight - 3 - 177;
				}
				
				else if ((key & LEFT_PRESSED) != 0) {
					mrole.setFrame(mrole.directionleft++);
					if (mrole.directionleft > 7)
						mrole.directionleft = 4;

					if (mrole.x - 4 <= 0)
						mrole.x = 0;
					else
						mrole.move(-4, 0);

					if (screenx - 4 > 0)
						screenmove(-4, 0);
					else
						screenx = 0;
				}
				
				else if ((key & RIGHT_PRESSED) != 0) {
					mrole.setFrame(mrole.directionright++);
					if (mrole.directionright > 11)
						mrole.directionright = 8;
					if (mrole.x >= imgbackgroundwidth - 16 - 4)
						mrole.x = imgbackgroundwidth - 16;
					else
						mrole.move(4, 0);

					if (screenx + 4 + screenwidth < imgbackgroundwidth)
						screenmove(4, 0);
					else
						screenx = imgbackgroundwidth - 4 - screenwidth;
				}
				// System.out.println(key);
			}
			
			//输入模式为1, 即游戏对话存在两种选择时
			else if (inputmode == 1) {

				if ((key & DOWN_PRESSED) != 0) {
					selecty += 10;
					if (selecty >= 162) {
						selecty = 162;

					}
				}
				else if ((key & UP_PRESSED) != 0) {
					selecty -= 10;
					if (selecty <= 152) {
						selecty = 152;
					}
				}
				else if ((key & FIRE_PRESSED) != 0) {
					inputmode = 0;
					// ismessage = false;
					//选择第一项时
					if (selecty == 152) {
						if (isSoldier) {   //与野怪接触时
							isAttack = true;
							isChoose = false;
							warEnd = true;
							tempNum = getrand(3);
							removeCommand(mainMenu);
						} else if (isBoss) {   //与boss接触
							isBossAttack = true;
							isChoose = false;
							isBoss = false;
							warEnd = false;
							tempNum = getrand(4);
							removeCommand(mainMenu);
						}
						else if(isSelectAttack){
//							isSelectAttack = false;
							isChoose = false;
							isJump = true;
							attackWidth = 70;
							attackHeight = 64;
						}
						else if (forSell) {  //与商人接触 ,选择卖商品时
							if (mroleProperty[1] > 0) {
								forSell = false;
								isSell = false;
								inputmode = 0;
								mroleProperty[4]++;
								mroleProperty[1]--;
							} else
								inputmode = 1;
						} else {    //与老人接触
							mainroleanswer = true;
							missionmessage = "帮助老人找他的猫";
							showyesorno = false;
						}
					}
					//选择第二项时
					else if (selecty == 162) {
						if (isSoldier) {     //与野怪接触
							isAttack = false;
							isChoose = false;
						} else if (isBoss) {   //与boss接触
							isBossAttack = false;
							isChoose = false;
							isBoss = false;
						}
						else if(isSelectAttack){
							isSelectAttack = false;
							isChoose = false;
							roleAttack = true;
							inputmode = 3;
						}
						else if (forSell) {   //选择卖商品时
							forSell = false;
							isSell = false;
							inputmode = 0;
						} else {            //与老人接触
							mainroleanswer = false;
							showyesorno = false;
						}
//						inputmode = 0;
					}
				}
				// showyesorno = false;
			}
			
			//输入模式为2时,即游戏进入场景2的状态控制
			else if (inputmode == 2) {
				if ((key & UP_PRESSED) != 0) {
					mrole.setFrame(mrole.directionup++);
					if (mrole.directionup > 15)
						mrole.directionup = 12;
					if (mrole.roomy - 4 <= 0)
						mrole.roomy = 0;
					else
						mrole.moveroom(0, -4);
				}

				if ((key & DOWN_PRESSED) != 0) {
					mrole.setFrame(mrole.directiondown++);
					if (mrole.directiondown > 3)
						mrole.directiondown = 0;
					if (mrole.roomy + 24 + 4 >= 177) {
						mrole.roomy = 177 - 24;
					} else
						mrole.moveroom(0, 4);
				}
				if ((key & LEFT_PRESSED) != 0) {
					mrole.setFrame(mrole.directionleft++);
					if (mrole.directionleft > 7)
						mrole.directionleft = 4;
					if (mrole.roomx - 4 <= 0)
						mrole.roomx = 0;
					else
						mrole.moveroom(-4, 0);
				}
				if ((key & RIGHT_PRESSED) != 0) {
					mrole.setFrame(mrole.directionright++);
					if (mrole.directionright > 11)
						mrole.directionright = 8;
					if (mrole.roomx >= 180 - 16 - 4)
						mrole.roomx = 180 - 16;
					else
						mrole.moveroom(4, 0);
				}
			}
			
			//输入模式为3时,即对话具有三种选择的状态
			if (inputmode == 3) {
				if ((key & DOWN_PRESSED) != 0) {
					attackSelecty += 10;
					if (attackSelecty >= screenheight - 15) {
						attackSelecty = screenheight - 15;

					}
				}
				else if ((key & UP_PRESSED) != 0) {
					attackSelecty -= 10;
					if (attackSelecty <= screenheight - 35) {
						attackSelecty = screenheight - 35;
					}
				}
				else if ((key & FIRE_PRESSED) != 0) {
					//选择第一项时
					// roleWord=" 星星燎燃 烈火溅射 深海狂啸";
					if (attackSelecty == screenheight - 15) {
						if (isRoleAttack) {   //与野怪或boss接触 ,选择第一招
							tempNum = 6;
							attackWidth = 80;
							attackHeight = 80;
							roleWord = "深海狂啸";
							roleBeat = true;
							roleAttack = false;
							isRoleAttack = false;
						} else if (roleChoose) {   //与商人接触
							roleChoose = false;
							sellerAsk = false;
							inputmode = 0;
						} else if (forBuy) {     //选择买商品时
							forBuy = false;
							isBuy = false;
							inputmode = 0;
						}
					}
					//选择第二项时
					else if (attackSelecty == screenheight - 25) {
						if (isRoleAttack) {   //与野怪或boss接触 ,选择第二招
							tempNum = 5;
							attackWidth = 80;
							attackHeight = 64;
							roleWord = "烈火溅射";
							roleBeat = true;
							roleAttack = false;
							isRoleAttack = false;
						} else if (roleChoose) {    //与商人接触
							roleChoose = false;
							sellerAsk = false;
							inputmode = 1;
							isSell = true;
							choose1 = "换钱:宝物-》黄金";
							choose2 = "放弃";
						} else if (forBuy) {      //选择买商品时
							if (mroleProperty[4] > 0) {
								forBuy = false;
								isBuy = false;
								inputmode = 0;
								mroleProperty[4]--;
								mroleProperty[1]++;
							} else
								inputmode = 3;    //当黄金数为0时,交易失败
						}
					}
					//选择第三项时
					else if (attackSelecty == screenheight - 35) {
						if (isRoleAttack) {    //与野怪或boss接触 ,选择第三招
							tempNum = 4;
							attackWidth = 48;
							attackHeight = 48;
							roleWord = "星星燎燃";
							roleBeat = true;
							roleAttack = false;
							isRoleAttack = false;
						} else if (roleChoose) {   //与商人接触
							roleChoose = false;
							sellerAsk = false;
							inputmode = 3;
							isBuy = true;
							choose1 = "补血:黄金-》血气";
							choose2 = "加功:黄金-》宝物";
							choose3 = "放弃";
						} else if (forBuy) {    //选择买商品时
							if (mroleProperty[4] > 0) {
								forBuy = false;
								isBuy = false;
								inputmode = 0;
								mroleProperty[4]--;
								roleBloodHeight += 3;
								if (roleBloodHeight >= 40)
									roleBloodHeight = 40;
							} else
								inputmode = 3;    //当黄金数为0时,交易失败
						}

					}

				}
			}
		}
		}


	public void commandAction(Command com, Displayable displayable) {
		//选择退出游戏
		if (com == exitGame) {
			this.midlet.exitMIDlet();
		}
		//游戏菜单
		else if (com == mainMenu) {
			removeCommand(mainMenu);
			addCommand(backGame);
			gameState = GAME_MENU;
		}
		//返回菜单
		else if (com == backMenu) {
			removeCommand(backMenu);
			if(!isFirstStart)addCommand(backGame);
			if (gameState == GAME_ABOUT)
				gameState = GAME_MENU;
		}
		//返回游戏
		else if (com == backGame) {
			if (gameState == GAME_MENU) {
				gameState = GAME_START;
				removeCommand(backGame);
				addCommand(mainMenu);
			}
		}
	}
}

⌨️ 快捷键说明

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