xxxxxxxxxx
46
var x = 200;
var y = 200;
var changeY = 0;
var changeX = 0;
function setup() {
createCanvas(400, 400);
background(0);
}
function draw() {
rect(x, y, 100, 100);
fill(0, 0, 255);
stroke(255);
if (random(10) < 1) {
var dir = int(random(4));
if (dir === 0) {
changeX = 5;
changeY = 0;
} else if (dir === 1) {
changeX = -5;
changeY = 0;
} else if (dir === 2) {
changeY = 5;
changeX = 0;
} else {
changeY = -5;
changeX = 0;
}
if(x>400){
x=380
}
if(y>400){
y=380
}
if(y<0){
y=20
}
if(x<0){
x=0
}
}
y = y + changeY;
x = x + changeX;
}