-
Notifications
You must be signed in to change notification settings - Fork 27
/
typer.js
142 lines (130 loc) · 3.57 KB
/
typer.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
/* global canvas ctx animation:writable gameLoop label loop paintCircle isIntersectingRectangleWithCircle generateRandomNumber generateRandomCharCode paintParticles createParticles processParticles */
let score = 0;
let lives = 10;
let caseSensitive = true;
const center = {
x: canvas.width / 2,
y: canvas.height / 2,
radius: 20,
color: '#FF0000'
};
const letter = {
font: '25px Monospace',
color: '#0095DD',
width: 15,
height: 20,
highestSpeed: 1.6,
lowestSpeed: 0.6,
probability: 0.02
};
let letters = [];
ctx.font = label.font;
letter.width = ctx.measureText('0').width;
document.addEventListener('keydown', keyDownHandler);
document.addEventListener('keyup', keyUpHandler);
window.addEventListener('resize', resizeHandler);
loop(function (frames) {
paintCircle(center.x, center.y, center.radius, center.color);
ctx.font = letter.font;
ctx.fillStyle = letter.color;
for (const l of letters) {
ctx.fillText(String.fromCharCode(l.code), l.x, l.y);
}
paintParticles();
ctx.font = label.font;
ctx.fillStyle = label.color;
ctx.fillText('Score: ' + score, label.left, label.margin);
ctx.fillText('Lives: ' + lives, label.right, label.margin);
processParticles(frames);
createLetters();
removeLetters(frames);
});
function createLetters () {
if (Math.random() < letter.probability) {
const x = Math.random() < 0.5 ? 0 : canvas.width;
const y = Math.random() * canvas.height;
const dX = center.x - x;
const dY = center.y - y;
const norm = Math.sqrt(dX ** 2 + dY ** 2);
const speed = generateRandomNumber(letter.lowestSpeed, letter.highestSpeed);
letters.push({
x,
y,
code: generateRandomCharCode(caseSensitive),
speedX: dX / norm * speed,
speedY: dY / norm * speed
});
}
}
function removeLetters (frames) {
for (const l of letters) {
if (isIntersectingRectangleWithCircle({ x: l.x, y: l.y - letter.height }, letter.width, letter.height, center, center.radius)) {
if (--lives === 0) {
window.alert('GAME OVER!');
window.location.reload(false);
} else if (lives > 0) {
window.alert('START AGAIN!');
letters = [];
}
break;
} else {
l.x += l.speedX * frames;
l.y += l.speedY * frames;
}
}
}
function type (i, l) {
letters.splice(i, 1);
score++;
createParticles(l.x, l.y);
}
window.changeCase = function () {
caseSensitive = !caseSensitive;
if (caseSensitive) {
document.getElementById('change-case-text').innerHTML = '';
} else {
document.getElementById('change-case-text').innerHTML = 'in';
}
};
function keyDownHandler (e) {
if (animation !== undefined && e.keyCode >= 65 && e.keyCode <= 90) {
for (let i = letters.length - 1; i >= 0; i--) {
const l = letters[i];
if (caseSensitive) {
if (e.shiftKey) {
if (e.keyCode === l.code) {
type(i, l);
return;
}
} else {
if (e.keyCode + 32 === l.code) {
type(i, l);
return;
}
}
} else {
if (e.keyCode === l.code || e.keyCode + 32 === l.code) {
type(i, l);
return;
}
}
}
score--;
}
}
function keyUpHandler (e) {
if (e.keyCode === 27) {
if (animation === undefined) {
animation = window.requestAnimationFrame(gameLoop);
} else {
window.cancelAnimationFrame(animation);
animation = undefined;
}
}
}
function resizeHandler () {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
center.x = canvas.width / 2;
center.y = canvas.height / 2;
}