-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
150 lines (127 loc) · 4.11 KB
/
app.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
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
var app = angular.module('plunker', ['ngMaterial']);
app.controller('MainCtrl', function ($scope, $window, $timeout) {
$scope.name = 'Gamer';
$scope.score = 0;
var boardSize = 25;
// colors set used for paint the cell in board
var paints = {
game_end: '#820303',
fruit: '#E80505',
snake_head: '#FF33F9',
snake_body: '#FFA500',
cell: '#808080'
};
// key code of directions
var runDirections = { LEFT: 37, UP: 38, RIGHT: 39, DOWN: 40, SPACE: 32 };
// initialization of snake & fruit
var snake = {
direction: runDirections.LEFT,
parts: [{ x: -1, y: -1 }]
};
var fruit = { x: -1, y: -1 };
var interval, tempDirection, isGameOver;
$scope.paintCell = function (col, row) {
if (isGameOver) {
return paints.game_end;
}
if (snake.parts[0].x == row && snake.parts[0].y == col) {
return paints.snake_head;
}
if ($scope.board[col][row] === true) {
return paints.snake_body;
}
if (fruit.x == row && fruit.y == col) {
return paints.fruit;
}
return paints.cell;
};
function generateBoard() {
$scope.board = [];
for (var indexOne = 0; indexOne < boardSize; indexOne++) {
$scope.board[indexOne] = [];
for (var indexTwo = 0; indexTwo < boardSize; indexTwo++) {
$scope.board[indexOne][indexTwo] = false;
}
}
}
generateBoard();
$scope.startGame = function () {
snake = { direction: runDirections.LEFT, parts: [] };
isGameOver = false;
interval = 500;
tempDirection = runDirections.LEFT;
// Set up initial snake
for (var i = 0; i < 5; i++) {
snake.parts.push({ x: 10 + i, y: 10 });
}
updateCells();
updateFruit();
console.log(snake);
}
function updateCells() {
var newCell = getNewCell();
// check violation of rules
if (newCell.x === boardSize || newCell.x === -1 || newCell.y === boardSize || newCell.y === -1) {
isGameOver = true;
$timeout(function () {
isGameOver = false;
}, 500);
generateBoard();
}
if (newCell.x === fruit.x && newCell.y === fruit.y) {
eatFruit();
}
var oldTail = snake.parts.pop();
$scope.board[oldTail.y][oldTail.x] = false;
snake.parts.unshift(newCell);
$scope.board[newCell.y][newCell.x] = true;
snake.direction = tempDirection;
$timeout(updateCells, interval);
}
function updateFruit() {
var x = Math.floor(Math.random() * boardSize);
var y = Math.floor(Math.random() * boardSize);
if ($scope.board[y][x] === true) {
return updateFruit();
}
fruit = { x: x, y: y };
}
function eatFruit() {
$scope.score++;
// Grow by 1
var tail = angular.copy(snake.parts[snake.parts.length - 1]);
snake.parts.push(tail);
updateFruit();
}
function getNewCell() {
var newCell = angular.copy(snake.parts[0]);
// Update Location
if (tempDirection === runDirections.LEFT) {
newCell.x -= 1;
} else if (tempDirection === runDirections.RIGHT) {
newCell.x += 1;
} else if (tempDirection === runDirections.UP) {
newCell.y -= 1;
} else if (tempDirection === runDirections.DOWN) {
newCell.y += 1;
}
return newCell;
}
// Keyboard event capture
$window.addEventListener("keyup", function (e) {
if (e.keyCode == runDirections.LEFT && snake.direction !== runDirections.RIGHT) {
tempDirection = runDirections.LEFT;
} else if (e.keyCode == runDirections.UP && snake.direction !== runDirections.DOWN) {
tempDirection = runDirections.UP;
} else if (e.keyCode == runDirections.RIGHT && snake.direction !== runDirections.LEFT) {
tempDirection = runDirections.RIGHT;
} else if (e.keyCode == runDirections.DOWN && snake.direction !== runDirections.UP) {
tempDirection = runDirections.DOWN;
}
});
}).config(function($mdThemingProvider) {
$mdThemingProvider.theme('dark-grey').backgroundPalette('grey').dark();
$mdThemingProvider.theme('dark-orange').backgroundPalette('orange').dark();
$mdThemingProvider.theme('dark-purple').backgroundPalette('deep-purple').dark();
$mdThemingProvider.theme('dark-blue').backgroundPalette('blue').dark();
});