//// Squid race: Create a race for an array of creatures. String title= "S Q U I D R A C E"; String q=""; String ahead=""; String author= "Your Name"; float surface, bottom, sideX, sideY; float goY, allY, leadY, resetY; float wave=50; int many=8; int race=0, count=0, leader=-1, winner=-1, delay=0; float sunX,sunY; int idle=0, maxidle=60; String names[]= { "Arthur", "Boris", "Charlie", "Dopey", "Ed", "Frank", "George", "Herbie"}; Squid school[]= new Squid[8]; ////// Setup: screen size, create array, etc. void setup() { size( 800, 600 ); surface= height/4; bottom= height-100; sideX= width-180; sideY= height-280; // goY= 90; allY= surface+60; leadY= surface+100; resetY= height-60; reset(); } ////// Create the array of squids, spaced across the window. void reset() { winner= -1; float x= 30; many= int( random(5,10.5) ); if (count==0) many=8; for (int i=0; imaxidle ) { text( "IDLE > "+ maxidle +" ticks; \nbegin AUTOPILOT.", 20, height-80 ); if (idle % 90 == 0) { if ( winner<0) race(); if (idle>600) reset(); } } } void scene() { sun(); fill( 0, 80, 40 ); noStroke(); rectMode(CORNER); rect( 0, surface, width, height*3/4 ); // Sea // Waves. for( float x=0; x=0 & winner <0) { fill( school[leader].r, school[leader].g, school[leader].b ); text( school[leader].name + " is in the lead!", width/4, 100 ); text( school[leader].name + " is in the lead!", width/4, height-50 ); school[leader].crown(); } } void sun() { fill(255,255,0); fill(127,127,50, 100); // Moon colors. (Use sun x y ) } // //////// BUTTONS //////// void buttons() { // GO button, to move upward. textSize(12); text( "Click GO to move all upward.", sideX, goY-10 ); fill( 0,255,0 ); stroke( 0,0,255 ); button( " G O ", sideX, goY ); // Buttons to drop back textSize(12); fill( 255,255,0); text( "Click to drop back.", sideX, surface+50 ); fill( 255,255,0 ); stroke( 255,0,0 ); button( " ALL back", sideX, allY ); fill( 0,255,255 ); stroke( 255,0,255 ); button( "Leader back", sideX, leadY ); // RESET fill( 255,0,255 ); stroke( 0 ); button( "RESET", sideX, resetY ); } void action() { // Now, show all of the creatures. fill( 255,0,255 ); showAll( school, many ); // if (winner>=0) { textSize(20); fill( 255, 0, 0 ) ; text( "G A M E O V E R", width/4, height/2 ); //// Display winner & loser in the table). strokeWeight(6); // Table // fill( 255,255,255 ); rectMode(CORNER); rect( sideX-10, sideY, 180, 200, 10 ); winner( school, many ); loser( school, many ); } } //// Draw one button. void button( String s, float x, float y ) { rectMode(CORNER); strokeWeight(4); rect( x,y, 160,30, 10 ); fill(0); textSize(24); text( s, x+10, y+25 ); } ////// Handle keyboard & click void keyPressed() { if (key == 'q') exit(); if (key == 'r') reset(); if (key == ' ') race(); idle=0; } void mousePressed() { if (click( sideX,goY, 160,40)) race(); if (click( sideX,allY, 160,40)) allBack(); if (click( sideX,leadY, 160,40)) leadBack(); idle=0; } boolean click( float x, float y, float w, float h ) { if (mouseXx+w) return false; if (mouseYy+h) return false; return true; } ////// Move all creatures upward, with a random value. void race() { count++; if (winner<0) { for (int i=0; i=0; i--) k= a[i].y>a[k].y ? i : k; return k; } //////////////////////////// CLASS Squid //////////////////// class Squid { //// PROPERTIES //// String name="?"; float x=50, y=bottom; // Position of this creature. float w=30, h=50; // width & height int legs=10; int r, g, b; // Color. //// CONSTRUCTORS //// Squid( float x, String s, float w) { this.x= x; this.name= s; this.w= w; h= w * random(1.5, 3); // h is 1.5 to 3 * w legs= int( random(2,12) ); // Random # of legs (2,12) setColors( int(random(255)), int(random(255)), int(random(255)) ); } Squid() { // Default constructor -- does nothing } // Store colors in member variables. // void setColors( int r, int g, int b ) { this.r= r; this.g= g; this.b= b; } // //// METHODS //// // Display the creature. // void show() { noStroke(); fill( r, g, b ); rectMode(CENTER); rect( x, y, w, h ); ellipseMode(CENTER); ellipse( x, y-h/2, w, h ); eye( x, y-h*3/4); showLegs(); // fill( (r+128)%256, (g+128)%256, (b+128)%256 ); textSize(14); text( name, 3+x-w/2, y ); textSize(10); text( legs+" legs", x-w/3, y+12 ); } void crown() { fill( r, g, b ); triangle( x, y-h-w/2, x-w/2,y-h, x+w/2,y-h ); } void eye( float eyeX, float eyeY ) { fill(255); ellipse( eyeX, eyeY, 20,20 ); fill(r,g,b); ellipse( eyeX, eyeY, 10,10 ); } void showLegs() { float hipX=x-w/2, hipY=y+h/2; float swim=5; if (int(y) % 2 > 0) swim= -5; strokeWeight(1); stroke(r,g,b); float spacing= w/legs; for (hipX= x-w/2+spacing/2; hipX