Skip to content

Instantly share code, notes, and snippets.

@slambert
Created November 4, 2015 20:54
Show Gist options
  • Save slambert/0359dc615994437ff36e to your computer and use it in GitHub Desktop.
Save slambert/0359dc615994437ff36e to your computer and use it in GitHub Desktop.
Alex's spot sketch, modified as we did in class.
// Alex's spot sketch.
// It could use more comments.
// Steve Lambert 2015-11-02
Spot[] spots; // Declare array
float gravity = 0.005;
float r;
float g;
float b;
float a;
void setup() {
//size(1000, 400, P2D);
fullScreen(P2D);
int numSpots = 100; // Number of objects
//int dia = width/numSpots; // Calculate diameter
//int dia = round(random(1,10));
int dia = 1;
spots = new Spot[numSpots]; // Create array
for (int i = 0; i < spots.length; i++) {
float x = map(i,0,spots.length,0,width); // this places the dots across the top at the beginning
float rate = random(0.1, 2.0);
// Create each object:
spots[i] = new Spot(x, 10, i, rate);
}
noStroke();
}
void draw() {
// random colors
r = random(255);
g = random(255);
b = random(255);
a = random(255);
fill(0);
rect(0, 0, width, height);
fill(r,g,b,a);
for (int i=0; i < spots.length; i++) {
spots[i].move(); // Move each object
spots[i].display(); // Display each object
spots[i].gravity(); // Gravity
}
}
class Spot {
float x, y; // X-coordinate, y-coordinate
float diameter; // Diameter of the circle
float speed; // Distance moved each frame
int direction = 1; // Direction of motion (1 is down, -1 is up)
// Constructor
Spot(float xpos, float ypos, float dia, float sp) {
x = xpos;
y = ypos;
diameter = dia;
speed = sp;
}
void move() {
y += (speed * direction);
if ((y > (height - diameter/2)) || (y < diameter/2)) {
direction *= -1;
}
}
void display() {
ellipse(x, y, diameter, diameter);
}
void gravity() {
speed = speed + gravity;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment