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

时间:2018-11-26 06:55:56

标签: java processing

这是我在太空飞船类中使用的代码:

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中的所有这些类都为空。如果您想进一步了解,请发表评论,我会尽力回答。

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

}

1 个答案:

答案 0 :(得分:0)

使用documentation中所述的loadImageimage函数:

PImage img;

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

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