Modify a text file: $/players4331j.java
$/players4331j.java
//// Exercise 4331j: Defining and using objects String title= "Exercise 4331j: Defining and using objects"; 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; 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 } 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(); } 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; } } void show() { //// Show all players //// george.show(); john.show(); thomas.show(); james.show(); andrew.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. // //// EVENT HANDLERS. //// void keyPressed() { //// Respond to keys. // Quit? (Give user another chance!) if (key == 'q') { if (countdown<1) { exit(); } else { countdown--; fill(0); quitcount= countdown+" Are you sure you want to quit? "; news= quitcount; } } else { //// Reset countdown, when any other key is pressed. countdown=5; news=""; quitcount=""; // Reeset messages, too. } news= "(Press ? key for instructions; d to debug.)"; //// Now, check fo other keys if (key == 'r') reset(); } void mousePressed() { //// Click handler. if ( george.hitme( mouseX, mouseY, 10) ) george.hitstart(); if ( john.hitme( mouseX, mouseY, 10) ) john.hitstart(); if ( thomas.hitme( mouseX, mouseY, 10) ) thomas.hitstart(); if ( james.hitme( mouseX, mouseY, 10) ) james.hitstart(); if ( andrew.hitme( mouseX, mouseY, 10) ) andrew.hitstart(); } void help() { //// Display instructions. background(255); fill(255,0,0); textSize(20); text( "I N S T R U C T I O N S", width/3, 80 ); text( "- - - - - - - - - - - -", width/3, 90 ); textSize(12); int j=0; text( "q quit", width/3, 100+12*j++ ); text( "r restart", width/3, 100+12*j++ ); text( "d debug", width/3, 100+12*j++ ); text( "blah blah blah", width/3, 100+12*j++ ); text( "blah blah blah", width/3, 100+12*j++ ); text( "blah blah blah", width/3, 100+12*j++ ); // +++++ STUB ++++ } class Zoog extends Player { } Zoog z;