1 // Declarations 2 PFont letters; 3 4 // Constants 5 int ex=900, why=900, span=100; 6 int buttonx=100, buttony=180, combuttonx = 185, combuttony = 100; 7 int randx, randy; // Random values for the yellow square 8 9 // State Information Describing the Turtles 10 int delx, dely, dir, done, spring; boolean pickedup = false; // Generic values 11 int xposr = 182, yposr=172, rdelx = 0, rdely=0, rdir=0, rdone=0; boolean rhid; // Raff's description 12 int xposm = 208, yposm=172, mdelx = 0, mdely=0, mdir=0, mdone=0; boolean mhid; // Mike's description 13 int xposl = 233, yposl=172, ldelx = 0, ldely=0, ldir=0, ldone=0; boolean lhid; // Leo's description 14 int xposd = 258, yposd=172, ddelx = 0, ddely=0, ddir=0, ddone=0; boolean dhid; // Don's description 15 16 // Colors used throughout the code 17 color raffc = color(255,0,0); 18 color mikec = color(250,122,0); 19 color leoc = color(0,0,255); 20 color donc = color(128,0,128); 21 color rmove = color(0,255,0); 22 color lmove = color(255,200,0); 23 color umove = color(255, 0, 255); 24 color dmove = color(0,255,255); 25 color hmove = color(0); 26 color amove = color(200); 27 color emove = color(0,130,0); 28 color wmove = color(165,100,252); 29 30 // Operational Variables 31 int sel = 4, com = 0, clicktype = 0, getinsts = 0, run = 0; 32 char[ ] codes = { 'S', 'R', 'L', 'U', 'D', 'H', 'A' }; 33 char[ ] team = { 'r', 'm', 'l', 'd'}; 34 String[ ] ninja, move; 35 String inst = "", guy = ""; 36 int pc = 0; 37 38 void setup( ) { 39 size(900,900); 40 background(255); 41 letters = loadFont("LucidaSans-20.vlw"); 42 textFont(letters); 43 44 // layout( ); // Setup the canvas with the game board 45 plane(180,150, span); 46 grid(180, 150, span); 47 randx = max(1,int(random(7))-1); 48 randy = max(1,int(random(7))-1); 49 fill(255,255,0); 50 rect((randx+1)*span+185, (randy+1)*span+155,span-10, span-10); 51 turtle(182, 172, raffc, false); 52 turtle(208, 172, mikec, false); 53 turtle(233, 172, leoc, false); 54 turtle(258, 172, donc, false); 55 frameRate(10); 56 tbutton(100,180, raffc, color(255), "Raff"); 57 tbutton(100,220, mikec, color(255), "Mike"); 58 tbutton(100,260, leoc, color(255), "Leo"); 59 tbutton(100,300, donc, color(255), "Don"); 60 cbutton(185,100, wmove, color(255), " Spring"); 61 cbutton(285,100, rmove, color(0,100,0), " Right"); 62 cbutton(385,100, lmove, color(0), " Left"); 63 cbutton(485,100, umove, color(255), " Up"); 64 cbutton(585,100, dmove, color(0,0,255), " Down"); 65 cbutton(685,100, hmove, color(255), " Hide"); 66 cbutton(785,100, amove, color(80), " Appear"); 67 rbutton(100,100, color(255), color(0), "RUN"); 68 rbutton(100,140, color(255), color(0), " +"); 69 } 70 71 void draw( ) { 72 grid(180, 150, span); 73 showSelect(70, 180); 74 if (clicktype == 1) { // A turtle button has been clicked 75 getinsts = 1; // It's OK to click the commands now 76 } 77 if (getinsts == 1 && clicktype == 2) { 78 guy = guy + '-' + team[sel]; //Add turtle who will do this 79 inst = inst + '-' + codes[com]; //Whats he gonna do? 80 showCode( ); //Display the program 81 clicktype = 0; //Clear type so we don't try to understand other clicks 82 } 83 if (clicktype == 3 && pc < ninja.length) { //Run! 84 decodeInst( ); //Figure out what inst is and who does it 85 nextInst( ); //Assign work and get next instruction if ready 86 } 87 execute( ); //Do it 88 } 89 90 //The following decodes the instrution that is pointed at by the "pc", the program counter; 91 //when it finds out what the instruction is, it saves the values needed to do the command in 92 //some generic variables; later we assign these values to the guy who does the work 93 void decodeInst( ) { 94 delx = 0; //Generic variables to store values needed for this instruction 95 dely = 0; // before we know who is doing it 96 dir = 0; 97 spring = 0; 98 done = 10; 99 if (move[pc].equals("R")) { // Move right 100 delx = 10; // Move 10 steps of lenght (it will turn out) 10 101 dir = 1; // Positive x 102 } 103 if(move[pc].equals("L")) { // ... and so forth 104 delx = 10; 105 dir = -1; 106 } 107 if(move[pc].equals("U")) { 108 dely = 10; 109 dir = -1; 110 } 111 if(move[pc].equals("D")) { 112 dely = 10; 113 dir = 1; 114 } 115 if(move[pc].equals("S")) { 116 dely = 3; //float down from a spring 117 spring = -30; //jump up 3 units 118 dir = 1; //come back down 119 } 120 } 121 122 //We know who does the work, now assign it ... hide and appear are tested specifically 123 void getwork(char dude) { 124 if (dude == 'r') { //Is Raff our Ninja for this work? 125 rdelx = delx; //Yes, then give him the values we set aside above 126 rdely = dely; 127 rdir = dir; 128 rdone = done; 129 yposr = yposr + spring; 130 if(move[pc].equals("H")) { //Check, since maybe this wasn't a move, but a hide 131 rhid = true; //It is ... bye, bye 132 } 133 if(move[pc].equals("A")) { //Check, since maybe this wasn't a move, but an appear 134 rhid = false; //Up 135 } 136 } 137 if (dude == 'm') { // ... and so on 138 mdelx = delx; 139 mdely = dely; 140 mdir = dir; 141 mdone = done; 142 yposm = yposm + spring; 143 if(move[pc].equals("H")) { 144 mhid = true; 145 } 146 if(move[pc].equals("A")) { 147 mhid = false; 148 } 149 } 150 if (dude == 'l') { 151 ldelx = delx; 152 ldely = dely; 153 ldir = dir; 154 ldone = done; 155 yposl = yposl + spring; 156 if(move[pc].equals("H")) { 157 lhid = true; 158 } 159 if(move[pc].equals("A")) { 160 lhid = false; 161 } 162 } 163 if (dude == 'd') { 164 ddelx = delx; 165 ddely = dely; 166 ddir = dir; 167 ddone = done; 168 yposd = yposd + spring; 169 if(move[pc].equals("H")) { 170 dhid = true; 171 } 172 if(move[pc].equals("A")) { 173 dhid = false; 174 } 175 } 176 } 177 178 void nextInst( ) { // Figures out, who does the work 179 if (rdone == 0 && ninja[pc].equals("r")) { // Work for Raff 180 getwork('r'); 181 pickedup = true; // Isn't busy, get going 182 } 183 if (mdone == 0 && ninja[pc].equals("m")) { // Work for Mike 184 getwork('m'); 185 pickedup = true; // Isn't busy, get going 186 } 187 if (ldone == 0 && ninja[pc].equals("l")) { // Work for Leo 188 getwork('l'); 189 pickedup = true; // Isn't busy, get going 190 } 191 if (ddone == 0 && ninja[pc].equals("d")) { // Work for Don 192 getwork('d'); 193 pickedup = true; // Isn't busy, get going 194 } 195 if (pickedup) { // If the operation is assigned 196 pc = min(pc + 1, ninja.length); // Do the next one, if there is one 197 pickedup = false; // Reset the "Is doing it" flag 198 } 199 } 200 201 void execute( ) { // Performs the work, if there's anything to be done 202 if (rdone > 0 ) { //Yes, Raff's got work 203 turtle(xposr, yposr, color(200), false); 204 xposr = xposr + rdelx*rdir; //Note, one of delx or dely is 0 205 yposr = yposr + rdely*rdir; 206 rdone = rdone - 1; 207 turtle(xposr, yposr, raffc, rhid); //Redraw in new position 208 } 209 if (mdone > 0 ) { //Yes, Mike's got work 210 turtle(xposm, yposm, color(200), false); 211 xposm = xposm + mdelx*mdir; 212 yposm = yposm + mdely*mdir; 213 mdone = mdone - 1; 214 turtle(xposm, yposm, mikec, mhid); 215 } 216 if (ldone > 0 ) { //Yes, Leo's got work 217 turtle(xposl, yposl, color(200), false); 218 xposl = xposl + ldelx*ldir; 219 yposl = yposl + ldely*ldir; 220 ldone = ldone - 1; 221 turtle(xposl, yposl, leoc, lhid); 222 } 223 if (ddone > 0 ) { //Yes, Don's got work 224 turtle(xposd, yposd, color(200), false); 225 xposd = xposd + ddelx*ddir; 226 yposd = yposd + ddely*ddir; 227 ddone = ddone - 1; 228 turtle(xposd, yposd, donc, dhid); 229 } 230 } 231 232 // Main Working Functions 233 234 void grid(int xpos, int ypos, int tick) { // Draw the lines on canvas 235 stroke(0); 236 for (int i = 0; i< 7; i++) { 237 line(xpos, ypos+i*tick, xpos+7*tick, ypos+i*tick); 238 } 239 for (int i = 0; i< 7 ; i++) { 240 line(xpos+i*tick, ypos, xpos+i*tick, ypos+7*tick); 241 } 242 noStroke( ); 243 } 244 245 void plane(int xpos, int ypos, int tick) { // Draw the gray on the canvas 246 fill(200,200,200); 247 rect(xpos, ypos, ex-2*tick, why-2*tick); 248 } 249 250 //The following code draws a turtle -- goggles can be a true color or gray, 251 //which is used for the "erasing" operation; also, note when hidden, only 252 //goggles get drawn 253 void turtle( float xoffset, float yoffset, color goggles, boolean hidden) { 254 if (goggles == color(200)) { 255 fill(200); //OK, we're erasing 256 if (!hidden) { 257 rect(xoffset, yoffset, 20, 79); //Erase whole ninja 258 } else { 259 rect(xoffset, 17+yoffset, 20, 4); //Erase only goggles 260 } 261 } else { //OK, this is the true color 262 if (!hidden) { 263 fill(0,100,0); // Whole Ninja form 264 rect(xoffset,56+yoffset, 20, 23); 265 fill(219,136,0); 266 rect(xoffset,31+yoffset, 20, 25); 267 fill(0,100,0); 268 rect(xoffset,21+yoffset, 20, 10); 269 fill(goggles); 270 rect(xoffset, 17+yoffset, 20, 4); 271 fill(0,100,0); 272 rect(xoffset, 9+yoffset, 20, 8); 273 } else { 274 fill(goggles); //Goggles only hidden form 275 rect(xoffset, 17+yoffset, 20, 4); 276 } 277 } 278 } 279 280 // The following code draws a left side turtle button 281 void tbutton(int xpos, int ypos, color b, color t, String name) { 282 //Draw the turtle buttons of "b" color with "name" printed in "t" color 283 fill(b); 284 rect(xpos, ypos, 55, 30); 285 fill(t); 286 text(name, xpos+5, ypos+22); 287 } 288 289 // The following code draws a top row command button 290 void cbutton(int xpos, int ypos, color b, color t, String name) { 291 fill(b); 292 rect(xpos, ypos, 90, 30); 293 fill(t); 294 text(name, xpos+5, ypos+22); 295 } 296 297 // This code draws the Run and + buttons 298 void rbutton(int xpos, int ypos, color b, color t, String name) { 299 stroke(180); 300 strokeWeight(4); 301 fill(b); 302 rect(xpos, ypos, 55, 30); 303 fill(t); 304 text(name, xpos+5, ypos+22); 305 strokeWeight(1); 306 noStroke( ); 307 } 308 309 void showSelect(int xpos, int ypos ) { //Draw the triangle pointer select icon 310 if(sel < 4) { //Make sure sel is set; else no change 311 fill(255); 312 rect(xpos, ypos, 20, 160); // Erase the triangle 313 fill(180); // Now redraw it 314 triangle(xpos, ypos+40*sel, xpos, ypos+30+40*sel, xpos+20, ypos+15+40*sel); 315 } 316 } 317 318 // The following displays the instructions; the color comes from "guy" and the 319 // operation comes from move; both are "split out" into an indexable list (or 320 // array), and we refer to them by their index, as in move[i] 321 void showCode( ) { 322 ninja = splitTokens(guy, "-"); //break guy apart, toss dashes, save lc letters 323 move = splitTokens(inst, "-"); //break inst apart, toss dashes, save UC letters 324 for (int i=0; i < ninja.length; i++) { // Go through the instructions 325 if (ninja[i].equals("r")) { // Raff's command? 326 fill(raffc); 327 text(move[i],185+20*i, 60); // Print the command's single letter 328 } 329 if (ninja[i].equals("m")) { // Mikes's command? 330 fill(mikec); 331 text(move[i],185+20*i, 60); // Print the command's single letter 332 } 333 if (ninja[i].equals("l")) { // Leo's command? 334 fill(leoc); 335 text(move[i],185+20*i, 60); // Print the command's single letter 336 } 337 if (ninja[i].equals("d")) { // Don's command? 338 fill(donc); 339 text(move[i],185+20*i, 60); // Print the command's single letter 340 } 341 } 342 } 343 344 void mousePressed( ){ //Figure out what was just clicked 345 clicktype = 0; // clear the clicktype 346 for (int i=0; i < 4; i++) { //Check to see if it is a turtle button 347 if (mouseX >= buttonx && mouseX <= buttonx+55 348 && mouseY >= buttony+40*i && mouseY <= buttony+30+40*i) { 349 sel = i; //Yup! Got it! 350 clicktype = 1; 351 } 352 } 353 for (int i=0; i < 7; i++) { //Check to see if it is command button 354 if (mouseX >= combuttonx+100*i && mouseX <= combuttonx+90+100*i 355 && mouseY >= combuttony && mouseY <= combuttony+30) { 356 com = i; //Yup! Got it! 357 clicktype = 2; 358 } 359 //Check if it's the run button 360 if (mouseX >= 100 && mouseX <= 155 361 && mouseY >= 100 && mouseY <= 130) { 362 clicktype = 3; //Yup! Got it! 363 } 364 if (mouseX >= 100 && mouseX <= 155 // Plus button? 365 && mouseY >= 140 && mouseY <= 170) { 366 clicktype = 1; //Yup! Got it! 367 } 368 } 369 } 370