xxxxxxxxxx
35
// Click on the canvas to begin detecting key presses.
let y = 50;
function setup() {
createCanvas(100, 100);
background(200);
describe(
'A gray square with a black circle at its center. The circle moves when the user presses an arrow key. It leaves a trail as it moves.'
);
}
function draw() {
// Update x and y if an arrow key is pressed.
if (keyIsPressed === true) {
if (keyCode === UP_ARROW) {
y -= 1;
} else if (keyCode === DOWN_ARROW) {
y += 1;
} else if (keyCode === LEFT_ARROW) {
x -= 1;
} else if (keyCode === RIGHT_ARROW) {
x += 1;
}
}
// Style the circle.
fill(0);
// Draw the circle at (x, y).
circle(x, y, 5);
}