Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
animating squares
/* animateSq - Lia */
Box [] boxes = new Box[2500];
int numShiftBoxes;
int counter;
int totalRows;
void setup() {
size (500, 500);
for (int q = 0; q < boxes.length; q++) {
boxes[q] = new Box ();
}
numShiftBoxes = 0;
}
void draw() {
background (0);
for (int q = 0; q < boxes.length; q++) {
int row = q/ (width/10);
int xpos = q - ((width/10)*row);
int ypos = row;
boxes[q].display (xpos*10, ypos*10);
}
//first increment row-by-row
if (counter < totalRows && (frameCount% .5 == 0)) {
for (int i = counter*50; i < counter*50 + 50; i++) {
boxes[i].shift();
}
counter ++;
}
//then at the end, increment one by one
if (counter == totalRows) {
for (int i = totalRows*50; i < totalRows*50 + (numShiftBoxes - totalRows*50); i++) {
boxes[i].shift();
}
}
}
void mousePressed() {
numShiftBoxes = int (random (boxes.length));
println ("NUMBER: " + numShiftBoxes);
totalRows = numShiftBoxes/ 50;
for (int i = 0; i < boxes.length; i++) {
boxes[i].reset();
}
counter = 0;
}
class Box {
int xpos, ypos;
color fillColor;
Box() {
fillColor = color (50, 50, 50);
}
void display(int _xpos, int _ypos) {
xpos = _xpos;
ypos = _ypos;
fill (fillColor);
rect (xpos, ypos, 10, 10);
}
void shift() {
fillColor = color (255, 0, 0);
}
void reset() {
fillColor = color (50, 50, 50);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment