// Declarations PFont letters; // Constants int ex=900, why=900, span=100; int buttonx=100, buttony=180, combuttonx = 185, combuttony = 100; int randx, randy; // Random values for the yellow square // State Information Describing the Turtles int delx, dely, dir, done, spring; boolean pickedup = false; // Generic values int xposr = 182, yposr=172, rdelx = 0, rdely=0, rdir=0, rdone=0; boolean rhid; // Raff's description int xposm = 208, yposm=172, mdelx = 0, mdely=0, mdir=0, mdone=0; boolean mhid; // Mike's description int xposl = 233, yposl=172, ldelx = 0, ldely=0, ldir=0, ldone=0; boolean lhid; // Leo's description int xposd = 258, yposd=172, ddelx = 0, ddely=0, ddir=0, ddone=0; boolean dhid; // Don's description // Colors used throughout the code color raffc = color(255,0,0); color mikec = color(250,122,0); color leoc = color(0,0,255); color donc = color(128,0,128); color rmove = color(0,255,0); color lmove = color(255,200,0); color umove = color(255, 0, 255); color dmove = color(0,255,255); color hmove = color(0); color amove = color(200); color emove = color(0,130,0); color wmove = color(165,100,252); // Operational Variables int sel = 4, com = 0, clicktype = 0, getinsts = 0, run = 0; char[ ] codes = { 'S', 'R', 'L', 'U', 'D', 'H', 'A' }; char[ ] team = { 'r', 'm', 'l', 'd'}; String[ ] ninja, move; String inst = "", guy = ""; int pc = 0; void setup( ) { size(900,900); background(255); letters = loadFont("LucidaSans-20.vlw"); textFont(letters); // layout( ); // Setup the canvas with the game board plane(180,150, span); grid(180, 150, span); randx = max(1,int(random(7))-1); randy = max(1,int(random(7))-1); fill(255,255,0); rect((randx+1)*span+185, (randy+1)*span+155,span-10, span-10); turtle(182, 172, raffc, false); turtle(208, 172, mikec, false); turtle(233, 172, leoc, false); turtle(258, 172, donc, false); frameRate(10); tbutton(100,180, raffc, color(255), "Raff"); tbutton(100,220, mikec, color(255), "Mike"); tbutton(100,260, leoc, color(255), "Leo"); tbutton(100,300, donc, color(255), "Don"); cbutton(185,100, wmove, color(255), " Spring"); cbutton(285,100, rmove, color(0,100,0), " Right"); cbutton(385,100, lmove, color(0), " Left"); cbutton(485,100, umove, color(255), " Up"); cbutton(585,100, dmove, color(0,0,255), " Down"); cbutton(685,100, hmove, color(255), " Hide"); cbutton(785,100, amove, color(80), " Appear"); rbutton(100,100, color(255), color(0), "RUN"); rbutton(100,140, color(255), color(0), " +"); } void draw( ) { grid(180, 150, span); showSelect(70, 180); if (clicktype == 1) { // A turtle button has been clicked getinsts = 1; // It's OK to click the commands now } if (getinsts == 1 && clicktype == 2) { guy = guy + '-' + team[sel]; //Add turtle who will do this inst = inst + '-' + codes[com]; //Whats he gonna do? showCode( ); //Display the program clicktype = 0; //Clear type so we don't try to understand other clicks } if (clicktype == 3 && pc < ninja.length) { //Run! decodeInst( ); //Figure out what inst is and who does it nextInst( ); //Assign work and get next instruction if ready } execute( ); //Do it } //The following decodes the instrution that is pointed at by the "pc", the program counter; //when it finds out what the instruction is, it saves the values needed to do the command in //some generic variables; later we assign these values to the guy who does the work void decodeInst( ) { delx = 0; //Generic variables to store values needed for this instruction dely = 0; // before we know who is doing it dir = 0; spring = 0; done = 10; if (move[pc].equals("R")) { // Move right delx = 10; // Move 10 steps of lenght (it will turn out) 10 dir = 1; // Positive x } if(move[pc].equals("L")) { // ... and so forth delx = 10; dir = -1; } if(move[pc].equals("U")) { dely = 10; dir = -1; } if(move[pc].equals("D")) { dely = 10; dir = 1; } if(move[pc].equals("S")) { dely = 3; //float down from a spring spring = -30; //jump up 3 units dir = 1; //come back down } } //We know who does the work, now assign it ... hide and appear are tested specifically void getwork(char dude) { if (dude == 'r') { //Is Raff our Ninja for this work? rdelx = delx; //Yes, then give him the values we set aside above rdely = dely; rdir = dir; rdone = done; yposr = yposr + spring; if(move[pc].equals("H")) { //Check, since maybe this wasn't a move, but a hide rhid = true; //It is ... bye, bye } if(move[pc].equals("A")) { //Check, since maybe this wasn't a move, but an appear rhid = false; //Up } } if (dude == 'm') { // ... and so on mdelx = delx; mdely = dely; mdir = dir; mdone = done; yposm = yposm + spring; if(move[pc].equals("H")) { mhid = true; } if(move[pc].equals("A")) { mhid = false; } } if (dude == 'l') { ldelx = delx; ldely = dely; ldir = dir; ldone = done; yposl = yposl + spring; if(move[pc].equals("H")) { lhid = true; } if(move[pc].equals("A")) { lhid = false; } } if (dude == 'd') { ddelx = delx; ddely = dely; ddir = dir; ddone = done; yposd = yposd + spring; if(move[pc].equals("H")) { dhid = true; } if(move[pc].equals("A")) { dhid = false; } } } void nextInst( ) { // Figures out, who does the work if (rdone == 0 && ninja[pc].equals("r")) { // Work for Raff getwork('r'); pickedup = true; // Isn't busy, get going } if (mdone == 0 && ninja[pc].equals("m")) { // Work for Mike getwork('m'); pickedup = true; // Isn't busy, get going } if (ldone == 0 && ninja[pc].equals("l")) { // Work for Leo getwork('l'); pickedup = true; // Isn't busy, get going } if (ddone == 0 && ninja[pc].equals("d")) { // Work for Don getwork('d'); pickedup = true; // Isn't busy, get going } if (pickedup) { // If the operation is assigned pc = min(pc + 1, ninja.length); // Do the next one, if there is one pickedup = false; // Reset the "Is doing it" flag } } void execute( ) { // Performs the work, if there's anything to be done if (rdone > 0 ) { //Yes, Raff's got work turtle(xposr, yposr, color(200), false); xposr = xposr + rdelx*rdir; //Note, one of delx or dely is 0 yposr = yposr + rdely*rdir; rdone = rdone - 1; turtle(xposr, yposr, raffc, rhid); //Redraw in new position } if (mdone > 0 ) { //Yes, Mike's got work turtle(xposm, yposm, color(200), false); xposm = xposm + mdelx*mdir; yposm = yposm + mdely*mdir; mdone = mdone - 1; turtle(xposm, yposm, mikec, mhid); } if (ldone > 0 ) { //Yes, Leo's got work turtle(xposl, yposl, color(200), false); xposl = xposl + ldelx*ldir; yposl = yposl + ldely*ldir; ldone = ldone - 1; turtle(xposl, yposl, leoc, lhid); } if (ddone > 0 ) { //Yes, Don's got work turtle(xposd, yposd, color(200), false); xposd = xposd + ddelx*ddir; yposd = yposd + ddely*ddir; ddone = ddone - 1; turtle(xposd, yposd, donc, dhid); } } // Main Working Functions void grid(int xpos, int ypos, int tick) { // Draw the lines on canvas stroke(0); for (int i = 0; i< 7; i++) { line(xpos, ypos+i*tick, xpos+7*tick, ypos+i*tick); } for (int i = 0; i< 7 ; i++) { line(xpos+i*tick, ypos, xpos+i*tick, ypos+7*tick); } noStroke( ); } void plane(int xpos, int ypos, int tick) { // Draw the gray on the canvas fill(200,200,200); rect(xpos, ypos, ex-2*tick, why-2*tick); } //The following code draws a turtle -- goggles can be a true color or gray, //which is used for the "erasing" operation; also, note when hidden, only //goggles get drawn void turtle( float xoffset, float yoffset, color goggles, boolean hidden) { if (goggles == color(200)) { fill(200); //OK, we're erasing if (!hidden) { rect(xoffset, yoffset, 20, 79); //Erase whole ninja } else { rect(xoffset, 17+yoffset, 20, 4); //Erase only goggles } } else { //OK, this is the true color if (!hidden) { fill(0,100,0); // Whole Ninja form rect(xoffset,56+yoffset, 20, 23); fill(219,136,0); rect(xoffset,31+yoffset, 20, 25); fill(0,100,0); rect(xoffset,21+yoffset, 20, 10); fill(goggles); rect(xoffset, 17+yoffset, 20, 4); fill(0,100,0); rect(xoffset, 9+yoffset, 20, 8); } else { fill(goggles); //Goggles only hidden form rect(xoffset, 17+yoffset, 20, 4); } } } // The following code draws a left side turtle button void tbutton(int xpos, int ypos, color b, color t, String name) { //Draw the turtle buttons of "b" color with "name" printed in "t" color fill(b); rect(xpos, ypos, 55, 30); fill(t); text(name, xpos+5, ypos+22); } // The following code draws a top row command button void cbutton(int xpos, int ypos, color b, color t, String name) { fill(b); rect(xpos, ypos, 90, 30); fill(t); text(name, xpos+5, ypos+22); } // This code draws the Run and + buttons void rbutton(int xpos, int ypos, color b, color t, String name) { stroke(180); strokeWeight(4); fill(b); rect(xpos, ypos, 55, 30); fill(t); text(name, xpos+5, ypos+22); strokeWeight(1); noStroke( ); } void showSelect(int xpos, int ypos ) { //Draw the triangle pointer select icon if(sel < 4) { //Make sure sel is set; else no change fill(255); rect(xpos, ypos, 20, 160); // Erase the triangle fill(180); // Now redraw it triangle(xpos, ypos+40*sel, xpos, ypos+30+40*sel, xpos+20, ypos+15+40*sel); } } // The following displays the instructions; the color comes from "guy" and the // operation comes from move; both are "split out" into an indexable list (or // array), and we refer to them by their index, as in move[i] void showCode( ) { ninja = splitTokens(guy, "-"); //break guy apart, toss dashes, save lc letters move = splitTokens(inst, "-"); //break inst apart, toss dashes, save UC letters for (int i=0; i < ninja.length; i++) { // Go through the instructions if (ninja[i].equals("r")) { // Raff's command? fill(raffc); text(move[i],185+20*i, 60); // Print the command's single letter } if (ninja[i].equals("m")) { // Mikes's command? fill(mikec); text(move[i],185+20*i, 60); // Print the command's single letter } if (ninja[i].equals("l")) { // Leo's command? fill(leoc); text(move[i],185+20*i, 60); // Print the command's single letter } if (ninja[i].equals("d")) { // Don's command? fill(donc); text(move[i],185+20*i, 60); // Print the command's single letter } } } void mousePressed( ){ //Figure out what was just clicked clicktype = 0; // clear the clicktype for (int i=0; i < 4; i++) { //Check to see if it is a turtle button if (mouseX >= buttonx && mouseX <= buttonx+55 && mouseY >= buttony+40*i && mouseY <= buttony+30+40*i) { sel = i; //Yup! Got it! clicktype = 1; } } for (int i=0; i < 7; i++) { //Check to see if it is command button if (mouseX >= combuttonx+100*i && mouseX <= combuttonx+90+100*i && mouseY >= combuttony && mouseY <= combuttony+30) { com = i; //Yup! Got it! clicktype = 2; } //Check if it's the run button if (mouseX >= 100 && mouseX <= 155 && mouseY >= 100 && mouseY <= 130) { clicktype = 3; //Yup! Got it! } if (mouseX >= 100 && mouseX <= 155 // Plus button? && mouseY >= 140 && mouseY <= 170) { clicktype = 1; //Yup! Got it! } } }