创建简单的编程猫小游戏代码编程
```html
canvas {
border: 1px solid black;
}
// 获取canvas元素
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
// 定义猫咪的初始位置
let catX = 200;
let catY = 200;
// 绘制猫咪
function drawCat() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "orange";
ctx.fillRect(catX, catY, 50, 50);
}
// 监听键盘按下事件
document.addEventListener("keydown", function(event) {
const speed = 5; // 移动速度
switch(event.key) {
case "ArrowUp":
catY = speed;
break;
case "ArrowDown":
catY = speed;
break;
case "ArrowLeft":
catX = speed;
break;
case "ArrowRight":
catX = speed;
break;
}
drawCat(); // 每次按键后重新绘制猫咪
});
// 绘制初始猫咪
drawCat();