-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake-view.js
77 lines (68 loc) · 2.24 KB
/
snake-view.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
( function() {
if (typeof Snake === "undefined") {
window.Snake = {};
}
var View = Snake.View = function ($el, newBoard) {
var that = this;
this.$el = $el;
this.board = newBoard;
this.setupBoard(20);
this.bindEvents();
this.addApples();
this.intervalId = window.setInterval(that.step.bind(that), 200);
this.appleId = window.setInterval(that.addApples.bind(that), 8000);
};
View.prototype.addApples = function() {
var numApples = Math.floor(Math.random() * 4) + 1;
for (var i=0; i< numApples; i++) {
var xCoord = Math.floor(Math.random()*20);
var yCoord = Math.floor(Math.random()*20);
$apple = $("[data-pos='" + xCoord + "," + yCoord + "']");
$apple.addClass("apple");
}
};
View.prototype.step = function() {
if (this.board.snake.isDead()) {
clearInterval(this.intervalId);
clearInterval(this.appleId);
alert("You lost!");
} else {
this.board.snake.move();
this.updateScore();
}
};
View.prototype.bindEvents = function () {
var that = this;
this.$el.on("keydown", that.handleKeyEvent.bind(that));
};
View.prototype.handleKeyEvent = function (event) {
if (event.keyCode === 37) {
this.board.snake.turn("W");
} else if (event.keyCode === 38) {
this.board.snake.turn("N");
} else if (event.keyCode === 39) {
this.board.snake.turn("E");
} else if (event.keyCode === 40) {
this.board.snake.turn("S");
}
};
View.prototype.setupBoard = function (n) {
for (var i =0 ; i<n; i++) {
var $outerDiv = $("<div></div>");
$outerDiv.attr("style", "width: 360; margin-top: -6px;");
this.$el.append($outerDiv);
for (var j = 0; j<n; j++) {
var $innerDiv = $("<div data-pos='" + i + "," + j + "'></div>");
$innerDiv.attr("style", "display: inline-block; border: 3px solid black; height: 15px; width: 15px; margin-left: -3px");
$outerDiv.append($innerDiv);
}
}
};
View.prototype.updateScore = function () {
var score = this.board.snake.segments.length;
var scoreString = "Score: "
var $scoreDiv = $("<div>" + "Score: " + score + "</div>");
$scoreDiv.addClass("score");
this.$el.append($scoreDiv);
};
})();