How do I Make it so When the Green Square Touches the Blue Rectangle it Teleports to the Start

How do I make it so when the green square touches the blue rectangle it teleports to the start with an array. This is for a frogger game. 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()
{
  if(dist(x, y, xPos,150) < 15) {

// hit

x=250;

y=475;

}
    fill(255,0,0);
    rect(xPos, 150, 25, 25);
  xPos=xPos+xDir;
  if (xPos>width || xPos<20)
  {
    xDir=-xDir;
  }
  if(dist(x, y, xPos2, 275) < 15) {



x=250;

y=475;

}
  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,41);
  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 -= 35;
    } else if (keyCode == DOWN) {
      y += 35;
    } else if (keyCode == LEFT) {
      x -= 35;
    } else if (keyCode == RIGHT) {
      x += 35;
    }  
  } 
}

Answers

  • edited May 2017

    do the same thing as if it tuch the red square put this in the void draw()

         if(y <= 115){
           x=250;
           y=475;
          }
    
  • use ctrl-t in the editor to indent your code nicely. it helps a lot with readability.

  • NoName , It works, but it needs to be linked to the blue rectangle, because i'm using the logs

  • I don't get it :/ what do you mean ?

  • so, like, if the green square touches the brown rectangles, it doesn't go back to the start, but if it touches the blue one, it does

  • owwww i get it x)

  • Notice the changes below.

    Kf

    final int THRESHOLD=125;
    
    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 draw() {
    
      drawScene();
      log();
    
      //Jumppy frog
      fill(0, 255, 0);
      rect(x, y, 25, 24);
    
      car();
    }
    
    void keyPressed() {
      if (key == CODED) {
    
        if (keyCode == UP) {
          y -= 35;
        } else if (keyCode == DOWN) {
          y += 35;
        } else if (keyCode == LEFT) {
          x -= 35;
        } else if (keyCode == RIGHT) {
          x += 35;
        }
    
        if(y<THRESHOLD)             //   ******** CHANGE: Added
        y=height-25;
      }
    }
    
    void drawScene() {
      fill(0);
      rect(0, 115, 500, 325);
    
    
      //Green strip: middle
      fill(0, 128, 0);
      rect(0, 350, 500, 375);
    
      //Blue strip: Top
      fill(0, 0, 255);
      rect(0, 0, 500, THRESHOLD);
    
      fill(1, 50, 32);
      rect(0, 0, 500, 41);
      for (int a=25; a < width; a=a+100)
      {
        fill(255, 255, 0);
        rect(a, 215, 45, 10);
      }
    }
    
    
    void car()
    {
      if (dist(x, y, xPos, 150) < 15) {
    
        // hit
    
        x=250;
    
        y=475;
      }
      fill(255, 0, 0);
      rect(xPos, 150, 25, 25);
      xPos=xPos+xDir;
      if (xPos>width || xPos<20)
      {
        xDir=-xDir;
      }
      if (dist(x, y, xPos2, 275) < 15) {
    
    
    
        x=250;
    
        y=475;
      }
      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;
      }
    }
    
Sign In or Register to comment.