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

📄 gameview.java

📁 我用WTK不能运行
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package com.xiaoyu.rpggame;

import javax.microedition.lcdui.*;
import java.io.*;
import java.util.*;
import javax.microedition.rms.*;
import javax.microedition.lcdui.game.Sprite;


public class GameView extends Canvas implements Runnable {

	// ========地图数据=========
	// 地图数据
	byte mapData[][];

	// 地图块尺寸
	final static byte MAP_TILE_SIZE = 24;

	// 地图的最大长和宽
	int maxWidth, maxHeight;

	// 当前地图编号
	byte mapID = 0;

	byte mapTrigger[];// 地图切换点

	boolean loadOver;// 当前读取是否完毕

	// ========END=========
	// ========图片数据=========
	Image imgMap = null;
	Image bgMap = null;

	Image imgLogoBack = null;

	Image imgLogoTitle = null;

	Image gameOver = null;
	int logoWidth, logoHeight;
	// ========END=========
	// ========文字========
	Font f;

	int fontWidth, fontHeight;

	int fontMargin = 4;// 文字间隔

	// ========END=========
	int curIndex;// 当前得菜单选项

	// 当前的按键
	int curKey;

	// 人物对象
	Hero player;
	int exp;
	
	//对话框的位置
	int dx,dy, wy;
	
	// 怪物数组
	Vector enemys;

	// 当前屏幕状态
	byte curScreenState;

	// 动画播放时间
	byte splashTime = 10;

	byte alertTime = -1;

	// 是否暂停,用于处理中断
	boolean isPause = false;

	int enemyIndex = 0;

	Enemy e = null;
	Sound sound;
	Random r = new Random();
	//记录名称
	String RMS_NAME;
	String[] Dialog;
	static String[] save = {"无记录", "无记录", "无记录"};
	public GameView() {
		// 设置全屏
		this.setFullScreenMode(true);

		// 图片初始化
		try {
			imgLogoBack = Image.createImage("/bg7.jpg");
			imgLogoTitle = Image.createImage("/menu.png");
			bufImage = Image.createImage(Const.SCREEN_WIDTH,
					Const.SCREEN_HEIGHT);
			sound = new Sound("/bg1.mid");
		} catch (IOException e) {
			System.out.println("图片读取失败!请检测地图相关路径是否准确!");
			e.printStackTrace();
		}
		sound.playSound();
		logoWidth = imgLogoTitle.getWidth();
		logoHeight = imgLogoTitle.getHeight();
		// 画笔初始化
		bufGraphics = bufImage.getGraphics();
//		Dialog = Const.PLAYER_DIALOGS[0];
//		 字体初始化
		f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
		fontHeight = f.getHeight();
		fontWidth = f.stringWidth("攻");
		// 地图数据静态初始化
		mapData = Const.MAP_DATA[mapID];
		//地图切换点初始化
		mapTrigger = Const.MAP_TRIGGER[mapID];
		curScreenState = Const.GAME_LOGO;

		RMS_NAME = Const.RMS_NAME;
		dx = 20;
		dy = 120;
		wy = dy + 30;
		Thread t = new Thread(this);
		t.start();

	}

	/**
	 * 读取地图数据
	 */
	public void LoadMapData(byte mapID) {

	}

	public void run() {
		while (true) {
			repaint();
			EventCheck();
			try {
				Thread.sleep(120);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	// 双缓冲
	Image bufImage;

	Graphics bufGraphics;

	// 当前要显示得屏幕位置
	int sx, sy;

	public void DrawMap(Graphics g) {
		// 将地图画在缓冲区
		// 当人物往左上角漫游时sx和sy的值
		sx = player.x - Const.SCREEN_WIDTH / 2 < 0 ? 0 : player.x
				- Const.SCREEN_WIDTH / 2;
		sy = player.y - Const.SCREEN_HEIGHT / 2 < 0 ? 0 : player.y
				- Const.SCREEN_HEIGHT / 2;
		// 当人物往右下角漫游时sx和sy的值
		sx = player.x + Const.SCREEN_WIDTH / 2 + player.playerWidth > maxWidth ? maxWidth
				- Const.SCREEN_WIDTH
				: sx;
		sy = player.y + Const.SCREEN_HEIGHT / 2 + player.playerHeight > maxHeight ? maxHeight
				- Const.SCREEN_HEIGHT
				: sy;
		// 需要绘制的地图的起始索引
		int startIndexX = sx / MAP_TILE_SIZE;
		int startIndexY = sy / MAP_TILE_SIZE;

		// x和y方向的偏移量
		int offSetX = sx % MAP_TILE_SIZE;
		int offSetY = sy % MAP_TILE_SIZE;

		// 备缓中图片当前索引
		int xPos = 0;
		int yPos = 0;
		bufGraphics.setColor(0);
		bufGraphics.fillRect(0, 0, Const.SCREEN_WIDTH, Const.SCREEN_HEIGHT);

		for (int i = startIndexX; i <= startIndexX + Const.SCREEN_WIDTH
				/ MAP_TILE_SIZE + 1; i++) {
			for (int j = startIndexY; j <= startIndexY + Const.SCREEN_HEIGHT
					/ MAP_TILE_SIZE + 1; j++) {
//				System.out.println(i + "   " + j);
				if (i >= mapData.length || j >= mapData[i].length)
					continue;
				drawClipImg(xPos * MAP_TILE_SIZE - offSetX, yPos
						* MAP_TILE_SIZE - offSetY, MAP_TILE_SIZE,
						MAP_TILE_SIZE, (mapData[j][i] - 1) * MAP_TILE_SIZE, 0,
						imgMap, bufGraphics);
				if(i == mapTrigger[0] && j == mapTrigger[1]) {
					bufGraphics.setColor(0xffff00);
					bufGraphics.drawRect(xPos * MAP_TILE_SIZE - offSetX, yPos
						* MAP_TILE_SIZE - offSetY, 20, 20);
				}
				yPos++;
			}
//			bufGraphics.setColor(0xff0000);
//			bufGraphics.drawRect((mapTrigger[0]-startIndexX) * MAP_TILE_SIZE + offSetX, 
//					(mapTrigger[1]-startIndexY) * MAP_TILE_SIZE + offSetY, MAP_TILE_SIZE, MAP_TILE_SIZE);
			yPos = 0;
			xPos++;
		}
		

	}

	// ==========================界面绘制=====================
	// 绘制半透明框
	public void DrawAlphaRect(int x, int y, int width, int height, int color,
			Graphics g) {
		int array[] = new int[height * width];
		for (int i = 0; i < array.length; i++) {
			array[i] = color;
		}
		g.drawRGB(array, 0, width, x, y, width, height, true);
		g.setColor(color);
		g.drawRoundRect(x - 1, y - 1, width + 1, height + 1, 10, 10);
		g.drawRoundRect(x - 2, y - 2, width + 3, height + 3, 10, 10);
	}

	// 绘制物品界面
	public void DrawTools() {
		DrawFightMap(bufGraphics);
		player.drawAnimationFight(bufGraphics);
		for (int i = 0; i < enemys.size(); i++) {
			Enemy e = (Enemy) enemys.elementAt(i);
			e.drawAnimation(bufGraphics);
		}
		String[] toolNames = new String[player.tools.size()];
		int id = ((Tool)player.tools.elementAt(curIndex)).id;
		for(int i=0; i<player.tools.size(); i++) {
			Tool t = (Tool)player.tools.elementAt(i);
			toolNames[i] = t.name + "     " + t.count;
		}
		DrawStringMenu(toolNames, bufGraphics);
		this.DrawIntroduce("功效HP加"+Const.TOOLS_PROPERTICE[id][1]+", MP加"+Const.TOOLS_PROPERTICE[id][2], bufGraphics);
		this.DrawCommand(Const.COMMAND[3], Const.COMMAND[1]);
	}
	
	
	public void DrawSystemTools() {
		DrawMap(bufGraphics);
		player.drawAnimationFight(bufGraphics);
		String[] toolNames = new String[player.tools.size()];
		int id = ((Tool)player.tools.elementAt(curIndex)).id;
		for(int i=0; i<player.tools.size(); i++) {
			Tool t = (Tool)player.tools.elementAt(i);
			toolNames[i] = t.name + "     " + t.count;
		}
		DrawStringMenu(toolNames, bufGraphics);
		this.DrawIntroduce("功效HP加"+Const.TOOLS_PROPERTICE[id][1]+", MP加"+Const.TOOLS_PROPERTICE[id][2], bufGraphics);
		this.DrawCommand(Const.COMMAND[3], Const.COMMAND[1]);
	}

	//绘制魔法菜单
	public void DrawMagicMenu() {
		DrawFightMap(bufGraphics);
		player.drawAnimationFight(bufGraphics);
		for (int i = 0; i < enemys.size(); i++) {
			Enemy e = (Enemy) enemys.elementAt(i);
			e.drawAnimation(bufGraphics);
		}
		DrawStringMenu(player.magic, bufGraphics);
		DrawCommand(Const.COMMAND[3], Const.COMMAND[1]);
	}
	
	
	
	// 绘制暂停界面
	public void DrawPause() {
		DrawMap(bufGraphics);
		player.drawAnimation(bufGraphics);
		this.DrawStringMenu(Const.PAUSE, bufGraphics);
		this.DrawCommand(Const.COMMAND[3], Const.COMMAND[2]);

	}

	//绘制系统界面
	public void DrawSystem() {
		DrawMap(bufGraphics);
		player.drawAnimation(bufGraphics);
		this.DrawStringMenu(Const.SYSTEM, bufGraphics);
		this.DrawCommand(Const.COMMAND[3], Const.COMMAND[2]);
	}
	
	// 绘制游戏界面
	public void DrawScreenScene() {
		DrawMap(bufGraphics);
		player.drawAnimation(bufGraphics);
		this.DrawCommand(Const.COMMAND[4], Const.COMMAND[6]);
	}
	
	// 绘制游戏封面logo
	public void DrawScreenLogo() {
		
		bufGraphics.drawImage(imgLogoBack, 0, 0, 20);
		bufGraphics.drawImage(imgLogoTitle, (Const.SCREEN_WIDTH-logoWidth)/2, Const.SCREEN_HEIGHT-logoHeight, 20);
		this.DrawAlphaRect((Const.SCREEN_WIDTH-logoWidth)/2, Const.SCREEN_HEIGHT-logoHeight+curIndex*logoHeight/4
				, logoWidth, logoHeight/4, 0x8900ffff, bufGraphics);
	}

	// 绘制读取界面
	public void DrawLoading() {

	}

	// 绘制帮助界面
	public void DrawHelp() {
		bufGraphics.setColor(0);
		bufGraphics.fillRect(0, 0, Const.SCREEN_WIDTH, Const.SCREEN_HEIGHT);
		bufGraphics.setColor(0x00ffff);
		bufGraphics.drawString("游戏简介", Const.SCREEN_WIDTH/2, 5, Graphics.TOP|Graphics.HCENTER);
		for(int i=0; i<Const.HELP[curIndex].length; i++) {
			bufGraphics.drawString(Const.HELP[curIndex][i], Const.SCREEN_WIDTH/2,
					30+i*fontHeight, Graphics.TOP|Graphics.HCENTER);
		}
		DrawCommand(Const.COMMAND[0], Const.COMMAND[1]);
		bufGraphics.drawString((curIndex+1)+"/"+Const.HELP.length, Const.SCREEN_WIDTH/2, 
				Const.SCREEN_HEIGHT, Graphics.BOTTOM|Graphics.HCENTER);
		

	}

	// 画提示文字
	String[] alertCount = new String[] {""};// 提示信息内容

	public void DrawAlert() {
		if(curIndex > splashTime) {
			if(player.levelUp()) {
				String[] s = new String[] {"恭喜你!你升到了" + player.level + "级!"};
				this.SetAlertCount(s);
				this.GotoAlert();
			} else {
				curScreenState = Const.GAME_SCENE;
				curIndex = 0;
			}
		}
		DrawMap(bufGraphics);
		player.drawAnimation(bufGraphics);
		this.DrawAlphaRect(30, 20, Const.ALERT_SCREEN_WIDTH, Const.ALERT_SCREEN_HEIGHT, 0x89ff0000, bufGraphics);
		bufGraphics.setColor(0xffffff);
		for(int i=0; i<alertCount.length; i++) {
			bufGraphics.drawString(alertCount[i], Const.SCREEN_WIDTH/2, 35 + i * fontHeight, Graphics.TOP|Graphics.HCENTER);
		}
		curIndex++;
	}
	
	//画人物状态
	public void DrawStatue() {
		DrawMap(bufGraphics);
		player.drawAnimation(bufGraphics);
		this.DrawAlphaRect(20, 10, Const.SCREEN_WIDTH - 2 * 20, Const.SCREEN_HEIGHT - 2 * 10, 0x89ff0000, bufGraphics);
		bufGraphics.setColor(0x00ffff);
		bufGraphics.drawString("姓名:  " + player.name, 40, 25 + fontHeight * curIndex++, Graphics.TOP|Graphics.LEFT);
		bufGraphics.drawString("门派:  " + player.brand, 40, 25 + fontHeight * curIndex++, Graphics.TOP|Graphics.LEFT);
		bufGraphics.drawString("等级:  " + player.level, 40, 25 + fontHeight * curIndex++, Graphics.TOP|Graphics.LEFT);
		bufGraphics.drawString("H P:   " + player.hp + " / " + player.hpTotal, 40, 25 + fontHeight * curIndex++, Graphics.TOP|Graphics.LEFT);
		bufGraphics.drawString("M P:   " + player.mp + " / " + player.mpTotal, 40, 25 + fontHeight * curIndex++, Graphics.TOP|Graphics.LEFT);
		bufGraphics.drawString("攻击:  " + player.at, 40, 25 + fontHeight * curIndex++, Graphics.TOP|Graphics.LEFT);
		bufGraphics.drawString("防御:  " + player.def, 40, 25 + fontHeight * curIndex++, Graphics.TOP|Graphics.LEFT);
		bufGraphics.drawString("灵力:  " + player.ma, 40, 25 + fontHeight * curIndex++, Graphics.TOP|Graphics.LEFT);
		bufGraphics.drawString("经验" + player.exp + " / " + (player.level <= 10 ? player.level * 10 : player.level * 20), 
				40, 25 + fontHeight * curIndex++, Graphics.TOP|Graphics.LEFT);
		curIndex = 0;
		this.DrawCommand(Const.COMMAND[0], Const.COMMAND[1]);
	}

	//画任务状态
	public void DrawTask() {
		this.DrawAlphaRect(10, 10, Const.SCREEN_WIDTH - 20, Const.SCREEN_HEIGHT - 20, 0x89009900, bufGraphics);
		this.DrawCommand(Const.COMMAND[3], Const.COMMAND[1]);
	}
	
	public void DrawVol() {
		DrawMap(bufGraphics);
		player.drawAnimation(bufGraphics);
		this.DrawStringMenu(Const.VOL, bufGraphics);
		this.DrawCommand(Const.COMMAND[3], Const.COMMAND[1]);
	}
	//画游戏结束画面
	public void DrawOver() {
		bufGraphics.drawImage(gameOver, 0, 0, 20);
	}
	// 画指示光标
	public void DrawCursor(Graphics g) {

	}

	// 绘制按钮
	public void DrawCommand(String left, String right) {
		bufGraphics.setColor(0x00ffff);
		bufGraphics.drawString(left, 0, Const.SCREEN_HEIGHT, Graphics.BOTTOM|Graphics.LEFT);
		bufGraphics.drawString(right, Const.SCREEN_WIDTH, Const.SCREEN_HEIGHT, Graphics.BOTTOM|Graphics.RIGHT);

	}
	
	//绘制对话
	public void DrawDialog() {
		/*if(wy + words.length * fontHeight <= dy) return;
		this.DrawAlphaRect(dx, dy, Const.SCREEN_WIDTH - 2 * dx, 50, 0x8900ffff, bufGraphics);
		bufGraphics.setClip(dx, dy, Const.SCREEN_WIDTH - 2 * dx, 50);
		for(int i=0; i<words.length; i++) {
			bufGraphics.setColor(0xff0000);
			bufGraphics.drawString(words[i], Const.SCREEN_WIDTH / 2, wy + i * fontHeight, Graphics.TOP | Graphics.HCENTER);
		}
		wy--;*/
		DrawMap(bufGraphics);
		player.drawAnimation(bufGraphics);
		this.DrawAlphaRect(dx, dy, Const.SCREEN_WIDTH - 2 * dx, 30, 0x8900ffff, bufGraphics);
		bufGraphics.setColor(0xff0000);
		bufGraphics.drawString("小鱼:" + Const.DIALOGS[0][player.curDialogIndex][curIndex], dx + 2, dy + 2, Graphics.TOP | Graphics.LEFT);
	}

	// 绘制战斗场景
	byte curFightState;// 当前战斗状态

	public void DrawScreenFight() {
		DrawFightMap(bufGraphics);
		player.drawAnimationFight(bufGraphics);
		for (int i = 0; i < enemys.size(); i++) {
			Enemy e = (Enemy) enemys.elementAt(i);
			e.drawAnimation(bufGraphics);
		}

		switch (curFightState) {
		case Const.FIGHT_HERO_WAIT:
			DrawStringMenu(Const.FIGHT_MENU, bufGraphics);
			break;
		case Const.FIGHT_HERO_MAGIC_SELECT:
			DrawMagicMenu();
			break;
		case Const.FIGHT_HERO_MAGIC:
			player.useMagic(curIndex);
			break;
		case Const.FIGHT_HERO_SELECT:
			Enemy e = null;
			e = (Enemy) enemys.elementAt(enemyIndex);
			this.DrawAlphaRect(e.fx, e.fy, e.enemyWidth, e.enemyHeight,
					0x89ff0000, bufGraphics);
			break;

		}

	}

	// 绘制战斗场景地图
	public void DrawFightMap(Graphics g) {
		/*for (int i = 0; i <= Const.SCREEN_WIDTH / MAP_TILE_SIZE; i++) {
			for (int j = 0; j <= Const.SCREEN_HEIGHT / MAP_TILE_SIZE; j++) {
				drawClipImg(i * MAP_TILE_SIZE, j * MAP_TILE_SIZE,
						MAP_TILE_SIZE, MAP_TILE_SIZE,
						(Const.FIGHT_DATA[j][i] - 1) * MAP_TILE_SIZE, 0,
						imgMap, bufGraphics);
			}

		}*/
		bufGraphics.setColor(0xffffff);
		bufGraphics.fillRect(0, 0, Const.SCREEN_WIDTH, Const.SCREEN_HEIGHT);
		bufGraphics.drawImage(bgMap, 0, 0, 20);
	}
	
	//绘制保存界面
	public void DrawSave() {
		DrawMap(bufGraphics);
		this.DrawAlphaRect(20, 20, Const.SCREEN_WIDTH - 2 * 20, Const.SCREEN_HEIGHT - 2 * 20, 0x89ffff00, bufGraphics);
		bufGraphics.setColor(0xff00ff);
		bufGraphics.drawString("保存记录", Const.SCREEN_WIDTH / 2, 30, Graphics.TOP | Graphics.HCENTER);
		bufGraphics.setColor(0);
		for(int i=1; i<4; i++) {
			bufGraphics.drawString("记录" + i + "      " + save[i-1], 25, 30 + 40 * i, Graphics.TOP | Graphics.LEFT);
		}
		
		this.DrawAlphaRect(20, 20 + (curIndex + 1) * 40, Const.SCREEN_WIDTH - 2 * 20, 40, 0x89ff0000, bufGraphics);
		this.DrawCommand(Const.COMMAND[3], Const.COMMAND[1]);

⌨️ 快捷键说明

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