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

📄 socanvas.java

📁 塞迪网校J2ME移动应用开发教程的所有源代码.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
              catch (MediaException me) {
              }

              // Hide the player car
              playerSprite.setVisible(false);

              // Update the high score list
              updateHiScores();

              gameOver = true;
              return;
            }
          }
        }

        missileSprite[i].update();
      }
    }

    // Randomly add a new alien (chance is based on score)
    if (score < 250) {
      if (rand.nextInt() % 40 == 0)
        addAlien();
    }
    else if (score < 500) {
      if (rand.nextInt() % 20 == 0)
        addAlien();
    }
    else if (score < 1000) {
      if (rand.nextInt() % 10 == 0)
        addAlien();
    }
    else {
      if (rand.nextInt() % 5 == 0)
        addAlien();
    }

    // Randomly fire a missile from an alien
    if (rand.nextInt() % 4 == 0) {
      switch (Math.abs(rand.nextInt() % 3)) {
      // Fire a missile from a Blobbo alien
      case 0:
        for (int i = 0; i < 3; i++)
          if (blobboSprite[i].isVisible()) {
            addMissile(blobboSprite[i]);
            break;
          }
        break;
      // Fire a missile from a Jelly alien
      case 1:
        for (int i = 0; i < 3; i++)
          if (jellySprite[i].isVisible()) {
            addMissile(jellySprite[i]);
            break;
          }
        break;
      // Fire a missile from a Timmy alien
      case 2:
        for (int i = 0; i < 3; i++)
          if (timmySprite[i].isVisible()) {
            addMissile(timmySprite[i]);
            break;
          }
        break;
      }
    }
  }

  private void draw(Graphics g) {
    // Draw the starry night background
    g.drawImage(background, 0, 0, Graphics.TOP | Graphics.LEFT);
    
    // Draw the layers
    layers.paint(g, 0, 0);

    if (gameOver) {
      // Draw the splash screen image and score
      g.drawImage(splash, 90, 10, Graphics.TOP | Graphics.HCENTER);
      g.setColor(255, 255, 255); // white
      g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE));
      for (int i = 0; i < 5; i++)
        g.drawString(Integer.toString(hiScores[i]), 90, 90 + (i * 15), Graphics.TOP | Graphics.HCENTER);
    }
    else {
    // Draw the remaining cars and the score
      for (int i = 0; i < carsLeft; i++)
        g.drawImage(smallCar, 2 + (i * 20), 2, Graphics.TOP | Graphics.LEFT);
      g.setColor(255, 255, 255); // white
      g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
      g.drawString(Integer.toString(score), 175, 2, Graphics.TOP | Graphics.RIGHT);
    }

    // Flush the offscreen graphics buffer
    flushGraphics();
  }

  private void newGame() {
    // Initialize the game variables
    gameOver = false;
    score = 0;
    carsLeft = 3;

    // Initialize the player car sprite
    playerSprite.setPosition(0, getHeight() - playerSprite.getHeight() - 10);
    playerSprite.setXSpeed(4);
    playerSprite.setYSpeed(0);
    playerSprite.setVisible(true);

    // Initialize the alien and explosion sprites
    for (int i = 0; i < 3; i++) {
      blobboSprite[i].setVisible(false);
      jellySprite[i].setVisible(false);
      timmySprite[i].setVisible(false);
      explosionSprite[i].setVisible(false);
    }

    // Initialize the missile sprites
    for (int i = 0; i < 10; i++) {
      missileSprite[i].setVisible(false);
    }

    // Start the music (at the beginning)
    try {
      musicPlayer.setMediaTime(0);
      musicPlayer.start();
    }
    catch (MediaException me) {
    }
  }

  private void placeSprite(Sprite sprite) {
    // Place the sprite at a random position
    sprite.setPosition(Math.abs(rand.nextInt() % (getWidth() - 50) + 25),
      Math.abs(rand.nextInt() % (getHeight() - 100)) + 25);
  }

  private void addAlien() {
    switch (Math.abs(rand.nextInt() % 3)) {
    // Add a Blobbo alien
    case 0:
      for (int i = 0; i < 3; i++)
        if (!blobboSprite[i].isVisible()) {
          placeSprite(blobboSprite[i]);
          blobboSprite[i].setVisible(true);
          break;
        }
      break;
    // Add a Jelly alien
    case 1:
      for (int i = 0; i < 3; i++)
        if (!jellySprite[i].isVisible()) {
          placeSprite(jellySprite[i]);
          jellySprite[i].setVisible(true);
          break;
        }
      break;
    // Add a Timmy alien
    case 2:
      for (int i = 0; i < 3; i++)
        if (!timmySprite[i].isVisible()) {
          placeSprite(timmySprite[i]);
          timmySprite[i].setVisible(true);
          break;
        }
      break;
    }
  }

  private void addMissile(MovingSprite sprite) {
    for (int i = 0; i < 10; i++)
      if (!missileSprite[i].isVisible()) {
        switch (Math.abs(sprite.getXSpeed())) {
        // Fire a Blobbo missile
        case 3:
          missileSprite[i].setFrame(1);
          missileSprite[i].setPosition(sprite.getX() + 5, sprite.getY() + 21);
          missileSprite[i].setXSpeed(sprite.getXSpeed() / 2);
          missileSprite[i].setYSpeed(5);
          break;
        // Fire a Jelly missile
        case 1:
          missileSprite[i].setFrame(2);
          missileSprite[i].setPosition(sprite.getX() + 5, sprite.getY() + 21);
          missileSprite[i].setXSpeed(0);
          missileSprite[i].setYSpeed(4);
          break;
        // Fire a Timmy missile
        case 5:
          missileSprite[i].setFrame(3);
          missileSprite[i].setPosition(sprite.getX() + 5, sprite.getY() + 11);
          missileSprite[i].setXSpeed(sprite.getXSpeed() / 2);
          missileSprite[i].setYSpeed(3);
          break;
        // Fire a player missile
        case 2:
        case 4:
          missileSprite[i].setFrame(0);
          missileSprite[i].setPosition(sprite.getX() + 6, sprite.getY() - 11);
          missileSprite[i].setXSpeed(0);
          missileSprite[i].setYSpeed(-4);
          break;
        }

        // Show the missile
        missileSprite[i].setVisible(true);

        break;
      }
  }

  private void addExplosion(MovingSprite sprite) {
    for (int i = 0; i < 3; i++)
      if (!explosionSprite[i].isVisible()) {
        // Add an explosion where the moving sprite is located
        explosionSprite[i].setFrame(0);
        explosionSprite[i].setPosition(sprite.getX(), sprite.getY());
        explosionSprite[i].setVisible(true);
        break;
      }
  }

  private void updateHiScores() {
    // See whether the current score made the hi score list
    int i;
    for (i = 0; i < 5; i++)
      if (score > hiScores[i])
        break;

    // Insert the current score into the hi score list
    if (i < 5) {
      for (int j = 4; j > i; j--) {
        hiScores[j] = hiScores[j - 1];
      }
      hiScores[i] = score;
    }
  }

  private void readHiScores()
  {
    // Open the hi scores record store
    RecordStore rs = null;
    try {
      rs = RecordStore.openRecordStore("HiScores", false);
    }
    catch (Exception e) {
    }

    if (rs != null) {
      // Read the hi score records
      try {
        int    len;
        byte[] recordData = new byte[8];

        for (int i = 1; i <= rs.getNumRecords(); i++) {
          // Re-allocate record holder if necessary
          if (rs.getRecordSize(i) > recordData.length)
            recordData = new byte[rs.getRecordSize(i)];

          // Read the score and store it in the hi score array
          len = rs.getRecord(i, recordData, 0);
          hiScores[i - 1] = (Integer.parseInt(new String(recordData, 0, len)));
        }
      }
      catch (Exception e) {
        System.err.println("Failed reading hi scores!");
      }

      // Close the record store
      try {
        rs.closeRecordStore();
      }
      catch (Exception e) {
        System.err.println("Failed closing hi score record store!");
      }
    }
    else {
      // The record store doesn't exist, so initialize the scores to 0
      for (int i = 0; i < 5; i++)
        hiScores[i] = 0;
    }
  }

  private void writeHiScores()
  {
    // Delete the previous hi scores record store
    try {
      RecordStore.deleteRecordStore("HiScores");
    }
    catch (Exception e) {
    }

    // Create the new hi scores record store
    RecordStore rs = null;
    try {
      rs = RecordStore.openRecordStore("HiScores", true);
    }
    catch (Exception e) {
      System.err.println("Failed creating hi score record store!");
    }

    // Write the scores
    for (int i = 0; i < 5; i++) {
      // Format each score for writing
      byte[] recordData = Integer.toString(hiScores[i]).getBytes();

      try {
        // Write the score as a record
        rs.addRecord(recordData, 0, recordData.length);
      }
      catch (Exception e) {
        System.err.println("Failed writing hi scores!");
      }
    }

    // Close the record store
    try {
      rs.closeRecordStore();
    }
    catch (Exception e) {
      System.err.println("Failed closing hi score record store!");
    }
  }
}

⌨️ 快捷键说明

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