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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
| var FPS = 10; var gLoop; var lastX; var lastY;
var canvas = document.getElementById('c'); var ctx = canvas.getContext('2d'); var info = document.getElementById('i'); var wrapper = document.getElementById('w'); var classic = document.getElementById('classic');
canvas.width = 500; canvas.height = 300; classic.addEventListener('click', init, false); wrapper.style.width = canvas.width + 'px'; info.style.width = canvas.width + 'px';
document.onkeydown = function(e) { snake.keyDown(e); };
function getRandomInt(min, max) { return ~~(Math.random() * (max - min + 1)) + min; }
function drawBox(x, y, color) { ctx.fillStyle = color; ctx.fillRect(x + 1, y + 1, 8, 8); }
function clear() { ctx.fillStyle = '#eee'; ctx.beginPath(); ctx.rect(0, 0, canvas.width, canvas.height); ctx.closePath(); ctx.fill();
function grid(increment, color) { ctx.fillStyle = color; for (var i = 0; i < canvas.width + 1; i += increment) { ctx.fillRect(i - 1, 0, 2, canvas.height); } for (var i = 0; i < canvas.height + 1; i += increment) { ctx.fillRect(0, i - 1, canvas.width, 2); } } grid(10, '#ddd'); grid(100, '#ccc'); }
var food = new function() { this.x = 10 * getRandomInt(0, (canvas.width - 10) / 10); this.y = 10 * getRandomInt(0, (canvas.height - 10) / 10); this.color = '#87af00'; this.draw = function() { drawBox(this.x, this.y, this.color); }; this.eaten = function() { this.x = 10 * getRandomInt(0, (canvas.width - 10) / 10); this.y = 10 * getRandomInt(0, (canvas.height - 10) / 10); }; }
var snake = new function() { this.x = canvas.width / 2; this.y = canvas.height / 2; this.direction = ''; this.color = '#268bd2'; this.tail = []; this.dead = true;
this.eat = function() { food.eaten(); this.tail.unshift([this.x, this.y]); };
this.keyDown = function(e) { if (this.dead) { return; } switch (e.keyCode) { case 38: e.preventDefault(); if (this.direction != 'down') { this.direction = 'up' } break case 40: e.preventDefault(); if (this.direction != 'up') { this.direction = 'down' } break case 37: e.preventDefault(); if (this.direction != 'right') { this.direction = 'left' } break case 39: e.preventDefault(); if (this.direction != 'left') { this.direction = 'right' } break } }; this.update = function() { if (!this.dead) { if (this.direction == 'up') { this.y -= 10; } if (this.direction == 'down') { this.y += 10; } if (this.direction == 'left') { this.x -= 10; } if (this.direction == 'right') { this.x += 10; } } else { this.direction = ''; }
if ((this.x == food.x) && (this.y == food.y)) { this.eat(); }
for (var i = 1; i < this.tail.length; i++) { if ((this.x == this.tail[i][0]) && (this.y == this.tail[i][1])) { this.dead = true; } } if (this.x > canvas.width - 10 || this.y > canvas.height - 10 || this.x < 0 || this.y < 0) { this.dead = true; }
}; this.draw = function() { drawBox(this.x, this.y, this.color); for (var i = 0; i < this.tail.length; i++) { drawBox(this.tail[i][0], this.tail[i][1], this.color); }
if (!this.dead) { lastX = this.x; lastY = this.y; this.tail.unshift([lastX, lastY]); this.tail.pop(); } }; }
function gameLoop() { snake.update();
clear(); food.draw(); snake.draw();
gLoop = setTimeout(gameLoop, 1000 / FPS); }
function init() { info.style.display = 'none'; snake.dead = false; gameLoop(); } clear();
|