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

📄 socanvas.java

📁 塞迪网校J2ME移动应用开发教程的所有源代码.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                // Create an explosion
                addExplosion(timmySprite[j]);

                // Hide the sprites and update the score
                timmySprite[j].setVisible(false);
                missileSprite[i].setVisible(false);
                score += 20;
                break;
              }
          }
        }
        // The missile is an alien missile
        else {
          // Did the missile hit the player car sprite
          if (missileSprite[i].collidesWith(playerSprite, false)) {
            // Play a wave sound for the car getting destroyed
            try {
              explosionPlayer.start();
            }
            catch (MediaException me) {
            }

            // Create an explosion
            addExplosion(playerSprite);

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

            // Hide the missile sprite
            missileSprite[i].setVisible(false);

            // See whether the game is over
            if (carsLeft-- == 0) {
              // Stop the music
              try {
                musicPlayer.stop();
              }
              catch (MediaException me) {
              }

              // Play a wave sound for the game ending
              try {
                gameoverPlayer.start();
              }
              catch (MediaException me) {
              }

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

              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);

    // 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);

    if (gameOver) {
      // Draw the game over message and score
      g.setColor(255, 255, 255); // white
      g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE));
      g.drawString("GAME OVER", 90, 40, Graphics.TOP | Graphics.HCENTER);
      g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM));
      g.drawString("Final Score : " + score, 90, 70, Graphics.TOP | Graphics.HCENTER);
    }

    // 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;
      }
  }
}

⌨️ 快捷键说明

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