Modify a text file: $/fastball.pde
$/fastball.pde
//// Billiard table, with moving balls. //// 7327b: Demonstrate simple classes, constructors. //// Swap velocities, when balls hit. String title= "Demonstrate simple classes, constructors."; String author="Prof.BAM:7327b"; float left=50, top=50, right=550, bottom=350, bump=25; float xspeed=3,yspeed=3,fast=1; // Velocity (pixels per frame); speedup int score=0; //// Instantiate the balls and pockets. Ball red, yellow, blue; void setup() { //// Setup: 600x400 (for 300x500 table). size(600,400); //// Initialize the 3 balls. red= new Ball(); red.c= color(255,0,0); yellow= new Ball(); yellow.c= color(255,255,0); blue= new Ball(); blue.c= color(0,0,200); reset(); } void reset() { //// Randomize the ball positions. red.x= left+random(200); red.y= top+50; yellow.x= left+random(200); yellow.y= top+100; blue.x= left+random(200); blue.y= top+150; fast=1; } void draw() { //// Table, with billiard balls. drawTable(); moveBalls(); drawBalls(); } void drawBalls() { //// Draw each ball. red.show(); yellow.show(); blue.show(); } void drawTable() { //// Draw 500x300 green light-table, with thick brown border, on tan backgroud. background( 250,200,50 ); // Orange floor. text( title, 25, 20); text( author, width-100, height-10 ); // fill( 255,0,0); text( "SCORE: "+score, 400, 10 ); fill( 0,0,200); text( "Press 'R' key to restart (randomly), 'Q' to quit.", 25, height-25); float f= int(fast*100) / 100.0; fill(127); text( " fast="+ f +" +/- for faster/slower.", 25, height-12); //// Table: fill( 100,200,100 ); // Light-green table. stroke( 100,50,50 ); // Brown (wood) rail. strokeWeight(25); rectMode(CORNERS); rect(left,top, right,bottom); // Table (with rail); rectMode(CORNER); strokeWeight(1); } void moveBalls() { //// Move each ball, based on its velocity (dx,dy), //// and detect hits. red.move(); yellow.move(); blue.move(); //// Check for collisions. check( red, yellow ); check( red, blue ); check( yellow, blue ); } 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; /* (Swap code like this will NOT work.) 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; //// Also flash ball colors, and show text above. background(0); p.show(); q.show(); fill(255); text( "H I T !", p.x, p.y-20 ); } } ////// EVENTS ////// void keyPressed() { //// "R" restarts game. if (key=='R') { reset(); } //// "Q" quits game. if (key=='Q') { exit(); } //// "+/-" speeds-up/slows-down. if (key=='+') { fast *= 1.1; } if (key=='-') { fast *= 0.9; } } ///////////////// CLASSES: class Ball { //// Billiard ball. color c; float x=0,y=0; // Position of this ball. float dx=xspeed,dy=yspeed; // Velocity (pixels per frame). float diam=20; // Diameter is 20. //// CONSTRUCTORS //// //// METHODS //// void move() { //// Move ball by (dx,dy) x= x + dx*fast; y= y + dy*fast; // Bounce off walls. if (x < left+bump) { dx= -dx; x=left+2*bump; } if (x > right-bump) { dx= -dx; x=right-2*bump; } if (y < top+bump) { dy= -dy; y=top+2*bump; } if (y > bottom-bump) { dy= -dy; y=bottom-2*bump; } } void show() { //// Draw at (x,y) fill(c); ellipseMode(CENTER); ellipse(x,y, diam,diam); } }