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

📄 gamecanvas.java~1~

📁 泡泡龙游戏原代码
💻 JAVA~1~
📖 第 1 页 / 共 2 页
字号:
package com.whkjava.popo;

/**
 * <p>Title: KMPoPo</p>
 * <p>Description: KMPoPo</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author HUAKUI_WANG
 * @version 1.0
 */

import javax.microedition.lcdui.Canvas;
import java.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import com.whkjava.util.midp.ImgUtil;

class GameCanvas
    extends Canvas
    implements Runnable, CommandListener {

  public static final int OFSET = -4;
  public static final int ONSIGN = -1;
  public static final int ONTITLE = 0;
  public static final int ONGAME = 1;
  public static final int ONOPTION = 2;
  public static final int STGOFX = 16;
  public static final int STGOFY = 20;
  public static final int S_PYONPA = 0;
  public static final int S_ERASE = 1;
  public static final int S_SHOT = 2;
  public static final int S_GOVER = 3;
  public static int trX;
  public static int trY;

  private int iS;
  private int pS;
  private int bgS;
  private int mV;
  private int sC;
  private int dR;
  private int lV;
  private int dV;
  private int eC;
  private int bR;
  private int kY;
  private int bobP;
  private int bAnim;
  private int mH;
  private int mS;
  private int eB;
  private int dB;
  private boolean sE;
  private boolean kF;
  private boolean gF;
  private boolean dF;
  private boolean upA;
  private boolean upB;
  private Bobble pA;
  //private Font sFont;

  public Image ofSc1;
  public Image ofSc2;
  public Graphics ofSg2;
  public Image ofSc3;
  public Image bomI;
  public Image awaI[];
  public Image plyI[];
  public Image numI[];
  private int aWidth;

  private KMPoPoMIDlet m_MIDlet;
  private Thread mThread;
  private RecordStore rStore;
  private Command cmdPause;
  private Command cmdExit;
  private Command cmdStart;
  private boolean pauseFlag;
  private int nGameState;
  private boolean resumeFlag;
  private boolean mNewGameFlg;

  public GameCanvas(KMPoPoMIDlet midlet) {
    resumeFlag = false;
    pauseFlag = false;
    rStore = null;

    try {
      rStore = RecordStore.openRecordStore("KMPOPO", true);
    }
    catch (Exception exception) {}
    loadData();
    loadImages();
    setBGImage();

    bgS = 0;

    cmdStart = new Command("开始", 1, 1); ;
    cmdPause = new Command("暂停", 1, 1);
    cmdExit = new Command("退出", 1, 2);
    mThread = new Thread(this);
    addCommand(cmdPause);
    addCommand(cmdExit);
    setCommandListener(this);

    init(mH, sE);
    mNewGameFlg = true;
    nGameState = 1;

    m_MIDlet = midlet;
    mThread.start();
  }

  private void loadData() {
    if (rStore == null) {
      return;
    }
    Object obj = null;
    try {
      if (rStore.getNumRecords() == 0) {
        ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
        DataOutputStream dataoutputstream = new DataOutputStream(
            bytearrayoutputstream);
        try {
          dataoutputstream.writeInt(0);
          dataoutputstream.writeBoolean(false);
        }
        catch (IOException ioexception) {}
        byte abyte0[] = bytearrayoutputstream.toByteArray();
        rStore.addRecord(abyte0, 0, abyte0.length);
      }
      else {
        byte abyte1[] = rStore.getRecord(1);
        if (abyte1 != null) {
          ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(
              abyte1);
          DataInputStream datainputstream = new DataInputStream(
              bytearrayinputstream);
          mH = datainputstream.readInt();
          sE = datainputstream.readBoolean();
        }
        else {
          mH = 0;
          sE = false;
        }
      }
    }
    catch (Exception exception) {}
  }

  private boolean loadImages() {
    awaI = new Image[8];
    for (int i = 0; i < awaI.length; i++) {
      awaI[i] = ImgUtil.createImage("/imgs/awa" + (i + 1) + ".png");

    }
    numI = new Image[10];
    for (int j = 0; j < numI.length; j++) {
      numI[j] = ImgUtil.createImage("/imgs/num" + j + ".png");

    }
    bomI = ImgUtil.createImage("/imgs/bom.png");
    plyI = new Image[5];
    plyI[0] = ImgUtil.createImage("/imgs/bub1.png");
    plyI[1] = ImgUtil.createImage("/imgs/bub2.png");
    plyI[2] = ImgUtil.createImage("/imgs/bub3.png");
    plyI[3] = ImgUtil.createImage("/imgs/bub4.png");
    plyI[4] = ImgUtil.createImage("/imgs/bub5.png");
    return true;
  }

  public boolean setBGImage() {
    ofSc1 = ImgUtil.createImage("/imgs/bg128LS.png");
    ofSc3 = ImgUtil.createImage("/imgs/sg.png");
    ofSc2 = Image.createImage(ofSc3.getWidth(), ofSc3.getHeight());
    ofSg2 = ofSc2.getGraphics();
    return true;
  }

  public void init(int i, boolean flag) {
    iS = 0;
    pS = 0;
    lV = 0;
    dV = 0;
    eC = 0;
    sC = 0;
    bR = 0;
    dR = 0xbbb61;
    mV = 21;
    kF = false;
    dF = false;
    mS = 0;
    eB = 0;
    dB = 0;
    bobP = 0;
    bAnim = 0;
    upA = false;
    upB = false;
    mH = i;
    sE = flag;
    aWidth = awaI[0].getWidth();
    if (pA == null) {
      pA = new Bobble();
    }
    Bobble.init();
    Bobble.sAID();
    drawBobbles(ofSg2, 0);
    gF = true;
  }

  private void drawGameOver(Graphics g) {
    int i = Font.getDefaultFont().getHeight();
    g.setColor(0xffff00);
    g.drawString("GAME",
                 (ofSc3.getWidth() - Font.getDefaultFont().stringWidth("GAME")) /
                 2 + 4, 32, 16 | 4);
    g.drawString("OVER",
                 (ofSc3.getWidth() - Font.getDefaultFont().stringWidth("OVER")) /
                 2 + 4, 34 + i, 16 | 4);
  }

  private void drawBobbles(Graphics g, int i) {
    int i2 = 0;
    int j = sC;
    int l = bR & 0xff;
    if (j >= ( (dR >> dV * 2 & 3) + 3) - 2) {
      if (j == ( (dR >> dV * 2 & 3) + 3) - 2) {
        if ( (l & 3) == 3) {
          i2 = 1;
        }
      }
      else
      if ( (l & 1) == 1) {
        i2 = 1;
      }
      if (l >= 240) {
        bR = 0;
      }
      else {
        bR++;
      }
    }
    g.drawImage(ofSc3, 0, 0, 16 | 4);
    upB = false;
    for (int k1 = 9; k1 >= 0; k1--) {
      int i1 = 0;
      if (Bobble.aA[5 + 6 * k1] == -1) {
        i1 = aWidth / 2;
      }
      for (int j1 = 0; j1 < 6; j1++) {
        int l1 = j1 + 6 * k1;
        int k = Bobble.aA[l1];
        if (k > 0) {
          if ( (k & 0x10) == 16) {
            if (pS == 4) {
              k &= 0xf;
              g.drawImage(bomI, j1 * aWidth + i1, k1 * aWidth, 20);
            }
            else {
              k &= 0xf;
              g.drawImage(awaI[k - 1], j1 * aWidth + i1,
                          k1 * aWidth + aWidth / 2, 20);
            }
            Bobble.cBA(l1);
          }
          else {
            k &= 0xf;
            if (pS == -1) {
              if (k1 >= i) {
                g.drawImage(awaI[7], j1 * aWidth + i1, k1 * aWidth, 20);
              }
              else {
                g.drawImage(awaI[k - 1], j1 * aWidth + i1, k1 * aWidth, 20);
              }
            }
            else {
              g.drawImage(awaI[k - 1], j1 * aWidth + i1, k1 * aWidth + i2, 20);
              if (k1 > 5) {
                upB = true;
              }
            }
          }
        }
      }

    }

    drawMoveBobble(g);
  }

  public void drawMoveBobble(Graphics g) {
    int k = Bobble.aN - 1;
    g.drawImage(awaI[k], ofSc2.getWidth() - aWidth, ofSc2.getHeight() - aWidth,
                16 | 4);
    if (pS >= 0) {
      g.drawImage(plyI[bobP], 0, ofSc2.getHeight() - plyI[bobP].getHeight(), 20);
      if (bobP > 0) {
        bobP = 0;
      }
    }
    else {
      g.drawImage(plyI[4], 0, ofSc2.getHeight() - plyI[bobP].getHeight(), 20);
    }
    if (upA) {
      mV = 21;
      upA = false;
    }
    int i = 28 + (Bobble.CO[mV] << 4) / 1000 + -4;
    int j = 84 + (Bobble.SI[mV] << 4) / 1000 + -4;
    k = 28 + (Bobble.CO[ (21 - mV) + 21] * 8) / 1000 + -4;
    int i1 = 84 + (Bobble.SI[ (21 - mV) + 21] * -8) / 1000 + -4;
    g.setColor(255, 255, 0);
    g.drawLine(i, j, k, i1);
    g.drawLine(i, j, ARW1[mV][0] + -4, ARW1[mV][1] + -4);
    g.drawLine(i, j, ARW2[mV][0] + -4, ARW2[mV][1] + -4);
    if (gF && pS < 3) {
      int l = Bobble.aM - 1;
      g.drawImage(awaI[l], Bobble.aX - aWidth / 2, Bobble.aY - aWidth / 2,
                  16 | 4);
    }
    else
    if (pS == 0) {
      g.drawImage(awaI[Bobble.aM - 1], Bobble.aX - aWidth / 2,
                  Bobble.aY - aWidth / 2, 16 | 4);
    }
  }

  private void drawScore(Graphics g) {
    int i = lV + 1;
    int j = mS;
    if (j > 99999) {
      j = 99999;
    }
    if (mS > mH) {
      mH = mS;
      setGConf(mH, sE);
      saveData();
    }
    int k = mH;
    if (k > 99999) {
      k = 99999;
    }
    try {
      char ac[] = Integer.toString(i).toCharArray();
      int l = ac.length;
      for (int i1 = 0; i1 < l; i1++) {
        g.drawImage(numI[Character.digit(ac[i1], 10)], 84 + (5 - (l - i1)) * 8,
                    24, 16 | 4);

      }
      ac = Integer.toString(j).toCharArray();
      l = ac.length;
      for (int j1 = 0; j1 < l; j1++) {
        g.drawImage(numI[Character.digit(ac[j1], 10)], 84 + (5 - (l - j1)) * 8,
                    58, 16 | 4);

      }
      ac = Integer.toString(k).toCharArray();
      l = ac.length;
      for (int k1 = 0; k1 < l; k1++) {
        g.drawImage(numI[Character.digit(ac[k1], 10)], 84 + (5 - (l - k1)) * 8,
                    92, 16 | 4);

      }
    }
    catch (Exception exception) {}
  }

  public void setGConf(int i, boolean flag) {
    mH = i;
    sE = flag;
  }

  public void saveData() {
    if (rStore == null) {
      return;
    }
    ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
    DataOutputStream dataoutputstream = new DataOutputStream(
        bytearrayoutputstream);
    try {
      dataoutputstream.writeInt(mH);
      dataoutputstream.writeBoolean(sE);
    }
    catch (IOException ioexception) {}
    byte abyte0[] = bytearrayoutputstream.toByteArray();
    try {
      rStore.setRecord(1, abyte0, 0, abyte0.length);
    }
    catch (RecordStoreException recordstoreexception) {}
  }

  public void commandAction(Command command, Displayable displayable) {
    if (command == cmdExit) {
      m_MIDlet.notifyDestroyed();
    }
    else
    if (command == cmdPause) {
      pauseFlag = !pauseFlag;
      if(pauseFlag){

      }else{

      }
    }
    if (command == cmdStart) {
    }
  }

  protected void hideNotify() {
    pauseFlag = true;
  }

  protected void showNotify() {
    pauseFlag = false;
    resumePlease();
  }

  public void resumePlease() {
    resumeFlag = true;
    if (pS < 0) {
      drawBobbles(ofSg2, 0);
      drawGameOver(ofSg2);
    }
    repaint();
    serviceRepaints();
  }

  public void retryPlease() {
    init(mH, sE);
    repaint();
  }

  public void run() {
    boolean flag = false;
    byte byte0 = 0;
    while (gF) {
      while (pauseFlag) {
        try {
          Thread.sleep(100L);
        }
        catch (Exception exception1) {}
      }
      try {
        if (pS == 4) {
          Thread.sleep(100L);
        }
        else
        if (pS == 5) {
          Thread.sleep(200L);
        }
        else {
          Thread.sleep(10L);
        }
      }
      catch (Exception exception2) {}
      if (pS == 2) {
        for (int i = 0; i < 8; i++) {
          int k;
          if ( (k = Bobble.move()) >= 2) {
            continue;

⌨️ 快捷键说明

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