Modify a text file: $/p4._rashid.pde
$/p4._rashid.pde
/// PROJECT #4: Racers //// Farah Rashid // CST 112 String title= "Dog Race (dog3)", author= "Farah Rashid", filename = "d1_dograce.pde", version = "20413"; String subtitle= "To move racer, press key (1,2,3) or button."; String hint= "Space bar moves all racers."; String news= ".", test = "."; String name1= "tom"; String name2= "jerry"; String name3= "micky"; int nr; int ba = nr, br = nr+1; int nb = nr+2; Button b1, b2, b3; Racer r1, r2, r3, micky, oswald; Button[] butts = new Button[nb]; float track1= 150, track2=250, track3=350; float start=100, finish=500, horizon=track1-50; float xNews=350, yNews=horizon+25; int winner=0; int fps =30; //frames per second void setup() { size( 640, 480 ); finish = width-100; frameRate( fps); init(); reset(); } //// ++++ ADD YOUR CODE BELOW TO INITIALIZE OBJECTS. void init() { //// Initialize the objects // r1 = new Racer( name1, start, track1 ); r2 = new Racer( name2, start, track2 ); // b1 = new Button( name1, 10, 10 ); b2 = new Button( name2, 10, 40 ); //// ++++ MAKE EACH BUTTON SAME COLOR AS RACER +++ // } void reset() { r1.x= start; r2.x= start; } void draw() { scene(); showAll(); messages(); //// Racers do not move UNLESS button is clicked. //// (No action() function is needed.) } void scene() { background( 200, 220, 255 ); // Blue sky fill( 200, 220, 200 ); // Green grass rect( 0, horizon, width, height-horizon ); // //// Draw the tracks; fill(0); text( "Track #1", start-60, track1 ); text( "Track #2", start-60, track2 ); text( "Track #3", start-60, track3 ); fill( 250, 200, 200 ); rect( start, track1, finish-start+100, 80 ); rect( start, track2, finish-start+100, 80 ); // Finish-line stroke( 255,0,0 ); strokeWeight( 8 ); line( finish+100,track1-10, finish+100,height-40 ); strokeWeight( 1 ); stroke(0); // // Show the buttons. // b1.show(); b2.show(); } void showAll() { r1.show(); r2.show(); } void messages() { textSize(30); text( title, width/4, 30 ); textSize(12); text( subtitle, width/4, 50 ); text( hint, width*3/4, height-5 ); text( author, 10, height-5 ); // //// Check for WINNER. //// if (r1.x > finish) { winner=1; textSize(24); fill(r1.c); text( r1.name+ " won the race!", xNews, yNews ); } else if (r2.x > finish) { winner=2; textSize(24); fill(r2.c); text( r2.name+ " wins the race!", xNews, yNews ); } else { winner=0; fill(150); text( "(No winner, yet)", xNews, yNews ); } textSize(12); } //////// EVENTS /////// void keyPressed() { if (key == 'q') { exit(); } if (key == 'r') { reset(); } // // Space bar moves all racers. // if (key == ' ') { moveAll(); } // // Cheat! // if (key == '1') { r1.x += random(99); } if (key == '2') { r2.x += random(99); } } void moveAll() { } void mousePressed() { } //////// OBJECTS /////// class Button { float x, y; float w=90, h=25; String name; color c=color(255); // // CONSTRUCTORS // Button() { }; Button( String name, float x, float y ) { this.name= name; this.x= x; this.y= y; } // METHODS // void show() { fill(c); rect( x, y, w, h ); // // +++ INSERT YOUR CODE to complete the button. +++ // fill(0); text( name, x+10, y+20 ); } boolean clicked() { // +++ MODIFY OR INSERT YOUR CODE to return true if button was clicked. return hit( mouseX, mouseY ); } boolean hit( float xx, float yy) { // // +++ INSERT YOUR CODE to return true if button was hit. // return false; } }// class Button // class Racer { float x, y; String name; color c; // // CONSTRUCTORS // Racer() { }; Racer( String newname, float newx, float newy ) { this.name= newname; this.x= newx; this.y= newy; } // METHODS // void show() { fill(c); // // +++ INSERT YOUR CODE to draw (and animate) racer. +++ // fill(0); text( name, x+10, y+20 ); } void move() { // This Racer moves (a random amount) forward. // // +++ INSERT YOUR CODE to move this racer. +++ } } // END OF class Racer // void help() { String s="."; s += "\n Button moves Kangaroo forward; clicking moves it backward!"; s += "\n q to quit, r to reset, SPACE bar moves all Kangaroos"; s += "\n d to display debugging data;"; s += "\n < or > changes frameRate. ( / resets to 30.)"; float x=width/3, y=80; fill( 155,100,155, 150 ); rect( x-10,y-10, width*2/3, width/2 ); fill(#FFF529); textSize(16); text( s, x,y ); textSize(12); }