Modify a text file: $/players4331b.java
$/players4331b.java
//// Exercise 4331b: Defining and using objects String title= "Exercise 4331b: Define and use objects"; String news= "(Press r key to restart; q to quit.)"; String author= "Prof. BAM"; int left=20, right= left+600; // Boundaries of a rectangular field. int top=50, bottom= top+400; // (Leave room for text). Player george, john, thomas, james, andrew; void setup() { //// Set up the screen and reset everything else. size( right+20, bottom+30 ); reset(); // New players. } void reset() { //// Create new players //// george= new Player(); george.name= "George"; george.start(); // john= new Player(); john.name= "John"; john.h= 49; // Gray, short and fat john.w= 36; john.start(); // thomas= new Player(); thomas.name= "Thomas"; thomas.h= 75; // Tall thomas.start(); } void draw() { //// Next frame: move the players, then draw them. background( 200, 200, 255 ); // Sky blue; scene(); action(); check(); show(); messages(); } void scene() { //// Draw a light-brown field, plus some grass at the bottom. fill( 200, 150, 100 ); noStroke(); rectMode(CORNERS); rect( left, top, right, bottom ); // Field. // Grass along bottom of screen. // stroke( 0, 150, 0 ); for (int x=10; x
right-w/2) dx= -dx; // Bounce off boundaries. if (y
bottom-h/2) dy= -dy; x += dx; y += dy; } void show() { //// Display this player on the screen (at x,y, with color rgb, etc.) rectMode( CENTER ); fill( r1,g1,b1 ); rect( x,y, w,h ); // Body. ellipseMode( CENTER ); ellipse( x, y-h/2-12, 24, 24 ); // Head. fill(0); text( name, x-w/2+3, y-10 ); } // Random start. // void start() { // Random position, speed, and color for this player. x= random( left+w, right-w ); y= random( top+h, bottom-h ); dx= random( 0.5, 3.5 ); dy= random( 0.5, 2.5 ); // Create a random color. r1= int( random(255) ); g1= int( random(255) ); b1= int( random(255) ); } }// END OF class Player. // //// EVENT HANDLERS. //// void keyPressed() { //// Respond to keys. if (key == 'q') exit(); if (key == 'r') reset(); }