//NIBBLES for the 40h //Adam Ribaudo | arribaud [at] gmail [dot] com | 2006.10.16 import processing.serial.*; import jklabs.monomic.*; Monome m; Nibbles n; Explosion e; Point food; boolean begin = false; void setup() { frameRate(5); //Modify the following line depending on your 40h connection m = new MonomeSerial(this, "COM3"); n = new Nibbles(new Point(0,0), 1); food = new Point(int(random(7)), int(random(7))); //Check to make sure the food isnt in 0,0 if (food.x == 0 && food.y==0) food = new Point(int(random(7)), int(random(7))); //add food and Nibbles to the grid m.lightOn(food.x, food.y); m.lightOn(n.head.x, n.head.y); } void draw() { if(begin) { if (n.isAlive) { m.lightsOff(); //Assign each tail segment to the location of the segment in front of it for (int i = n.length-1; i > 0; i--) { n.body[i].x = n.body[i-1].x; n.body[i].y = n.body[i-1].y; } //Set the explosion origin before nibbles moves in case he dies e = new Explosion(n.head, m); //Move the head n.move(); //If Nibble's head is in a valid location, set the lights if (n.isValid()) { m.lightOn(n.head.x, n.head.y); for (int i = n.length-1; i > 0; i--) { m.lightOn(n.body[i].x, n.body[i].y); } //If Nibble's body is on food, increase the length if (n.head.x == food.x && n.head.y == food.y) { n.length++; n.body[n.length-1] = new Point(n.head.x, n.head.y); food = new Point(int(random(7)), int(random(7))); } m.lightOn(food.x, food.y); } else { //Nibbles died n.isAlive = false; m.lightsOff(); println("Congradulations! Nibbles grew to be [" + n.length + "] years old. I'm sure it was all downhill from there anyway."); } } else{ //Animate explosion e.nextFrame(); } } } void keyPressed() { if (key == CODED) { if (keyCode == UP) { n.direction = "U"; begin = true; } else if (keyCode == DOWN) { n.direction = "D"; begin = true; } else if (keyCode == LEFT) { n.direction = "L"; begin = true; } else if (keyCode == RIGHT) { n.direction = "R"; begin = true; } } } class Point { int x; int y; Point (int _x,int _y) { x = _x; y = _y; } } class Explosion { Point origin; Monome m; int frame; Explosion(Point p, Monome _m){ origin = new Point(p.x, p.y); frame = 0; m=_m; } void nextFrame(){ if(this.isValid(new Point(origin.x+frame, origin.y+frame)))m.lightOn(origin.x+frame, origin.y+frame); if(this.isValid(new Point(origin.x-frame, origin.y-frame)))m.lightOn(origin.x-frame, origin.y-frame); if(this.isValid(new Point(origin.x-frame, origin.y+frame)))m.lightOn(origin.x-frame, origin.y+frame); if(this.isValid(new Point(origin.x+frame, origin.y-frame)))m.lightOn(origin.x+frame, origin.y-frame); if(this.isValid(new Point(origin.x+frame, origin.y)))m.lightOn(origin.x+frame, origin.y); if(this.isValid(new Point(origin.x, origin.y+frame)))m.lightOn(origin.x, origin.y+frame); if(this.isValid(new Point(origin.x-frame, origin.y)))m.lightOn(origin.x-frame, origin.y); if(this.isValid(new Point(origin.x, origin.y-frame)))m.lightOn(origin.x, origin.y-frame); frame++; } boolean isValid(Point p){ if (p.x <= 7 && p.x >= 0 && p.y <= 7 && p.y >= 0) { return true; } else return false; } } class Nibbles { String direction; int length; Point head; Point[] body; boolean isAlive; //Constructor Nibbles (Point p, int _length) { length = _length; direction = ""; head = p; body = new Point[64]; body[0] = head; isAlive = true; } //Change the location of Nibble's head according to the key pressed void move() { if (n.direction == "U") { n.head.y = n.head.y - 1; } else if (n.direction == "D") { n.head.y = n.head.y + 1; } else if (n.direction == "L") { n.head.x = n.head.x - 1; } else if (n.direction == "R") { n.head.x = n.head.x + 1; } } //Checks to see if the point is in the array of points constituting Nibble's "body" boolean isInBody(Point p) { for (int i=1; i= 0 && n.head.y <= 7 && n.head.y >= 0) { if(!n.isInBody(n.head)) { return true; } } return false; } }