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

📄 mygamecanvas.java

📁 J2ME开发的手机游戏需要安装诺基亚手机模拟器才能正常运行
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package org.gamecollege.j2me.rpg;

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.midlet.*;

import java.util.*;

/**
 * @author Jagie
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class MyGameCanvas extends GameCanvas implements CommandListener,
		Runnable {

	/**
	 * 游戏MIDlet
	 */
	MIDlet midlet;

	/**
	 * 游戏运行开关
	 */
	boolean isRunning;

	/**
	 * 消息队列
	 */
	Vector messageQueue;

	public static final int GAME_LOOP_INTERVAL = 100;

	MyRPGGameMIDlet myRPGGameMIDlet;

	public static final int STATUS_SHOWFLASH = 0;//显示flash

	public static final int STATUS_POPMENU_CONFIRM = 1;//确认买卖

	public static final int STATUS_BUYING = 11;//购买道具

	public static final int STATUS_WALKING = 2;//主角行走

	public static final int STATUS_FIGHTING = 3;////战斗

	public static final int STATUS_MAIN_MENU = 4;//主菜单

	public static final int STATUS_HELP = 5;//帮助

	public static final int STATUS_ABOUT = 6;//关于

	public static final int STATUS_PLAYER_INFO = 8;//主角属性

	public static final int STATUS_MISSION_INFO = 9;//任务列表

	public static final int STATUS_GAME_OVER = 10;//游戏结束

	public static final int STATUS_LOADING_LEVEL = 12;//装载关卡

	public static final int STATUS_SALING = 13;//卖东西

	public static final int STATUS_FIGHT_OVER = 14;//战斗结束

	public static final int STATUS_MISSION_OVER = 15;//任务结束

	private int gameStatus = STATUS_SHOWFLASH;

	//屏幕宽度,高度
	int width, height;

	//主角
	Player myPlayer;

	LayerManager lm;

	//当前关卡号
	int curLevelNo;

	//1:买卖武器护具,2:药品买卖
	int saleType;

	//当前菜单项索引
	int curMenuIndex;

	//当前关卡
	Level curLevel;

	//可出售的武器集合
	Vector weaponVec = new Vector();

	//可出售的护具集合
	Vector jacketVec = new Vector();

	//可出售的药品集合
	Vector medicVec = new Vector();

	//购买武器护具时,区别到底是武器还是护具
	boolean isArm;

	LayerManager fightLayerManager;

	GameInnerThread engine;

	String curMessage;

	Thread gameThread;

	long lastPopMsgTime;

	int beforeFightHp;

	int beforeFightExp;

	int beforeFightMp;

	int beforeFightMoney;

	int beforeFightHp_npc;

	//是否在战斗时查看技能
	boolean popSkill;

	long lastFightOverTime;

	long lastGameOverTime;

	//在战斗中随机出现的敌人
	NPC ranEnemy;

	//战斗结束后获得的技能
	Skill ranSkill;

	Sprite fightBackGround;

	int curMenuIndex2;

	int killTigerCount;

	GameStore gs;

	boolean hasStored;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.gamecollege.j2me.rpg.RPGGameCanvas#drawMessage(javax.microedition.lcdui.Graphics)
	 */

	public void commandAction(Command com, Displayable disp) {
		if (com == List.SELECT_COMMAND) {

			gs.deleteRecord();
			myPlayer = new Player(ResourceLoader.StringResource[0]);
			List list = (List) disp;
			curLevelNo = list.getSelectedIndex();
			curLevel = new Level();
			LevelLoader.instance.loadLevel(curLevel);

			Display.getDisplay(myRPGGameMIDlet).setCurrent(this);
		} else if (com.getCommandType() == Command.BACK) {
			this.setGameStatus(STATUS_MAIN_MENU);
			Display.getDisplay(myRPGGameMIDlet).setCurrent(this);
		}
	}

	public void showLevels() {
		List list = new List(ResourceLoader.StringResource[25], Choice.IMPLICIT);
		list.append(ResourceLoader.StringResource[24], null);
		list.addCommand(new Command(ResourceLoader.StringResource[34],
				Command.BACK, 1));
		list.setCommandListener(this);

		Display.getDisplay(myRPGGameMIDlet).setCurrent(list);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.gamecollege.j2me.rpg.RPGGameCanvas#stopGame()
	 */
	public void stopGame() {
		System.gc();
		// TODO Auto-generated method stub
		engine.stop();
		//this.isRunning=false;

	}

	protected void drawMessage() {
		Graphics g = this.getGraphics();
		if (curMessage != null) {
			g.setColor(255, 255, 128);

			g.drawImage(ResourceLoader.menu[17], width / 2, height - 20,
					Graphics.HCENTER | Graphics.TOP);
			g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
					Font.SIZE_SMALL));

			g.drawString(curMessage, width / 2, height - 18, Graphics.HCENTER
					| Graphics.TOP);

		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.gamecollege.j2me.rpg.RPGGameCanvas#input()
	 */
	protected void input() {

		int keyStates = getKeyStates();

		switch (getGameStatus()) {
		//主菜单状态
		case STATUS_MAIN_MENU:
			if (gs.canLoad()) {
				handleMenuKeyBrowse(keyStates, 4);
			} else {
				handleMenuKeyBrowse(keyStates, 3);
			}

			if ((keyStates & FIRE_PRESSED) != 0) {

				if (hasStored) {

					switch (curMenuIndex) {
					case 0:
						showLevels();
						break;
					case 1:
						loadGame();
						break;
					case 2:
						setGameStatus(STATUS_ABOUT);
						break;
					case 3:
						setGameStatus(STATUS_HELP);
						break;
					case 4:
						MyRPGGameMIDlet.midlet.notifyDestroyed();
						break;
					}
				} else {
					switch (curMenuIndex) {
					case 0:
						showLevels();
						break;
					case 1:
						setGameStatus(STATUS_ABOUT);
						break;
					case 2:
						setGameStatus(STATUS_HELP);
						break;
					case 3:
						MyRPGGameMIDlet.midlet.notifyDestroyed();
						break;
					}
				}
			}
			break;
			//帮助界面
		case STATUS_HELP:
			//关于界面
		case STATUS_ABOUT:
			if (keyStates != 0) {
				setGameStatus(STATUS_MAIN_MENU);
			}
			break;
			//行走界面
		case STATUS_WALKING:
			//按着左键
			if ((keyStates & LEFT_PRESSED) != 0) {
				myPlayer.move(Player.LEFT);
			}
			//按着右键
			else if ((keyStates & RIGHT_PRESSED) != 0) {
				myPlayer.move(Player.RIGHT);
			}
			//按着上键
			else if ((keyStates & UP_PRESSED) != 0) {
				myPlayer.move(Player.UP);
			}
			//按着下键
			else if ((keyStates & DOWN_PRESSED) != 0) {
				myPlayer.move(Player.DOWN);
			} else {
				//未按方向键
				myPlayer.move(Player.STILL);
			}
			break;
			//确认界面
		case STATUS_POPMENU_CONFIRM:
			handleMenuKeyBrowse(keyStates, 2);
			if ((keyStates & FIRE_PRESSED) != 0) {
				switch (curMenuIndex) {
				case 0:

					setGameStatus(STATUS_BUYING);
					break;
				case 1:

					setGameStatus(STATUS_SALING);

					break;
				case 2:
					setGameStatus(STATUS_WALKING);
					break;

				}

			}
			break;
			//购买物品界面
		case STATUS_BUYING:
			int maxMenuIndex = 0;
			Property pro = null;
			switch (this.saleType) {
			//武器护具
			case 1:
				//注意多了一个[退出]菜单
				if (this.isArm) {
					maxMenuIndex = this.weaponVec.size();
					if (curMenuIndex < weaponVec.size()) {
						pro = (Property) weaponVec.elementAt(curMenuIndex);
					}
				} else {
					maxMenuIndex = this.jacketVec.size();
					if (curMenuIndex < jacketVec.size()) {
						pro = (Property) jacketVec.elementAt(curMenuIndex);
					}
				}
				break;
			//药品
			case 2:
				if (curMenuIndex < medicVec.size()) {
					pro = (Property) medicVec.elementAt(curMenuIndex);
				}
				maxMenuIndex = this.medicVec.size();
				break;
			}
			handleMenuKeyBrowse(keyStates, maxMenuIndex);
			if ((keyStates & LEFT_PRESSED) != 0
					|| (keyStates & RIGHT_PRESSED) != 0) {
				isArm = !isArm;
			} else if ((keyStates & FIRE_PRESSED) != 0) {
				if (pro != null) {
					if (pro.price <= myPlayer.money) {
						myPlayer.addSth(pro);
						myPlayer.money -= pro.price;
					}
				} else {
					setGameStatus(STATUS_POPMENU_CONFIRM);
				}
			}
			break;
			//出售物品界面
		case STATUS_SALING:
			Vector ps = null;
			if (this.saleType == 1) {
				if (isArm) {
					ps = myPlayer.weaponVec;
				} else {
					ps = myPlayer.jacketVec;
				}

			} else {
				ps = myPlayer.medicVec;
			}
			maxMenuIndex = ps.size();
			handleMenuKeyBrowse(keyStates, maxMenuIndex);
			if ((keyStates & LEFT_PRESSED) != 0
					|| (keyStates & RIGHT_PRESSED) != 0) {
				isArm = !isArm;
			} else if ((keyStates & FIRE_PRESSED) != 0) {
				if (curMenuIndex < ps.size()) {
					pro = (Property) ps.elementAt(curMenuIndex);
					myPlayer.removeSth(pro);
					//卖东西,只能得到80%的收益
					myPlayer.money += pro.price * 8 / 10;
				} else {
					setGameStatus(STATUS_POPMENU_CONFIRM);
				}

			}
			break;
			//战斗界面
		case STATUS_FIGHTING:
			if ((keyStates & LEFT_PRESSED) != 0) {
				popSkill = false;
				curMenuIndex2 = 0;
				curMenuIndex = curMenuIndex - 1 >= 0 ? curMenuIndex - 1 : 2;

			} else if ((keyStates & RIGHT_PRESSED) != 0) {
				popSkill = false;
				curMenuIndex2 = 0;
				curMenuIndex = curMenuIndex + 1 > 2 ? 0 : curMenuIndex + 1;
			} else if ((keyStates & FIRE_PRESSED) != 0) {
				if (!popSkill) {
					switch (curMenuIndex) {
					case 0:
						if (myPlayer.attackStatus == -1) {
							myPlayer.attackStatus = 0;
						}

						break;
					case 1:
						//显示技能列表
						this.popSkill = true;
						break;

					case 2:
						//退出战斗
						synchronized (engine) {
							this.setGameStatus(STATUS_WALKING);
							this.ranEnemy = null;
							myPlayer.attackStatus = -1;
						}
						break;
					}
				} else {
					popSkill = false;
					if (myPlayer.skillVec.size() > 0) {
						fireSkill(curMenuIndex2);
					}

				}

			} else {
				this.handleMenuKeyBrowse2(keyStates,
						myPlayer.skillVec.size() - 1);
			}
			break;
			//主角属性界面
		case STATUS_PLAYER_INFO:
			Vector v = this.weaponVec;
			switch (curMenuIndex) {
			case 1:
				v = myPlayer.weaponVec;
				break;
			case 2:
				v = myPlayer.medicVec;
				break;
			case 3:
				v = myPlayer.jacketVec;
				break;
			case 4:
				v = myPlayer.skillVec;
				break;

			}
			if ((keyStates & LEFT_PRESSED) != 0) {
				curMenuIndex = curMenuIndex - 1 >= 0 ? curMenuIndex - 1 : 4;
			} else if ((keyStates & RIGHT_PRESSED) != 0) {
				curMenuIndex = curMenuIndex + 1 > 4 ? 0 : curMenuIndex + 1;
			} else if ((keyStates & FIRE_PRESSED) != 0) {
				if (curMenuIndex == 0) {
					this.setGameStatus(STATUS_WALKING);
				} else {
					if (curMenuIndex2 < v.size() && curMenuIndex != 4) {
						RPGObject ro = (RPGObject) v.elementAt(curMenuIndex2);
						myPlayer.useObject(ro);
					} else {
						this.setGameStatus(STATUS_WALKING);
					}
				}
			} else {

				handleMenuKeyBrowse2(keyStates, v.size());
			}
			break;
		}

	}

	private void fireSkill(int index) {
		Skill sk = (Skill) myPlayer.skillVec.elementAt(index);
		if (sk.requiredLevel <= myPlayer.rank && sk.hpCost <= myPlayer.hp
				&& sk.mpCost <= myPlayer.mp) {
			myPlayer.fireSkill = sk;
			myPlayer.setAttackStatusSprite(1979);
		}

	}

	public void handleGameOver() {
		ranSkill = null;
		this.ranEnemy = null;
		myPlayer.attackStatus = -1;
		myPlayer.hp = 10;
		saveGame();
		engine.stop();
		lastGameOverTime = System.currentTimeMillis();
		this.setGameStatus(STATUS_GAME_OVER);
	}

	/**
	 * 菜单上下遍历的通用方法
	 * @param keyStates 按键状态
	 * @param maxMenuIndex 最大的菜单索引
	 */
	private void handleMenuKeyBrowse(int keyStates, int maxMenuIndex) {

		if ((keyStates & DOWN_PRESSED) != 0) {
			curMenuIndex = curMenuIndex + 1 > maxMenuIndex ? 0
					: curMenuIndex + 1;
		}

		if ((keyStates & UP_PRESSED) != 0) {
			curMenuIndex = curMenuIndex - 1 < 0 ? maxMenuIndex
					: curMenuIndex - 1;
		}
	}

	private void handleMenuKeyBrowse2(int keyStates, int maxMenuIndex) {

		if ((keyStates & DOWN_PRESSED) != 0) {
			curMenuIndex2 = curMenuIndex2 + 1 > maxMenuIndex ? 0
					: curMenuIndex2 + 1;
		}

		if ((keyStates & UP_PRESSED) != 0) {
			curMenuIndex2 = curMenuIndex2 - 1 < 0 ? maxMenuIndex
					: curMenuIndex2 - 1;
		}
	}

	public void startGame() {
		this.isRunning = true;
		Thread t = new Thread(this);
		this.gameThread = t;
		
		t.start();

	}

	public void handleRanEnemy() {
		if (this.ranEnemy != null) {
			this.constructsFightMap();
			this.setGameStatus(STATUS_FIGHTING);
			//System.out.println("handleRanEnemy over");
			//engine.ranEnemy = null;
		}
	}

	public void handleFightOver() {
		if (ranEnemy.ID == 3) {
			this.killTigerCount++;
		}
		gameStatus = STATUS_FIGHT_OVER;
		lastFightOverTime = System.currentTimeMillis();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.gamecollege.j2me.rpg.RPGGameCanvas#render(javax.microedition.lcdui.Graphics)
	 */
	protected void render() {
		Graphics g = this.getGraphics();
		g.setColor(255, 255, 255);
		g.fillRect(0, 0, width, height);
		g.setColor(0, 0, 0);

		switch (getGameStatus()) {

		case STATUS_WALKING:

			lm.paint(g, 0, 0);

⌨️ 快捷键说明

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