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

📄 j2me

📁 华容道是十分经典的小游戏
💻
📖 第 1 页 / 共 4 页
字号:
    }

    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]--;//退回来

⌨️ 快捷键说明

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