Can't get the balls to move correctly

One ball should be randomly moving and the other is controlled by the arrow keys on the keyboard. They are not working correctly and I can’t figure out why.

int x1=30;
int y1;
int x2;
int y2=30;
int r=255;
int g=255;
int b=255;
int width = 400;
int height = 400;
int score=0;

public void setup()
{
  size(400,400);
  background(0);
  
}

public void draw()
{
  frameRate(30);
  background (0);
  createShapes();
  change();
  keyPressed();
  checkLocation();
  text();
  y1=int(random(height,70));
  x2=int(random(width,70));
}
public void createShapes()
{
  ellipse(x1,y1,30,30);
  fill(r,g,b);
  
  ellipse(x2,y2,30,30);
  fill(r,g,b);
}
public void text()
{
text("score = "+score, 10, 30);
}

public void change()
{    
  for(y2=30; y2>height; y2=30)  {
    y2=+2;
  }

}
public void keyPressed()
{
  if(key==CODED)
  {
  if(keyCode==UP)
  y1-=2;
  if(keyCode==DOWN)
  y1+=2;
  if (keyCode==LEFT)
  x1-=2;
  if(keyCode==RIGHT)
  x1+=2;
  }
 
}
public void checkLocation()
{
  if(Math.abs(x1-x2) < 20 && Math.abs(y1-y2) <20) {
    y2=30;
    x2=(int)random(height-20);
    score++;
   
}
}
1 Like

For the ball that moves randomly, this line and the one below, should be:

random (min number, max number)

You have: random (max number, min number)

However, please format your code, as this is hard to read.
Hit edit pencil, highlight the code, then click on </> in the format bar!
:nerd_face:

2 Likes

Thank you so much. I agree that I need to clean up the code, I wish that I had an edit pencil on my version of Processing! I can’t even get the debugger to work right.

The edit pencil is just below your post to the left of the reply arrow…

Ohhhh…I thought you meant in the processing program! Thank you for showing me how to do this here. I am learning. I appreciate your help!

@GKBeginner were you able to resolve this problem?

Why is your code using the public keyword – it has no effect, but it seems maybe you got your examples from someplace other than the beginner reference, examples, and tutorials.

did you ever find a solution for this?