Skip to content

Instantly share code, notes, and snippets.

@osteele
Last active October 30, 2020 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osteele/87563fa4daec182a8e8a64ed9946b1da to your computer and use it in GitHub Desktop.
Save osteele/87563fa4daec182a8e8a64ed9946b1da to your computer and use it in GitHub Desktop.
/* A reproduction, with modification, of a work by Vera Molnar.
* Click a square to start it falling.
* Author: Oliver Steele
* Source: https://gist.github.com/osteele/87563fa4daec182a8e8a64ed9946b1da
* This work is licensed under a Creative Commons Attribution 4.0 International License.
*/
final int size = 100;
final int displacement = 6;
FloatList xs = new FloatList();
FloatList ys = new FloatList();
FloatList dys = new FloatList();
FloatList initialYs = new FloatList();
BooleanList isMoving = new BooleanList();
void setup() {
size(800, 600);
float x = 0;
float y = height - 100;
while (y >= 0) {
xs.append(x);
ys.append(y);
dys.append(0);
initialYs.append(y);
isMoving.append(false);
x += 100;
if (x >= width) {
x = 0;
y -= 100;
}
}
}
void draw() {
background(255);
randomSeed(1);
noFill();
for (int i = 0; i < xs.size(); i++) {
float x = xs.get(i);
float y = ys.get(i);
float dy = dys.get(i);
if (mousePressed && pointInRect(mouseX, mouseY, x, y, size, size)) {
if (!isMoving.get(i)) {
isMoving.set(i, true);
dy = y + 150 <= height ? 0 : -20;
dys.set(i, dy);
}
}
if (isMoving.get(i)) {
dy += 0.5;
y += dy;
if (y + size > height && dy >= 0) {
dy *= -0.9;
if (abs(dy) < 0.1) {
y = initialYs.get(i);
ys.set(i, y);
isMoving.set(i, false);
}
}
ys.set(i, y);
dys.set(i, dy);
}
push();
translate(x, y);
//if (pointInRect(mouseX, mouseY, x, y, size, size)) {
// rotateAbout(frameCount / 50.0, size/2, size/2);
//}
drawSquare();
pop();
}
}
void drawSquare() {
int margin = 0;
for (int i = 0; i < 8; i++) {
int width = size - margin;
int height = size - margin;
push();
fill(255);
if (random(10) <= 1) {
fill(random(255));
}
if (i == 7 && random(10) <= 1) {
fill(random(255), random(255), random(255));
}
beginShape();
vertex(margin + random(displacement), margin + random(displacement));
vertex(width - random(displacement), margin + random(displacement));
vertex(width - random(displacement), height - random(displacement));
vertex(margin + random(displacement), height - random(displacement));
endShape(CLOSE);
pop();
margin += random(3, 6);
}
}
boolean pointInRect(float x, float y, float left, float top, float width, float height) {
return left <= x && x < left + width && top <= y && y < top + height;
}
void rotateAbout(float angle, float x, float y) {
translate(x, y);
rotate(angle);
translate(-x, -y);
}
class BooleanList {
void append(boolean value) {
_items.append(value ? 1 : 0);
}
boolean get(int index) {
return _items.get(index) == 1;
}
void set(int index, boolean value) {
_items.set(index, value ? 1 : 0);
}
private IntList _items = new IntList();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment