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

📄 game.java

📁 应用j2me技术开发的手机游戏
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
import java.io.IOException;
import java.util.Random;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;

/**
 * 
 * 
 * 本类处理游戏逻辑绘制等工作,是完成游戏进程的主要类
 * 
 * @author lin
 * 
 */
public class Game extends GameCanvas implements Runnable {

	private static TiledLayer magicbalk, magicroad, bossbalk, bossroad,
			princeXanaduRoad, princeXanaduBalk, plainRoad, plainBalk;// 障碍物和可通行物;定义成静态是因为一次只有一个游戏对象在运行,不允许两个以上的共存;所以变量就可以认为成是静态的;这个认识是错误的,因为在读档时又新建了对象,它的状态不应该是和上一个一样的如果读不同的挡,
	private Graphics g;// 脱机屏幕画笔;
	private MainMidlet mid;// 主程序的一个引用;
	private LayerManager l, lboss, lprince, lplain;// 图层管理器;l大地图的,lboss为boss地图;
	public Hero lin; // 主角;
	private ListBox lbselect[] = new ListBox[10];// 对话中的选择框;
	private ListBox lbSystem = new ListBox();// 游戏进行状态时弹出的系统对话框;
	private ListBox lbSystemout = new ListBox();// 系统退出的菜单;
	private int k = 0;// 控制游戏开始场景的对话;
	private boolean pagedown = true;// 游戏中对话翻页;
	private boolean selected = false;// 游戏中对话是否进入选择阶段;
	private boolean music = true;
	private int viewX, viewY;// 当前视窗移动的像素;
	public int GAMESTATE = 1;// 初始为游戏开始的状态;
	private int count;// 步数控制主角帧转换;
	public static int SCREENWIDTH; // 屏幕宽度
	public static int SCREENHEIGHT;// 屏幕高度;

	private boolean brun = true; // 控制游戏运行;
	private long ts1, ts2;// 控制游戏中按键延迟;
	private int i = 0;// 控制碰撞的图层;

	protected Game(MainMidlet mid) {
		super(true);
		this.mid = mid;
		g = getGraphics();
		SCREENWIDTH = getWidth();
		SCREENHEIGHT = getHeight();

		try {

			lin = new Hero();
			lin.defineCollisionRectangle(4, 22, 8, 8);// 定义碰撞区域为脚步;
			l = new LayerManager();
			lboss = new LayerManager();
			lprince = new LayerManager();
			lplain = new LayerManager();
			try {

				magicbalk = new TiledLayer(32, 32, Image.createImage("/h.png"),
						16, 16);
				magicroad = new TiledLayer(32, 32, Image.createImage("/h.png"),
						16, 16);
				bossbalk = new TiledLayer(10, 10, Image
						.createImage("/map0.png"), 24, 24);
				bossroad = new TiledLayer(10, 10, Image
						.createImage("/map0.png"), 24, 24);
				princeXanaduRoad = new TiledLayer(20, 20, Image
						.createImage("/prince.png"), 16, 16);

				princeXanaduBalk = new TiledLayer(20, 20, Image
						.createImage("/prince.png"), 16, 16);
				plainRoad = new TiledLayer(30, 20, Image
						.createImage("/fenghuoxiagu.png"), 24, 16);
				plainBalk = new TiledLayer(30, 20, Image
						.createImage("/fenghuoxiagu.png"), 24, 16);
				;

			} catch (IOException e) {

				e.printStackTrace();
			}

			drawMap(Const.MagicMaproad, Const.MagicMapbalk, magicroad,
					magicbalk);

			drawMap(Const.plainRoad, Const.plainBalk, plainRoad, plainBalk);
			for (int i = 0; i < lbselect.length; i++) {
				lbselect[i] = new ListBox();
				lbselect[i].setLineSpace(10);
				lbselect[i].setBackground(false);
				lbselect[i].setFontSize(Font.SIZE_LARGE);
				lbselect[i].setBarColor(0xff5678, true);
				lbselect[i].setBackground(true);
			}
			lbselect[0].addItem("一:永生");
			lbselect[0].setBorder(true);
			lbselect[0].addItem("二:我太累了,让我解脱吧");

			lbselect[1].addItem("一:失去眼睛……");
			lbselect[1].addItem("二:直觉告诉我这个……");
			lbselect[1].addItem("三:选这个的话就有些 ……");
			lbselect[1].setBorder(true);

			lbSystem.addItem("保存进度");// 扩展列表框的应用;
			lbSystem.addItem("读取进度");//
			lbSystem.addItem("系统选项");
			lbSystem.addItem("主角菜单");
			lbSystem.addItem("任务提示");

			lbSystem.setLineSpace(20);
			lbSystem.setBackground(false);
			lbSystem.setFontSize(Font.SIZE_LARGE);
			lbSystem.setBarColor(0xff5678, true);
			lbSystem.setBackground(true);

			lbSystemout.addItem("音乐开");
			lbSystemout.addItem("音乐关");
			lbSystemout.addItem("返回初始界面");

			lbSystemout.setLineSpace(20);
			lbSystemout.setBackground(false);
			lbSystemout.setFontSize(Font.SIZE_LARGE);
			lbSystemout.setBarColor(0xff5678, true);
			lbSystemout.setBackground(true);

			npcSchoolMaster = new Npc(loadImage("/character/charger.png"), 0,
					position);

			npcSchoolMaster.npcName = "希尔法学院院长";
			this.npcFeng = new Npc(loadImage("/character/feng.png"), 0,
					position1);
			npcFeng.npcName = "雪华*枫";
			int npcPrincePosition[] = { 8 * 16, 3 * 16 };
			npcPrince = new Npc(loadImage("/character/prince.png"), 2,
					npcPrincePosition, 2000, 150, 50, 2);

			npcPrince.npcName = "谢尔德王子 ";
			npcPrince.exp = 2000;
			npcPrince.station = 30;
			npcPrince.gold = 2000;
			npcPrincePosition[0] = 4 * 24;
			npcPrincePosition[1] = 8 * 16;

			npcEnemyGuard = new Npc(loadImage("/character/weibin.png"), 2,
					npcPrincePosition, 2000, 35, 20, 0);// 怪物卫兵;

			npcEnemyGuard.npcName = "王国卫兵";
			npcEnemyGuard.exp = 200;
			npcEnemyGuard.station = 8;
			npcEnemyGuard.gold = 300;

			int w[] = { 16 * 24, 3 * 16 };
			npcWendini = new Npc(loadImage("/character/souren.png"), 2, w,
					5000, 100, 30, 1);
			npcWendini.npcName = "兽人";
			npcWendini.station = 15;
			npcWendini.exp = 500;
			npcWendini.gold = 1000;
			w = null;
			int b[] = { 0, 3 * 16 };
			int a[] = { 19 * 24, 17 * 16 };
			this.npcBox = new Npc(loadImage("/item/baoxiang.png"), 1, b);
			this.npcBox2 = new Npc(loadImage("/item/baoxiang.png"), 1, a);
			npcBox.station = 15;
			npcBox2.station = 25;
			a = null;
			b = null;
			imSchoolMaster = loadImage("/character/charger.png");
			imPrince = loadImage("/character/prince.png");
			chara = Image.createImage("/zhuangtaiww.png");
			ind = Image.createImage("/fangxiang1.png");
			charaBuff = Image.createImage(chara.getWidth(), chara.getHeight());
			charBuffg = charaBuff.getGraphics();

			// 物品栏
			for (int i = 0; i < cmdr.length; i++) {
				for (int j = 0; j < cmdr[i].length; j++)
					cmdr[i][j] = -1;// 所有的物品栏均无物品;
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	int position1[] = { 4 * 16, 4 * 16 };
	int position[] = { 15 * 16, 1 * 16 };

	void start() {

		if (GAMESTATE == Const.GAME_START) // 游戏状态切换;
			GAMESTATE = Const.GAME_DIALOG;
		new Thread(this).start(); //

	}

	void setView(LayerManager l, int i) {

		lin.setPosition(lin.hx, lin.hy);
		// 设置主角坐标,以及初始是窗;
		viewX = lin.hx - SCREENWIDTH / 2;
		viewY = lin.hy - SCREENHEIGHT / 2;
		if (viewX <= 0)
			viewX = 0;
		if (viewY <= 0)
			viewY = 0;
		if (viewX + SCREENWIDTH > l.getLayerAt(i).getWidth())
			viewX = l.getLayerAt(i).getWidth() - SCREENWIDTH;

		if (viewY + SCREENHEIGHT > l.getLayerAt(i).getHeight())
			viewY = l.getLayerAt(i).getHeight() - SCREENHEIGHT;

		l.setViewWindow(viewX, viewY, SCREENWIDTH, SCREENHEIGHT);
	}

	public void run() {

		try {
			while (brun) {

				long time = System.currentTimeMillis();
				input();
				logic();
				render();
				try {

					if ((System.currentTimeMillis() - time) < 40)
						Thread.sleep(40);
					if (GAMESTATE != Const.GAME_RUNNING)
						Thread.sleep(100);
				} catch (InterruptedException e) {

					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();

		}
	}

	/**
	 * 逻辑处理,包括状态的转换;
	 */
	void logic()

	{
		if (GAMESTATE != Const.GAME_FIGHT) {

			if (!fightInput)
				if (music)
					mid.s.playSound();
				else
					mid.s.stopSound();
		}

		if (GAMESTATE == Const.GAME_DIALOG) {// 根据主角坐标决定对话选择 ;
			dialogSelect();
		}

		if (GAMESTATE == Const.GAME_RUNNING) {
			mid.sfight.stopSound();
			switch (lin.zmap) {// 根据主角所在大地图来加载不同的图层管理器;
			case 0:
				// 可通行区 ,不可通行区 ,精灵;
				if ((lin.firstLoad || lin.load) && lin.task[0] != 1) {

					l.append(npcSchoolMaster);
					l.append(lin);
					l.append(magicbalk);
					l.append(magicroad);
					this.setView(l, 2);
					npcSchoolMaster.setPosition(npcSchoolMaster.position[0],
							npcSchoolMaster.position[1]);
					lin.firstLoad = false;
					lin.load = false;
					i = 2;

				}

				if ((lin.secondLoad || lin.load) && lin.task[0] == 1) {

					if (!lin.load && l.getSize() > 0) {

						for (int i = 0; i < l.getSize(); i++)
							l.remove(l.getLayerAt(i));

					}

					l.append(npcSchoolMaster);
					l.append(npcFeng);
					l.append(lin);
					l.append(magicbalk);
					l.append(magicroad);

					this.setView(l, 3);
					npcSchoolMaster.setPosition(npcSchoolMaster.position[0],
							npcSchoolMaster.position[1]);
					this.npcFeng.setPosition(position1[0], position1[1]);
					i = 3;

					lin.secondLoad = false;
					lin.load = false;
				}
				gameRunning(l, i);
				break;
			case 1:

				if (lin.firstLoad) {
					lboss.append(lin);
					lboss.append(bossbalk);
					lboss.append(bossroad);
					this.setView(lboss, 1);
					lin.firstLoad = false;
				}
				gameRunning(lboss, 1);
				break;

			case 2:
				if ((lin.firstLoad && lin.task[0] == 0) || lin.load) {
					drawMap(Const.princeXanaduRoad, Const.princeXanaduBalk,
							princeXanaduRoad, princeXanaduBalk);

					lin.load = false;

					if (lprince.getSize() > 0) {
						for (int i = 0; i < lprince.getSize(); i++)
							lprince.remove(lprince.getLayerAt(i));

					}
					lprince.append(lin);
					lprince.append(this.npcPrince);
					lprince.append(princeXanaduBalk);
					lprince.append(princeXanaduRoad);

					npcPrince.setPosition(this.npcPrince.position[0],
							this.npcPrince.position[1]);
					this.setView(lprince, 2);
					lin.firstLoad = false;//
				}
				gameRunning(lprince, 2);

				break;

			case 3:
				if ((lin.firstLoad || lin.load) && lin.task[2] == 0)

				{
					if (lplain.getSize() > 0) {
						for (int i = 0; i < lplain.getSize(); i++)
							lplain.remove(lplain.getLayerAt(i));

					}

					lplain.append(lin);
					lplain.append(this.npcEnemyGuard);
					lplain.append(this.npcWendini);
					lplain.append(this.npcBox);
					lplain.append(this.npcBox2);
					lplain.append(plainBalk);
					lplain.append(plainRoad);
					this.npcWendini.cHp = this.npcWendini.maxHp;
					this.npcEnemyGuard.cHp = this.npcEnemyGuard.maxHp;
					this.npcEnemyGuard.setVisible(true);
					this.npcWendini.setVisible(true);
					this.npcBox.setVisible(true);
					this.npcBox2.setVisible(true);
					lin.firstLoad = false;
					lin.load = false;
					this.npcEnemyGuard.setPosition(
							this.npcEnemyGuard.position[0],
							this.npcEnemyGuard.position[1]);

					this.npcBox.setPosition(npcBox.position[0],
							npcBox.position[1]);

					this.npcBox2.setPosition(npcBox2.position[0],
							npcBox2.position[1]);
					this.npcWendini.setPosition(this.npcWendini.position[0],
							this.npcWendini.position[1]);
					this.setView(lplain, 5);

				}

				gameRunning(lplain, 5);

				break;

			}
		}

		if (GAMESTATE == Const.GAME_SYSTEM) {
			drawSystem();
		}

		if (GAMESTATE == Const.GAME_SAVE) {
			drawSave();
		}

		if (GAMESTATE == Const.GAME_LOAD) {
			drawLoad();
		}

		if (GAMESTATE == Const.GAME_SYSTEM_TOOLS) {
			drawSystemout();
		}

		if (GAMESTATE == Const.GAME_CHARCTERSTATUE) {

			drawCharcter();
		}

		if (GAMESTATE == Const.GAME_TASK) {
			drawTask();
		}
		if (GAMESTATE == Const.GAME_DIALOGTASK) {
			drawDialogTask();
		}

		if (GAMESTATE == Const.GAME_FIGHT) {

			mid.s.stopSound();
			if (music)
				mid.sfight.playSound();
			else
				mid.sfight.stopSound();
			drawFight();

		}
		if (GAMESTATE == Const.GAME_SELLORBUY) {
			this.drawSellOrBuy();

		}

	}

	/**************************************************************************
	 * 游戏界面的按键处理 包括:游戏运行时按键处理; 游戏对话时的按键处理; 等等不同的游戏进行状态下,分别进行控制变量下的按键处理;
	 **************************************************************************/

	void input() {
		lin.hdx = 0;
		lin.hdy = 0;
		int key = this.getKeyStates();

		if (key != 0) {
			bSell = false;
			switch (GAMESTATE) {

			case Const.GAME_RUNNING:
				runInput(key);
				break;

			case Const.GAME_SYSTEM:
				systemInput(key);
				break;
			case Const.GAME_DIALOG:
				dialogInput(key);
				break;
			case Const.GAME_SAVE:
				saveInput(key);
				break;
			case Const.GAME_LOAD:
				loadInput(key);
				break;
			case Const.GAME_SYSTEM_TOOLS:
				SystemoutInput(key);
				break;
			case Const.GAME_CHARCTERSTATUE:
				charcterInput(key);
				break;
			case Const.GAME_TASK:
				taskInput(key);
				break;
			case Const.GAME_DIALOGTASK:
				dialogTaskInput(key);
				break;

			case Const.GAME_FIGHT:
				fightInput(key);
				break;

			case Const.GAME_SELLORBUY:
				this.SellOrBuyInput(key);
				break;

			}
		}
	}

	void render() {
		g.setClip(0, 0, SCREENWIDTH, SCREENHEIGHT);// 处理剪裁区域;
		flushGraphics();
	}

	/**
	 * @param key
	 *            游戏响应的按键;
	 */
	void runInput(int key) {
		switch (key) {
		case DOWN_PRESSED:
			lin.setFrame(lin.framedown[(count++ % 3)]);
			if (count >= 3)
				count = 0;
			lin.hdy = 5;
			break;

		case UP_PRESSED:
			lin.setFrame(lin.frameup[(count++ % 3)]);
			if (count >= 3)
				count = 0;
			lin.hdy = -5;
			break;

		case LEFT_PRESSED:
			lin.setFrame(lin.frameleft[(count++ % 3)]);
			if (count >= 3)
				count = 0;
			lin.hdx = -5;
			break;

⌨️ 快捷键说明

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