-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.js
227 lines (186 loc) · 5.12 KB
/
snake.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
223
224
225
226
window.onload = function() {
// Define canvas
var canvas = document.getElementById("scene");
var score_board = document.getElementById("score");
var ctx = canvas.getContext('2d');
// Set board size
canvas.width = 508;
canvas.height = 508;
var active = 1; // 1 if the game is running
snake = new Array(5); // The game starts with a snake of length 5
// Create map
map = new Array(60);
for(var i = 0; i < 60; i++)
map[i] = new Array(60);
/*
Directions:
Up = 1
Down = 2
Left = 3
Right = 4
*/
direction = 2;
// Food coordinates
var food_x = 0;
var food_y = 0;
var score = 0; // Game score
function clear_map() {
// Clears the map
ctx.clearRect(0, 0, canvas.width, canvas.height);
for(var x = 0; x < map.length; x++) {
for(var y = 0; y < map[x].length; y++) {
map[x][y] = 0;
}
}
}
function create_food() {
// Generate the food coordinates
coord_x = Math.round(Math.random() * 46) + 2;
coord_y = Math.round(Math.random() * 46) + 2;
// If the place on the map is not free, it will generate another coordinates until the condition is met
while(map[coord_x][coord_y] == 1 && coord_x != 0 && coord_x != 50 && coord_y != 0 && coord_y != 50) {
coord_x = Math.round(Math.random() * 46) + 2;
coord_y = Math.round(Math.random() * 46) + 2 ;
}
food_x = coord_x;
food_y = coord_y;
}
function create_snake() {
// Generate the snake for the first time
coord_x = 25;
coord_y = 25;
for(var i = 0; i < snake.length; i++) {
snake[i] = {'x': coord_x, 'y': coord_y-i};
map[coord_x][coord_y-i] = 1;
}
}
function render_map() {
// Renders the elements of the map
// Render the food
ctx.fillStyle = "#ff6600";
ctx.fillRect(food_x*10+1, food_y*10+1, 8, 8);
// Render the snake
for(var x = 0; x < map.length; x++) {
for(var y = 0; y < map[x].length; y++) {
if(map[x][y] == 1) {
ctx.fillStyle = "#c68c53";
ctx.fillRect(x*10, y*10, 10, 10);
ctx.fillStyle = "#3366ff";
ctx.fillRect(x*10+1, y*10+1, 8, 8);
}
}
}
}
function render_score() {
// Show the score
score_board.innerHTML = "Score: " + score;
}
function render_scene() {
// Render the scene
// Render the table
ctx.fillStyle = '#c68c53';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Render the walls
ctx.lineWidth = 4;
ctx.strokeStyle = '#663200';
ctx.strokeRect(2, 2, canvas.width - 4, canvas.height-4);
}
function eat_food() {
// Check if the snake ate the food, if so returns true
for(var i = 0; i < snake.length; i++)
if(snake[i]['x'] == food_x && snake[i]['y'] == food_y)
{
return true;
}
}
function check_for_canibalism() {
// Checks if the snake ate himself, if so returns true
for(var i = 0; i < snake.length; i++) {
for(var j = 0; j < snake.length; j++)
{
if(i != j) {
if(snake[i]['x'] == snake[j]['x'] && snake[i]['y'] == snake[j]['y'])
{
return true;
}
}
}
}
}
render_scene();
create_snake();
create_food();
render_map();
window.addEventListener('keydown', function(e) {
// Check if the snake needs to change direction
if(e.keyCode == 37 && direction != 3 && direction != 4) {
direction = 3;
setTimeout(function(){}, 700);
}
if(e.keyCode == 38 && direction != 1 && direction != 2) {
direction = 1;
setTimeout(function(){}, 700);
} else
if(e.keyCode == 39 && direction != 4 && direction != 3) {
direction = 4;
setTimeout(function(){}, 700);
} else
if(e.keyCode == 40 && direction != 2 && direction != 1) {
direction = 2;
setTimeout(function(){}, 700);
}
});
window.setInterval(function() {
if(active == 1) {
clear_map();
// Update snake's coordinates
newsnake = Array(5);
for(var i = snake.length - 1 ; i > 0; i--) {
newsnake[i] = snake[i-1];
map[newsnake[i]['x']][newsnake[i]['y']] = 1;
}
newsnake[0] = {'x': 0, 'y': 0};
if(direction == 1) {
newsnake[0]['y'] = snake[0]['y'] - 1;
newsnake[0]['x'] = snake[0]['x'];
} else
if(direction == 2) {
newsnake[0]['y'] = snake[0]['y'] + 1;
newsnake[0]['x'] = snake[0]['x'];
} else
if(direction == 3) {
newsnake[0]['y'] = snake[0]['y'];
newsnake[0]['x'] = snake[0]['x'] - 1;
} else
if(direction == 4) {
newsnake[0]['y'] = snake[0]['y'];
newsnake[0]['x'] = snake[0]['x'] + 1;
}
map[newsnake[0]['x']][newsnake[0]['y']] = 1;
// Update snake position
snake = newsnake;
// Check if the snake hit the wall, if so, the game ends
if(snake[0]['x'] == 0 || snake[0]['x'] == 50 || snake[0]['y'] == 0 || snake[0]['y'] == 50)
active = 0;
// Check if the snake ate the food
if(eat_food() == true)
{
snake.push({'x': snake[snake.length-1]['x'], 'y': snake[snake.length-1]['y']});
create_food();
score = score + 1;
} else {
// Check if the snake ate himself, if so the game ends
if(check_for_canibalism() == true) {
active = 0;
}
}
// If everything went well, update the scene
render_scene();
render_map();
render_score();
} else {
// If the game ended, print the message
score_board.innerHTML = "Game over! Please refresh the page to try again.";
}
}, 100);
};