-
Notifications
You must be signed in to change notification settings - Fork 5
/
snake.js
44 lines (40 loc) · 1001 Bytes
/
snake.js
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
onload = init();
function init(){
var s = newSnake()
draw(s);
}
function draw(points){
var board = document.getElementById('board');
board.innerHTML = ''; // clears out elements from last render
for (var i = 0; i<10; i++){
var row = document.createElement('div');
row.classList.add('row');
board.appendChild(row);
for (var j = 0; j<10; j++){
var tile = document.createElement('div');
tile.classList.add('tile');
row.appendChild(tile);
if (containsPoint(points, {x: j, y: i})){
tile.classList.add('snake');
}
console.log('create tile');
}
}
}
function containsPoint(points, point){
for (var i=0; i<points.length; i++){
if (point.x === points[i].x && point.y === points[i].y){
return true;
}
}
return false;
}
// Make a new snake... maybe?
function newSnake(){
var length = 5;
snake = [];
for (var i = 0; i < length; i++){
snake.push({x:i, y:0});
}
return snake;
}