Modify a text file: $/p5._rashid.pde
$/p5._rashid.pde
//// Billiard table, with array of moving balls. //Project 5. String title="Bouncing Balls."; String subtitle="Array of Ball objects"; String author="Farah Rashid"; // Global declaration for the Ball array. int arraysize=20; Ball[] b = new Ball[arraysize]; int many=10; //// Assume size(600,400) for 300x500 table. float left=50, top=50, right=550, bottom=350, bump=25; float center=(left+right)/2, middle=(top+bottom)/2; int score=0, sweeps=0, frames=0, games=0; void setup() { //// Setup: 600x400 (for 300x500 table). size(600,400); frameRate(30); newGame(); } void newGame() { ++games; score=sweeps=frames=0; init(); b[0].reset(); resetAll(); } void init() { many = int( random(7,17) ); subtitle = "Array of "+many+" Ball objects."; //// Initialize the cueball. 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; resetAll(); } // if (keyPressed) return; // No action when a key is presses. ++frames; moveBalls(); b[0].move(); } void drawBalls() { b[0].show(); //// Draw each ball. for (int i=1; i
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); } // 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. //b[n].reset(); //// ++++ ADD YOUR CODE HERE ++++ //// for (int i=1; i