/** Billiard Balls I THIS IS NOT MY WORK **/ String title = "Billiard Balls"; String news; //instantiate balls Ball red, green, blue, white; int score = 0; void setup ( ) { //setup: 600x400 for 300x500 table size ( 600, 400 ); //initialize 3 balls red = new Ball ( 255, 0, 0 ); green = new Ball ( 0, 200, 0 ); blue = new Ball ( 0, 0, 255 ); white = new Ball ( 255, 255, 255 ); initialize ( ); } void initialize ( ) { //randomize ball positions & velocities red.randomize ( ); green.randomize ( ); blue.randomize ( ); white.x = 150; white.y = 200; white.dx = 0; white.dy = 0; news = ""; } void keyPressed ( ) { //"Q" quits game if ( key == 'q' ) { exit ( ); } //"S" restarts game if ( key == 's' ) { initialize ( ); } //stop or start any ball if ( key == 'r' ) { red.dx = 0; red.dy = 0; news = "STOP" + key; } if ( key == 'y' ) { green.dx = 0; green.dy = 0; news = "STOP" + key; } if ( key == 'b' ) { blue.dx = 0; blue.dy = 0; news = "STOP" + key; } if ( key == 'w' ) { white.x = 150; white.y = 250; white.dx = 0; white.dy = 0; news = "STOP CUE-BALL"; } //start if ( key == '1' ) { red.randomize ( ); } if ( key == '2' ) { green.randomize ( ); } if ( key == '3' ) { blue.randomize ( ); } if ( key == 'W' ) { white.dx = 1 + random ( 2 ); white.dy = 1 + random ( 2 ); } //speed up or slow down if ( key == '+' ) { red.speedup ( ); blue.speedup ( ); green.speedup ( ); } if ( key == '-' ) { red.slowdown ( ); blue.slowdown ( ); green.slowdown ( ); } } void draw ( ) { //table with billiard balls drawTable ( ); moveBalls ( ); drawBalls ( ); //text on screen fill ( 0 ); text ( title, 25, 10 ); fill ( 255, 0, 0 ); text ( "SCORE:" + score, 400, 10 ); fill ( 255 ); text ( news, 200, 30 ); fill ( 100, 100, 200 ); //-- text ( "(Ball tunnels when it goes into pocket and table flashes)", 400, 20 ); } void drawBalls ( ) { //draw each ball red.show ( ); green.show ( ); blue.show ( ); white.show ( ); } void drawTable ( ) { //draw 500x300 green table with thick brown border on tan backgroud background ( 200, 150, 100 );//---------tan floor fill ( 0, 0, 200 ); text ( "Press 'S' to start all billiards from top left corner.", 25, height - 25 ); text ( "Press 'W' to start the cue-ball and 'Q' to quit.", 25, height-15 ); //-- text ( "", 25, height - 5 ); //table fill ( 150, 150, 250 );//---------------light blue table stroke ( 100, 50, 50 );//---------------brown (wood) rail strokeWeight ( 25 ) ; rectMode ( CORNER ); rect ( 50, 50, 500, 300 );//------------table with rail } void moveBalls ( ) { //move each ball based on its velocity ( dx, dy ) red.move ( ); green.move ( ); blue.move ( ); white.move ( ); //check for collisions check ( red, green ); check ( red, blue ); check ( green, blue ); //check for cue-ball (white) if ( hit ( white, red ) ) { red.randomize ( ); score += 50; news = "RED"; } if ( hit ( white, green ) ) { green.randomize ( ); score += 50; news = "GREEN"; } if ( hit ( white, blue ) ) { blue.randomize ( ); score += 50; news = "BLUE"; } } boolean hit ( Ball p, Ball q ) { //return true if p hits q return dist ( p.x, p.y, q.x, q.y ) < 20; } void check ( Ball p, Ball q ) { //check for collisions. reverse both if hit. if ( dist ( p.x, p.y, q.x, q.y ) < 20 ) { score -= 10; elastic ( p, q ); /* p.dx = -p.dx; p.dy = -p.dy; q.dx = -q.dx; q.dy = -q.dy; */ //flash ball colors and show text above background ( 255 ); p.show ( ); q.show ( ); fill ( 255 ); text ("HIT!", p.x, p.y - 20 ); text ( "*", p.x, p.y ); text ( "*", q.x, q.y ); news = "(" + int ( p.x ) + "," + int ( p.y ) + ")HIT(" + int ( q.x ) + "," + int ( q.y ) + ")"; } } void elastic ( Ball p, Ball q ) { //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; } //CLASSES class Ball { //billiard ball color c; float x = 0, y = 0;//-------------------position of this ball float dx = 4, dy = 4;//-----------------velocity (pixels per frame) //CONSTRUCTORS Ball ( float x0, float y0 ) { //2-arg constructor sets position only x = x0; y = y0; } Ball ( int r, int g, int b ) { //3-arg constructor sets color RGB c = color ( r, g, b ); } Ball ( ) { //default constructor must be defined if any others are defined } //METHODS void move ( ) { //move ball by ( dx, dy ) x = x + dx; y = y + dy; //bounce off walls if ( x < 75 ) { dx = -dx; x = x + dx; news = ""; } if ( x > 525 ) { dx = -dx; x = x + dx; news = ""; } if ( y < 75 ) { dy = -dy; y = y + dy; news = ""; } if ( y > 325 ) { dy = -dy; y = y + dy; news = ""; } } void show ( ) { //draw at ( x, y ) fill ( c ); noStroke ( ); //-- text (c, x, y + 30 ); ellipseMode ( CENTER ); ellipse (x, y, 20, 20 );//-----------diameter is 20 } void randomize ( ) { //set random values for x, y, dx, dy x = 100 + random ( 100 ); y = 100 + random ( 100 ); dx = 1 + random ( 3 ); dy = 1 + random ( 3 ); } void speedup ( ) { dx *= 1.1; dy += 1.1; } void slowdown ( ) { dx *= .9; dy += .9; } }