简体   繁体   中英

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

Here is my code for what is used in the spaceship 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;
             }
            }

If you guys could assist me with either the code or the way that this was posted (I haven't really used a site like this) then by all mean to tell me and I will try my best to respond to you guys and update this post.

Edit1: The GameObject is just some minor code which is all said in this class which is my manual code it is used to be overwritten and that is all - the classes used are: show(), act, and hasDied, each of these classes in the GameObject are all empty with some exceptions. If you would like to know further, comment and I will try my best to answer it.

Edit2: Here is what my end goal for this question is: To make the rectangle be transparent and have the image(for this example a spaceship) shown whilst also keeping the rectangle function to be used as a hitbox for taking damage and what not. (Hope this helped clear any misconceptions of what I was trying to achieve).

Edit3: Thanks mate, here is where I am at now and what the current code is:

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 snip of the game: The Game and where the character is:

Edit 4 (Final Edit): Here is the final finished version for all who want to do something similar thank you to all who helped me with this dilemma and looking forward to asking more questions in the future.

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

}

Use loadImage and image functions as described in the documentation :

PImage img;

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM