bouncing ball

click to drop again.

code


boolean button = false; // initial state for start/stop switch

// setting up variables
float locX = 0; // location X
float locY = 100; // location Y
float xSpeed = random(1,3); // Speed on X axis
float ySpeed = random(1,3); // Speed on Y axis
float gravity = 0.1; // gravity!

void setup() {
size(200,200);
ellipseMode(CORNER);
}

void draw() {
background(255);
stroke(255);
fill(255,166,70); // orange!

locX = locX + xSpeed; // x = x + 1 ... but with variables
locY = locY + ySpeed; // y = y + 1 ... but with variables

ySpeed = ySpeed + gravity; // speed on y axis is affected by gravity, always pushing down

// if object gets to the edge, go the other way.
if ((locX > width-21) || (locX < 1)){ xSpeed = xSpeed * -1; } if ((locY > height-21) || (locY < 1)){ ySpeed = ySpeed * -.75; // inertia? } //draw the circle ellipse(locX,locY,20,20); // Increment x if (button) { locX = locX + xSpeed; locY = locY + ySpeed; } else { locX = locX; locY = locY; } locX = constrain(locX,0,width-20); locY = constrain(locY,0,height-20); } void mousePressed() { locX = mouseX; locY = mouseY; xSpeed = int(random(-3,3)); ySpeed = int(random(-3,3)); } void keyPressed() { button = !button; }

expanding circles

code


// declaring variables
int circleX = 100;
int circleY = 100;
int circleW = 50;
int circleH = 50;
int circleBg = 100;
int circleStroke = 255;
int circleFill = 0;

// setup
void setup() {
size(200,200);
}

// draw
void draw() {
background(circleBg);
stroke(circleStroke);
fill(circleFill);

// four circles
ellipse(circleX-50,circleY-50,circleH,circleW);
ellipse(circleX+50,circleY-50,circleH,circleW);
ellipse(circleX+50,circleY+50,circleH,circleW);
ellipse(circleX-50,circleY+50,circleH,circleW);

// action!
circleW = circleW+2;
circleH = circleH+2;
circleBg = circleH;
circleFill = (-circleH*1)+(255);

// constraints
circleFill = constrain(circleFill,0,255);
circleH = constrain(circleH,0,height*2);
circleW = constrain(circleW,0,width*2);
}

void mousePressed() {
// when mouse is pressed, reset it all and start again
circleW = 1;
circleH = 1;
loop();
}

Random Line Maker

the code


// Random Line Maker
// I wanted to connect the lines, but I will come back to that idea

// declare variables
//colors
float r;
float g;
float b;
float a;

//construction
float startX = width/2;
float startY = height;
float endX;
float endY;

void setup() {
size(400,400);
background(255);
smooth();
}
void draw() {
// pick a random color
r = random(255);
g = random(255);
b = random(255);
a = random(255);

startX = random(width);
startY = random(height);
endX = random(width);
endY = random(height);

// create a random colored line of random dimensions
stroke(r,g,b,a);
line(startX,startY,endX,endY);

}

// if the mouse is pressed, redraw the background
void mousePressed() {
background(255);
}