如何将处理中的矩形更改为我正在制作的游戏的图像?

How to change a rectangle in processing into a image for a game that I am making?

这是我在宇宙飞船中使用的代码 class:

class SpaceShip extends GameObject
        {
          //contructor for the player's spaceship
          SpaceShip()
          {
             x = width/2;
             y = height/2;
             dx = 0;
             dy = 0;
          }

          void show()
          {
            fill(193, 152 , 240);
            //This is what is needed to be changed into an image whilst keeping this as a sort of hitbox per-say
            rect(x, y, 50, 50);
          }
          void act()
            {
            dx = 0;
            dy = 0;

            if(upkey) dy = -5;
            if(leftkey) dx = -5;
            if(rightkey) dx = 5;
            if(downkey) dy = 5;
            //if(spacekey)//fire 

              x = x + dx;
              y = y + dy;
            }

             boolean hasDied()
             {
               return false;
             }
            }

如果你们可以帮助我提供代码或 posted 的方式(我还没有真正使用过这样的网站)那么一定要告诉我,我会尽力回应你们更新这个 post.

Edit1:GameObject 只是一些次要代码,在这个 class 中都提到了,这是我的手动代码,它曾经被覆盖,仅此而已 - 使用的 classes 是: show()、act 和 hasDied,GameObject 中的这些 class 中的每一个都是空的,但有一些例外。如果你想进一步了解,请评论,我会尽力回答。

Edit2:这是我对这个问题的最终目标: 使矩形透明并显示图像(本例中为宇宙飞船),同时保持矩形功能用作受到伤害的碰撞箱,等等。 (希望这有助于消除对我试图实现的目标的任何误解)。

Edit3:谢谢伙计,这是我现在所在的位置 当前代码是:

PImage img;

class SpaceShip extends GameObject
{
  //contructor for the player's spaceship
  SpaceShip()
  {
     img = loadImage("SpaceShipSprite1.png");
     x = width/2;
     y = height/2;
     dx = 0;
     dy = 0;
  }

  void show()
  {
    fill(LightPurple_SpaceShip);
    //This is what is needed to be changed into an image whilst keeping this as a sort of hitbox per-sa
    rect(x, y, 50, 50);
    image(img, x, y, 50, 50);

  }
  void act()
  {
    dx = 0;
    dy = 0;

    if(upkey) dy =-5;
    if(leftkey) dx =-5;
    if(rightkey) dx =+5;
    if(downkey) dy =+5;
    if(firekey) engine.add(new Bullet());

    x = x + dx;
    y = y + dy;
  }

  boolean hasDied()
  {
    return false;
  }

}

游戏截图: The Game and where the character is:

编辑 4(最终编辑):这是所有想要做类似事情的人的最终完成版本,感谢所有帮助我解决这个难题的人,并期待在未来提出更多问题。

PImage img;

class SpaceShip extends GameObject
{
  //contructor for the player's spaceship
  SpaceShip()
  {
     img = loadImage("SpaceShipSprite1.png");
     x = width/2;
     y = height/2;
     dx = 0;
     dy = 0;
  }

  void show()
  {
    noStroke();
    noFill();
    //This is what is needed to be changed into an image whilst keeping this as a sort of hitbox per-sa
    rect(x, y, 50, 50);
    image(img, x - 24.8, y, 50, 50);
  }
  void act()
  {
    dx = 0;
    dy = 0;

    if(upkey) dy =-5;
    if(leftkey) dx =-5;
    if(rightkey) dx =+5;
    if(downkey) dy =+5;
    if(firekey) engine.add(new Bullet());

    x = x + dx;
    y = y + dy;
  }

  boolean hasDied()
  {
    return false;
  }

}

使用 loadImageimage 函数,如 documentation:

中所述
PImage img;

SpaceShip() {
  // Images must be in the "data" directory to load correctly
  img = loadImage("spaceship.jpg");
  ...
}

void show() {
  image(img, x, y);
}