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

📄 playcanvas.java

📁 一款运行于诺基亚6688上的手机游戏——大富翁源代码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
          for (int k = 0; k < 2; k++)
            player_stock[i][j][k] = 0;
      }
      //股票
      this.stock_name = new String[4];
      stock_name[0] = "嘟嘟控股";
      stock_name[1] = "华西医大";
      stock_name[2] = "Microsoft";
      stock_name[3] = "Linux";
      this.stock_price = new int[4];
      stock_price[0] = 10;
      stock_price[1] = 4;
      stock_price[2] = 12;
      stock_price[3] = 6;
      this.stock_amplitude = new int[4];
      stock_amplitude[0] = 5 - this.fetchRandom(10);
      stock_amplitude[1] = 5 - this.fetchRandom(10);
      stock_amplitude[2] = 5 - this.fetchRandom(10);
      stock_amplitude[3] = 5 - this.fetchRandom(10);
      this.stock_price_fraction = new int[4];
      stock_price_fraction[0] = 0;
      stock_price_fraction[1] = 0;
      stock_price_fraction[2] = 0;
      stock_price_fraction[3] = 0;
      //其他
      choiceIndex = 0;
      gameStatusGoOrWinOrFail = 0;
      turnCount = 0;
      nowPlayerID = 0;
      ManControlStatus_CanNotPlay = false;
    }
    /**
     * 控制股票升降
     */

    private void changeStock() {
      for (int i = 0; i < 4; i++ ){
        stock_amplitude[i] += (7 - this.fetchRandom(14));
        if (stock_amplitude[i] > 10)
          stock_amplitude[i] = 10; //涨幅不能大于10%
        if (stock_amplitude[i] < -10)
          stock_amplitude[i] = -10; //涨幅不能小于-10%
        int price = stock_price[i] * 10000 + stock_price_fraction[i];
        price = price * (100 + stock_amplitude[i]) / 100;
        stock_price[i] = price / 10000;
        stock_price_fraction[i] = price - stock_price[i] * 10000;
        if (stock_price[i] < 2) { //价格不能小于 2块钱
          stock_price[i] = 2;
        }
      }
    }
    /**
     * 游戏结束
     * @param status 结束标记
     */

    private void gameOver(int status)
    {
        if(status == 1)
        {
            gameStatusGoOrWinOrFail = 2;
            repaint();
            serviceRepaints();
            richMan.freshHighScore(player_sequence[0], turnCount);
        } else
        {
            gameStatusGoOrWinOrFail = 3;
            repaint();
            serviceRepaints();
        }
    }
    /**
     * 自动保存文件
     */

    public void autoSaveGame()
    {
      try {
        RecordStore recordstore = RecordStore.openRecordStore("RichMan", true);
        byte bytes[] = saveGame();
        if (recordstore.getNumRecords() == 0)
          recordstore.addRecord(bytes, 0, bytes.length);
        else {
          recordstore.setRecord(1, bytes, 0, bytes.length);
          System.out.print("AutoSetSaveGame Success!");
        }
        recordstore.closeRecordStore();
      }
      catch (RecordStoreException ex) {
        System.out.println("AutoSave failed!");
      }
    }
    /**
     * 获得游戏记录
     */

    public boolean fetchRecords()
    {
      try {
        RecordStore recordstore = RecordStore.openRecordStore("RichMan", false);
        if (recordstore == null)
          return false;
        rebuildGame(recordstore.getRecord(1));
        recordstore.closeRecordStore();
      }
      catch (Exception exception) {
        return false;
      }
      play(true);
      return true;
    }
    /**
     * 保存游戏
     */

    public byte[] saveGame()
    {
        byte bytes[] = null;
        try
        {
            ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
            DataOutputStream dataoutputstream = new DataOutputStream(bytearrayoutputstream);
            // 玩家数据
            for(int i = 0; i < 3; i++)
            {
                dataoutputstream.writeInt(player_location[i]);
                dataoutputstream.writeInt(player_money[i]);
                dataoutputstream.writeInt(player_sequence[i]);
                dataoutputstream.writeInt(player_prinson_canNotMoveNum[i]);
                for (int j = 0; j < 4; j++)
                  for (int k = 0; k < 2; k++)
                    dataoutputstream.writeInt(this.player_stock[i][j][k]);
                for(int j = 0; j < 5; j++)
                  dataoutputstream.writeInt(player_cards[i][j]);
            }
            // 地皮数据
            for(int i = 0; i < groundForBuilding.length; i++)
              dataoutputstream.writeInt(groundForBuilding[i]);
              // 股票数据
            for(int i = 0; i < 4; i++) {
              dataoutputstream.writeInt(this.stock_price[i]);
              dataoutputstream.writeInt(this.stock_amplitude[i]);
              dataoutputstream.writeInt(this.stock_price_fraction[i]);
            }
            // 其他数据
            dataoutputstream.writeInt(turnCount);
            dataoutputstream.writeInt(this.gameSpeed);
            dataoutputstream.writeBoolean(this.isLightOn);
            dataoutputstream.writeBoolean(this.isMusicOn);

            bytes = bytearrayoutputstream.toByteArray();
            bytearrayoutputstream.close();
            dataoutputstream.close();
        }
        catch(Exception exception) { }
        return bytes;
    }
    /**
     * 重建游戏(load game)
     */

    public void rebuildGame(byte recordData[])
    {
        try
        {
            ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(recordData);
            DataInputStream datainputstream = new DataInputStream(bytearrayinputstream);
            for(int i = 0; i < 3; i++)
            {
                player_location[i] = datainputstream.readInt();
                player_money[i] = datainputstream.readInt();
                player_sequence[i] = datainputstream.readInt();
                player_prinson_canNotMoveNum[i] = datainputstream.readInt();
                for (int j = 0; j < 4; j++)
                  for (int k = 0; k < 2; k++)
                    player_stock[i][j][k] = datainputstream.readInt();
                for(int j = 0; j < 5; j++)
                  player_cards[i][j] = datainputstream.readInt();

            }

            for(int i = 0; i < groundForBuilding.length; i++)
                groundForBuilding[i] = datainputstream.readInt();
              for(int i = 0; i < 4; i++) {
                this.stock_price[i] = datainputstream.readInt();
                this.stock_amplitude[i] = datainputstream.readInt();
                this.stock_price_fraction[i] = datainputstream.readInt();
              }

            turnCount = datainputstream.readInt();
            this.gameSpeed = datainputstream.readInt();
            this.isLightOn = datainputstream.readBoolean();
            this.isMusicOn = datainputstream.readBoolean();

            this.setIsLightOn(this.isLightOn);
            this.setIsMusicOn(this.isIsMusicOn());

            bytearrayinputstream.close();
            datainputstream.close();
        }
        catch(Exception exception) { }
    }
    /**
     * 购买土地
     */

    public void buyLand()
    {
        switch(groundForBuilding[player_location[nowPlayerID]])
        {
        case 0: // '\0'
            player_money[nowPlayerID] -= fetchGroundPrice(player_location[nowPlayerID]);
            break;

        case 1: //
        case 10: //
        case 100: //
            player_money[nowPlayerID] -= (fetchGroundPrice(player_location[nowPlayerID]) * 2) / 5;
            break;

        case 2: //
        case 20: //
        case 200:
            player_money[nowPlayerID] -= (fetchGroundPrice(player_location[nowPlayerID]) * 3) / 5;
            break;

        case 3: //
        case 30: //
        case 300:
            player_money[nowPlayerID] -= (fetchGroundPrice(player_location[nowPlayerID]) * 4) / 5;
            break;

        case -2: //衰神的土地
            player_money[nowPlayerID] -= fetchGroundPrice(player_location[nowPlayerID]);
            break;
        case -1: //财神的土地
          break;
        }
        if(groundForBuilding[player_location[nowPlayerID]] == -2)
            groundForBuilding[player_location[nowPlayerID]] = 0; // 恢复衰神的土地
        else
            groundForBuilding[player_location[nowPlayerID]] += nowPlayerID != 0 ? nowPlayerID != 1 ? 100 : 10 : 1;
        adjustGroundOverBuilding(player_location[nowPlayerID]);
    }
    /**
     * 处理电脑玩家玩游戏
     */

    void dealWithComputerActorPlay()
    {
      ManControlStatus_CanNotPlay = true;
      int cardIndex = -1;
      //自动获得卡片
      if(player_cards[nowPlayerID][0] != 16)
        cardIndex = autoFetchCardIndex();
      if(cardIndex != -1 &&
         this.fetchRandom(5) > 3) // 电脑有2/6机会使用卡片
      {
        choiceIndex = 1;
        repaint();
        serviceRepaints();
        try
        {
          Thread.sleep(1000L);
        }
        catch(Exception exception) { }

        richMan.useCard_ComputerActor(player_cards[nowPlayerID], cardIndex);

        /*
        choiceID = 0;
        repaint();
        serviceRepaints();
        try
        {
          Thread.sleep(1000L);
        }
        catch(Exception exception) { }
        */
      }else
        richMan.setDisplayToDiceCanvas();
    }
    /**
     * 自动获得卡片
     */

    private int autoFetchCardIndex()
    {
      int cardCount = 0;
      //计算卡片的数目
      for(int i1 = 0; i1 < 5; i1++)
        if(player_cards[nowPlayerID][i1] != 16)
          cardCount++;

      for(int index = 0; index < cardCount; index++)
        switch(player_cards[nowPlayerID][index])
        {
          case 4: // 免罪卡
          default:
            break;

          case 2: // 强占卡
            if(!isNowPlayerIDGround())
              return index;
            break;

          case 8: // 财神卡
          case 9: // 衰神卡
            if(groundForBuilding[player_location[nowPlayerID]] == 0 &&
               player_location[nowPlayerID] != 2 &&
               player_location[nowPlayerID] != 5 &&
               player_location[nowPlayerID] != 11 &&
               player_location[nowPlayerID] != 16 &&
               player_location[nowPlayerID] != 20 &&
               player_location[nowPlayerID] != 25 &&
               player_location[nowPlayerID] != 28 &&
               player_location[nowPlayerID] != 32 &&
               player_location[nowPlayerID] != 37 &&
               player_location[nowPlayerID] != 42 &&
               player_location[nowPlayerID] != 45 &&
               player_location[nowPlayerID] != 49)
              return index;
            break;

          case 0: //其他卡片
          case 1: //
          case 3: //
          case 5: //
          case 6: //
          case 7: //
            return index;
        }
      return -1;
    }
    /**
     * 设置背景灯
     * @param isLightOn 是否开灯
     */

  public void setIsLightOn(boolean isLightOn) {
    this.isLightOn = isLightOn;
    if (this.isLightOn)
      com.siemens.mp.game.Light.setLightOn();
    else
      com.siemens.mp.game.Light.setLightOff();
  }
  /**
   * 设置音乐
   * @param isMusicOn 是否打开音乐
   */

  public void setIsMusicOn(boolean isMusicOn) {
    this.isMusicOn = isMusicOn;
    if (this.isMusicOn)
      this.composer.getMelody().play();
    else
      this.composer.getMelody().stop();
  }
  /**
   * 获取背景灯状态
   */

  public boolean isIsLightOn() {
    return isLightOn;
  }
  /**
   * 获取背景音乐状态
   */

  public boolean isIsMusicOn() {
    return isMusicOn;
  }
  /**
   * 获取游戏速度
   */

  public int getGameSpeed() {
    return gameSpeed;
  }
  /**
   * 设置游戏速度
   */

  public void setGameSpeed(int gameSpeed) {
    this.gameSpeed = gameSpeed;
  }
  /**
   * 初始化音乐
   */

  public void initialMusic() {
    if (composer == null)
      composer = new com.siemens.mp.game.MelodyComposer();
    else
      composer.resetMelody();
    composer.setBPM(240);
    try {
      com.siemens.mp.io.File file = new com.siemens.mp.io.File();
      int musicFile = file.open("music" + this.music +".msc");
      int length = file.length(musicFile);
      byte[] bytes = new byte[length];
      file.read(musicFile,bytes,0,length);

      for (int i = 0; i < length / 2; i++) {
        composer.appendNote((int)bytes[i*2], (int)bytes[i*2+1]);
      }
      composer.appendNote(composer.TONE_REPEV, 1);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    /*
    try {
      composer.appendNote(composer.TONE_E3, composer.TONELENGTH_DOTTED_1_4);
      composer.appendNote(composer.TONE_D3, composer.TONELENGTH_DOTTED_1_16);
      composer.appendNote(composer.TONE_PAUSE, composer.TONELENGTH_DOTTED_1_16);
      composer.appendNote(composer.TONE_E3, composer.TONELENGTH_DOTTED_1_4);
      composer.appendNote(composer.TONE_D3, composer.TONELENGTH_DOTTED_1_16);
      composer.appendNote(composer.TONE_PAUSE, composer.TONELENGTH_DOTTED_1_16);
      composer.appendNote(composer.TONE_C3, composer.TONELENGTH_DOTTED_1_16);
      composer.appendNote(composer.TONE_PAUSE, composer.TONELENGTH_DOTTED_1_16);
      composer.appendNote(composer.TONE_H2, composer.TONELENGTH_DOTTED_1_16);
      composer.appendNote(composer.TONE_PAUSE, composer.TONELENGTH_DOTTED_1_16);

      composer.appendNote(composer.TONE_A2, composer.TONELENGTH_1_8);
      composer.appendNote(composer.TONE_A2, composer.TONELENGTH_DOTTED_1_16);
      composer.appendNote(composer.TONE_PAUSE, composer.TONELENGTH_DOTTED_1_16);
      composer.appendNote(composer.TONE_G2, composer.TONELENGTH_DOTTED_1_16);
      composer.appendNote(composer.TONE_PAUSE, composer.TONELENGTH_DOTTED_1_16);
      composer.appendNote(composer.TONE_A2, composer.TONELENGTH_DOTTED_1_4);
      composer.appendNote(composer.TONE_G2, composer.TONELENGTH_DOTTED_1_16);
      composer.appendNote(composer.TONE_PAUSE, composer.TONELENGTH_DOTT

⌨️ 快捷键说明

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