showbullet.java

来自「j2me 小的子弹精灵,简单介绍sprite 的一个例子」· Java 代码 · 共 56 行

JAVA
56
字号
package spritebullet;

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;

public class ShowBullet
    extends GameCanvas  implements Runnable,CommandListener {
  Bullet bullet;
  Graphics g;
  int x, y, width;

  public ShowBullet(int x, int y, int width) {
    super(true);
    this.x = x;
    this.y = y;
    this.width = width;
    addCommand(new Command("Exit", Command.EXIT, 1));
    setCommandListener(this);
    g = this.getGraphics();
    bullet = new Bullet();
    Thread t = new Thread(this);
    t.start();
  }

  public void commandAction(Command command, Displayable displayable) {
    if (command.getCommandType() == Command.EXIT) {
      MainMIDlet.quitApp();
    }
  }

  public void input() {
    if (getKeyStates() == FIRE_PRESSED) {
      bullet.addBullet(getWidth()/2,getHeight()/2,32);
    }
  }

  public void clear() {
    g.setColor(0, 0, 0);
    g.fillRect(0, 0, this.getWidth(), this.getHeight());
  }

  public void run() {
    while (true) {
      clear();
      input();
      bullet.DrawSelf(g);
      this.flushGraphics();
      bullet.Move();
      try {
        Thread.sleep(100);
      }
      catch (Exception e) {}
    }
  }
}

⌨️ 快捷键说明

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