////Pool table using class ////Valerie Courgis ////Project 5 String title="Bouncing balls - pool table."; String news=""; String last=""; String author="Valerie Courgis, CST112, Project 5"; int left=30, top=60, right= 630, bottom= 460; int many=0, toomany=20; // Number of Balls int score = 0; Ball val; Ball[] school; Button b1; Player b2; void setup() { ////setup screen size and reset. size(right+100, bottom+140); reset(); } void reset() { ////Create new balls val= new Ball(); val.number= "Val"; ////number it school= new Ball[toomany]; for (int j=0; jright-w/2) dx= -dx; // reverse speed direction. if (ybottom-h/2) dy= -dy; } void show() { //// Display this Ball on the screen (at x,y, with color rgb, etc.) stroke(0); strokeWeight(2); fill( r1,g1,b1 ); ////random color of ball ellipse( x,y, w+20,h+5 ); //// draw the ball fill(255); ////white color of ball center ellipse( x,y-5, w,h-15 ); //// draw the ball center ellipseMode( CENTER ); textSize( 15 ); fill(0); text( number, x-w/2+2, y+5 ); ///numbers on balls smooth(); } //// CONSTRUCTORS //// // Ball need to have a number! //// Ball() {} Ball( String s ) { //// number and color only. number= s; r1= int( random(255) ); // random color() g1= int( random(255) ); b1= int( random(255) ); start(); // Start with random coordinates and velocity. diameter = 25; } boolean gotHit(float x1, float y1, float d1, float x2, float y2, float d2 ) { float d = dist(x1, y1, x2, y2); if ( d < (d1/2 + d2/2) ) return true; else return false; } // Methods used by constructors. // void start() { // Random position and speed for this Ball. r1= int( random(255) ); // random colors g1= int( random(255) ); b1= int( random(255) ); x= random(left,right); // random position y= random(top,bottom); dx= random(1,5); // random position dy= 0; dy= random( 0.5, 2.5 ); } }// END OF class Ball. // void keyPressed() { //// Handle keys. if (key == 'q') exit(); if (key == 'r') reset(); if (key == '+') { if (many >= toomany) { news= title= "TOO MANY BALLS! Maximum is "+toomany; return; } else { Ball m= new Ball( many+"" ); school[many]= m; many++; } } if (key == '-') { if (many>0) many--; } if (key == '0') { many=0; school= new Ball[0]; } } void help() { //// Show instructions int x=100, y=100, j=0; fill(0); text( "I N S T R U C T I O N S", x, y+12*j++ ); text( "* * * * * * * * * * * * * *", x, y+12*j++ ); x= x+20; text( "q: quit", x, y+12*j++ ); text( "r: reset all ball", x, y+12*j++ ); text( "+: more ball", x, y+12*j++ ); text( "-: fewer ball", x, y+12*j++ ); text( "0: no ball (except Val)", x, y+12*j++ ); } class Button { // Button float x=20,y=20, w=80,h=40; String number="Click Val!"; void show() { // Show the button // fill( 200 ); strokeWeight(4); rectMode( CORNER ); fill(255,255,0); rect( x+60,y-20, w,h ); fill(255,0,255); text( number, x+65, y ); } } class Player { ////players float x=650,y=150, w=40,h=40, dx=2, dy=1; float diameter; void show() { // Show the player // diameter = 25; fill( 255,122,200 ); strokeWeight(2); rectMode( CORNER ); fill(200,100,100); rect( x+20,y+25, w,h+25 ); ////player body ellipse(x+40, y+20,w+5,h+5); ////Player head rect( x+20,y+75, w-30,h+5 ); ////player left leg rect( x+50,y+75, w-30,h+5 ); ////player right leg fill(255); ellipse(x+30, y+20,w-35,h-35); ////Player Left eye ellipse(x+50, y+20,w-35,h-35); ////Player right eye ////Player with Pool stick fill(195); rect( x+20,y+35, w-180,h-25 ); ////player pool stick ////move the player x += dx; y += dy; if (xright-500) dx= -dx; // reverse speed direction. if (ybottom-100) dy= -dy; } }