Modify a text file: $/billiards-1B30b.pde
$/billiards-1B30b.pde
//// Billiard table, with moving balls. //// BAM 1B30b: Demonstrate simple classes, with constructors //// Instantiate the balls. Ball b1= new Ball(color(255,0,0), 100,100); Ball b2= new Ball(color(255,255,0), 100,130); Ball b3= new Ball(color(0,0,200), 100,160); void setup() { //// Setup: 600x400 (for 300x500 table). size(600,400); initialize(); } void initialize() { //// Initialize 3 balls. b1.c= color(255,0,0); b1.x=100+random(100); b1.y=100; b2.c= color(255,255,0); b2.x=100+random(100); b2.y=130; b3.c= color(0,0,200); b3.x=100+random(100); b3.y=160; // +++ Use constructors, instead. } void keyPressed() { //// "R" restarts game. if (key=='R') { initialize(); } } void draw() { //// Table, with billiard balls. drawTable(); moveBalls(); drawBalls(); } void drawTable() { //// Draw 500x300 green light-table, with thick brown border, on tan backgroud. background( 200,150,100 ); // Tan floor. text( "BAM 1B30b: Simple classes, with constructor", 25, 20); text( "Press 'R' key to restart (randomly)", 25, height-20); //// Table: fill( 100,200,100 ); // Light-green table. stroke( 100,50,50 ); // Brown (wood) rail. strokeWeight(25); rectMode(CORNER); rect(50,50, 500,300); // Table (with rail); //// 6 pockets. drawPocket( 65,65 ); drawPocket( 65,335 ); drawPocket( 300,65 ); drawPocket( 300,335 ); drawPocket( 535,65 ); drawPocket( 535,335 ); } void drawPocket( float x, float y) { //// Draw a black pocket at (x,y). strokeWeight(0); fill(0); ellipse( x,y, 30,30 ); } void moveBalls() { //// Move each ball, based on its velocity (dx,dy), //// and detect hits. b1.move(); b2.move(); b3.move(); //// Check for collisions. check(b1, b2); check(b1, b3); check(b2, b3); //// Pocket (tunnel). tunnel(b1); tunnel(b2); tunnel(b3); } void tunnel( Ball b ) { //// Check if ball is in pocket; if so, then tunnel it. //++++ } void check( Ball p, Ball q) { //// Check for collisions; reverse both if hit. if (dist( p.x,p.y, q.x,q.y) < 10) { p.dx= -p.dx; p.dy= -p.dy; q.dx= -q.dx; q.dy= -q.dy; //// 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; } } void tunnel( Ball p, Ball q) { //// Check for collisions; reverse both if hit. if (dist( p.x,p.y, q.x,q.y) < 10) { } drawPocket( 65,65 ); drawPocket( 65,335 ); drawPocket( 300,65 ); drawPocket( 300,335 ); drawPocket( 535,65 ); drawPocket( 535,335 ); } void drawBalls() { //// Draw each ball. b1.show(); b2.show(); b3.show(); } ///////////////// CLASSES class Ball { //// Billiard ball. color c; float x=0,y=0; // Position of this ball. float dx=10,dy=10; // Velocity (pixels per frame). //// CONSTRUCTORS //// Ball( float x0, float y0 ) { //// 2-arg constructor sets position only. x= x0; y= y0; } Ball( color c0, float x0, float y0 ) { //// 3-arg constructor sets color & position. c= c0; x= x0; y= y0; } 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; } 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. } }