How do I get two PImages to detect each other when they collide?

edited June 2014 in Questions about Code
Here is my code so far

    Slimy[]slimys = new Slimy[35];

    PImage space;
    int x, y;
    PImage happy;
    int i;
    int sx, sy;
    boolean lost = false;

    void setup()
    {
      size(800,600);
      background(250,250,250);
       space = loadImage("space.png");
       imageMode(CENTER);
       happy = loadImage("happy.png");
      x = 50;
      y = 400;
      sx =700;
      sy = 80;
      for(int i = 0; i < slimys.length;i++)
      {
        slimys[i] = new Slimy();
      }

    }

    void draw()
    {
      image(space, width/2, height/2, 
        width, (float(width)/space.width)*space.height);

      image(happy, x, y, 0.2*happy.width, 0.2*happy.height);


    for(int i = 0; i < slimys.length;i++)
      {
        slimys[i].drawSlimy();
      }
    fill(170,50,120);
     ellipse(sx,sy,60,60);

    if(y>600)
    {
      noLoop();
      textSize(40);
      fill(255);
      text("GAME OVER", 310, 370);
    }
    if(x>800)
    {
      noLoop();
      textSize(40);
      fill(255);
      text("GAME OVER", 310, 370);
    }
     if(y<0)
     {
       noLoop();
       textSize(40);
      fill(255);
      text("GAME OVER", 310, 370);
     }
     if(x<0)
     {
       noLoop();
       textSize(40);
      fill(255);
      text("GAME OVER", 310, 370);
     }


    }

    void keyPressed()
    {
      if(key == CODED)
      {
        if(keyCode == UP)
        {
          y -= 10;
        }
        if(keyCode == DOWN)
        {
          y += 10;
        }
        if(keyCode == LEFT)
        {
          x -= 10;
        }
        if(keyCode == RIGHT)
        {
          x += 10;
        }
      }
    }



    class Slimy
    {
      float a, b;

      PImage img;


      Slimy()
      {
         a = random(0, width);
        b = random(0, height);
        img = loadImage("slimy.png");
         imageMode(CENTER);

      }

      void drawSlimy()
      {

        image(img, a, b,0.15*img.width, 0.15*img.height);

        for(int i = 0; i < slimys.length;i++)
      {
        a-= .018;

      }

     if(a<0)
     {
       a = 800;
       a -= .018;
     }
      if(x>200)
      {
        a -= .3;
      }

      if(x>400)
      {
        a -= .6;
      }
      if(x>600)
      {
        a -= 1;
      }
     }

I'm having trouble getting my program to detect when the PImage "happy" touches the array of PImages "slimy". Do you use getX() and getY()?Also I know it'll probably be easier to make the PImage "happy" have its own class, but the problem is when I create it in a seperate class I have trouble getting its keyPressed() function to work correctly

Answers

Sign In or Register to comment.