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++;
}
}