Modify a text file: $/q4_rashid.pde
$/q4_rashid.pde
//// PROJECT #4: Racers //// Farah Rashid // CST 112 String author= "Farah Rashid"; String title= "Racer"; String subtitle= "To move racer, press key (1,2,3) or button."; String hint= "Space bar moves all racers."; // String name1= "rocky"; String name2= "jocky"; String name3= "jocky"; String name4= "socky"; String name5= "focky"; String name6= "All racers"; // Button b1, b2, b3, b4, b5, b6; Racer r1, r2, r3, r4, r5; Varmit mickey, sickey, jickey, bickey; color c1, c2, c3, c4, c5; // float track1= 150, track2=250, track3=350, track4 = 450,track5= 550; float start=100, finish=500, horizon=track1-50; float xNews=350, yNews=horizon+25; int winner=0, count= 0; void setup() { size( 840,680 ); init(); reset(); colorest(); } void init() { //// Initialize the objects // r1 = new Racer( name1, start, track1 ); r2 = new Racer( name2, start, track2 ); r3 = new Racer( name3, start, track3 ); r4 = new Racer( name4, start, track4 ); r5 = new Racer( name5, start, track5 ); // mickey = new Varmit(start+30, track2-10, color (#5FF3FF)); sickey = new Varmit (finish-30, track3-10, color (#F77D7D)); jickey = new Varmit(start+30, track4-10, color(#F5EF2F)); bickey = new Varmit (finish-30,track5-10, color( #F55BD1)); b1 = new Button( name1, 10, 10 ); b2 = new Button( name2, 10, 40 ); b3= new Button(name3,10,70); b4= new Button(name4,10,100); b5= new Button(name5, 10, 130); b6= new Button (name6, 10,160); //// ++++ MAKE EACH BUTTON SAME COLOR AS RACER +++ // r1.c= b1.c; r2.c= b2.c; r3.c= b3.c; r4.c = b4.c; r5.c = b5.c; } void reset() { r1.x= start; r2.x= start; r3.x=start; r4.x=start; r5.x=start; mickey.x=start+50; sickey.x=finish-50; jickey.x=start+70; bickey.x= finish-70; } void colorest() { b1.c=r1.c=colorracer(); b2.c=r2.c=colorracer(); b3.c=r3.c=colorracer(); b4.c=r4.c=colorracer(); b5.c=r5.c=colorracer(); b6.c=color(0,0,255); } color colorracer() { int r= (int) random(90,254), g=(int) random (0,90), b= (int) random (0,35); return color (r,g,b); } void draw() { scene(); showAll(); messages(); //// Racers do not move UNLESS button is clicked. //// (No action() function is needed.) } void scene() { background( #F599BC ); // fill( #7C6F74); // 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 ); text( "Track #4", start-60, track4); text ("Track #5", start-60, track5); fill( 250, 200, 200 ); rect( start, track1, finish-start+100, 100 ); rect( start, track2, finish-start+100, 100 ); rect (start, track3, finish-start+100, 100); rect(start, track4, finish-start+100, 100); rect (start, track5, finish-start+100,100); // Finish-line stroke( 255,0,0 ); strokeWeight( 8 ); line( finish+100,track1-6, finish+100,height-43 ); strokeWeight( 1 ); stroke(0); // // Show the buttons. // b1.show(); b2.show(); b3.show(); b4.show(); b5.show(); b6.show(); } void showAll() { r1.show(); r2.show(); r3.show(); r4.show(); r5.show(); mickey.show(); mickey.move(); sickey.show(); sickey.move(); jickey.show(); jickey.move(); bickey.show(); bickey.move(); } 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(26); fill(r2.c); text( r2.name+ " wins the race!", xNews, yNews ); } else if(r3.x>finish) { winner=3; textSize(26); fill(r3.c); text(r3.name+"wins the race!", xNews, yNews); } else if (r4.x>finish) { winner =3; textSize(26); fill(r3.c); text("No winner yet!", xNews, yNews); } else { winner=0; fill(255); textSize(24); text( "(No winner, yet!)", xNews, yNews ); } textSize(10); } //////// 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); } if(key =='3') {r3.x +=random(99);} if(key=='4') {r4.x +=random(99);} if(key=='5') {r5.x +=random(99);} } void moveAll() { // +++ INSERT YOUR CODE to move ALL racers. +++ r1.move(); r2.move(); r3.move(); r4.move(); r5.move(); } void mousePressed() { if (b1.clicked())r1.move(); if (b2. clicked())r2. move(); if(b3.clicked())r3.move(); if (b4.clicked())r4.move(); if(b5.clicked())r5.move(); if (b6. clicked())moveAll(); } //////// OBJECTS /////// class Button { float x, y; float w=89, h=30; 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 ); fill(120); fill(0); text( name, x+15, y+25 ); } 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. // if(xx
start) { if (count%10 ==0) { wag = random(20); } line (x,y+4, x-30, y+12+wag); } // // +++ INSERT YOUR CODE to draw (and animate) racer. +++ // strokeWeight(1); float step =10 ; if (x%10<5) step=-10; leg(x+8, y-4+h, step); leg(x+16,y-4+h, step); leg (x+w-16, y-4+h, step); leg (x+w-8, y-4+h, step); stroke(0); fill(100); ellipse(xHead+10, y-15, 10,8); fill(0,0,255); ellipse( xHead+9+random(1), y-15, 3,3); if (x>start) { stroke (150); fill(100); rect (xHead +25+random(2),y-15,15,random(2,3)); fill(200); ellipse(xHead+30+random(5), y+random(20), random(3,5), random(3,5)); ellipse(xHead+35+random(5),y+random(20),random(2,4), random(2,4)); ellipse( xHead+40+random(5), y+random(20), random(2,4), random(2,4)); stroke(0); } fill(255); text( name, x+9, y+22 ); } void leg(float x, float y, float step) { line( x,y, step+x, 24+y); } void move() { x=x + random(1,10); } } // END OF class Racer // class Varmit { float x, y; float dx =5, dy = 0; color c=0; float w= 36, h= 16; Varmit (float x, float y, color c) { this. c=c; this. x=x; this. y=y; } // METHODS // void show() { fill(c); float xx=x, yy=y+random (-2,+2); fill(c); ellipse(xx,yy,w,h); float xHead=xx - 5 + w/2; if (dx<0 ) { xHead = xx+5- w/2; } ellipse(xHead, yy-10,w/3,h/3); fill(255,255,0); ellipse(xHead-2, yy-10,4,4); float step= 5; if (x%50 <25) { step= -5; leg(xx+2-w/2,yy-4+h/2, step); leg( xx+8-w/2, yy-4+h/2, step); leg( xx-2+w/2, yy-4+h/2, step); leg (xx-8+w/2, yy-4+h/2,step); } fill(0); } void leg (float x, float y, float step) { line (x,y, step+x, 12+y); } void move() { x+=dx+ random(3,-3); if (x>=finish+50); { dx=-random (3,8); } if (x<=start+30); { dx=random (3,8); } }}// class Varmit //