-
Notifications
You must be signed in to change notification settings - Fork 5
/
1_snake-speech.html
223 lines (185 loc) · 6.39 KB
/
1_snake-speech.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
<!DOCTYPE html>
<html>
<head>
<title>Snake in pure HTML/JavaScript/CSS</title>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/[email protected]"></script>
<style>
html {
text-align: center;
font-family: Helvetica, Arial, Helvetica, sans-serif;
}
#board {
width: calc(26 * 24px);
margin: auto;
height: 400px;
}
#board div {
background-color: black;
border: 1px solid grey;
box-sizing: border-box;
float: left;
width: 24px;
height: 24px;
}
#board .snake {
background-color: green;
}
#board .apple {
background-color: red;
}
#reference {
position: absolute;
bottom: 10px;
}
</style>
</head>
<body onkeydown="enterKey(event)">
<h1>Voice Controlled Snake</h1>
<div id="board"></div>
<h1 id="command"></h1>
<br>
<div id="reference">Snake implementation from https://hjnilsson.com/</div>
<script>
const board = [];
const boardWidth = 26, boardHeight = 16;
const speed = 1500;
var snakeX;
var snakeY;
var snakeLength;
var snakeDirection;
window.onload = async () => {
const recognizer = speechCommands.create('BROWSER_FFT', 'directional4w');
await recognizer.ensureModelLoaded();
console.log('inint game')
initGame()
recognizer.listen(result => {
const words = recognizer.wordLabels()
let bestWord;
let bestWordScore;
for (let i = 0; i < words.length; ++i) {
if (!bestWordScore || result.scores[i] > bestWordScore) {
bestWord = words[i]
bestWordScore = result.scores[i]
}
}
console.log(bestWord, bestWordScore)
if (['up', 'down', 'left', 'right'].includes(bestWord)) {
setDirection(bestWord)
}
}, {
probabilityThreshold: 0.93
});
};
function initGame() {
const boardElement = document.getElementById('board');
for (var y = 0; y < boardHeight; ++y) {
var row = [];
for (var x = 0; x < boardWidth; ++x) {
var cell = {};
// Create a <div></div> and store it in the cell object
cell.element = document.createElement('div');
// Add it to the board
boardElement.appendChild(cell.element);
// Add to list of all
row.push(cell);
}
// Add this row to the board
board.push(row);
}
startGame();
// Start the game loop (it will call itself with timeout)
gameLoop();
}
function placeApple() {
// A random coordinate for the apple
var appleX = Math.floor(Math.random() * boardWidth);
var appleY = Math.floor(Math.random() * boardHeight);
board[appleY][appleX].apple = 1;
}
function startGame() {
// Default position for the snake in the middle of the board.
snakeX = Math.floor(boardWidth / 2);
snakeY = Math.floor(boardHeight / 2);
snakeLength = 5;
snakeDirection = 'up';
// Clear the board
for (var y = 0; y < boardHeight; ++y) {
for (var x = 0; x < boardWidth; ++x) {
board[y][x].snake = 0;
board[y][x].apple = 0;
}
}
// Set the center of the board to contain a snake
board[snakeY][snakeX].snake = snakeLength;
// Place the first apple on the board.
placeApple();
}
function gameLoop() {
// Update position depending on which direction the snake is moving.
switch (snakeDirection) {
case 'up': snakeY--; break;
case 'down': snakeY++; break;
case 'left': snakeX--; break;
case 'right': snakeX++; break;
}
// Check for walls, and restart if we collide with any
if (snakeX < 0 || snakeY < 0 || snakeX >= boardWidth || snakeY >= boardHeight) {
startGame();
}
// Tail collision
if (board[snakeY][snakeX].snake > 0) {
startGame();
}
// Collect apples
if (board[snakeY][snakeX].apple === 1) {
snakeLength++;
board[snakeY][snakeX].apple = 0;
placeApple();
}
// Update the board at the new snake position
board[snakeY][snakeX].snake = snakeLength;
// Loop over the entire board, and update every cell
for (var y = 0; y < boardHeight; ++y) {
for (var x = 0; x < boardWidth; ++x) {
var cell = board[y][x];
if (cell.snake > 0) {
cell.element.className = 'snake';
cell.snake -= 1;
}
else if (cell.apple === 1) {
cell.element.className = 'apple';
}
else {
cell.element.className = '';
}
}
}
// This function calls itself, with a timeout of 1000 milliseconds
setTimeout(gameLoop, speed / snakeLength);
}
function enterKey(event) {
// Update direction depending on key hit
switch (event.key) {
case 'ArrowUp': setDirection('up'); break;
case 'ArrowDown': setDirection('down'); break;
case 'ArrowLeft': setDirection('left'); break;
case 'ArrowRight': setDirection('right'); break;
default: break;
}
// This prevents the arrow keys from scrolling the window
event.preventDefault();
}
function setDirection(direction) {
if (direction === 'up' && snakeDirection === 'down') return;
if (direction === 'down' && snakeDirection === 'up') return;
if (direction === 'left' && snakeDirection === 'right') return;
if (direction === 'right' && snakeDirection === 'left') return;
snakeDirection = direction
const command = document.getElementById("command")
command.innerText = direction.toUpperCase()
setTimeout(() => command.innerText = '', 1500)
}
</script>
</body>
</html>