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

📄 ufocanvas.java

📁 非常好的几个案例,19个小游戏源码。。。 希望大家喜欢
💻 JAVA
字号:
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;
import java.io.*;

public class UFOCanvas extends GameCanvas implements Runnable {
  private Display  display;
  private boolean  sleeping;
  private long     frameDelay;
  private Random   rand;
  private Sprite   ufoSprite;
  private int      ufoXSpeed, ufoYSpeed;
  private Sprite[] roidSprite = new Sprite[3];

  public UFOCanvas(Display d) {
    super(true);
    display = d;

    // Set the frame rate (30 fps)
    frameDelay = 33;
  }
  
  public void start() {
    // Set the canvas as the current screen
    display.setCurrent(this);

    // Initialize the random number generator
    rand = new Random();

    // Initialize the UFO and roids sprites
    ufoXSpeed = ufoYSpeed = 0;
    try {
      ufoSprite = new Sprite(Image.createImage("/Saucer.png"));
      ufoSprite.setPosition((getWidth() - ufoSprite.getWidth()) / 2,
        (getHeight() - ufoSprite.getHeight()) / 2);

      Image img = Image.createImage("/Roid.png");
      roidSprite[0] = new Sprite(img, 42, 35);
      roidSprite[1] = new Sprite(img, 42, 35);
      roidSprite[2] = new Sprite(img, 42, 35);
    }
    catch (IOException e) {
      System.err.println("Failed loading images!");
    }

    // Start the animation thread
    sleeping = false;
    Thread t = new Thread(this);
    t.start();
  }
  
  public void stop() {
    // Stop the animation
    sleeping = true;
  }
  
  public void run() {
    Graphics g = getGraphics();
    
    // The main game loop
    while (!sleeping) {
      update();
      draw(g);
      try {
        Thread.sleep(frameDelay);
      }
      catch (InterruptedException ie) {}
    }
  }
  
  private void update() {
    // Process user input to control the UFO speed
    int keyState = getKeyStates();
    if ((keyState & LEFT_PRESSED) != 0)
      ufoXSpeed--;
    else if ((keyState & RIGHT_PRESSED) != 0)
      ufoXSpeed++;
    if ((keyState & UP_PRESSED) != 0)
      ufoYSpeed--;
    else if ((keyState & DOWN_PRESSED) != 0)
      ufoYSpeed++;
    ufoXSpeed = Math.min(Math.max(ufoXSpeed, -8), 8);
    ufoYSpeed = Math.min(Math.max(ufoYSpeed, -8), 8);

    // Move the UFO sprite
    ufoSprite.move(ufoXSpeed, ufoYSpeed);
    checkBounds(ufoSprite);

    // Update the roid sprites
    for (int i = 0; i < 3; i++) {
      // Move the roid sprites
      roidSprite[i].move(i + 1, 1 - i);
      checkBounds(roidSprite[i]);

      // Increment the frames of the roid sprites
      if (i == 1)
        roidSprite[i].prevFrame();
      else
        roidSprite[i].nextFrame();

      // Check for a collision between the UFO and roids
      if (ufoSprite.collidesWith(roidSprite[i], true)) {
        // Play an alert sound
        AlertType.ERROR.playSound(display);

        // Reset the sprite positions and speeds
        ufoSprite.setPosition((getWidth() - ufoSprite.getWidth()) / 2,
          (getHeight() - ufoSprite.getHeight()) / 2);
        ufoXSpeed = ufoYSpeed = 0;
        for (int j = 0; j < 3; j++)
          roidSprite[j].setPosition(0, 0);

        // No need to continue updating the roid sprites
        break;
      }
    }
  }
  
  private void draw(Graphics g) {
    // Clear the display
    g.setColor(0x000000);
    g.fillRect(0, 0, getWidth(), getHeight());
    
    // Draw the UFO sprite
    ufoSprite.paint(g);

    // Draw the roid sprites
    for (int i = 0; i < 3; i++)
      roidSprite[i].paint(g);
    
    // Flush the offscreen graphics buffer
    flushGraphics();
  }

  private void checkBounds(Sprite sprite) {
    // Wrap the sprite around the screen if necessary
    if (sprite.getX() < -sprite.getWidth())
      sprite.setPosition(getWidth(), sprite.getY());
    else if (sprite.getX() > getWidth())
      sprite.setPosition(-sprite.getWidth(), sprite.getY());
    if (sprite.getY() < -sprite.getHeight())
      sprite.setPosition(sprite.getX(), getHeight());
    else if (sprite.getY() > getHeight())
      sprite.setPosition(sprite.getX(), -sprite.getHeight());
  }
}

⌨️ 快捷键说明

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