java.lang.String author="© Prof.BAM © (DO NOT COPY THIS CODE!)"; java.lang.String title="Dog Race!"; java.lang.String subtitle="Space bar advances all dogs, randomly." +"\n(q to quit; r to reset)\nA for autopilot."; Button b1,b2,b3,b4; Mouse mickey,oswald; Dog d1,d2,d3; color c1,c2,c3; java.lang.String name1="Rex"; java.lang.String name2="Spot"; java.lang.String name3="Mutt"; 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 count=0,down=120; boolean autopilot=false; void setup( ){ size(640,480 ); init( ); reset( ); coloreset( ); down=1 + down/2; autopilot =true;} void init( ){ b1=new Button(name1,10,10 ); b2=new Button(name2,10,40 ); b3=new Button(name3,10,70 ); b4=new Button("ALL",10,100 ); d1=new Dog(name1,start,track1+25,c1 ); d2=new Dog(name2,start,track2+25,c2 ); d3=new Dog(name3,start,track3+25,c3 ); mickey=new Mouse(start+30,track2-10,color(0 ) ); oswald=new Mouse(finish-30,track3-10,color(255 ) ); } void reset( ){ d1.x=d2.x=d3.x=start; mickey.x=start+50; oswald.x=finish-50; } void coloreset( ){ b1.c=d1.c =colordog( ); b2.c=d2.c =colordog( ); b3.c=d3.c =colordog( ); b4.c=color(0,0,255 ); } color colordog( ){ int r=(int )random(100,200 ),g=(int )random(0,100 ),b=(int )random(0,50 ); return color(r,g,b ); } void draw( ){ scene( ); showAll( ); messages( ); ++count; if(!autopilot )return; text(down - count%down,width-50,height-20 ); if(count%down ==0 ){ if(winner>0 )reset( ); moveAll( ); } } void scene( ){ background(200,240,255 ); fill(180,250,200 ); rect(0,horizon,width,height-horizon ); 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 ); rect(start,track3,finish-start+100,80 ); b1.show( ); b2.show( ); b3.show( ); b4.show( ); } void showAll( ){ d1.show( ); d2.show( ); d3.show( ); fill(0 ); text((int )(d1.x-start ),finish+100,track1 ); text((int )(d2.x-start ),finish+100,track2 ); text((int )(d3.x-start ),finish+100,track3 ); mickey.update( ); oswald.update( ); } void messages( ){ textSize(30 ); fill(0,0,255 ); text(title,width/3,30 ); textSize(12 ); text(subtitle,10+width/3,50 ); text(author,10,height-5 ); if(d1.x > finish ){ winner=1; textSize(24 ); fill(d1.c ); text(d1.name+ " won the race!",xNews,yNews ); } else if(d2.x > finish ){ winner=2; textSize(24 ); fill(d2.c ); text(d2.name+ " wins the race!",xNews,yNews ); } else if(d3.x > finish ){ winner=3; textSize(24 ); fill(d3.c ); text(d3.name+ " wins the race!",xNews,yNews ); } else { winner=0; textSize(12 ); fill(150 ); text("(No winner, yet)",xNews,yNews ); } textSize(12 ); } void keyPressed( ){ if(key =='q' ){ exit( ); } if(key =='r' ){ reset( ); coloreset( ); } if(key =='c' ){ coloreset( ); } if(key ==' ' ){ moveAll( ); } if(key =='a' ){ down=120; autopilot=! autopilot; } if(key =='A' ){ down=1 + down/2; autopilot =true; } if(key =='1' ){ d1.x +=random(99 ); } if(key =='2' ){ d2.x +=random(99 ); } if(key =='3' ){ d3.x +=random(99 ); } } void moveAll( ){ d1.move( ); d2.move( ); d3.move( ); } void mousePressed( ){ if(b1.clicked( ) )d1.move( ); if(b2.clicked( ) )d2.move( ); if(b3.clicked( ) )d3.move( ); if(b4.clicked( ) )moveAll( ); } class Button { float x,y; float w=90,h=25; java.lang.String name; color c=color(255 ); Button( ){ }; Button(java.lang.String name,float x,float y ){ this.name=name; this.x=x; this.y=y; } Button(java.lang.String name,float x,float y,color c ){ this.name=name; this.x=x; this.y=y; this.c=c; } void show( ){ fill(c ); rect(x,y,w,h ); fill(255,255,0 ); text("Move"+name,x+5,y+20 ); } boolean clicked( ){ return hit(mouseX,mouseY ); } boolean hit(float xx,float yy ){ return xx>x && xx<x+w && yy>y && yy<y+h; } } class Dog { float x,y,w=100,h=40; java.lang.String name; color c; float wag=5; Dog( ){ }; Dog(java.lang.String name,float x,float y ){ this.name=name; this.x=x; this.y=y; } Dog(java.lang.String name,float x,float y,color c ){ this.c=c; this.name=name; this.x=x; this.y=y; } void show( ){ fill(c ); rect(x,y,w,h ); float xHead=x + w - 20; rect(xHead,y-20,40,20 ); strokeWeight(8 ); stroke(c ); if(x>start ){ if(count%10 ==0 ){ wag=random(20 ); } line(x,y+4,x-30,y+12+wag ); } strokeWeight(6 ); 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 ); strokeWeight(1 ); stroke(0 ); fill(255 ); 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(255 ); rect(xHead+25+random(2 ),y-5,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,255,0 ); text(name,x+20,y+20 ); } void leg(float x,float y,float step ){ line(x,y,step+x,24+y ); } void move( ){ x=x + random(1,10 ); } } class Mouse { float x,y,dx=5,dy=0; color c=0; float w=36,h=16; Mouse(float x,float y,color c ){ this.c=c; this.x=x; this.y=y; } void update( ){ if(winner>0 )return; if(d1.x<start+10 && d2.x<start+10 & d3.x<start+10 )return; show( ); move( ); } void show( ){ 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 ); } 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 ); } } }