[摘要](!isInRange(this.loc[0] + i, this.loc[1] + j) && this.MyMap.Grid[this.loc[1] + j...
(!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
关键词:j2me游戏开发案例讲解