xxxxxxxxxx
50
let directionX = "c";
let directionY = "d";
let w = 50;
let h = 50;
let velocityX = 10;
let velocityY = 10;
let [x, y] = [0, 0];
function setup() {
createCanvas(600, 600);
frameRate(30);
}
function draw() {
background(220);
if (directionX == "d") {
x = x + velocityX;
}
if (directionX == "e") {
x = x - velocityX;
}
if (directionY == "b") {
y = y + velocityY;
}
if (directionY == "c") {
y = y - velocityY;
}
rect(x, y, w, h);
console.log(x, y, directionX, directionY);
if (x <= 0) {
directionX = "d";
velocityX = floor(random(0, 10));
}
if (x >= 600 - w) {
directionX = "e";
velocityX = floor(random(0, 10));
}
if (y <= 0) {
directionY = "b";
velocityY = floor(random(0, 10));
}
if (y >= 600 - w) {
directionY = "c";
velocityY = floor(random(0, 10));
}
}