📄 game.java~169~
字号:
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) {
switch (keyCode) {//将按键的值转化成方向常量
case Canvas.KEY_NUM2://向上
if (!this.selected) { //还没有选定要移动的区域
if (this.loc[1] - 1 >=0) { //向上还有移动空间
this.loc[1]--; //向上移动一下
setRange(); //设置光标移动的区域,该函数能将光标移动到地图主位置
repaint(); //重新绘图,调用paint()函数
}
}
else { //已经选定了要移动的区域
if (this.loc[1] - 1>= 0) { //向上还有移动空间
this.loc[1]--; //向上移动一下
if (setMoveRange()) { //能够移动,该函数能够设置要移动到的区域
repaint(); //重新绘图
}
else { //不能移动
this.loc[1]++; //退回来
}
}
}
break;
case Canvas.KEY_NUM8://向下
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) { //向下还有移动空间
this.loc[1]++; //向下移动一下
setRange(); //设置光标移动的区域,//该函数能将光标移动到图片主位置
repaint(); //重新绘图
}
else { //向下没有移动空间
this.loc[1]--; //退回来
}
}
else { //该图片只有一个格高
this.loc[1]++; //向下移动一下
setRange(); //设置光标移动的区域,该函数能将光标移动到图片主位置
repaint(); //重新绘图
}
}
else { }
}
else { //已经选定了要移动的区域
if (this.loc[1] + 1<Images.HEIGHT) { //向下还有移动空间
this.loc[1]++; //向下移动一下
if (setMoveRange()) { //能够移动,该函数能够设置要移动到的区域
repaint(); //重新绘图
}
else { //不能移动
this.loc[1]--; //退回来
}
}
}
break;
case Canvas.KEY_NUM4:
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.KEY_NUM6:
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) { //向右还有移动空间
this.loc[0]++; //向右移动一下
setRange(); //设置光标移动的区域,该函数能将光标移动到地图主位置
repaint(); //重新绘图
}
else { //向右没有移动空间
this.loc[0]--; //退回来
}
}
else { //该图片只有一个格宽
this.loc[0]++; //向右移动一下
setRange(); //设置光标移动的区域, //该函数能将光标移动到图片主位置
repaint(); //重新绘图
}
}
else {
}
}
else { //已经选定了要移动的区域
if (this.loc[0] + 1<Images.WIDTH) { //向右还有移动空间
this.loc[0]++; //向右移动一下
if (setMoveRange()) { //能够移动,该函数能够设置要移动到的区域
repaint(); //重新绘图
}
else { //不能移动
this.loc[0]--; //退回来
}
}
}
break;
case Canvas.KEY_NUM5:
if(this.state== 0){
this.state=1;
repaint();
}
if (this.selected) { //已经选定了要移动的区域
Move(); //将要移动的区域移动到刚选中的区域
repaint(); //重新绘图
this.selected = false; //清除已选定要移动区域的标志
if (win()) {//判断是否完成游戏
this.WinGame=true ;
}
}
else { //还没有选定要移动的区域
if (this.MyMap.Grid[this.loc[1]][this.loc[0]] == Images.BLANK) { //要移动的位置是一个空白
}
else { //要移动的位置不是空白
this.selected = true; //设置已选定要移动区域的标志
this.DrawSelected=true;
}
repaint(); //重新绘图
}
break;
}
}
private void Move() {//将要移动的区域移动到刚选中的区域
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]];
//复制要移动的区域,因为这块区域可能会被覆盖掉
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];
}
}
//将要移动的区域移动到刚选中的区域(即要移动到的区域)
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];
}
}
//将要移动的区域中无用内容置成空白
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)) { //该点是不在要移动到的区域之内,需置空
this.MyMap.Grid[this.SelectArea[1] + i] [this.SelectArea[0] + j] = Images.BLANK;
}
else {
}
}
}
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; //清空要移动到的位置
}
}
private boolean win(){ //判断是否已经救出了曹操
if (this.MyMap.Grid[Images.HEIGHT - 2][Images.WIDTH - 3] == Images.CAOCAO)
return true;
else
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -