-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
222 lines (195 loc) · 5.55 KB
/
script.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
$(function () {
var canvas = $('#canvas')[0];
var ctx = canvas.getContext('2d');
var cHeight = canvas.height;
var cWidth = canvas.width;
var snakeHeight = 10;
var snakeWidth = 10;
var blockSize = 10;
var score = 0;
var snake = [{
x: 200,
y: 40,
oldX: 0,
oldY: 0,
drawn: false
},
{
x: 200,
y: 30,
oldX: 0,
oldY: 0,
drawn: false
},
{
x: 200,
y: 20,
oldX: 0,
oldY: 0,
drawn: false
},
{
x: 200,
y: 10,
oldX: 0,
oldY: 0,
drawn: false
},
];
var food = {
x: 50,
y: 50,
eaten: false
};
const LEFT = 37;
const UP = 38;
const RIGHT = 39;
const DOWN = 40;
var keyPressed = DOWN;
var game;
startGame();
function startGame() {
game = setInterval(gameLoop, 300);
}
function stopGame() {
clearInterval(game);
alert('Game over');
}
function gameLoop() {
clearCanvas();
drawFood();
moveSnake(keyPressed);
drawSnake();
}
function drawSnake() {
ctx.fillStyle = 'red';
ctx.lineWidth = 2;
ctx.strokeStyle = 'white';
$.each(snake, function (index, value) {
ctx.fillRect(value.x, value.y, snakeWidth, snakeHeight);
ctx.strokeRect(value.x, value.y, snakeWidth, snakeHeight);
snake[index].drawn = true;
if (index == 0) {
if (collided(value.x, value.y)) {
stopGame();
} else {
if (caughtFood(value.x, value.y)) {
updateScore();
updateFoodEatenFlag();
makeSnakeBigger();
}
}
}
});
}
function updateScore() {
score++;
$('#score').text(score);
}
function updateFoodEatenFlag() {
food.eaten = true;
}
function makeSnakeBigger() {
snake.push({
x: snake[snake.length - 1].x,
y: snake[snake.length - 1].y
});
}
function collided(x, y) {
return snake.filter((item, index) => {
return index != 0 && item.x == x && item.y == y
}).length > 0 || x < 0 || x > cWidth || y < 0 || y > cHeight;
}
function caughtFood(x, y) {
return (x == food.x && y == food.y);
}
function drawFood() {
ctx.fillStyle = 'red';
let xy = getPositionForFood();
food = {
x: xy.x,
y: xy.y,
eaten: false
};
ctx.fillRect(food.x, food.y, snakeWidth, snakeHeight);
}
function getPositionForFood() {
let xy;
if (food.eaten == true) {
let xArray = yArray = [];
$.each(snake, function (index, value) {
if ($.inArray(value.x, xArray) == -1) {
xArray.push(value.x);
}
if ($.inArray(value.y, yArray) == -1) {
yArray.push(value.y);
}
});
xy = getEmptyBlock(xArray, yArray);
} else {
xy = food;
}
return xy;
}
function getEmptyBlock(xArray, yArray) {
let newXY = {};
newX = getRandomNumber(cWidth - 10, 10);
newY = getRandomNumber(cHeight - 10, 10);
if ($.inArray(newX, xArray) == -1 && $.inArray(newY, yArray) == -1) {
newXY.x = newX;
newXY.y = newY;
return newXY;
} else {
return getEmptyBlock(xArray, yArray);
}
}
function getRandomNumber(max, multipleOf) {
let result = Math.floor(Math.random() * max);
result = (result % 10 == 0) ? result : result + (multipleOf - result % 10);
return result;
}
function clearCanvas() {
ctx.clearRect(0, 0, cWidth, cHeight);
}
$(document).keydown(function (e) {
if ($.inArray(e.which, [LEFT, UP, RIGHT, DOWN]) != -1) {
keyPressed = checkKeyAllowed(e.which);
}
});
function checkKeyAllowed(tempKey) {
let key;
if (tempKey == DOWN) {
key = (keyPressed != UP) ? tempKey : keyPressed;
} else if (tempKey == UP) {
key = (keyPressed != DOWN) ? tempKey : keyPressed;
} else if (tempKey == LEFT) {
key = (keyPressed != RIGHT) ? tempKey : keyPressed;
} else if (tempKey == RIGHT) {
key = (keyPressed != LEFT) ? tempKey : keyPressed;
}
return key;
}
function moveSnake(keyPressed) {
$.each(snake, function (index, value) {
if (snake[index].drawn == true) {
snake[index].oldX = value.x;
snake[index].oldY = value.y;
if (index == 0) {
if (keyPressed == DOWN) {
snake[0].y = snake[0].y + blockSize;
} else if (keyPressed == UP) {
snake[0].y = snake[0].y - blockSize;
} else if (keyPressed == LEFT) {
snake[0].x = snake[0].x - blockSize;
} else if (keyPressed == RIGHT) {
snake[0].x = snake[0].x + blockSize;
}
} else {
snake[index].x = snake[index - 1].oldX;
snake[index].y = snake[index - 1].oldY;
}
snake[index].drawn = false;
}
});
}
});