-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
130 lines (124 loc) · 4.26 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
<!doctype html>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html,
body {
padding: 0;
padding-bottom: 32px;
margin: 0;
width: 100%;
height: 100%;
box-sizing: border-box;
}
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
background: rgb(234, 231, 220);
background: linear-gradient(222deg, rgba(234, 231, 220, 1) 0%, rgba(205, 196, 163, 1) 100%);
}
#board {
display: grid;
grid-template-rows: repeat(18, 30px);
grid-template-columns: repeat(10, 30px);
grid-auto-rows: 30px;
grid-gap: 4px;
width: 340px;
height: 612px;
border: solid 8px #E98074;
padding-left: 4px;
}
#board>*.filled {
background-color: #E98074;
}
#score-container {
color: #E98074;
font-size: 48px;
font-family: monospace;
font-weight: bold;
margin-bottom: 10px;
}
</style>
<body>
<div id="score-container">Score: <span id="score"></span></div>
<div id="board"></div>
<script lang="javascript">
const board = document.getElementById("board");
const score = document.getElementById("score");
for (let i = 0; i < 200; i++) {
const elem = document.createElement("div");
if (i < 20) {
elem.style.display = "none";
}
board.appendChild(elem);
}
WebAssembly
.instantiateStreaming(fetch('tetris.wasm'), { js: { debug: (x) => console.log("debug:", x) } })
.then(wasm => {
const tetris = wasm.instance.exports;
const memory = new Int32Array(tetris.memory.buffer);
tetris.init();
tetris.resetFigure();
let tickCount = 0;
let tickDivider = 6;
const render = () => {
let i = 9;
for (const node of board.childNodes) {
node.setAttribute("class", memory[i++] ? "filled" : "")
}
for (let i = 0; i < 9; i++) {
const y = Math.floor(i / 3);
const x = i % 3;
if (memory[i]) {
board.childNodes[
(tetris.cursorRow.value + y) * 10 +
(tetris.cursorColumn.value + x)
].setAttribute("class", "filled");
}
}
score.innerText = tetris.score.value;
}
setInterval(() => {
if (!tetris.isGameOver.value) {
render();
}
if (tickCount++ % tickDivider === 0) {
tetris.rng.value = Math.floor(Math.random() * 7);
tetris.tick();
}
}, 50);
document.addEventListener("keydown", (e) => {
switch (e.key) {
case "Enter":
case " ": {
tetris.rotateFigure();
break;
}
case "d":
case "ArrowRight": {
tetris.moveRight();
break;
}
case "a":
case "ArrowLeft": {
tetris.moveLeft();
break;
}
case "s":
case "ArrowDown": {
tickDivider = 2;
break;
}
}
});
document.addEventListener("keyup", (e) => {
if (e.key === "ArrowDown" || e.key === "s") {
tickDivider = 6;
}
})
});
</script>
</body>