-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake-ui.js
60 lines (49 loc) · 1.56 KB
/
snake-ui.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
(function () {
var SnakeGame = window.SnakeGame = (window.SnakeGame || {});
var View = SnakeGame.View = function ($el) {
this.$el = $el;
};
var start = View.prototype.start = function () {
this.setup();
this.board = new SnakeGame.Board();
this.board.makeApples(5);
var that = this;
$('body').on("keydown", function (event) {
that.board.turn(event.keyCode);
});
var that = this;
window.setInterval(function(){
if (!that.board.gameOver) {
that.board.step();
that.redraw();
} else {
$('.message').text("Game Over");
}
}, 150);
};
var setup = View.prototype.setup = function () {
for(var i = 0; i < SnakeGame.Board.BOARD_SIZE; i++) {
for(var j = 0; j < SnakeGame.Board.BOARD_SIZE; j++) {
var $tile = $("<div class='tile'></div>");
$tile.attr("data-row", i);
$tile.attr("data-col", j);
this.$el.append($tile);
}
}
}
var redraw = View.prototype.redraw = function () {
$(".snake").removeClass("snake");
$(".apple").removeClass("apple");
var apples = this.board.apples;
for (var i = 0; i < apples.length; i++) {
var $appleTile = $('div[data-row='+apples[i].ypos+'][data-col='+apples[i].xpos+']');
$appleTile.addClass('apple');
}
var segments = this.board.snake.segments;
for (var i = 0; i < segments.length; i++) {
var $snakeTile = $('div[data-row='+segments[i].ypos+'][data-col='+segments[i].xpos+']');
$snakeTile.addClass('snake');
$snakeTile.removeClass('apple');
}
}
})();