Modify a text file: $/Project5_rashid_.pde
$/Project5_rashid_.pde
//// Billiard table, with array of moving balls. String title="Bouncing Balls."; String subtitle="Array of Ball objects"; String author="Farah Rashid"; String hint = "Press 1, 2, 3, etc. reset a ball."; // Global declaration for the Ball array. int arraysize=20; Ball[] b = new Ball[arraysize]; int many=10; boolean debug=false; //// Assume size(600,400) for 300x500 table. float left=50, top=50, right=550, bottom=350, bump=25; float center=int(left+right)/2, middle=int(top+bottom)/2; float spot = int(left+center)/2, wide=right-left, high= bottom-top; int score=0, sweeps=0, frames=0, games=0; //BALL Ball red, yellow, blue; Ball zero, one, two, three, four, five; void setup() { //// Setup: 600x400 (for 300x500 table). size(600,400); frameRate(30); newGame(); reset(); } void newGame() { ++games; score=sweeps=frames=0; init(); b[0].reset(); } void init() { many = int( random(7,17) ); subtitle = "Array of "+many+" Ball objects."; //// Initialize . b[0] = new Ball( 0, 255,255,255 ); b[0].c = color(255,255,255); // WHITE cueball //// Initialize the other balls. for (int i=1; i
=100) { textSize(30); text( "G A M E O V E R", width/3, height/2 ); textSize(12); text( " Press 'g' key for new game.", width/3, 30+height/2 ); return; } // Check if any balls left int n = countAll(); if (n<1) { ++ sweeps; reset(); } // if (keyPressed) return; // No action when a key is presses. ++frames; moveBalls(); b[0].move(); } int countAll() { int m=0; if ( ! b[1].dead) m=m+1; if ( ! b[2].dead) m=m+1; if ( ! b[3].dead) m=m+1; if ( ! b[4].dead) m=m+1; if ( ! b[5].dead) m=m+1; return m; } void drawBalls() { b[0].show(); //// Draw each ball. b[0].show(); b[1].show(); b[2].show(); b[3].show(); b[4].show(); b[5].show(); } void moveBalls() { //// Move each ball b[1].move(); b[2].move(); b[3].move(); b[4].move(); b[5].move(); //// Collision with b[0] kills the ball. if (hit(b[0],b[1])) { ++score; b[1].kill(); } if (hit(b[0],b[2])) { ++score; b[2].kill(); } if (hit(b[0],b[3])) { ++score; b[3].kill(); } if (hit(b[0],b[4])) { ++score; b[4].kill(); } if (hit(b[0],b[5])) { ++score; b[5].kill(); } // } boolean hit( Ball p, Ball q ) { // Return true iff p is near q; float near=50; float d = dist( p.x,p.y, q.x,q.y ); if (dist( p.x,p.y, q.x,q.y ) < near) return true; else return false; } void drawTable() { //// Draw 500x300 green light-table, with thick brown border, on cyan backgroud. 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 messages() { fill(0); textSize(24); text( title, width/3, 20); textSize(12); text( subtitle, width/3, 36); text( "Author: "+author, width*3/4, height-10 ); fill( 255,0,0); textSize(16); text( "SCORE: "+score, 420, 15 ); textSize(12); text( "(g for new game)", 420, 30 ); fill( 0,0,200); String help1="Cueball kills other balls!"; String help2= "Press 1, 2, 3, etc. reset a ball."; text( help1, left+10, height-25); text( help2, left+10, height-10); } ///////////////// CLASSES class Ball { //// Billiard ball. int n; color c; float x,y; // Position of this ball. float xx,yy; // Velocity (pixels per frame). boolean dead=false; //// CONSTRUCTORS //// Ball( int number, int r, int g, int b ) { //// 4-arg constructor sets number & color; n= number; c= color( r, g, b ); } //// METHODS //// void move() { if (dead) return; //// Move ball by (xx,yy) x= x + xx; y= y + yy; // Bounce off walls. if (x < left+bump) { xx = -xx; x=left+2*bump; } if (x > right-bump) { xx = -xx; x=right-2*bump; } if (y < top+bump) { yy = -yy; y=top+2*bump; } if (y > bottom-bump) { yy = -yy; y=bottom-2*bump; } } void show() { //// Draw at (x,y) fill(c); ellipseMode(CENTER); ellipse(x,y, 30,30); // Diameter is 30. fill(0); text(n, x-5, y+5); if(debug) text(int(xx)+"\n"+int(yy), x-5,y+35); } // void kill() { dead=true; x= 40; y= random( top+30, bottom-30 ); xx=0; yy=0; background(c); } void restore() { dead=false; reset(); } void reset() { if (dead) return; x= random( left+30, center-100 ); y= random( top+30, bottom-30 ); xx= random( 2, 5 ); yy= random( 1, 3 ); } boolean clicked() { // true iff clicked nearby. if ( dist(x,y, mouseX,mouseY) < 40) return true; else return false; } }// END OF class Ball // //////// EVENT HANDLERS //////// void keyPressed() { if (key=='g') newGame(); //// "g" new game. if (key=='q') exit(); //// "q" quits game. int n = key - '0'; // n = number of the key. // n=1 for '1', n=2 for '2', etc. //// "Reset a ball by pressing key 1, 2, 3, etc. if (key=='1') b[1].reset(); if (key=='2') b[2].reset(); if (key=='3') b[3].reset(); if (key=='4') b[4].reset(); if (key=='5') b[5].reset(); } void mousePressed() { if (b[0].clicked()) { // cueball was clicked. } if (b[1].clicked()) b[1].reset(); if (b[2].clicked()) b[2].reset(); if (b[3].clicked()) b[3].reset(); if (b[4].clicked()) b[4].reset(); if(b[5].clicked()) b[5].reset(); }