代码拉取完成,页面将自动刷新
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
margin: 0;
padding: 0;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<canvas id="can" height="400" width="400" style="background-color: black;">对不起,您的浏览器不支持canvas</canvas>
<script>
var snake = [41, 40],
direction = 1,
food = 42,
speed = 150,
n,
box = document.getElementById('can').getContext('2d');
function draw(seat, color) {
box.fillStyle = color;
box.fillRect(seat % 20 * 20 + 1, ~~(seat / 20) * 20 + 1, 18, 18);
console.log(seat % 20 * 20 + 1, ~~(seat / 20) * 20 + 1)
}
function drawfood() {
if(Math.random() < 0.2) {
draw(food, 'red');
speed = speed > 20 ? speed - 10 : 10;
} else {
draw(food, 'yellow');
speed = speed > 15 ? speed - 5 : 10;
}
}
document.onkeydown = function(evt) {
direction = snake[1] - snake[0] == (n = [-1, -20, 1, 20][(evt || event).keyCode - 37] || direction) ? direction : n;
}
!function() {
snake.unshift(n = snake[0] + direction);
if(snake.indexOf(n, 1) > 0 || //crash yourself
n < 0 || //over top
n > 399 || //over bottom
direction == 1 && n % 20 == 0 || //over right
direction == -1 && n & 20 == 19) { //over left
return alert('game over');
}
draw(n, "lime");
if(n == food) {
while(snake.indexOf(food = ~~(Math.random() * 400)) > 0);
drawfood();
} else {
draw(snake.pop(), "black");
}
setTimeout(arguments.callee, speed);
}();
</script>
</body>
</html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。