How do i draw a shape to my mouseX and mouseY?

edited April 2017 in How To...

Im making a "game" where you are in control of a ship and you have to avoid "asteroids". right now, it's quite simple, but it's barely my first game.

previusly, i was using a rect(); as a ship, but now im trying to use a shape based on this paint drawing:

ship

but i dont know how to make it go where my mouse is!

any ideas? thanks in advance ;)

Answers

  • Answer ✓

    Check the reference for an example of PGraphics object: https://processing.org/reference/PGraphics.html

    Then after you designed your ship in this object, you can draw it like this:

    image(yourPGraphics, mouseX,mouseY);

    Make sure you call imageMode(CENTER); and rectMode(CENTER); in your setup() function as these calls will make your task at hand easier.

    Kf

  • edited April 2017

    @P14082003 --

    A second option is, if you want to load a graphic of your ship rather than drawing it in processing, use loadImage() and then draw the ship with image() at your mouseX, mouseY location -- like this:

    PImage ship;
    void setup(){
      size(400,400);
      ship = loadImage("https://" + "forum.processing.org/two/uploads/imageupload/434/8CV7KEBRGLKS.png");
      imageMode(CENTER);
    }
    
    void draw(){
      background(255);
      image(ship, mouseX, mouseY);
    }
    

    A third option is that you could load the ship graphic into the mouse cursor itself with cursor() -- but this is not very flexible if you want things other than the mouse to affect the location of the ship.

    PImage ship;
    void setup(){
      size(400,400);
      ship = loadImage("https://" + "forum.processing.org/two/uploads/imageupload/434/8CV7KEBRGLKS.png");
      imageMode(CENTER);
      cursor(ship, ship.width/2, ship.height/2);
    }
    
    void draw(){
      background(255);
    }
    
  • thanks kf,jeremy, both work perfectly ;)

  • And, just like that, i got into assets :)

  • Great. Updated my answer to also give a cursor() example -- although image() is more flexible.

  • wish people on stack exchange were as helpful as you..

Sign In or Register to comment.