-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
572 lines (469 loc) · 20.3 KB
/
index.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Tetris for microcontrollers</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script>
// https://github.com/robertrypula/simple-tetris
// Robert Rypuła
/*
- this Tetris game was made for educational purposes
- the game code (except simulation wrapper) has
no JS syntactic sugar
- there are no calls to external or even build
in libraries (like JS's Math)
- you can easily port it to any other language like C,
just implement your own INPUT and OUTPUT ports handling
and you are good to go
- yes, that's the only file you need (linked above)
from this repo to have a working Tetris
If you will find the code usefull please Star the project
on GitHub - this really gives me a kick for making more
stuff like that. Thanks in advance! :)
*/
/* TODO:
+ CSS for pixels
+ move switches to function
- CSS for buttons
- rotation
- random generator
*/
const $ports = { input: 0, output: [] };
// ---------------------------------------------------------------------------------------------------------
const INPUT_BIT_LEFT = 1 << 0;
const INPUT_BIT_ROTATE = 1 << 1;
const INPUT_BIT_NEW_GAME = 1 << 2;
const INPUT_BIT_DOWN = 1 << 3;
const INPUT_BIT_RIGHT = 1 << 4;
const SIZE_X = 8;
const SIZE_Y = 16;
let gameOver = 1;
let tActiveShape = 0;
let tXCurrent = 0;
let tYCurrent = 0;
let tRCurrent = 0;
let tXNext = 0;
let tYNext = 0;
let tRNext = 0;
let inputBits = 0;
let inputBitsRaw = 0;
let inputBitsRawPrevious = 0;
let repeatCounter = 0;
let loopCounter = 0;
let autoDownCounter = 0;
let autoDownThreshold = 0;
let waitForInputBitsChange = 0;
const getPortInput = () => $ports.input;
const getPortOutput = (i) => $ports.output[i];
const setPortOutput = (i, value) => ($ports.output[i] = value);
const getInputBitsRaw = () => {
return getPortInput();
};
const getPixel = (x, y) => {
if (x < 0 || y < 0 || x >= SIZE_X || y >= SIZE_Y) {
return 1;
}
return getPortOutput(y) & (1 << (SIZE_X - 1 - x)) ? 1 : 0;
};
const setPixel = (x, y, bit) => {
if (x < 0 || y < 0 || x >= SIZE_X || y >= SIZE_Y) {
return;
}
setPortOutput(
y,
bit ? getPortOutput(y) | (1 << (SIZE_X - 1 - x)) : getPortOutput(y) & ~(1 << (SIZE_X - 1 - x))
);
};
const getTetrominoLine = (lineIndex) => {
// 'I' shape
if (lineIndex === 0x00) return 0b0000010000000010;
if (lineIndex === 0x01) return 0b0000010011110010;
if (lineIndex === 0x02) return 0b1111010000000010;
if (lineIndex === 0x03) return 0b0000010000000010;
// 'J' shape
if (lineIndex === 0x10) return 0b0000010000000110;
if (lineIndex === 0x11) return 0b1110010010000100;
if (lineIndex === 0x12) return 0b0010110011100100;
if (lineIndex === 0x13) return 0b0000000000000000;
// 'L' shape
if (lineIndex === 0x20) return 0b0000010000000110;
if (lineIndex === 0x21) return 0b0010010011100010;
if (lineIndex === 0x22) return 0b1110011010000010;
if (lineIndex === 0x23) return 0b0000000000000000;
// 'T' shape
if (lineIndex === 0x30) return 0b0000001000001000;
if (lineIndex === 0x31) return 0b1110011001001100;
if (lineIndex === 0x32) return 0b0100001011101000;
if (lineIndex === 0x33) return 0b0000000000000000;
// 'O' shape
if (lineIndex === 0x40) return 0b0000000000000000;
if (lineIndex === 0x41) return 0b1100110011001100;
if (lineIndex === 0x42) return 0b1100110011001100;
if (lineIndex === 0x43) return 0b0000000000000000;
// 'S' shape
if (lineIndex === 0x50) return 0b0000100001101000;
if (lineIndex === 0x51) return 0b0110110011001100;
if (lineIndex === 0x52) return 0b1100010000000100;
if (lineIndex === 0x53) return 0b0000000000000000;
// 'Z' shape
if (lineIndex === 0x60) return 0b0000010011000100;
if (lineIndex === 0x61) return 0b1100110001101100;
if (lineIndex === 0x62) return 0b0110100000001000;
if (lineIndex === 0x63) return 0b0000000000000000;
return 0;
};
const getTetrominoPixel = (x, y, rotation) => {
if (x < 0 || y < 0 || x >= 4 || y >= 4) {
return 0;
}
return getTetrominoLine((tActiveShape << 4) | y) & (1 << (15 - (x + (rotation << 2)))) ? 1 : 0;
};
const setCurrentTetrominoPixels = (bit) => {
for (let y = 0; y < 4; y++) {
for (let x = 0; x < 4; x++) {
if (getTetrominoPixel(x, y, tRCurrent)) {
setPixel(tXCurrent + x, tYCurrent + y, bit);
}
}
}
};
const isNextTetrominoColliding = () => {
for (let y = 0; y < 4; y++) {
for (let x = 0; x < 4; x++) {
if (getTetrominoPixel(x, y, tRNext) && getPixel(tXNext + x, tYNext + y)) {
return 1;
}
}
}
return 0;
};
const newCurrentTetromino = () => {
tXCurrent = 2;
tYCurrent = 0;
tRCurrent = 0;
tActiveShape = loopCounter % 7; // TODO get rid of division
waitForInputBitsChange = 1;
};
const setNextTetrominoToCurrent = () => {
tXNext = tXCurrent;
tYNext = tYCurrent;
tRNext = tRCurrent;
};
const setCurrentTetrominoToNext = () => {
tXCurrent = tXNext;
tYCurrent = tYNext;
tRCurrent = tRNext;
};
const startNewGame = () => {
for (let y = 0; y < SIZE_Y; y++) {
for (let x = 0; x < SIZE_X; x++) {
setPixel(x, y, 0);
}
}
newCurrentTetromino();
setNextTetrominoToCurrent();
gameOver = 0;
loopCounter = 0;
autoDownCounter = 0;
autoDownThreshold = 60;
waitForInputBitsChange = 0;
};
const findAndRemoveFullLines = () => {
let fullLineFound;
for (let y = SIZE_Y - 1; y >= 0; y--) {
fullLineFound = 1;
for (let x = 0; x < SIZE_X; x++) {
if (!getPixel(x, y)) {
fullLineFound = 0;
}
}
if (fullLineFound) {
for (let yy = y; yy >= 1; yy--) {
for (let x = 0; x < SIZE_X; x++) {
setPixel(x, yy, getPixel(x, yy - 1));
}
}
for (let x = 0; x < SIZE_X; x++) {
setPixel(x, 0, 0);
}
y++; // if line disappears you have to check the same line again as pixels fell off one level "down"
autoDownThreshold--;
}
}
};
const getRepeatCounter = () => {
// there should be some repeat delay before key will repeat at constant rate so repeatCounter needs to 'lag' a bit
if (inputBitsRaw === INPUT_BIT_ROTATE) return 1;
if (inputBitsRaw === INPUT_BIT_RIGHT) return -9;
if (inputBitsRaw === INPUT_BIT_DOWN) return 1;
if (inputBitsRaw === INPUT_BIT_LEFT) return -9;
if (inputBitsRaw === INPUT_BIT_NEW_GAME) return 1;
return 0;
};
const getRepeatThreshold = () => {
// repeat the key at constant rate
if (inputBitsRaw === INPUT_BIT_ROTATE) return 30;
if (inputBitsRaw === INPUT_BIT_RIGHT) return 2;
if (inputBitsRaw === INPUT_BIT_DOWN) return 2;
if (inputBitsRaw === INPUT_BIT_LEFT) return 2;
if (inputBitsRaw === INPUT_BIT_NEW_GAME) return 30;
return 0;
};
const setProperInputBitsStateFromUserInput = () => {
inputBitsRaw = getInputBitsRaw();
if (inputBitsRawPrevious !== inputBitsRaw) {
inputBits = inputBitsRaw;
repeatCounter = getRepeatCounter();
waitForInputBitsChange = 0;
} else {
inputBits = 0;
if (inputBitsRaw && !waitForInputBitsChange) {
if (repeatCounter >= getRepeatThreshold()) {
inputBits = inputBitsRaw;
repeatCounter = 0;
}
repeatCounter++;
}
}
inputBitsRawPrevious = inputBitsRaw;
};
const processInputBits = () => {
setNextTetrominoToCurrent();
if (inputBits === INPUT_BIT_ROTATE) {
tRNext = (tRCurrent + 1) & 0x3;
} else if (inputBits === INPUT_BIT_RIGHT) {
tXNext = tXCurrent + 1;
} else if (inputBits === INPUT_BIT_DOWN) {
tYNext = tYCurrent + 1;
} else if (inputBits === INPUT_BIT_LEFT) {
tXNext = tXCurrent - 1;
} else if (inputBits === INPUT_BIT_NEW_GAME) {
startNewGame();
}
if (gameOver || !inputBits) {
return;
}
setCurrentTetrominoPixels(0); // remove current Tetromino from the board
if (isNextTetrominoColliding()) {
setCurrentTetrominoPixels(1);
if (inputBits === INPUT_BIT_DOWN) {
findAndRemoveFullLines();
newCurrentTetromino();
setNextTetrominoToCurrent();
gameOver = isNextTetrominoColliding();
setCurrentTetrominoPixels(1);
}
} else {
setCurrentTetrominoToNext(); // next becomes current and then we can draw it
setCurrentTetrominoPixels(1);
}
};
const loop60Hz = () => {
setProperInputBitsStateFromUserInput();
processInputBits();
if (autoDownCounter >= autoDownThreshold) {
autoDownCounter = 0;
if (inputBits !== INPUT_BIT_DOWN) {
inputBits = INPUT_BIT_DOWN;
processInputBits();
}
}
autoDownCounter++;
loopCounter++;
};
// ---------------------------------------------------------------------------------------------------------
class SimulationWrapper {
inputKeys;
outputSizeX;
outputSizeY;
ports;
loop;
frameCount = 0;
constructor(inputKeys, outputSizeX, outputSizeY, ports, loop) {
this.inputKeys = inputKeys;
this.outputSizeX = outputSizeX;
this.outputSizeY = outputSizeY;
this.ports = ports;
this.loop = loop;
}
requestAnimationFrameHandler() {
this.loop();
this.renderOutput();
this.renderInput();
this.frameCount++;
requestAnimationFrame(this.requestAnimationFrameHandler.bind(this));
}
renderOutput() {
const outputPixelElements = document.querySelectorAll('#output div');
for (let y = 0; y < this.outputSizeY; y++) {
for (let x = 0; x < this.outputSizeX; x++) {
const bit = this.ports.output[y] & (1 << (this.outputSizeX - 1 - x)) ? 1 : 0;
outputPixelElements[y * this.outputSizeX + x].style.opacity = bit ? '1' : '0.05';
}
}
}
renderInput() {
Object.keys(this.inputKeys).forEach((code, index) => {
const isHigh = (this.ports.input >>> index) & 1;
const classList = document.querySelector(`#input div[code=${code}]`).classList;
isHigh ? classList.add('pressed') : classList.remove('pressed');
});
}
bitSet(value, position) {
return (1 << position) | value;
}
bitClear(value, position) {
return ~(1 << position) & value;
}
inputHandler(event, code, isUp) {
const position = Object.keys(this.inputKeys).indexOf(code);
if (position !== -1) {
event.preventDefault();
this.ports.input = isUp
? this.bitClear(this.ports.input, position)
: this.bitSet(this.ports.input, position);
}
}
createHTMLElementsOutput() {
const outputElement = document.getElementById('output');
let divElement;
for (let i = 0; i < this.outputSizeX * this.outputSizeY; i++) {
divElement = document.createElement('div');
divElement.style.width = `${100 / this.outputSizeX}%`;
divElement.style.paddingBottom = `${100 / this.outputSizeX}%`;
outputElement.append(divElement);
}
divElement = document.createElement('div');
divElement.classList.add('clearfix');
outputElement.append(divElement);
this.ports.output = [];
for (let y = 0; y < this.outputSizeY; y++) {
this.ports.output.push(0);
}
}
createHTMLElementsInput() {
const inputElement = document.getElementById('input');
Object.keys(this.inputKeys).forEach((code) => {
const inputKeyElement = document.createElement('div');
inputKeyElement.setAttribute('code', code);
inputKeyElement.innerHTML = this.inputKeys[code];
inputElement.append(inputKeyElement);
});
}
registerEvents() {
document.addEventListener('keydown', (event) => this.inputHandler(event, event.code, false));
document.addEventListener('keyup', (event) => this.inputHandler(event, event.code, true));
document.querySelectorAll('#input div').forEach((element) => {
const code = element.getAttribute('code');
element.addEventListener('mousedown', (event) => this.inputHandler(event, code, false));
element.addEventListener('mouseup', (event) => this.inputHandler(event, code, true));
element.addEventListener('touchstart', (event) => this.inputHandler(event, code, false));
element.addEventListener('touchend', (event) => this.inputHandler(event, code, true));
});
}
main() {
this.createHTMLElementsOutput();
this.createHTMLElementsInput();
this.registerEvents();
requestAnimationFrame(this.requestAnimationFrameHandler.bind(this));
}
}
const inputKeys = { ArrowLeft: '<', ArrowUp: 'R', Enter: 'NEW', ArrowDown: `\\/`, ArrowRight: '>' };
const simulationWrapper = new SimulationWrapper(inputKeys, SIZE_X, SIZE_Y, $ports, loop60Hz);
</script>
<style>
html {
margin: 0;
padding: 0;
}
body {
margin: 0;
padding: 16px 0 0 0;
background-color: #758c7a;
color: #061006;
}
* {
box-sizing: border-box;
}
#output {
outline: 3px solid #061006;
width: 304px;
margin: 0 auto;
position: relative;
}
#output > div:not(.clearfix) {
position: relative;
float: left;
}
#output > div:not(.clearfix):before,
#output > div:not(.clearfix):after {
position: absolute;
content: ' ';
}
#output > div:not(.clearfix):before {
border: 5px solid #061006;
bottom: 1px;
left: 1px;
right: 1px;
top: 1px;
}
#output > div:not(.clearfix):after {
background-color: #061006;
bottom: 10px;
left: 10px;
right: 10px;
top: 10px;
}
.clearfix {
height: 0;
overflow: hidden;
clear: both;
}
#input {
padding-top: 16px;
padding-bottom: 16px;
font-size: 0;
text-align: center;
}
#input > div {
box-sizing: border-box;
width: 40px;
height: 40px;
margin: 0 4px 4px 0;
background-color: #06100605;
font-size: 13px;
font-weight: bold;
line-height: 1em;
padding-top: 12px;
display: inline-block;
border: 1px solid #758c7a;
outline: 1px solid #061006;
text-align: center;
cursor: pointer;
}
#input > div.pressed {
background-color: #061006;
color: #758c7a;
}
footer {
text-align: center;
}
footer a {
color: black;
}
</style>
</head>
<body onload="simulationWrapper.main()">
<div id="output"></div>
<div id="input"></div>
<footer>
<div>
Use arrows & Enter
</div>
<div>
<a href="https://github.com/robertrypula/simple-tetris" target="_blank">https://github.com/robertrypula/simple-tetris</a>
</div>
</footer>
</body>
</html>