How to make a shape teleport when it hits another

I'm working on a class project(frogger) and i want it so when the red squares hit the green one, the green one teleport back to the start(preferable with an array), same with when it hits the blue rectangle. here is my code:

int x = 250;
int y = 475;

int xPos=225;
int xDir=3;
int xPos2=200;
int xDir2=2;
int xPos3=25;
int xDir3=1;
int xPos4=25;
int xDir4=2;
void setup()
{
  size(500,500);

}
  void car(){
    fill(255,0,0);
    rect(xPos, 150, 25, 25);
  xPos=xPos+xDir;
  if (xPos>width || xPos<20)
  {
    xDir=-xDir;
  }
  rect(xPos2,275,25,25);
  xPos2=xPos2+xDir2;
  if (xPos2>width || xPos2<20)
  {
    xDir2=-xDir2;
  }
  }
  void log() {


   fill(128,64,0);
   rect(xPos3, 80, 125, 35);

  xPos3=xPos3+xDir3;
  if (xPos3>width || xPos3<20)
  {
    xDir3=-xDir3;
  }
  fill(128,64,0);
   rect(xPos4, 42, 125, 37);

  xPos4=xPos4+xDir4;
  if (xPos4>width || xPos4<20)
  {
    xDir4=-xDir4;
  }
  }
void draw()
{
    fill(0);
  rect(0,115,500,325);

  fill(0,128,0);
  rect(0,350,500,375);
  fill(0,0,255);
  rect(0,0,500,125);
  fill(1,50,32);
  rect(0,0,500,25);
  for(int a=25; a < width; a=a+100)
{
  fill(255,255,0);
  rect(a,215,45,10);}

  log();

  fill(0,255,0);
  rect(x,y,25,24);
  car();

}

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

it would also be nice if you could tell me how to make the game better.

Answers

  • Answer ✓

    You can check the distance by command dist() - see reference

    if(dist(x, y, .....) < 67) {

    // hit

    x=250;

    Y=475;

    }

    Repeat for each car

    To beautify:

    Maybe you can draw a frog in paint and then load it as image

    Same for cars and street

    Chrisir ;-)

  • thanks

Sign In or Register to comment.