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

📄 hrgame.java

📁 华容道游戏源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package huaroad;import java.util.*;import com.siemens.mp.io.*;import com.siemens.mp.game.*;import java.io.IOException;import javax.microedition.lcdui.*;// 游戏类public class HRGame extends Canvas implements CommandListener, Backable{	private static int BLOCKSIZE = 0;	// 每块大小	private static int IMGOFFSET = 0;   // 名字图片对于边框的偏移量	// 布局定义 (4*5)块	// 0-空, 1~4-兵, 5-张飞, 6-赵云, 7-马超, 8-黄忠, 9-关羽, 10-曹操	private static final byte gamelayout[][][] = {		{   // 横刀立马			{ 5,10,10, 6 },			{ 5,10,10, 6 },			{ 7, 9, 9, 8 },			{ 7, 2, 3, 8 },			{ 1, 0, 0, 4 },		},		{   // 过五关			{ 1,10,10, 3 },			{ 2,10,10, 4 },			{ 5, 5, 6, 6 },			{ 7, 7, 8, 8 },			{ 0, 9, 9, 0 },		},		{   // 水泄不通			{ 1,10,10, 5 },			{ 2,10,10, 5 },			{ 6, 6, 7, 7 },			{ 8, 8, 9, 9 },			{ 3, 0, 0, 4 },		},		{   // 小燕出巢			{ 5,10,10, 6 },			{ 5,10,10, 6 },			{ 7, 7, 8, 8 },			{ 1, 9, 9, 3 },			{ 2, 0, 0, 4 },		},		{   // 进在咫尺			{ 1, 5, 6, 7 },			{ 2, 5, 6, 7 },			{ 8, 8, 3, 4 },			{ 9, 9,10,10 },			{ 0, 0,10,10 },		},		{   // 左右夹击			{ 1,10,10, 2 },			{ 5,10,10, 6 },			{ 5, 9, 9, 6 },			{ 7, 3, 4, 8 },			{ 7, 0, 0, 8 },		},		{   // 重兵挡道			{ 5,10,10, 6 },			{ 5,10,10, 6 },			{ 1, 2, 3, 4 },			{ 7, 9, 9, 8 },			{ 7, 0, 0, 8 },		}	};	private static final byte posSxy[][] = {	// 各布局光标起始位置(x,y)		{ 1, 4 }, { 0, 4 }, { 1, 4 }, { 1, 4 }, { 0, 4 },		{ 1, 4 }, { 1, 4 }	};	private static final Command COMMANDS[] = {   // 定义菜单		new Command("继续游戏", 4, 0),		new Command("新游戏", 1, 1),		new Command("退出", 6, 2),		new Command("按键设置", 1, 3),		new Command("帮助信息", 1, 4)	};	public byte ctrlkey[] = {    // 按键定义(初始按键)		'4',    // left   - 4		'6',    // right  - 6		'2',    // up     - 2		'8',    // down   - 8		'5',    // select - 5	};	private Command SndCmd, VibCmd;	final private static String openSnd="开启声音", closeSnd="关闭声音", openVib="开启振动", closeVib="关闭振动";	public boolean Snd, Vib;	private byte grid[][] = new byte[4][5];		// 游戏布局矩阵	private int posx, posy;  // 当前光标位置	private int oldx, oldy;	 // 上次光标位置	private int offx, offy;  // 棋子的大小偏移量(相对于当前块)。							 // 比如当前块是曹操的右上角,那么offx=-1,offy=1							 // 表示x负方向还有一块,y的正方向有一块	private int steps, tmpstep;	// 已走步数	private Image nameImg[]; // 名字图片(由于可写文字太大,所以用图片)	private SelNew gamelist;	private Display display;	private Quitable winQuit;	private boolean selected, gameOver;   // 是否选中/是否游戏结束	private ExtendedImage ExScreenImg;		// 扩展图片作为绘图缓存(Siemens扩展)	private Graphics Exg;					// 缓存的Graphics对象	private MelodyComposer melody;			// midi播放(Siemens扩展)	public HRGame()	{		if (getHeight() > 64)   // 根据屏幕高度得到图形尺寸,以便针对不同机型		{			// 6688i使用			BLOCKSIZE = 15; // 每块大小			IMGOFFSET = 3;  // 名字图片对于边框的偏移量		}		else		{			// 3118使用			BLOCKSIZE = 12;			IMGOFFSET = 1;		}		nameImg = new Image[26];  // 载入名字图片		try		{			nameImg[0] = Image.createImage("images/zhang.png");		// 张			nameImg[1] = Image.createImage("images/zhang2.png");	// 张(反色)			nameImg[2] = Image.createImage("images/fei.png");		// 飞			nameImg[3] = Image.createImage("images/fei2.png");		// 飞(反色)			nameImg[4] = Image.createImage("images/zhao.png");		// 赵			nameImg[5] = Image.createImage("images/zhao2.png");		// 赵(反色)			nameImg[6] = Image.createImage("images/yun.png");		// 云			nameImg[7] = Image.createImage("images/yun2.png");		// 云(反色)			nameImg[8] = Image.createImage("images/ma.png");		// 马			nameImg[9] = Image.createImage("images/ma2.png");		// 马(反色)			nameImg[10] = Image.createImage("images/chao.png");		// 超			nameImg[11] = Image.createImage("images/chao2.png");	// 超(反色)			nameImg[12] = Image.createImage("images/huang.png");	// 黄			nameImg[13] = Image.createImage("images/huang2.png");	// 黄(反色)			nameImg[14] = Image.createImage("images/zhong.png");	// 忠			nameImg[15] = Image.createImage("images/zhong2.png");	// 忠(反色)			nameImg[16] = Image.createImage("images/guan.png");		// 关			nameImg[17] = Image.createImage("images/guan2.png");	// 关(反色)			nameImg[18] = Image.createImage("images/yu.png");		// 羽			nameImg[19] = Image.createImage("images/yu2.png");		// 羽(反色)			nameImg[20] = Image.createImage("images/cao.png");		// 曹			nameImg[21] = Image.createImage("images/cao2.png");		// 曹(反色)			nameImg[22] = Image.createImage("images/c.png");		// 操			nameImg[23] = Image.createImage("images/c2.png");		// 操(反色)			nameImg[24] = Image.createImage("images/bing.png");		// 兵			nameImg[25] = Image.createImage("images/bing2.png");	// 兵(反色)		}		catch(IOException ioexception) { }		ExScreenImg = new ExtendedImage(Image.createImage(104, getHeight()));  	// 新建绘图缓存		Exg = ExScreenImg.getImage().getGraphics();		// 缓存绘图的Graphics对象		melody = new MelodyComposer();					// 新建midi		Snd = true;		// 音效开启		Vib = true;		// 震动开启		/////////////////////////////////////////////		// 读取按键设置 共7字节,依次表示:左按键、右按键、上按键、下按键、选中按键、音效、震动		try		{			byte bflag[] = new byte[2];			bflag[0] = bflag[1] = 1;			File keyfile = new File();			int fid = keyfile.open("CtrlKey");		// 打开文件(Siemens扩展)			if(keyfile.length(fid) > 0) keyfile.read(fid, ctrlkey, 0, 5);	// 读取按键参数到ctrlkey			if(keyfile.length(fid) > 5) keyfile.read(fid, bflag, 0, 2);		// 读取文件			Snd = (bflag[0] == 1);	// 音效参数 (1-开启,其他-关闭)			Vib = (bflag[1] == 1);	// 震动参数 (1-开启,其他-关闭)			keyfile.close(fid);		}		catch(IOException ioexception) { }		catch(NullPointerException npe){ }		//////////////////////////////////////////////		if (Snd) SndCmd = new Command(closeSnd, Command.OK, 5);		// 根据参数设置菜单		else SndCmd = new Command(openSnd, Command.OK, 5);		if (Vib) VibCmd = new Command(closeVib, Command.OK, 6);		else VibCmd = new Command(openVib, Command.OK, 6);		for(int i = 0; i < COMMANDS.length; i++)	// 添加菜单			addCommand(COMMANDS[i]);		addCommand(SndCmd);		addCommand(VibCmd);	}	public void activate(Display disp, Quitable quitable)		// 激活	{		winQuit = quitable;		display = disp;		show();	}	public void getkeys(byte ckeys[])		// 从自定义按键结果设置按键,并显示游戏界面	{		for (byte i=0; i<5; i++) ctrlkey[i] = ckeys[i];		show();	}	public void show()		// 显示游戏画面	{		display.setCurrent(this);		setCommandListener(this);	}	public void newGame(SelNew gmlist)    // 新游戏	{		gamelist = gmlist;		int gnum = gamelist.getSelectedIndex();		// 获取游戏布局编号		int i, j;		for (i=0; i<4; i++)							// 根据编号初始化游戏矩阵			for (j=0; j<5; j++)				grid[i][j] = gamelayout[gnum][j][i];		posx = posSxy[gnum][0];		// 初始化当前光标位置		posy = posSxy[gnum][1];		offx = offy = 0;		steps = 0;				// 已走步数为0		gameOver = false;		selected = false;		// 没有选中某一块		ExScreenImg.clear((byte)0);		// 清空缓存		Exg.drawRect(4, 0, 2+BLOCKSIZE*4, 2+BLOCKSIZE*5);	// 画边框		Exg.drawRect(4+BLOCKSIZE, 2+BLOCKSIZE*5, 2+BLOCKSIZE*2, 2);		// 画布局名称		String gname,gname2;		switch(gamelist.getSelectedIndex())		{		case 0:			gname = "横刀"; gname2 = "立马"; break;		case 1:			gname = "过"; gname2 = "五关"; break;		case 2:			gname = "水泄"; gname2 = "不通"; break;		case 3:			gname = "小燕"; gname2 = "出巢"; break;		case 4:			gname = "进在"; gname2 = "咫尺"; break;		case 5:			gname = "左右"; gname2 = "夹击"; break;		default:			gname = "重兵"; gname2 = "挡道"; break;		}		Exg.drawString(gname, 70, 0, Graphics.TOP|Graphics.LEFT);		Exg.drawString(gname2, 70, 13, Graphics.TOP|Graphics.LEFT);		// 画步数		Exg.drawString("步数", 70, 34, Graphics.TOP|Graphics.LEFT);		Exg.setColor(0xffffff);		Exg.fillRect(68, 55, 33, 13);		Exg.setColor(0);		Exg.drawString(Integer.toString(steps), 100, 47, Graphics.TOP|Graphics.RIGHT);		// 画棋子		byte layout[] = {0,0,0,0,0, 0,0,0,0,0};  // 分别表示 10个棋子是否已经处理过,依次为:												 // 1~4-兵, 5-张飞, 6-赵云, 7-马超, 8-黄忠, 9-关羽, 10-曹操		for(i=0; i<4; i++)			for(j=0; j<5; j++)			{				if (grid[i][j] != 0)					if (layout[grid[i][j]-1] == 0)	// 如果这个棋子没有画过					{						moveSelbox(i, j, false);	// 画一个棋子						layout[grid[i][j]-1] = 1;	// 已经画过了,标志置1					}			}		drawSelbox();  // 画选择框		SoundPlay(0);  // 播放游戏开始音效	}	protected void paint(Graphics parag)	{		redraw();	}	// 画选择框	private void drawSelbox()	{		if (selected)   // 选中状态		{			drawBox(posx, posy, offx, offy, 0xffffff, false, 1, 0);			for (int i=0; i<4; i++)				for (int j=0; j<5; j++)				{					if (grid[i][j] == 0) drawBox(i, j, 0, 0, 0xffffff, true, 1, 0);				}			drawBox(posx, posy, offx, offy, 0, true, 0, 2);		}		else    // 候选状态		{			if (grid[posx][posy] != 0)  // 当前块不是空的			{				drawBox(posx, posy, offx, offy, 0xffffff, true, 0, 1);				drawBox(posx, posy, offx, offy, 0, false, 0, 0);			}			drawBox(posx, posy, offx, offy, 0, false, 2, 0);   //画外框		}	}	// 移动选择框	// i,j当前块的(x,y)坐标, moving:true-移动到该棋子,false-重画该棋子	private void moveSelbox(int i, int j, boolean moving)	{		int ox = 0;		int oy = 0;		if (grid[i][j] != 0)   // 该块不是空的		{			if (i > 0)		// 左侧				if (grid[i][j] == grid[i-1][j]) ox = -1;// 左侧属于同一个棋子			if (i < 3)		// 右侧				if (grid[i][j] == grid[i+1][j]) ox = 1;	// 右侧属于同一个棋子			if (j > 0)		// 上面				if (grid[i][j] == grid[i][j-1]) oy = -1;// 上面属于同一个棋子			if (j < 4)		// 下面				if (grid[i][j] == grid[i][j+1]) oy = 1;// 下面属于同一个棋子		}		if (moving)		{			offx = ox;			offy = oy;			drawSelbox();	// 移动选择框		}		else			drawBox(i, j, ox, oy, 0, false, 0, 1);	// 画棋子	}	// 向缓存画棋子或外框:(px,py)焦点格坐标,(ox,oy)整块相对焦点的偏移量,	//    color颜色, fill是否填充黑色,bigbox是否大块(分3级),img是否画人名:0-不画、1-正常、2-反色	private void drawBox(int px, int py, int ox, int oy, int color, boolean fill, int bigbox, int img)	{		int gx[] = new int[2];		int gy[] = new int[2];		boolean dir = (ox==0);		if (bigbox != 0)		{			gx[0] = gx[1] = 5;			gy[0] = gy[1] = 1;		}

⌨️ 快捷键说明

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