Modify a text file: $/players4331k.java
$/players4331k.java
//// Exercise 4331k: Extending classes! String title= "Exercise 4331k: Extending classes!"; String author= "Prof. BAM"; String news= "(Press ? key for instructions; d to debug.)"; String quitcount=""; int left=20, right= left+600; // Boundaries of a rectangular field. int top=50, bottom= top+400; // (Leave room for text). int grassSlant=2; int countdown=5; Player george, john, thomas, james, andrew; Zoog z = new Zoog( "Zorro" ); Zoog yoyo = new Zoog( "Yoyo" ); Zoog xerxes = new Zoog( "Xexes" ); 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( 100, 100, "George" ); john= new Player( "John", 150, 150, 150 ); // Gray, short and fat john.h= 49; john.w= 36; thomas= new Player( "Thomas" ); thomas.h= 75; // Tall james= new Player( "James" ); andrew= new Player( width/2, height/2, 5, 3, "Old Hickory" ); // Fast // z = new Zoog( "Zorro" ); yoyo = new Zoog( "Yoyo" ); yoyo.w = 22; yoyo.h = 33; yoyo.r2=0; xerxes = new Zoog( "Xexes" ); xerxes.h = 150; xerxes.w = 99; xerxes.g2=0; } 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. // for (int x=10; x
mouseX+50) xtop= x - 9; else xtop=x; // stroke( 0, 150, 0 ); strokeWeight(4); line( x,height, xtop,height-25 ); //// One blade of grass from bottom of screen. if ( abs(mouseX-xtop) < 10 ) { fill(255,255,0); // Yellow flower. ellipse(xtop,height-22, 10,10 ); } } strokeWeight(1); } void messages() { //// Now, add text messages. stroke(0); fill(0); textSize( 20 ); text( title, width/4, 25 ); textSize( 12 ); text( news, width/3, 40 ); text( author, 20, height-15 ); fill(255,0,0); text( quitcount, 10, 40 ); } void action() { //// Move all players. //// if (key == '?') { help(); return; } george.move(); john.move(); thomas.move(); james.move(); andrew.move(); // z.move(); yoyo.move(); xerxes.move(); } void check() { // Check for collisions // STUB ++++++++++ if (george.hitme( thomas.x, thomas.y, 20 )) { background(0); // Flash black news= "George hit thomas!"; } if (andrew.hitme( james.x, james.y, 20 )) { background(255,0,0); // Flash red news= "Andrew hit james!"; james.hitcount += 10; } if (z.hitme( yoyo.x, yoyo.y, 20 )) { background(0,255,0); // Flash green news= "Zoog hit yoyo!"; yoyo.hitcount += 10; yoyo.w *= 0.95; // SHRINK!! yoyo.x=100; } if (z.hitme( xerxes.x, xerxes.y, 20 )) { background(0,0,255); // Flash blue news= "Zoog hit xerxes!"; xerxes.hitcount += 10; xerxes.x=200; xerxes.h *= 0.9; xerxes.w *= 0.9; // SHRINK!! } } void show() { //// Show all players //// george.show(); john.show(); thomas.show(); james.show(); andrew.show(); // z.show(); yoyo.show(); xerxes.show(); } class Player { //////// Player HAS: has position, velocity, etc. and drawing characteristics. //// Player DOES: move(), show() float x,y; // Position on the screen (i.e. "coordinates") float dx,dy; // How far to move in one flame (i.e. "velocity") String name; // Name of this player int r1, g1, b1; // Color of this player (body); int r2, g2, b2; // Second for this player. int w=30,h=50; // Width and height of this player's body (with default values). int hitcount=0; //// METHODS //// void move() { //// Move this player. //// if (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 ); ellipseMode( CENTER ); fill( r2,g2,b2 ); ellipse( x, y-h/2-12, 24, 24 ); // Head text( name, x-w/2+3, y-10 ); if (hitcount>0) { text( hitcount, x-10, y+10 ); } // DEBUG // if (key == 'd') { text( x, x-20, y ); text( y, x-20, y+20 ); text( dx, x+50, y ); text( dy, x+50, y+20 ); } } //// METHODS TO DETECT HITS //// boolean hitme( float xx, float yy, float near ) { //// Return true if point (xx,yy) is near me. if ( xx < x - w/2 - near ) return false; // Too far to the left. if ( xx > x + w/2 + near ) return false; if ( yy < y - h/2 - 24 - near ) return false; // Above my head! if ( yy > y + h/2 + 10 - near ) return false; // Below my feet! return true; //// YIKES! I've been hit! } void hitstart() { //// Bump hitcount, then start ar random position. hitcount++; news= name+" was hit!"; start(); } //// CONSTRUCTORS //// // NOTE: There is no default constructor. Player must have a name! //// Player( String s ) { //// Name and color only. name= s; r1= int( random(255) ); // ??? Replace with rancolor() g1= int( random(255) ); b1= int( random(255) ); contrast(); start(); // Start with random coordinates and velocity. } Player( String s, int r, int g, int b ) { //// Name and color only. Random position and velocity. name= s; r1= r; g1= g; b1= b; contrast(); start(); // Start with random coordinates and velocity. } Player( float x, float y, String s ) { //// Specify position (but not color). //// name= s; rancolor(); contrast(); // Start with specified coordinates and random velocity. this.x= x; this.y= y; dx= random( 0.5, 3.5 ); dy= random( 0.5, 2.5 ); } Player( float x, float y, String s, int r, int g, int b ) { //// Specify everything except speed (and second color). //// name= s; rancolor(); contrast(); // Start with specified coordinates and random velocity. this.x= x; this.y= y; dx= random( 0.5, 3.5 ); dy= random( 0.5, 2.5 ); } Player( float x, float y, float dx, float dy, String s ) { //// Specify everything (except contrasting color). name= s; rancolor(); contrast(); // Specified coordinates and velocity. this.x= x; this.y= y; this.dx= dx; this.dy= dy; } Player( float x, float y, float dx, float dy, String s, int r, int g, int b ) { //// Specify everything (except contrasting color). name= s; r1= r; g1= g; b1= b; contrast(); // Start with specified coordinates and velocity. this.x= x; this.y= y; this.dx= dx; this.dy= dy; } Player() {} // Methods used by constructors. // void start() { // Random position and speed 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 ); } void rancolor() { // Create random color. r1= int( random(255) ); g1= int( random(255) ); b1= int( random(255) ); } void contrast() { // Create a contrasting color. r2= (r1+128) % 256; g2= (g1+128) % 256; b2= (b1+128) % 256; } }// END OF class Player. // class Zoog extends Player { Zoog( String s ) { //// use constructor from parent class super(s); // r1=g1=b1=0; r2= 255; g2=255; b2=0; start(); } void show() { //// Override super.show(); // Add arms, along each side. stroke(r2,g2,b2); strokeWeight(1); for (float yarm=y-h/2; yarm