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

📄 game.java~169~

📁 我用j2me编写的索爱手机有游戏程序屏幕(128*160)
💻 JAVA~169~
📖 第 1 页 / 共 2 页
字号:

import javax.microedition.lcdui.*;
import javax.microedition.media.*;



public class Game extends Canvas implements  CommandListener {
  /** Constructor */
   private Map MyMap = new Map();//地图类
   private int[] loc = new int[2]; //光标的当前位置,0是水平位置,1是竖直位置,从0开始计数
   private boolean selected;//是否已经选中要移动区域的标志
   private boolean DrawSelected;//是否要画选择区域
   private int[] SelectArea = new int[4];//被选定的区域,即要移动的区域
   private int[] MoveArea = new int[4];//要移动到的区域
   private boolean WinGame;
   private int state;
   private Image logo;





  public Game() {
     setFullScreenMode(true); //全屏函数
    try {
      //  logo = Image.createImage("/logo.png");
      Images.init();//初始化图片常量
      jbInit();//初始化参数

    }
    catch(Exception e) {
      e.printStackTrace();
    }


   // Thread th = new Thread(this);//制作新线程
   // th.start();//开始线程

  }


  /**Component initialization*/
  private void jbInit() throws Exception {//初始化游戏,设置选择区域,清空要移动到的区域
    this.selected = false;//初始化为没有被选中的要移动区域
    this.WinGame =false;//初始化游戏结束的标量

    this.loc [0]=2; //设置光标的初始位置//0为水平位置,1为竖直位置
    this.loc [1]=4; //从0开始计数
    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;
    this.state=0;



    // Set up this Displayable to listen to command events
    setCommandListener(this);
    // add the Exit command
    addCommand(new Command("Exit", Command.EXIT, 1));
  }

  /**Handle command events*/
  public void commandAction(Command command, Displayable displayable) {
    /** @todo Add command handling code */
    if (command.getCommandType() == Command.EXIT) {
// stop the MIDlet
      GameMIDlet.quitApp();
    }
  }








  /** Required paint implementation */
  protected void paint(Graphics g) {

     if(this.state==0){
       try {
         logo = Image.createImage("/logo.png");
       }
       catch (Exception e){
       }

       g.setColor(255,255,255);
       g.fillRect(0, 0, getWidth(), getHeight());
       g.drawImage(logo, getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER); //将LOGO画到屏幕中间
       g.setColor(0, 0, 0);
       g.drawString("press 5", (getWidth() / 2)-17,getHeight()-20, Graphics.LEFT | Graphics.TOP);
       g.drawString("http://blog.sina.com.cn/drg2007", (getWidth() / 2)-60,5, Graphics.LEFT | Graphics.TOP);
     }


   if(this.state ==1){
      g.setColor(110, 110, 220); //画背景框
      g.fillRect(0, 0, getWidth(), getHeight());
      g.setColor(255, 255, 255);
      g.drawRect(0, 0, getWidth(), getHeight());
      g.drawRect(1, 1, getWidth() - 2, getHeight() - 2);
      g.drawRect(2, 2, getWidth() - 4, getHeight() - 4);
      g.setColor(0, 70, 240);
      g.drawRect(2, 3, getWidth() - 4, getHeight() - 6);
      g.setColor(255, 180, 0);
      g.fillRect(39, 157, 50, 3);
      g.setColor(255, 120, 0);
      g.fillRect(37, 158, 54, 2);
      g.setColor(255, 76, 0);
      g.fillRect(35, 159, 58, 1);

      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); //画出选择区域,如果被选中,就用绿色否则,使用黑色
        g.setColor(255, 255, 255); //恢复画笔颜色
      }

      if (this.DrawSelected) {
        g.setColor(255, 0, 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); //画出选择区域,如果被选中,就用绿色否则,使用黑色
        g.setColor(255, 255, 255); //恢复画笔颜色
        this.DrawSelected = false;
      }

      if (this.selected) { //已经选中了要移动的区域
        g.setColor(255, 0, 0); //改用红色
        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); //恢复画笔颜色

        if (this.WinGame) { //完成游戏
          g.setColor(255, 255, 255);
          g.drawString("GoodGame^_^", 30, 40, Graphics.LEFT | Graphics.TOP);
        }
      }
    }
  }


private void setRange() {//设置移动后能够选中的区域//调整当前光标位置到图片的主位置,即记录人物信息的位置
                              //设置SelectArea[]的4个值
    if (this.MyMap.Grid[this.loc[1]][this.loc[0]] == Images.DLEFT) {//值为1
      this.loc[0] -= 1; //光标向左调
    }
    else if (this.MyMap.Grid[this.loc[1]][this.loc[0]] == Images.DUP) {//值为2
      this.loc[1] -= 1; //向上调
    }
    else if (this.MyMap.Grid[this.loc[1]][this.loc[0]] == Images.DLEFTUP) {//值为3
      this.loc[0] -= 1; //向左调
      this.loc[1] -= 1; //向上调
    }
    //对被选中的区域进行设置:
    this.SelectArea[0] = this.loc[0]; //0:设置光标的水平位置
    this.SelectArea[1] = this.loc[1]; //1:设置光标的竖直位置

    if (this.loc[0] + 1<Images.WIDTH) {//2:设置光标的宽度
      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) {//3:设置光标的高度
      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 {

⌨️ 快捷键说明

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