/** Billiard Table II THIS IS NOT MY WORK I'm experimenting with the teacher's version I need to toy with arrays still array of balls should be the last name with it and the array of buttons should be with your first name **/ //instantiate billiards and pockets Pocket [ ] tunnels = new Pocket [ 6 ]; billiard [ ] billiards = new billiard [ 3 ]; int score = 0; void setup ( ) { //setup: 600x400 for 300x500 table size ( 600, 400 ); //initialize 3 billiards billiards [ 0 ] = new billiard ( color ( 255, 0, 0 ), 100, 100 ); billiards [ 1 ] = new billiard ( color ( 0, 225, 0 ), 100, 130 ); billiards [ 2 ] = new billiard ( color ( 0, 0, 225 ), 100, 160 ); //initialize pockets (and tunnel ends) tunnels [ 0 ] = new Pocket ( 65, 65, 335, 85 ); tunnels [ 1 ] = new Pocket ( 65, 335, 65, 85 ); tunnels [ 2 ] = new Pocket ( 300, 65, 300, 315 ); tunnels [ 3 ] = new Pocket ( 300, 335, 300, 85 ); tunnels [ 4 ] = new Pocket ( 535, 65, 535, 315 ); tunnels [ 5 ] = new Pocket ( 535, 335, 535, 85); } void initializeRed ( ) { //randomize billiard positions billiards [ 0 ] .x = 100 + random ( 100 ); billiards [ 0 ] .y = 100; } void initializeGreen ( ) { billiards [ 1 ] .x = 100 + random ( 100 ); billiards [ 1 ] .y = 130; } void initializeBlue ( ) { billiards [ 2 ] .x = 100 + random ( 100 ); billiards [ 2 ] .y = 160; } void keyPressed ( ) { //"1" restarts red billiard if ( key == '1' ) { initializeRed ( ); } //"2" restarts green billiard if ( key == '2' ) { initializeGreen ( ); } //"3" restarts blue billiard if ( key == '3' ) { initializeBlue ( ); } //"Q" quits game if ( key == 'q' ) { exit ( ); } } void draw ( ) { //table with billiard billiards drawTable ( ); movebilliards ( ); drawbilliards ( ); drawButton ( ); } void drawbilliards ( ) { //draw each billiard for ( int k = 0; k < 3; k++ ) { billiards [ k ] .show ( ); } } void drawTable ( ) { //draw 500x300 green table with thick brown border on tan backgroud background ( 200, 150, 100 );//---------tan floor text ( "Billiard Table II", 25, 10 ); fill ( 255, 0, 0 ); text ( "SCORE:" + score, 400, 10 ); fill ( 100, 100, 200 ); //-- text ( "(billiard tunnels when it goes into a pocket, and the table flashes)", 25, 30 ); fill ( 0, 0, 200 ); text ( "Press '1, 2, 3' to restart and 'Q' to quit.", 25, height - 25 ); //table fill ( 125, 125, 250 );//-----------------green table stroke ( 100, 50, 50 );//---------------brown (wood) rail strokeWeight ( 25 ); rectMode ( CORNER ); rect ( 50, 50, 500, 300, 6 );//------------table (with rail) //6 pockets for ( int j = 0; j < 6; j++ ) { fill ( 0 ); tunnels [ j ] .drawPocket ( ); } } void movebilliards ( ) { //move each billiard based on its velocity (dx,dy) and detect hits for ( int j = 0; j < 3; j++ ) { billiards [ j ] .move ( ); } //check for collisions check ( billiards [ 0 ], billiards [ 1 ] ); check ( billiards [ 0 ], billiards [ 2 ] ); check ( billiards [ 1 ], billiards [ 2 ] ); //check each pocket for tunneling if a billiard is in a pocket for ( int j = 0; j < 6; j++ ) { for ( int k = 0; k < 3; k++ ) { if ( tunnels [ j ] .inPocket ( billiards [ k ] ) ) tunnels [ j ] .wormhole ( billiards [ k ] ); } } } void check (billiard p, billiard q ) { //check for collisions. reverse both if hit if ( dist ( p.x, p.y, q.x, q.y ) < 20 ) { score += 10; /* p.dx = -p.dx; p.dy = -p.dy; q.dx = -q.dx; q.dy = -q.dy; */ //swap velocities float tmp; tmp = p.dx; p.dx = q.dx; q.dx = tmp; tmp = q.dx; q.dx = p.dx; p.dx = tmp; //eliminate "ringing" by moving both in new direction p.x += 2 * p.dx; p.y += 2 * p.dy; q.x += 2 * q.dx; q.y += 2 * q.dy; //flash billiard colors and show text above background ( 0 ); p.show ( ); q.show ( ); fill ( 255 ); text ( "HIT!", p.x, p.y - 20 ); text ( "*", p.x, p.y ); text ( "*", q.x, q.y ); } } //CLASSES class billiard { //billiard billiard color c; float x = 0, y = 0;//-------------------position of this billiard float dx = 10, dy = 10;//---------------velocity (pixels per frame) //CONSTRUCTORS billiard ( float x0, float y0 ) { //2-arg constructor sets position only x = x0; y = y0; } billiard ( color c0, float x0, float y0 ) { //3-arg constructor sets color & position c = c0; x = x0; y = y0; } billiard ( ) { //Default constructor must be defined if any others are defined } //METHODS void move ( ) { //move billiard by ( dx, dy ) x = x + dx; y = y + dy; //bounce off walls if ( x < 75 ) { dx = -dx; } if ( x > 525 ) { dx = -dx; } if ( y < 75 ) { dy = -dy; } if ( y > 325 ) { dy = -dy; } } void show ( ) { //draw at ( x, y ) fill ( c ); //-- text ( c, x, y + 30 ); ellipseMode ( CENTER ); ellipse ( x, y, 20, 20 );//-----------diameter is 20 } void showBlack ( ) { //draw at ( x, y ) fill ( 0 ); ellipseMode ( CENTER ); ellipse ( x, y, 20, 20 );//-----------diameter is 20 } } class Pocket { //pocket (tunnel) float x = 0, y = 0;//-------------------position of this pocket float xx = 0, yy = 0;//-----------------position of tunnel end //CONSTRUCTOR(S) Pocket ( float x0, float y0, float xx0, float yy0 ) { x = x0; y = y0; xx = xx0; yy = yy0; } //METHOD(S) void drawPocket ( ) { //draw a black pocket at ( x, y ) strokeWeight ( 0 ); ellipse ( x, y, 30, 30 ); } boolean inPocket ( billiard p ) { //return true if billiard is in this pocket return dist ( p.x, p.y, x, y ) < 20; } void wormhole ( billiard p ) { //tunneling moves billiard from ( x, y ) to ( xx, yy ) p.x = xx; p.y = yy; //also flash billiard color fill ( p.c ); drawPocket ( ); //-- rect ( 50, 50, 500, 300 );//----table } } void drawButton ( ) { fill ( 255 ); rect ( 120, 10, 50, 20, 6 ); }