Trying to create a billiard game

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”?

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);

Hi @iox007 ,

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

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

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

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);
}
1 Like

ok thanks so have you heard of 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 :

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 :wink: ) :

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…)

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

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! :yum:

1 Like