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; }