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

📄 sprite.java

📁 一个非常好的赛车游戏代码
💻 JAVA
字号:

import javax.microedition.lcdui.*;

public class Sprite
{
  protected Image image;
  protected long x;
  protected long y;
  protected long vx;
  protected long vy;
  protected int width;
  protected int height;
  protected int numFrames;
  protected int frame;
  protected boolean visible;

  public Sprite(Image image, int width, int height, int numFrames)
      throws Exception
  {
    this.image = image;
    this.width = width;
    this.height = height;
    this.numFrames = numFrames;
    if (image.getHeight() != height)
        throw new Exception("Height not correct.");
    if (image.getWidth() / numFrames != width)
        throw new Exception("Width not correct.");
  }

  public boolean collide(Sprite sprite)
  {
    if ((Float.getInteger(x) + width) > Float.getInteger(sprite.getX()) &&
        Float.getInteger(x) < Float.getInteger(sprite.getX()) + sprite.getWidth() &&
        (Float.getInteger(y) + height) > Float.getInteger(sprite.getY()) &&
        Float.getInteger(y) < Float.getInteger(sprite.getY()) + sprite.getHeight())
            return true;
    return false;
  }

  public void paint(Graphics g)
  {
    g.setClip((int)Float.getInteger(x), (int)Float.getInteger(y),
        width, height);
    g.drawImage(image, (int)Float.getInteger(x) - frame * width,
        (int)Float.getInteger(y), Graphics.TOP | Graphics.LEFT);
    g.setClip(0, 0, Cache.width, Cache.height);
  }

  public Image getImage()
  {
    return this.image;
  }

  public long getX()
  {
    return this.x;
  }

  public void setX(long x)
  {
    this.x = x;
  }

  public long getY()
  {
    return this.y;
  }

  public void setY(long y)
  {
    this.y = y;
  }

  public long getVx()
  {
    return this.vx;
  }

  public void setVx(long vx)
  {
    this.vx = vx;
  }

  public long getVy()
  {
    return this.vy;
  }

  public void setVy(long vy)
  {
    this.vy = vy;
  }

  public int getWidth()
  {
    return this.width;
  }

  public int getHeight()
  {
    return this.height;
  }

  public int getFrame()
  {
    return this.frame;
  }

  public void setFrame(int frame)
      throws Exception
  {
    if (frame < 0 || frame > numFrames - 1)
        throw new Exception ("Not correct frame number.");
    this.frame = frame;
  }

  public boolean isVisible()
  {
    return this.visible;
  }

  public void setVisible(boolean visible)
  {
    this.visible = visible;
  }
}

⌨️ 快捷键说明

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