-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.html
261 lines (218 loc) · 5.89 KB
/
snake.html
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
html, body {
height: 100%;
margin: 0;
}
body {
background: black;
display: flex;
align-items: center;
justify-content: center;
}
canvas {
border: 1px solid white;
}
</style>
</head>
<body>
<button class="echo-button" id="connect">Connect</button>
<button class="echo-button" id="disconnect">Disconnect</button><br>
<!-- Your Name: <input class="draw-border" id="userName" size=47 style="margin-bottom: 5px;"><br> -->
<canvas tabindex='1' width="400" height="400" id="game"></canvas>
<script>
var canvas = document.getElementById('game');
var context = canvas.getContext('2d');
var grid = 16;
var count = 0;
var snake = {
x: 160,
y: 160,
// snake velocity. moves one grid length every frame in either the x or y direction
dx: grid,
dy: 0,
// keep track of all grids the snake body occupies
cells: [],
// length of the snake. grows when eating an apple
maxCells: 4
};
var snake2 = {
x: 160,
y: 160,
// snake velocity. moves one grid length every frame in either the x or y direction
dx: grid,
dy: 0,
// keep track of all grids the snake body occupies
cells: [],
// length of the snake. grows when eating an apple
maxCells: 4
};
var apple = {
x: 320,
y: 320
};
// game loop
function loop() {
// console.log("calling loop")
// slow game loop to 15 fps instead of 60 (60/15 = 4)
// if (++count < 4) {
// return;
// }
// count = 0;
context.clearRect(0,0,canvas.width,canvas.height);
// // move snake by it's velocity
// snake.x += snake.dx;
// snake.y += snake.dy;
// // wrap snake position horizontally on edge of screen
// if (snake.x < 0) {
// snake.x = canvas.width - grid;
// }
// else if (snake.x >= canvas.width) {
// snake.x = 0;
// }
// // wrap snake position vertically on edge of screen
// if (snake.y < 0) {
// snake.y = canvas.height - grid;
// }
// else if (snake.y >= canvas.height) {
// snake.y = 0;
// }
// // keep track of where snake has been. front of the array is always the head
// snake.cells.unshift({x: snake.x, y: snake.y});
// // remove cells as we move away from them
// if (snake.cells.length > snake.maxCells) {
// snake.cells.pop();
// }
// draw snake one cell at a time
context.fillStyle = 'green';
snake.cells.forEach(function(cell, index) {
// drawing 1 px smaller than the grid creates a grid effect in the snake body so you can see how long it is
context.fillRect(cell.x, cell.y, grid-1, grid-1);
// console.log("filling rectangle " + cell.x + ", " + cell.y)
}
);
context.fillStyle = 'red';
snake2.cells.forEach(function(cell, index) {
// drawing 1 px smaller than the grid creates a grid effect in the snake body so you can see how long it is
context.fillRect(cell.x, cell.y, grid-1, grid-1);
// console.log("filling rectangle " + cell.x + ", " + cell.y)
}
);
context.fillStyle = 'yellow';
context.fillRect(apple.x, apple.y, grid-1, grid-1);
}
function setposition(sliceofpositiontuples) {
snake.cells = []
for (index = 0; index < sliceofpositiontuples.length; index++) {
parts = sliceofpositiontuples[index].split(",")
snake.cells.unshift({x: (parts[0] * 16), y: (parts[1] * 16)});
}
}
function setposition2(sliceofpositiontuples) {
snake2.cells = []
for (index = 0; index < sliceofpositiontuples.length; index++) {
parts = sliceofpositiontuples[index].split(",")
snake2.cells.unshift({x: (parts[0] * 16), y: (parts[1] * 16)});
}
}
function setapple(x,y) {
apple.x = x*16
apple.y = y*16
}
function setdirection(direction) {
if (direction === "left" && snake.dx === 0) {
snake.dx = -grid;
snake.dy = 0;
}
// up arrow key
else if (direction === "up" && snake.dy === 0) {
snake.dy = -grid;
snake.dx = 0;
}
// right arrow key
else if (direction === "right" && snake.dx === 0) {
snake.dx = grid;
snake.dy = 0;
}
// down arrow key
else if (direction === "down" && snake.dy === 0) {
snake.dy = grid;
snake.dx = 0;
}
}
// start the game
var ws = null;
connect.onclick = function() {
ws = new WebSocket("ws://35.243.131.250:80");
ws.onopen = function(ev) {
console.log("[connection opened]\n");
};
ws.onclose = function(ev) {
console.log("[connection closed]\n");
};
ws.onmessage = function(ev) {
// messages.innerText += ev.data + "\n";
// console.log("receive: " + ev.data + "\n");
// setdirection(ev.data);
snakes = ev.data.split('@')
elements = snakes[0].split('.');
applecoords = elements[0].split(',');
setapple(applecoords[0], applecoords[1]);
setposition(elements.slice(1));
snake2coords = snakes[1].split('.')
setposition2(snake2coords)
loop()
};
ws.onerror = function(ev) {
messages.innerText += "[error]\n";
console.log(ev);
};
};
disconnect.onclick = function() {
ws.close();
};
canvas.onkeydown = function(ev) {
console.log("onkeydownpressed")
ev.preventDefault();
str = ""
if (event.keyCode === 37) {
str += "left"
}
if (event.keyCode === 38) {
str += "up"
}
if (event.keyCode === 39) {
str += "right"
}
if (event.keyCode === 40) {
str += "down"
}
if (str != "") {
str = 1 + str;
ws.send(str)
} else {
if (event.keyCode === 65) {
str += "left"
}
if (event.keyCode === 87) {
str += "up"
}
if (event.keyCode === 68) {
str += "right"
}
if (event.keyCode === 83) {
str += "down"
}
if (str != "") {
str = 2 + str;
ws.send(str)
}
}
}
requestAnimationFrame(loop);
</script>
</body>
</html>