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

标签 java processing

这是我在 spaceship 类中使用的代码:

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

如果你们可以通过代码或发布方式帮助我(我还没有真正使用过这样的网站),那么一定要告诉我,我会尽力回复你们并更新这篇文章。

Edit1:GameObject 只是一些次要代码,这些代码都在这个类中说了,这是我的手动代码,它被用来覆盖,仅此而已 - 使用的类是:show()、act 和 hasDied,GameObject 中的每个类都是空的,但有一些异常(exception)。如果您想进一步了解,请评论,我会尽力回答。

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

关于java - 如何将处理中的矩形更改为我正在制作的游戏的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53476055/

相关文章:

java - 对 Spring Boot 的 Angular HTTP POST 请求导致空值

limit - 如何使用处理和 Twitter4j 从用户时间轴获取 20 多个结果?

java - 从命令行调用时使用处理 opengl 库缺少什么?

processing - 使物体以逼真的重力跳跃一次

java - thymeleaf 中的 if 语句

java - 这可能是一个愚蠢的问题,但我想澄清一下。我们可以将 'super' 说成一个对象而不是 java 中的关键字吗?

java - 在哪里可以找到 LWJGL 的示例使用?

java - 处理复制/重复调整大小问题

java - 我可以将参数传递给动态创建的类吗?

java - 使用 MultivalueMap 后调用中的问题