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

📄 tukcanvas.java

📁 骑三轮车拉客的小游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      b = this.map.getCode(this.myCar.pos.x + 1, this.myCar.pos.y + 1);
    else if (this.imgCar.equals(this.imgCarRight))
      b = this.map.getCode(this.myCar.pos.x + 2, this.myCar.pos.y);
    else
      b = this.map.getCode(this.myCar.pos.x + 1, this.myCar.pos.y);

    if ((b < 3) || (b > 20))
    {
      return;
    }

    if (!(this.isFull)) {
      this.fuel -= 1;
      if (this.fuel < 0)
        newLife();
    }
    if (this.imgCar.equals(this.imgCarFront))
      this.myCar.pos.y += 1;
    if (this.imgCar.equals(this.imgCarRight))
      this.myCar.pos.x += 1;
    this.imgCar = this.imgCarRight;
    if (this.myCar.pos.x > this.mapX - this.imgCar.getWidth() / this.pixel)
      this.myCar.pos.x = (this.mapX - this.imgCar.getWidth() / this.pixel);
    if (this.cPos.x + this.width / this.pixel - this.myCar.pos.x + this.imgCar.getWidth() / this.pixel < this.fromEdge) {
      this.cPos.x += 1;
      if (this.cPos.x > this.mapX - this.width / this.pixel - 1)
        this.cPos.x = (this.mapX - this.width / this.pixel - 1);
    }

    repaint(); }

  public void goUp() {
    this.carDirection = 1;
    byte b = 0;
    if (this.imgCar.equals(this.imgCarRight))
      b = this.map.getCode(this.myCar.pos.x + 1, this.myCar.pos.y - 1);
    else
      b = this.map.getCode(this.myCar.pos.x, this.myCar.pos.y - 1);
    if ((b < 3) || (b > 20))
    {
      return;
    }

    if (!(this.isFull)) {
      this.fuel -= 1;
      if (this.fuel < 0)
        newLife();
    }

    if (this.imgCar.equals(this.imgCarRight)) {
      this.myCar.pos.x += 1;
      this.myCar.pos.y -= 1;
    }
    if (this.imgCar.equals(this.imgCarLeft))
      this.myCar.pos.y -= 1;
    if (this.imgCar.equals(this.imgCarBack))
      this.myCar.pos.y -= 1;
    this.imgCar = this.imgCarBack;
    if (this.myCar.pos.y < 0)
      this.myCar.pos.y = 0;
    if (this.myCar.pos.y - this.cPos.y < this.fromEdge) {
      this.cPos.y -= 1;
      if (this.cPos.y < 0)
        this.cPos.y = 0;
    }

    repaint();
  }

  public void goDown() {
    this.carDirection = 6;
    byte b = 0;
    if (this.imgCar.equals(this.imgCarRight))
      b = this.map.getCode(this.myCar.pos.x + 1, this.myCar.pos.y + 1);
    else if (this.imgCar.equals(this.imgCarFront))
      b = this.map.getCode(this.myCar.pos.x, this.myCar.pos.y + 2);
    else
      b = this.map.getCode(this.myCar.pos.x, this.myCar.pos.y + 1);
    if ((b < 3) || (b > 20))
    {
      return;
    }

    if (!(this.isFull)) {
      this.fuel -= 1;
      if (this.fuel < 0)
        newLife();
    }
    if (this.imgCar.equals(this.imgCarRight))
      this.myCar.pos.x += 1;

    if (this.imgCar.equals(this.imgCarFront))
      this.myCar.pos.y += 1;

    this.imgCar = this.imgCarFront;
    if (this.myCar.pos.y > this.mapY - this.imgCar.getHeight() / this.pixel)
      this.myCar.pos.y = (this.mapY - this.imgCar.getHeight() / this.pixel);
    if (this.cPos.y + this.height / this.pixel - this.myCar.pos.y + this.imgCar.getHeight() / this.pixel < this.fromEdge) {
      this.cPos.y += 1;
      if (this.cPos.y > this.mapY - this.height / this.pixel - 1)
        this.cPos.y = (this.mapY - this.height / this.pixel - 1);
    }
    repaint(); }

  public Man findPickUpMan() {
    for (int i = 0; i < this.manNo; ++i)
      if ((this.mans[i].pos.x == this.pickPos.x) && (this.mans[i].pos.y == this.pickPos.y))
        return this.mans[i];

    return null; }

  public void pickUp(int x, int y) {
    this.score += 20;
    this.count += 1;
    this.pickPos = new Point(x, y);
    this.isExplain = true;
    this.isHalt = true;
    Man man = findPickUpMan();
    try {
      this.destination = this.map.getObject(man.destCode).getName();
    }
    catch (Exception Exception) {
      this.destination = this.map.getObject(30).getName();
    }
    repaint();
    try {
      Thread.sleep(3000L); } catch (Exception Exception) {
    }
    this.isFull = true;
    this.isExplain = false;
    this.isHalt = false;
    this.map.setCode(x, y, man.roadCode);
    Thread t = new Thread(this.eachTime);
    this.eachTime.reset();
    t.start(); }

  public void pickDown() {
    this.eachTime.stop();
    calScore();
    this.isExplain = true;
    this.isHalt = true;
    this.isShowTime = true;
    repaint();
    this.fuel += 50;
    if (this.fuel > 150)
      this.fuel = 150;
    try
    {
      Thread.sleep(3500L); } catch (Exception localException) {
    }
    if (this.count >= this.manNo) {
      finishGame(false);
      return;
    }

    this.isExplain = false;
    this.isShowTime = false;
    this.isHalt = false;
    this.isFull = false;
    this.destination = "";
  }

  public void calScore() {
    int time = this.eachTime.getTimeCount();
    if (time > 150)
      this.score += 50;
    else
      this.score += 200 - time;
  }

  public void checkPlace(Point pt) {
    if ((this.destination.equals("")) || (this.destination == null))
      this.isFull = false;
    for (int i = pt.x - 1; i <= pt.x + 1; ++i)
      for (int j = pt.y - 1; j <= pt.y + 1; ++j) {
        byte code = this.map.getCode(i, j);
        if (code == 0) {
          checkPlace(new Point(i, j));
          return;
        }
        if (code == 20) {
          if (!(this.isFull))
            pickUp(i, j);

          return; }
        if ((code >= 21) && (code <= 41)) {
          this.isShowMsg = true;
          this.mesg = this.map.getObject(code).getName();
          repaint();
          if ((this.mesg.equals(this.destination)) && (this.isFull))
            pickDown();

          return;
        }
      }

    this.isShowMsg = false;
    this.mesg = "";
    repaint(); }

  public void finishGame(boolean isG) {
    this.thread = null;
    this.isEnable = false;
    this.isGameOver = isG;
    if (isG)
      this.mesgMode = -1;
    else
      this.mesgMode = 0;
    this.totalTime.stop();
    repaint();
    try {
      Thread.sleep(5000L); } catch (Exception localException) {
    }
    this.isMenuShow = true;
    this.isExitMenu = isG;
    this.isHalt = true;
    repaint();
  }

  public void actionRepeat(int key) {
    int vStep;
    int code = getGameAction(key);
    if (!(this.isViewMapMode)) {
      if (!(this.isEnable)) return;
      if (this.isHalt) return;

      checkPlace(this.myCar.getPosition());
    }
    switch (code) {
    case 2:
      goLeft();

      break;
    case 5:
      goRight();

      break;
    case 1:
      goUp();

      break;
    case 6:
      goDown();
    case 3:
    case 4:
    default:
      return;

      vStep = 5;
      switch (code)
      {
      case 2:
        if (this.cMapPos.x > vStep)
          this.cMapPos.x -= vStep;
        else
          this.cMapPos.x = 0;

        repaint();

        break;
      case 5:
        if ((this.map.sizeX - this.cMapPos.x) * this.viewMapPixel > this.width + vStep)
          this.cMapPos.x += vStep;

        repaint();

        break;
      case 1:
        if (this.cMapPos.y > vStep)
          this.cMapPos.y -= vStep;
        else
          this.cMapPos.y = 0;

        repaint();

        break;
      case 6:
        if ((this.map.sizeY - this.cMapPos.y) * this.viewMapPixel > this.height + vStep)
          this.cMapPos.y += vStep;

        repaint();
      case 3:
      case 4:
      }
    }
  }

  protected void keyPressed(int keyCode)
  {
    switch (keyCode)
    {
    case 49:
      this.isViewMapMode = (!(this.isViewMapMode));
      if (this.isViewMapMode) {
        if (this.score - 5 < 0)
          return;
        this.score -= 5;
        this.isHalt = true;

        if (this.cPos.x > this.width / this.viewMapPixel * 2) {
          if (this.cPos.x + this.width / this.viewMapPixel * 2 < this.map.sizeX * this.viewMapPixel)
            this.cMapPos.x = (this.cPos.x - this.width / this.viewMapPixel * 2);
          else
            this.cMapPos.x = (this.map.sizeX * this.viewMapPixel - this.width / this.viewMapPixel * 2);

        }
        else {
          this.cMapPos.x = 0;
        }

        if (this.cPos.y > this.height / this.viewMapPixel * 2) {
          if (this.cPos.y + this.height / this.viewMapPixel * 2 < this.map.sizeY * this.viewMapPixel)
            this.cMapPos.y = (this.cPos.y - this.height / this.viewMapPixel * 2);
          else
            this.cMapPos.y = (this.map.sizeY * this.viewMapPixel - this.height / this.viewMapPixel * 2);

        }
        else
          this.cMapPos.y = 0;
      }
      else
      {
        this.isHalt = false;
      }

      repaint();
      break;
    case -6:
      if (this.isMenuShow) {
        if (this.isExitMenu)
          this.midlet.closeApp();
        else
          initGame();
      } else {
        this.isMenuShow = true;
        this.isExitMenu = true;
        this.isHalt = true;
      }
      repaint();
      break;
    case -7:
      if (this.isMenuShow) {
        if (this.isExitMenu) {
          this.isMenuShow = false;
          this.isHalt = false;
          repaint();
        } else {
          this.midlet.closeApp();
        }

      }

    }

    this.carDirection = getGameAction(keyCode);
    if (this.isViewMapMode)
      actionRepeat(keyCode);
  }

  protected void keyReleased(int keyCode)
  {
  }

  protected void keyRepeated(int keyCode)
  {
  }

  protected void pointerDragged(int x, int y)
  {
  }

  class TimeCounter
  implements Runnable
  {
    boolean isRunning;
    boolean isPause;
    int hour;
    int second;
    int minite;
    int time;
    private final TukCanvas this$0;

    TimeCounter()
    {
      this.this$0 = this$0;
      this.isRunning = true;
    }

    public void reset() {
      this.hour = (this.minite = this.second = 0);
      this.isRunning = true;
      this.isPause = false; }

    public void stop() {
      this.isRunning = false; }

    public String getTime() {
      String h = String.valueOf(String.valueOf(this.hour)).concat("");
      for (int i = h.length(); i < 2; ++i)
        h = "0".concat(String.valueOf(String.valueOf(h)));
      String m = String.valueOf(String.valueOf(this.minite)).concat("");
      for (int i = m.length(); i < 2; ++i)
        m = "0".concat(String.valueOf(String.valueOf(m)));
      String s = String.valueOf(String.valueOf(this.second)).concat("");
      for (int i = s.length(); i < 2; ++i)
        s = "0".concat(String.valueOf(String.valueOf(s)));

      return String.valueOf(String.valueOf(new StringBuffer(String.valueOf(String.valueOf(h))).append(" : ").append(m).append(" : ").append(s))); }

    public void pause() {
      this.isPause = true; }

    public void start() {
      this.isPause = false; }

    public int getTimeCount() {
      return this.time; }

    public void run() {
      while (true) {
        do {
          do { do { while (true) { if (this.isPause);
                if (!(this.this$0.isHalt)) break; 
              }
              try {
                Thread.sleep(1000L); } catch (Exception localException) {
              }
              if (!(this.isRunning))
              {
                return;
              }
              this.time += 1;
              this.second += 1; }
            while (this.second <= 59);
            this.second = 0;
            this.minite += 1; }
          while (this.minite <= 59);
          this.hour += 1; }
        while (this.hour <= 99);
        reset();
      }
    }
  }

  class CarRunner
  implements Runnable
  {
    private final TukCanvas this$0;

    CarRunner()
    {
      this.this$0 = this$0; }

    public void run() {
      if ((this.this$0.isExplain) || (this.this$0.isHalt));
      try {
        this.this$0.actionRepeat(this.this$0.getKeyCode(this.this$0.carDirection));
        Thread.sleep(this.this$0.speed);
      }
      catch (Exception localException)
      {
      }
    }
  }
}

⌨️ 快捷键说明

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