# Can't get the balls to move correctly

**URL:** <https://discourse.processing.org/t/cant-get-the-balls-to-move-correctly/18518>\
**Category:** Beginners\
**Created:** [March 8, 2020, 11:26pm UTC](https://discourse.processing.org/t/cant-get-the-balls-to-move-correctly/18518 "2020-03-08T23:26:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![GKBeginner](https://avatars.discourse-cdn.com/v4/letter/g/5fc32e/32.png) [@GKBeginner](https://discourse.processing.org/u/GKBeginner)\
**Post date:** [March 8, 2020, 11:26pm UTC](https://discourse.processing.org/t/cant-get-the-balls-to-move-correctly/18518/1 "2020-03-08T23:26:18Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [March 8, 2020, 11:50pm UTC](https://discourse.processing.org/t/cant-get-the-balls-to-move-correctly/18518/2 "2020-03-08T23:50:12Z")

</div>

> [@GKBeginner](#):
>
> y1=int(random(height,70));

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!  
🤓

---

<div class="post-metadata">

**Author:** ![GKBeginner](https://avatars.discourse-cdn.com/v4/letter/g/5fc32e/32.png) [@GKBeginner](https://discourse.processing.org/u/GKBeginner)\
**Post date:** [March 9, 2020, 1:35am UTC](https://discourse.processing.org/t/cant-get-the-balls-to-move-correctly/18518/3 "2020-03-09T01:35:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [March 9, 2020, 2:36am UTC](https://discourse.processing.org/t/cant-get-the-balls-to-move-correctly/18518/4 "2020-03-09T02:36:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![GKBeginner](https://avatars.discourse-cdn.com/v4/letter/g/5fc32e/32.png) [@GKBeginner](https://discourse.processing.org/u/GKBeginner)\
**Post date:** [March 9, 2020, 3:36am UTC](https://discourse.processing.org/t/cant-get-the-balls-to-move-correctly/18518/5 "2020-03-09T03:36:56Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 11, 2020, 4:56pm UTC](https://discourse.processing.org/t/cant-get-the-balls-to-move-correctly/18518/6 "2020-03-11T16:56:33Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![thughes](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@thughes](https://discourse.processing.org/u/thughes)\
**Post date:** [October 11, 2020, 12:14am UTC](https://discourse.processing.org/t/cant-get-the-balls-to-move-correctly/18518/7 "2020-10-11T00:14:09Z")

</div>

did you ever find a solution for this?
