# Trying to create a billiard game

**URL:** <https://discourse.processing.org/t/trying-to-create-a-billiard-game/30318>\
**Category:** Coding Questions\
**Created:** [May 26, 2021, 4:31pm UTC](https://discourse.processing.org/t/trying-to-create-a-billiard-game/30318 "2021-05-26T16:31:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![iox007](https://avatars.discourse-cdn.com/v4/letter/i/3be4f8/32.png) [@iox007](https://discourse.processing.org/u/iox007)\
**Post date:** [May 26, 2021, 4:31pm UTC](https://discourse.processing.org/t/trying-to-create-a-billiard-game/30318/1 "2021-05-26T16:31:31Z")

</div>

Hey everyone,

So my teacher gave me a task to create a billiard game which in all honesty is very very challenging. So far i have managed to create something but im stuggling with two issues:

How can i aim the white ball myself and “shoot” it once i click the mouse?

How can i create a line that goes from the white ball to the other balls as a type of “aim”?

```auto
int anz = 16;
int r = 15;
int speed = 6;
boolean show = true;
float tmp;

float x[] = new float[anz];
float y[] = new float[anz];
float sx[] = new float[anz];
float sy[] = new float[anz];
color c[] = new color[anz];

void setup() 
{
  size(800,600);
  background(0,255,0);
  noStroke();
  // balls in the billiard formation
  float xs = 600;
  float ys = 300;
  float dx = sqrt(3)*r;
  float dy = r;
  
  int number = 0;
  for( int i=0; i<5; i++) {
    ys = 300 - i*dy;
    for(int j=0;j<=i; j++) {
      sx[number] = 0;
      sy[number] = 0;
      x[number] = xs;
      y[number] = ys;
      c[number] = color(random(255), random(255), random(255));
      ball(x[number],y[number],2*r,c[number]);
      ys+=2*dy;
      number++;
    }
    xs+=dx;
  }
  // white ball
  c[15] = color(255,255,255);
  sx[15] = 6;
  sy[15] = -6;  
  x[15] = 100;
  y[15] = 300;
  ball(x[15],y[15],2*r,c[15]);
}

void draw()
{
  background(0,255,0);
  float dis;
  
  for( int i = 0; i < anz; i++) {
    
    for(int k=0; k<anz; k++) {
      dis = dist(x[k], y[k], x[i], y[i]);
      if( (dis>0 && dis<2*r) && k!=i) {
        tmp = sx[i];
        sx[i] = sx[k];
        sx[k] = tmp;
        tmp = sy[i];
        sy[i] = sy[k];
        sy[k] = tmp; 
      }
    }
    
    if( (x[i]<=r) || (x[i] >= width-r) ) sx[i] *= -1;
    if( (y[i]<=r) || (y[i] >= height-2*r) ) sy[i] *= -1;
    
    if(sx[i]!=0) sx[i] -= sx[i]/400;
    if(sy[i]!=0) sy[i] -= sy[i]/400;
    
    x[i] += sx[i];
    y[i] += sy[i];

    ball(x[i],y[i],r*2,c[i]);
  }
}

void ball(float x, float y, int d, color c) {
  fill(c);
  ellipse(x,y,d,d);

```

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [May 26, 2021, 4:56pm UTC](https://discourse.processing.org/t/trying-to-create-a-billiard-game/30318/2 "2021-05-26T16:56:33Z")

</div>

Hi @iox007 ,

> [@iox007](#):
>
> which in all honesty is very very challenging

Sure it is but for now, having a single ball bouncing shouldn’t be too complicated 😉

Can you post your whole code so we can properly test it? (there’s missing code at the end)

---

<div class="post-metadata">

**Author:** ![iox007](https://avatars.discourse-cdn.com/v4/letter/i/3be4f8/32.png) [@iox007](https://discourse.processing.org/u/iox007)\
**Post date:** [May 26, 2021, 4:59pm UTC](https://discourse.processing.org/t/trying-to-create-a-billiard-game/30318/3 "2021-05-26T16:59:37Z")

</div>

Oh sorry, thats the whole code, was only missing a } at the end

```auto
int anz = 16;
int r = 15;
int speed = 6;
boolean show = true;
float tmp;

float x[] = new float[anz];
float y[] = new float[anz];
float sx[] = new float[anz];
float sy[] = new float[anz];
color c[] = new color[anz];

void setup() 
{
  size(800,600);
  background(0,255,0);
  noStroke();
  // balls in the billiard formation
  float xs = 600;
  float ys = 300;
  float dx = sqrt(3)*r;
  float dy = r;
  
  int number = 0;
  for( int i=0; i<5; i++) {
    ys = 300 - i*dy;
    for(int j=0;j<=i; j++) {
      sx[number] = 0;
      sy[number] = 0;
      x[number] = xs;
      y[number] = ys;
      c[number] = color(random(255), random(255), random(255));
      ball(x[number],y[number],2*r,c[number]);
      ys+=2*dy;
      number++;
    }
    xs+=dx;
  }
  // white ball
  c[15] = color(255,255,255);
  sx[15] = 6;
  sy[15] = -6;  
  x[15] = 100;
  y[15] = 300;
  ball(x[15],y[15],2*r,c[15]);
}

void draw()
{
  background(0,255,0);
  float dis;
  
  for( int i = 0; i < anz; i++) {
    
    for(int k=0; k<anz; k++) {
      dis = dist(x[k], y[k], x[i], y[i]);
      if( (dis>0 && dis<2*r) && k!=i) {
        tmp = sx[i];
        sx[i] = sx[k];
        sx[k] = tmp;
        tmp = sy[i];
        sy[i] = sy[k];
        sy[k] = tmp; 
      }
    }
    
    if( (x[i]<=r) || (x[i] >= width-r) ) sx[i] *= -1;
    if( (y[i]<=r) || (y[i] >= height-2*r) ) sy[i] *= -1;
    
    if(sx[i]!=0) sx[i] -= sx[i]/400;
    if(sy[i]!=0) sy[i] -= sy[i]/400;
    
    x[i] += sx[i];
    y[i] += sy[i];

    ball(x[i],y[i],r*2,c[i]);
  }
}

void ball(float x, float y, int d, color c) {
  fill(c);
  ellipse(x,y,d,d);
}

```

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [May 26, 2021, 5:09pm UTC](https://discourse.processing.org/t/trying-to-create-a-billiard-game/30318/4 "2021-05-26T17:09:24Z")

</div>

ok thanks so have you heard of [object oriented programming](https://en.wikipedia.org/wiki/Object-oriented_programming)? Basically Java (which Processing is built on) is a language made for that.

You can check the basic tutorial on the Processing website about it :

> **[Objects](https://processing.org/tutorials/objects/)**
>
> The basics of object-oriented programming.

The idea is to represent data in your code as logical objects.

Let’s take the example of a billiard ball : it has a location (x and y coordinated), a velocity (can be represented as a vector), a color… Those are **attributes** , it’s what define a billiard ball.

And you may want to create multiple balls in a billiard game, those are called **objects**. And those objects are variations of the same kind of entity : the `Ball` **class** for example.

You can also make actions on your ball objects : apply a force, display them on the screen, change their color, position them… these are called **methods**

You can check a post I did on this subject (with balls also 😉 ) :

> [@Problems with my mini game](https://discourse.processing.org/t/problems-with-my-mini-game/21949/2):
>
> Hi, Welcome to the community! slight_smile Before trying to solve your problem, you are facing a well known problem where the structure of your code is actually describing [object oriented programming](https://en.wikipedia.org/wiki/Object-oriented_programming) : So object oriented programming is a way to structure your code by using objects to model the data that your are manipulating. In this special case, your are creating Balls, and a Ball is described as : a position (x and y coordinates on the screen) a speed (described as a vector, xDir and y…

You will see that your code will be very structured and clear (because for now it’s hard to read) and it will be easier to add features (as the future trajectory or throw on click…)

---

<div class="post-metadata">

**Author:** ![iox007](https://avatars.discourse-cdn.com/v4/letter/i/3be4f8/32.png) [@iox007](https://discourse.processing.org/u/iox007)\
**Post date:** [May 26, 2021, 5:59pm UTC](https://discourse.processing.org/t/trying-to-create-a-billiard-game/30318/5 "2021-05-26T17:59:19Z")

</div>

oh my, this requires a fair bit of time to learn. Im quite a beginner to processing so i guess ill have to suffer a bit. Thanks anyways, ill try to figure this out

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [May 26, 2021, 8:21pm UTC](https://discourse.processing.org/t/trying-to-create-a-billiard-game/30318/6 "2021-05-26T20:21:58Z")

</div>

> [@iox007](#):
>
> this requires a fair bit of time to learn

yes but I can assure you that this is going to be valuable as a beginner as soon as you are going to understand how it can be useful! 😋
