-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathmain.js
201 lines (170 loc) · 5 KB
/
main.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
const canvas = document.getElementById('board');
const ctx = canvas.getContext('2d');
const canvasNext = document.getElementById('next');
const ctxNext = canvasNext.getContext('2d');
let accountValues = {
score: 0,
level: 0,
lines: 0
};
function updateAccount(key, value) {
let element = document.getElementById(key);
if (element) {
element.textContent = value;
}
}
let account = new Proxy(accountValues, {
set: (target, key, value) => {
target[key] = value;
updateAccount(key, value);
return true;
}
});
let requestId = null;
let time = null;
const moves = {
[KEY.LEFT]: (p) => ({ ...p, x: p.x - 1 }),
[KEY.RIGHT]: (p) => ({ ...p, x: p.x + 1 }),
[KEY.DOWN]: (p) => ({ ...p, y: p.y + 1 }),
[KEY.SPACE]: (p) => ({ ...p, y: p.y + 1 }),
[KEY.UP]: (p) => board.rotate(p, ROTATION.RIGHT),
[KEY.Q]: (p) => board.rotate(p, ROTATION.LEFT)
};
let board = new Board(ctx, ctxNext);
initNext();
showHighScores();
function initNext() {
// Calculate size of canvas from constants.
ctxNext.canvas.width = 4 * BLOCK_SIZE;
ctxNext.canvas.height = 4 * BLOCK_SIZE;
ctxNext.scale(BLOCK_SIZE, BLOCK_SIZE);
}
function addEventListener() {
document.removeEventListener('keydown', handleKeyPress);
document.addEventListener('keydown', handleKeyPress);
}
function handleKeyPress(event) {
if (event.keyCode === KEY.P) {
pause();
}
if (event.keyCode === KEY.ESC) {
gameOver();
} else if (moves[event.keyCode]) {
event.preventDefault();
// Get new state
let p = moves[event.keyCode](board.piece);
if (event.keyCode === KEY.SPACE) {
// Hard drop
if (document.querySelector('#pause-btn').style.display === 'block') {
dropSound.play();
}else{
return;
}
while (board.valid(p)) {
account.score += POINTS.HARD_DROP;
board.piece.move(p);
p = moves[KEY.DOWN](board.piece);
}
board.piece.hardDrop();
} else if (board.valid(p)) {
if (document.querySelector('#pause-btn').style.display === 'block') {
movesSound.play();
}
board.piece.move(p);
if (event.keyCode === KEY.DOWN &&
document.querySelector('#pause-btn').style.display === 'block') {
account.score += POINTS.SOFT_DROP;
}
}
}
}
function resetGame() {
account.score = 0;
account.lines = 0;
account.level = 0;
board.reset();
time = { start: performance.now(), elapsed: 0, level: LEVEL[account.level] };
}
function play() {
addEventListener();
if (document.querySelector('#play-btn').style.display == '') {
resetGame();
}
// If we have an old game running then cancel it
if (requestId) {
cancelAnimationFrame(requestId);
}
animate();
document.querySelector('#play-btn').style.display = 'none';
document.querySelector('#pause-btn').style.display = 'block';
backgroundSound.play();
}
function animate(now = 0) {
time.elapsed = now - time.start;
if (time.elapsed > time.level) {
time.start = now;
if (!board.drop()) {
gameOver();
return;
}
}
// Clear board before drawing new state.
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
board.draw();
requestId = requestAnimationFrame(animate);
}
function gameOver() {
cancelAnimationFrame(requestId);
ctx.fillStyle = 'black';
ctx.fillRect(1, 3, 8, 1.2);
ctx.font = '1px Arial';
ctx.fillStyle = 'red';
ctx.fillText('GAME OVER', 1.8, 4);
sound.pause();
finishSound.play();
checkHighScore(account.score);
document.querySelector('#pause-btn').style.display = 'none';
document.querySelector('#play-btn').style.display = '';
}
function pause() {
if (!requestId) {
document.querySelector('#play-btn').style.display = 'none';
document.querySelector('#pause-btn').style.display = 'block';
animate();
backgroundSound.play();
return;
}
cancelAnimationFrame(requestId);
requestId = null;
ctx.fillStyle = 'black';
ctx.fillRect(1, 3, 8, 1.2);
ctx.font = '1px Arial';
ctx.fillStyle = 'yellow';
ctx.fillText('PAUSED', 3, 4);
document.querySelector('#play-btn').style.display = 'block';
document.querySelector('#pause-btn').style.display = 'none';
sound.pause();
}
function showHighScores() {
const highScores = JSON.parse(localStorage.getItem('highScores')) || [];
const highScoreList = document.getElementById('highScores');
highScoreList.innerHTML = highScores
.map((score) => `<li>${score.score} - ${score.name}`)
.join('');
}
function checkHighScore(score) {
const highScores = JSON.parse(localStorage.getItem('highScores')) || [];
const lowestScore = highScores[NO_OF_HIGH_SCORES - 1]?.score ?? 0;
if (score > lowestScore) {
const name = prompt('You got a highscore! Enter name:');
const newScore = { score, name };
saveHighScore(newScore, highScores);
showHighScores();
}
}
function saveHighScore(score, highScores) {
highScores.push(score);
highScores.sort((a, b) => b.score - a.score);
highScores.splice(NO_OF_HIGH_SCORES);
localStorage.setItem('highScores', JSON.stringify(highScores));
}