1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); var scoretext = document.getElementById("Score"); var snack = { body: [{ x: 180, y: 140 }, { x: 170, y: 140 }, { x: 160, y: 140 }, { x: 150, y: 140 }, { x: 140, y: 140 }], bodysize: 5, direct: "right" }; var apple = {}; var score = 0; move(); applecreate(); var time = setInterval(move, 100);
document.addEventListener("keydown", function (e) { if (e.keyCode == "37") { snack.direct = "left"; } else if (e.keyCode == "39") { snack.direct = "right";
} else if (e.keyCode == "38") { snack.direct = "top";
} else if (e.keyCode == "40") { snack.direct = "down";
} });
function move() { updatescore(); ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.fillStyle = "#9fe8fa"; ctx.fillRect(0, 0, 300, 300); ctx.fillStyle = "#26baee"; ctx.fillRect(apple.x, apple.y, 10, 10); ctx.fillStyle = "#FFE"; ctx.strokeStyle = "#26baee"; ctx.lineWidth = 1; direction(); for (var i = 0; i < snack.body.length; i++) { ctx.fillRect(snack.body[i].x, snack.body[i].y, 10, 10); ctx.strokeRect(snack.body[i].x, snack.body[i].y, 10, 10); } for (var i = 0; i < snack.body.length; i++) { if (i >= snack.bodysize) snack.body.pop(); } }
function direction() { if (snack.direct == "left") { snack.body.unshift({ x: snack.body[0].x - 10, y: snack.body[0].y }); } else if (snack.direct == "top") { snack.body.unshift({ x: snack.body[0].x, y: snack.body[0].y - 10 }); } else if (snack.direct == "down") { snack.body.unshift({ x: snack.body[0].x, y: snack.body[0].y + 10 }); } else if (snack.direct == "right") { snack.body.unshift({ x: snack.body[0].x + 10, y: snack.body[0].y });
} eatapple(); gameover(); }
function gameover() { for (var i = 1; i < snack.body.length; i++) { if (snack.body[0].x == snack.body[i].x && snack.body[0].y == snack.body[i].y) { reset(); break; } } if (snack.body[0].x < 0 || snack.body[0].x >= 300 || snack.body[0].y >= 300 || snack.body[0].y < 0) { reset(); } }
function applecreate() { var x = Math.floor(Math.random() * 30) * 10; var y = Math.floor(Math.random() * 30) * 10; for (var i = 0; i < snack.body.length; i++) { if (x == snack.body[i].x && y == snack.body[i].y) { x = Math.floor(Math.random() * 30) * 10; y = Math.floor(Math.random() * 30) * 10; i = 0; } } apple.x = x; apple.y = y; }
function eatapple() { if (snack.body[0].x == apple.x && snack.body[0].y == apple.y) { applecreate(); snack.bodysize++; score += 10; } }
function updatescore() { scoretext.innerText = score; }
function reset() { snack.body = [{ x: 180, y: 140 }, { x: 170, y: 140 }, { x: 160, y: 140 }, { x: 150, y: 140 }, { x: 140, y: 140 }]; snack.bodysize = 5; snack.direct = "right"; move(); applecreate(); score = 0; }
|