-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapp.js
127 lines (112 loc) · 3.16 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
const resetButtonElement = document.querySelector('#resetButton');
const countdownElement = document.querySelector('#countdown');
const promptElement = document.querySelector('#prompt');
const definitionElement = document.querySelector('#definition');
const width = 300;
const height = 500;
const pixelSize = 10;
const prompts = [
'Yorkshire Terrier',
'pumpkin',
'remote',
'power cord',
'foliage',
'Philosophy',
'nature',
'mosquito',
'Chicken',
'brain',
];
const canvas = document.querySelector('#draw-area');
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
let gameOver = false;
const currentLocation = {
x: width / 2,
y: height / 2,
};
resetButtonElement.addEventListener('click', () => {
startRound();
});
document.body.addEventListener('keydown', (e) => {
if (gameOver) return;
drawPixel('black');
switch(e.code) {
case 'KeyW': {
currentLocation.y -= pixelSize;
break;
}
case 'KeyA': {
currentLocation.x -= pixelSize;
break;
}
case 'KeyS': {
currentLocation.y += pixelSize;
break;
}
case 'KeyD': {
currentLocation.x += pixelSize;
break;
}
default: break;
}
if (currentLocation.x < 0) {
currentLocation.x = 0;
} else if (currentLocation.x + pixelSize > width) {
currentLocation.x = width - pixelSize;
}
if (currentLocation.y < 0) {
currentLocation.y = 0;
} else if (currentLocation.y + pixelSize > height) {
currentLocation.y = height - pixelSize;
}
drawPixel('grey');
});
function drawPixel(color) {
ctx.fillStyle = color || 'black';
ctx.fillRect(currentLocation.x, currentLocation.y, pixelSize, pixelSize);
}
function getRandomValue(array) {
const randomIndex = Math.floor(Math.random() * array.length);
return array[randomIndex];
}
async function getRandomPrompt(prompt) {
const response = await fetch(`https://api.datamuse.com/words?ml=${prompt}&md=d`);
const json = await response.json();
const nouns = json.filter((item) => item.tags.includes('n'));
if (!nouns.length) {
console.log(prompt);
}
const randomRelated = getRandomValue(nouns);
return {
word: randomRelated.word,
definition: (randomRelated.defs || ['not found']).find((def) => def.startsWith('n')).replace('n\t', ''),
};
}
async function startRound() {
resetButtonElement.style.display = 'none';
gameOver = false;
countdownElement.textContent = '60';
currentLocation.x = width / 2;
currentLocation.y = height / 2;
const endTime = Date.now() + (60 * 1000);
const initialPrompt = getRandomValue(prompts);
const prompt = await getRandomPrompt(initialPrompt);
promptElement.textContent = prompt.word;
definitionElement.textContent = prompt.definition;
ctx.fillStyle = '#F8FA90';
ctx.fillRect(0, 0, width, height);
drawPixel();
const countDownInterval = setInterval(() => {
const secondsLeft = Math.floor((endTime - Date.now()) / 1000);
countdownElement.textContent = secondsLeft;
if (secondsLeft <= 0) {
countdownElement.textContent = 'GAME OVER';
gameOver = true;
clearInterval(countDownInterval);
resetButtonElement.style.display = '';
}
}, 500);
}
startRound();