xxxxxxxxxx
float x = 0; // x location of square
float y = 0; // y location of square
float speed = 0; // speed of square
boolean isGravity = true;
float gravity = 0.3;
void setup() {
fullScreen();
x = width/2;
}
// if clicked, removes the gravity
void draw() {
background(212, 221, 237);
if (isGravity) {
noStroke();
fill(0, 34, 128);
} else {
noStroke();
fill(255, 88, 94);
}
stroke(0);
rectMode(CENTER);
rect(x, y, 10, 10);
// Add speed to location.
y = y + speed;
// Do things regarding to gravity
if (isGravity) {
// Add gravity to speed if gravity is present
speed = speed + gravity;
// bounce
if (y > height) {
// Multiplying by -0.95 instead of -1 slows the square down each time it bounces (by decreasing speed).
// This is known as a "dampening" effect and is a more realistic simulation of the real world (without it, a ball would bounce forever).
speed = speed * -0.95;
y = height;
}
} else {
// simply go theough the borders
if (y>height)
y = 0;
else if (y<0)
y = height;
}
// nullify gravity
if (keyPressed) {
isGravity = !isGravity;
}
}