-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
194 lines (174 loc) · 4.44 KB
/
script.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
const canvas = document.getElementById("tetris");
const ctx = canvas.getContext("2d");
const startButton = document.getElementById("start");
const resetButton = document.getElementById("reset");
const modal = document.getElementById("myModal");
const instructionsButton = document.getElementById("instructions");
const span = document.getElementsByClassName("close")[0];
ctx.scale(20, 20);
startButton.addEventListener("click", () => {
update();
startButton.style.display = "none";
});
resetButton.addEventListener("click", () => {
location.reload();
});
instructionsButton.addEventListener("click", () => {
modal.style.display = "block";
});
span.addEventListener("click", () => {
modal.style.display = "none";
});
window.onclick = function (event) {
if (event.target == modal) {
modal.style.display = "none";
}
};
const piece = [
[0, 0, 0],
[0, 1, 1],
[1, 1, 0],
];
function createMatrix(w, h) {
const matrix = [];
while (h--) {
matrix.push(new Array(w).fill(0));
}
return matrix;
}
const field = createMatrix(12, 20);
console.log(field);
console.table(field);
const player = {
pos: { x: 5, y: -2 },
piece: piece,
};
function drawPiece(piece, offset) {
piece.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
ctx.fillStyle = "blue";
ctx.fillRect(x + offset.x, y + offset.y, 1, 1);
}
});
});
}
function draw() {
ctx.fillStyle = "#000";
ctx.fillRect(0, 0, canvas.width, canvas.height);
drawPiece(field, { x: 0, y: 0 });
drawPiece(player.piece, player.pos);
}
let dCounter = 0;
let dropInterval = 500;
let lastTime = 0;
function update(time = 0) {
const deltaTime = time - lastTime;
lastTime = time;
dCounter += deltaTime;
if (dCounter > dropInterval) {
player.pos.y++;
if (collide(field, player)) {
player.pos.y--;
join(field, player);
player.pos.y = 0;
}
dCounter = 0;
}
draw();
requestAnimationFrame(update);
}
function join(field, player) {
player.piece.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
field[y + player.pos.y][x + player.pos.x] = value;
}
});
});
}
function collide(field, player) {
const b = player.piece;
const o = player.pos;
for (let y = 0; y < b.length; y++) {
for (let x = 0; x < b[y].length; x++) {
if (b[y][x] !== 0 && (field[y + o.y] && field[y + o.y][x + o.x]) !== 0) {
return true;
}
}
}
return false;
}
document.addEventListener("keydown", (e) => {
if (e.keyCode === 37) {
function playerMove(control) {
player.pos.x += control;
if (collide(field, player)) {
player.pos.x -= control;
}
}
playerMove(-1);
} else if (e.keyCode === 39) {
function playerMove(control) {
player.pos.x += control;
if (collide(field, player)) {
player.pos.x -= control;
}
}
playerMove(+1);
} else if (e.keyCode === 40) {
player.pos.y++;
if (collide(field, player)) {
player.pos.y--;
join(field, player);
player.pos.y = -2;
}
dCounter = 0;
} else if (e.keyCode === 65) {
const pos = player.pos.x;
let offset = 1;
function playerRotate(control) {
rotate(player.piece, control);
}
playerRotate(-1);
while (collide(field, player)) {
player.pos.x += offset;
offset = -(offset + (offset > 0 ? 1 : -1));
if (offset > player.piece[0].length) {
rotate(player.piece, control);
player.pos.x = pos;
return;
}
}
} else if (e.keyCode === 68) {
const pos = player.pos.x;
let offset = 1;
function playerRotate(control) {
rotate(player.piece, control);
}
playerRotate(+1);
while (collide(field, player)) {
player.pos.x += offset;
offset = -(offset + (offset > 0 ? 1 : -1));
if (offset > player.piece[0].length) {
rotate(player.piece, control);
player.pos.x = pos;
return;
}
}
}
console.log(field);
console.table(field);
});
function rotate(piece, control) {
for (let y = 0; y < piece.length; y++) {
for (let x = 0; x < y; x++) {
[piece[x][y], piece[y][x]] = [piece[y][x], piece[x][y]];
}
}
if (control > 0) {
piece.forEach((row) => row.reverse());
} else {
piece.reverse();
}
}