“Moving Boxes” by MATTHEW NAGOWSKI
https://openprocessing.org/sketch/812303
License CreativeCommons Attribution ShareAlike
https://creativecommons.org/licenses/by-sa/3.0
{{filePath}}
{{width}} x {{height}}
Report Sketch
Oh, that naughty sketch! Please let us know what the issue is below.
Apply Template
Applying this template will reset your sketch and remove all your changes. Are you sure you would like to continue?
Report Sketch
Report Comment
Please confirm that you would like to report the comment below.
We will review your submission and take any actions necessary per our Community Guidelines. In addition to reporting this comment, you can also block the user to prevent any future interactions.
Please report comments only when necessary. Unnecessary or abusive use of this tool may result in your own account being suspended.
Are you sure you want to delete your sketch?
Any files uploaded will be deleted as well.
Delete Comment?
This will also delete all the replies to this comment.
Delete this tab? Any code in it will be deleted as well.
Select a collection to submit your sketch
We Need Your Support
Since 2008, OpenProcessing has provided tools for creative coders to learn, create, and share over a million open source projects in a friendly environment.
Niche websites like ours need your continued support for future development and maintenance, while keeping it an ad-free platform that respects your data and privacy!
Please consider subscribing below to show your support with a "Plus" badge on your profile and get access to many other features!
CC Attribution ShareAlike
Moving Boxes
NAGOWSKI
xxxxxxxxxx
ArrayList<Rect> rects;
int rectWidth = 200;
int rectHeight = 200;
int x = 0;
int y = 0;
void setup() {
size(550, 500);
rectMode(CORNERS);
// Create an empty ArrayList (will store Ball objects)
rects = new ArrayList<Rect>();
// Start by adding 50 elementa
for(int a = 1;a<50; a++){
x = round(random(0,width));
y = round(random(0,height));
rects.add(new Rect(x, y, x+random(50,50+rectWidth), y+random(50,50+rectHeight)));
}
}
void draw() {
background(255);
// With an array, we say balls.length, with an ArrayList, we say balls.size()
// The length of an ArrayList is dynamic
// Notice how we are looping through the ArrayList backwards
// This is because we are deleting elements from the list
for (int i = rects.size()-1; i >= 0; i--) {
// An ArrayList doesn't know what it is storing so we have to cast the object coming out
Rect rect = rects.get(i);
rect.move();
rect.display();
//if (rect.finished()) {
// // Items can be deleted with remove()
// rects.remove(i);
//}
}
}
void mousePressed() {
// A new rect object is added to the ArrayList (by default to the end)
rects.add(new Rect(mouseX, mouseY, mouseX+random(50,50+rectWidth), mouseY+random(50,50+rectHeight) ));
}
// Simple bouncing ball class
class Rect {
float x1;
float y1;
float x2;
float y2;
float xdir;
float ydir;
float speed;
float r1;
float r2;
float s1;
float s2;
float s3;
float s4;
float l1;
float l2;
float l3;
float l4;
float life = 255;
Rect(float tempX1, float tempY1, float tempX2, float tempY2) {
x1 = tempX1;
y1 = tempY1;
x2 = tempX2;
y2 = tempY2;
xdir=0;
ydir=0;
s1 = round(random(50,100));
s2 = random(200,255);
s3 = round(random(1,1));
s4 = random(225,255);
l1 = random(50,100);
l2 = random(100,200);
l3 = random(100,200);
l4 = random(0,1);
r1 = random(0,5);
if(r1<1) {
xdir = -1;
}
if(r1>=1 & r1 <2) {
xdir = 1;
}
if(r1>=2 & r1<3) {
ydir = -1;
}
if(r1>=3 & r1 <4) {
ydir = 1;
}
speed = (random(0.05,2));
}
void move() {
if(x2<0) {
x2 = width + (x2-x1);
x1 = width;
}
if(x1>width) {
x1 = 0 - (x2-x1);
x2 = 0;
}
if(y2<0) {
y2 = height + (y2-y1);
y1 = height;
}
if(y1>height) {
y1 = 0 - (y2-y1);
y2 = 0;
}
x1 = x1+xdir*speed;
x2 = x2+xdir*speed;
y1 = y1+ydir*speed;
y2 = y2+ydir*speed;
}
boolean finished() {
// Balls fade out
life--;
if (life < 0) {
return true;
} else {
return false;
}
}
void display() {
// Display the circle
strokeWeight(s3);
stroke(s1,s2);
fill(s4,10);
//stroke(0,life);
rect(x1,y1,x2,y2);
for (float j = 0; j<l1; j = j+1) {
strokeWeight(l4);
stroke(l2,l3);
if(ydir != 0) {
line(x1+(x2-x1)/l1*j,y1,x1+(x2-x1)/l1*j,y2);
}
if(xdir !=0) {
line(x1,y1+(y2-y1)/l1*j,x2,y1+(y2-y1)/l1*j);
}
}
}
}
See More Shortcuts