This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snakeUI.js
76 lines (69 loc) · 1.67 KB
/
snakeUI.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
var game = new SnakeGame.Game()
var size = SnakeGame.size
game.start();
var run_step = function(){
_.each(game.board.grid, function(elem, index){
square = $('#'+index);
square.removeClass("snake apple");
square.removeClass("rattle");
if (elem === "snake"){
square.addClass("snake");
}
else if (elem === "rattle"){
square.addClass("rattle")
}
else if (elem === "apple"){
square.addClass("apple")
}
});
$('#score').html('<h1>' + game.score + '</h1>')
if (game.stop){
$('#gameover').html('<h1>GAME OVER. HIT ENTER TO RESET</h1>')
}
else{
$('#gameover').html('')
}
}
$(function(){
//register key handlers
$(document).keydown(function(event){
if (event.keyCode === 38){
game.north = false;
game.south = true;
game.east = false;
game.west = false;
}
else if(event.keyCode === 40){
game.north = true;
game.south = false;
game.east = false;
game.west = false;
}
else if(event.keyCode === 37){
game.nort = false;
game.south = false;
game.east = false;
game.west = true;
}
else if(event.keyCode === 39){
game.nort = false;
game.south = false;
game.east = true;
game.west = false;
}
else if(event.keyCode === 13){
if(game.stop){
game.resetGame();
}
}
})
//draw empty grid of divs
for (var row = 0; row < size; row++){
$('table').append('<tr id=row_' + row + '></tr>');
for (var column = 0; column < size; column++){
var id = row * size + column;
$('#row_'+row).append('<td><div id=' + id + '></div></td>');
}
}
window.setInterval(run_step, 16);
});