game
let c = document.getElementById(“game”);
let ctx = c.getContext(“2d”);
// player
let x = 130;
// falling block
let bx = Math.random() * 280;
let by = 0;
document.addEventListener(“keydown”, e => {
if (e.key === “ArrowLeft”) x -= 20;
if (e.key === “ArrowRight”) x += 20;
});
function draw() {
ctx.clearRect(0, 0, 300, 300);
// player (white bar)
ctx.fillStyle = “white”;
ctx.fillRect(x, 270, 40, 10);
// falling block
ctx.fillRect(bx, by, 20, 20);
by += 3;
// reset if it falls
if (by > 300) {
by = 0;
bx = Math.random() * 280;
}
requestAnimationFrame(draw);
}
draw();