package huarongroad; import javax.microedition.lcdui.*; public class Displayable1 extends Canvas implements CommandListener { private int[] loc = new int[2]; <A href="file://光">file://光</A>标的当前位置,0是水平位置,1是竖直位置 private int[] SelectArea = new int[4];//被选定的区域,即要移动的区域 private int[] MoveArea = new int[4];//要移动到的区域 private Map MyMap = new Map();//地图类 private boolean selected;//是否已经选中要移动区域的标志 private int level;//但前的关面 public Displayable1() {//构造函数 try { jbInit();//JBuilder定义的初始化函数 }catch (Exception e) { e.printStackTrace(); } } private void Init_game(){ //初始化游戏,读取地图,设置选择区域,清空要移动到的区域 this.loc = MyMap.read_map(this.level);//读取地图文件,并返回光标的初始位置 //0为水平位置,1为竖直位置 this.SelectArea[0] = this.loc[0];//初始化选中的区域 this.SelectArea[1] = this.loc[1]; this.SelectArea[2] = 1; this.SelectArea[3] = 1; this.MoveArea[0] = -1;//初始化要移动到的区域 this.MoveArea[1] = -1; this.MoveArea[2] = 0; this.MoveArea[3] = 0; } private void jbInit() throws Exception {//JBuilder定义的初始化函数 <A href="file://初">file://初</A>始化实例变量 this.selected = false;//设置没有被选中的要移动区域 this.level = 1; Images.init();//初始化图片常量 Init_game();//初始化游戏,读取地图,设置选择区域,清空要移动到的区域 setCommandListener(this);//添加命令监听,这是Displayable的实例方法 addCommand(new Command("Exit", Command.EXIT, 1));//添加“退出”按钮 } public void commandAction(Command command, Displayable displayable) { //命令处理函数 if (command.getCommandType() == Command.EXIT) {//处理“退出” MIDlet1.quitApp(); } } protected void paint(Graphics g) { //画图函数,用于绘制用户画面,即显示图片,勾画选中区域和要移动到的区域 try { g.drawImage(Images.image_Frame, 0, 0, Graphics.TOP | Graphics.LEFT);//画背景 MyMap.draw_map(g);//按照地图内容画图 if ( this.selected ) g.setColor(0,255,0);//如果被选中,改用绿色画出被选中的区域 g.drawRect(this.SelectArea[0] * Images.UNIT + Images.LEFT, this.SelectArea[1] * Images.UNIT + Images.TOP, this.SelectArea[2] * Images.UNIT, this.SelectArea[3] * Images.UNIT);//画出选择区域, <A href="file://如">file://如</A>果被选中,就用绿色 <A href="file://否">file://否</A>则,使用黑色 g.setColor(255,255,255);//恢复画笔颜色 if (this.selected) {//已经选中了要移动的区域 g.setColor(255, 0, 255);//改用红色 g.drawRect(this.MoveArea[0] * Images.UNIT + Images.LEFT, this.MoveArea[1] * Images.UNIT + Images.TOP, this.MoveArea[2] * Images.UNIT, this.MoveArea[3] * Images.UNIT);//画出要移动到的区域 g.setColor(255, 255, 255);//恢复画笔颜色 } }catch (Exception ex) { } System.out.println(Runtime.getRuntime().freeMemory()); System.out.println(Runtime.getRuntime().totalMemory()); } private void setRange() { //设置移动后能够选中的区域 //调整当前光标位置到地图的主位置,即记录人物信息的位置 if (this.MyMap.Grid[this.loc[1]][this.loc[0]] == Images.DLEFT) { this.loc[0] -= 1;//向左调 }else if (this.MyMap.Grid[this.loc[1]][this.loc[0]] == Images.DUP) { this.loc[1] -= 1;//向上调 }else if (this.MyMap.Grid[this.loc[1]][this.loc[0]] == Images.DLEFTUP) { this.loc[0] -= 1;//向左调 this.loc[1] -= 1;//向上调 } this.SelectArea[0] = this.loc[0];//设置光标的水平位置 this.SelectArea[1] = this.loc[1];//设置光标的竖直位置 //设置光标的宽度 if (this.loc[0] + 1 < Images.WIDTH) { this.SelectArea[2] = this.MyMap.Grid[this.loc[1]][this.loc[0] + 1] != (byte) '1' ? 1 : 2; }else { this.SelectArea[2] = 1; } //设置光标的高度 if (this.loc[1] + 1 < Images.HEIGHT) { this.SelectArea[3] = this.MyMap.Grid[this.loc[1] + 1][this.loc[0]] != (byte) '2' ? 1 : 2; }else { this.SelectArea[3] = 1; } } private boolean setMoveRange() { //设置要移动到的区域,能够移动返回true,否则返回false for (int i = 0; i < this.SelectArea[2]; i++) { for (int j = 0; j < this.SelectArea[3]; j++) { if (this.loc[1] + j >= Images.HEIGHT || this.loc[0] + i >= Images.WIDTH || (!isInRange(this.loc[0] + i, this.loc[1] + j) && this.MyMap.Grid[this.loc[1] + j][this.loc[0] + i] != Images.BLANK)) { return false; } } } this.MoveArea[0] = this.loc[0]; this.MoveArea[1] = this.loc[1]; this.MoveArea[2] = this.SelectArea[2]; this.MoveArea[3] = this.SelectArea[3]; return true; } private boolean isInRange(int x, int y) { //判断给定的(x,y)点是否在选定区域之内,x是水平坐标,y是竖直坐标 if (x >= this.SelectArea[0] && x < this.SelectArea[0] + this.SelectArea[2] && y >= this.SelectArea[1] && y < this.SelectArea[1] + this.SelectArea[3]) { return true; }else { return false; } } private boolean isInRange2(int x, int y) { //判断给定的(x,y)点是否在要移动到的区域之内,x是水平坐标,y是竖直坐标 if (x >= this.MoveArea[0] && x < this.MoveArea[0] + this.MoveArea[2] && y >= this.MoveArea[1] && y < this.MoveArea[1] + this.MoveArea[3]) { return true; }else { return false; } } protected void keyPressed(int keyCode) { //处理按下键盘的事件,这是Canvas的实例方法 switch (getGameAction(keyCode)) {//将按键的值转化成方向常量 case Canvas.UP://向上 if (!this.selected) {//还没有选定要移动的区域 if (this.loc[1] - 1 >= 0) {//向上还有移动空间 this.loc[1]--;//向上移动一下 setRange();//设置光标移动的区域,该函数能将光标移动到地图主位置 repaint();//重新绘图 } }else {//已经选定了要移动的区域 if (this.loc[1] - 1 >= 0) {//向上还有移动空间 this.loc[1]--;//向上移动一下 if (setMoveRange()) {//能够移动,该函数能够设置要移动到的区域 repaint();//重新绘图 }else {//不能移动 this.loc[1]++;//退回来 } } } break; case Canvas.DOWN://向下 if (!this.selected) {//还没有选定要移动的区域 if (this.loc[1] + 1 < Images.HEIGHT) {//向下还有移动空间 if (this.MyMap.Grid[this.loc[1] + 1][this.loc[0]] == Images.DUP){//该图片有两个格高 this.loc[1]++;//向下移动一下 if (this.loc[1] + 1 < Images.HEIGHT) {//向下还有 <A href="file://移">file://移</A>动空间 this.loc[1]++;//向下移动一下 setRange();//设置光标移动的区域, <A href="file://该">file://该</A>函数能将光标移动到地图主位置 repaint();//重新绘图 }else {//向下没有移动空间 this.loc[1]--;//退回来 } }else {//该图片只有一个格高 this.loc[1]++;//向下移动一下 setRange();//设置光标移动的区域, <A href="file://该">file://该</A>函数能将光标移动到地图主位置 repaint();//重新绘图 } }else { } }else {//已经选定了要移动的区域 if (this.loc[1] + 1 < Images.HEIGHT) {//向下还有移动空间 this.loc[1]++;//向下移动一下 if (setMoveRange()) {//能够移动,该函数能够设置要移动到的区域 repaint();//重新绘图 }else {//不能移动 this.loc[1]--;//退回来 } } } break; case Canvas.LEFT://向左 if (!this.selected) {//还没有选定要移动的区域 if (this.loc[0] - 1 >= 0) {//向左还有移动空间 this.loc[0]--;//向左移动一下 setRange();//设置光标移动的区域,该函数能将光标移动到地图主位置 repaint();//重新绘图 } }else {//已经选定了要移动的区域 if (this.loc[0] - 1 >= 0) {//向左还有移动空间 this.loc[0]--;//向左移动一下 if (setMoveRange()) {//能够移动,该函数能够设置要移动到的区域 repaint();//重新绘图 }else {//不能移动 this.loc[0]++;//退回来 } } } break; case Canvas.RIGHT://向右 if (!this.selected) {//还没有选定要移动的区域 if (this.loc[0] + 1 < Images.WIDTH) {//向右还有移动空间 if (this.MyMap.Grid[this.loc[1]][this.loc[0] + 1] == Images.DLEFT) {//该图片有两个格宽 this.loc[0]++;//向右移动一下 if (this.loc[0] + 1 < Images.WIDTH) {//向右还有 <A href="file://移">file://移</A>动空间 this.loc[0]++;//向右移动一下 setRange();//设置光标移动的区域, <A href="file://该">file://该</A>函数能将光标移动到地图主位置 repaint();//重新绘图 }else {//向右没有移动空间 this.loc[0]--;//退回来 } }else {//该图片只有一个格宽 this.loc[0]++;//向右移动一下 setRange();//设置光标移动的区域, <A href="file://该">file://该</A>函数能将光标移动到地图主位置 repaint();//重新绘图 } }else { } }else {//已经选定了要移动的区域 if (this.loc[0] + 1 < Images.WIDTH) {//向右还有移动空间 this.loc[0]++;//向右移动一下 if (setMoveRange()) {//能够移动,该函数能够设置要移动到的区域 repaint();//重新绘图 }else {//不能移动 this.loc[0]--;//退回来 } } } break; case Canvas.FIRE: if (this.selected) {//已经选定了要移动的区域 Move();//将要移动的区域移动到刚选中的区域 repaint();//重新绘图 this.selected = false;//清除已选定要移动区域的标志 if ( win()) { System.out.println("win"); } }else {//还没有选定要移动的区域 if (this.MyMap.Grid[this.loc[1]][this.loc[0]] == Images.BLANK) {//要移到的位置是一个空白 }else {//要移到的位置不是空白 this.selected = true;//设置已选定要移动区域的标志 } repaint();//重新绘图 } break; } } private boolean win(){ <A href="file://判">file://判</A>断是否已经救出了曹操 if ( this.MyMap.Grid[Images.HEIGHT - 2 ][Images.WIDTH - 3 ] == Images.CAOCAO ) return true; else return false; } private void PrintGrid(String a) { <A href="file://打">file://打</A>印当前地图的内容,用于调试 System.out.println(a); for (int i = 0; i < Images.HEIGHT; i++) { for (int j = 0; j < Images.WIDTH; j++) { System.out.print( (char)this.MyMap.Grid[i][j]); } System.out.println(""); } } private void Move() { <A href="file://将">file://将</A>要移动的区域移动到刚选中的区域 if (this.MoveArea[0] == -1 || this.MoveArea[1] == -1 || this.SelectArea[0] == -1 || this.SelectArea[1] == -1) {//没有选中区域 }else {//已经选中了要移动的区域和要移动到的区域 byte[][] temp = new byte[this.SelectArea[3]][this.SelectArea[2]]; <A href="file://复">file://复</A>制要移动的区域,因为这块区域可能会被覆盖掉 for (int i = 0; i < this.SelectArea[2]; i++) { for (int j = 0; j < this.SelectArea[3]; j++) { temp[j][i] = this.MyMap.Grid[this.SelectArea[1] +j] [this.SelectArea[0] + i]; } } <A href="file://PrintGrid">file://PrintGrid</A>("1"); // 调试信息 <A href="file://将">file://将</A>要移动的区域移动到刚选中的区域(即要移动到的区域) for (int i = 0; i < this.SelectArea[2]; i++) { for (int j = 0; j < this.SelectArea[3]; j++) { this.MyMap.Grid[this.MoveArea[1] + j] [this.MoveArea[0] + i] = temp[j][i]; } } <A href="file://PrintGrid">file://PrintGrid</A>("2");// 调试信息 <A href="file://将">file://将</A>要移动的区域中无用内容置成空白 for (int i = 0; i < this.SelectArea[3]; i++) { for (int j = 0; j < this.SelectArea[2]; j++) { if (!isInRange2(this.SelectArea[0] + j, this.SelectArea[1] + i)) {//该点是不在要移动到 <A href="file://的">file://的</A>区域之内,需置空 this.MyMap.Grid[this.SelectArea[1] + i] [this.SelectArea[0] + j] = Images.BLANK; }else { } } } <A href="file://PrintGrid">file://PrintGrid</A>("3");// 调试信息 this.SelectArea[0] = this.MoveArea[0];//重置选中位置的水平坐标 this.SelectArea[1] = this.MoveArea[1];//重置选中位置的竖直坐标 this.MoveArea[0] = -1;//清空要移动到的位置 this.MoveArea[1] = -1;//清空要移动到的位置 this.MoveArea[2] = 0;//清空要移动到的位置 this.MoveArea[3] = 0;//清空要移动到的位置 } } } |