xxxxxxxxxx
51
let x = 400; // x position
let y = 400; // y position
let r = 50; // radius
let speedX = 0; // speed in X
let speedY = 0; // speed in Y
function setup() {
createCanvas(600, 400);
}
function draw() {
background('pink');
// update position
x = x + speedX;
y = y + speedY;
// make the ball bounce of walls
if (x < r || x > width-r) {
speedX = speedX * -1;
}
if (y > height-r) {
speedY = speedY * -0.9;
y = height-r;
}
// we hit the left edge
if (x < r) {
// check if we have an Arduino connected
if (writer) {
// send the signal
writer.write("!");
}
}
// make the ball be affected by gravity and drag
speedY = speedY + 0.25;
speedX = speedX * 0.99;
ellipse(x, y, 2*r, 2*r);
}
function mouseClicked() {
// add some random acceleration when we click the ball
if (dist(mouseX, mouseY, x, y) < r) {
print("Clicked the ball");
speedX = random(-25, 25);
speedY = random(-20, 20);
}
}