-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtetris.js
314 lines (272 loc) · 8.08 KB
/
tetris.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
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
/**
* Simple JS tetris thingy.
* @author Gerard van Helden <drm@melp.nl>
*/
/*jshint laxbreak:true */
var Tetris = (function(document) {
var Keys = {
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40,
A: 'A'.charCodeAt(0),
a: 'a'.charCodeAt(0),
Z: 'Z'.charCodeAt(0),
z: 'z'.charCodeAt(0),
DOT: '.'.charCodeAt(0),
COMMA: ','.charCodeAt(0)
};
var Rotation = {
RIGHT: [[0, 1], [-1, 0]],
LEFT: [[0, -1], [1, 0]],
apply: function(point, rotation) {
// multiply vector (x, y) by rotation matrix.
return [
point[0] * rotation[0][0] + point[1] * rotation[0][1],
point[0] * rotation[1][0] + point[1] * rotation[1][1]
];
}
};
var tetrominoes = [
[[0, 1], [1, 1], [1, 0]],
[[1, 0], [1, 1], [0, 1]],
[[1], [1], [1], [1]],
[[1, 0], [1, 0], [1, 1]],
[[0, 1], [0, 1], [1, 1]],
[[1, 1], [1, 1]],
[[0, 1], [1, 1], [0, 1]]
];
function iterateGrid(grid, fn) {
for (var dx = 0; dx < grid.length; dx ++) {
for (var dy = 0; dy < grid[dx].length; dy++) {
fn(dx, dy, grid[dx][dy]);
}
}
}
function mergeToGrid(grid, piece) {
iterateGrid(piece.type, function(x, y, v) {
if (v) {
grid[piece.x + x][piece.y + y] = 1;
}
});
}
function randomPiece() {
return tetrominoes[Math.floor(Math.random() * tetrominoes.length)];
}
function clonePiece(piece) {
var ret = {x: piece.x, y: piece.y, type: []};
iterateGrid(piece.type, function(x, y, v) {
if (typeof ret.type[x] === 'undefined') {
ret.type[x] = [];
}
ret.type[x][y] = v;
});
return ret;
}
var kill = false;
var gridSize = [10, 15], startX = 0;
var grid = [];
var piece = {}, queue = [randomPiece()], canvas;
function spawnPiece() {
piece.x = startX;
piece.y = -1;
piece.type = queue.shift();
queue.push(randomPiece());
}
function isCollision(piece, gridB) {
var ret = false;
iterateGrid(piece.type, function(x, y, v) {
if (v && !ret) {
var realX = x + piece.x;
var realY = y + piece.y;
if (
realX >= gridSize[0]
|| realX < 0
|| realY >= gridSize[1]
|| gridB[x+piece.x][y + piece.y]
) {
ret = true;
}
}
});
return ret;
}
function deleteRow(grid, row) {
var newGrid = [];
for (var x = 0; x < gridSize[0]; x++) {
newGrid[x] = [0];
}
iterateGrid(grid, function(x, y, v) {
if (y < row) {
newGrid[x][y+1] = v;
} else if (y > row) {
newGrid[x][y] = v;
}
});
return newGrid;
}
function checkRows(grid) {
for (var y = 0; y < gridSize[1]; y++) {
var isFull = true;
for (var x = 0; x < gridSize[0]; x ++) {
isFull &= grid[x][y];
}
if (isFull) {
grid = deleteRow(grid, y);
}
}
return grid;
}
function fallDown() {
var tmp = clonePiece(piece);
if (isCollision(piece, grid)) {
gameover();
}
tmp.y ++;
if(isCollision(tmp, grid)) {
mergeToGrid(grid, piece);
grid = checkRows(grid);
spawnPiece();
} else {
piece = tmp;
}
}
function rotatePiece(piece, rotation) {
var origin = [piece.x, piece.y];
var points = [];
iterateGrid(piece.type, function(x, y, v) {
points.push(
[Rotation.apply([x, y], rotation), v]
);
});
var minX = points[0][0][0];
var minY = points[0][0][1];
var i;
for (i = 1; i < points.length; i ++) {
minX = Math.min(points[i][0][0], minX);
minY = Math.min(points[i][0][1], minY);
}
// rebase the coordinates in the origin of the piece.
piece.type = [];
for (i = 0; i < points.length; i ++) {
points[i][0][0] -= minX;
points[i][0][1] -= minY;
if (typeof piece.type[points[i][0][0]] === 'undefined') {
piece.type[points[i][0][0]] = [];
}
piece.type[points[i][0][0]][points[i][0][1]] = points[i][1];
}
}
function keyHandler (e) {
var handled = false;
var ghost = clonePiece(piece);
switch (e.keyCode || e.charCode) {
case Keys.LEFT:
case Keys.COMMA:
handled = true;
ghost.x --;
break;
case Keys.DOT:
case Keys.RIGHT:
handled = true;
ghost.x ++;
break;
case Keys.UP:
case Keys.a: case Keys.A:
handled = true;
rotatePiece(ghost, Rotation.RIGHT);
break;
case Keys.z: case Keys.Z:
handled = true;
rotatePiece(ghost, Rotation.LEFT);
break;
case Keys.DOWN:
handled = true;
ghost.y ++;
break;
}
if (!isCollision(ghost, grid)) {
piece = ghost;
}
if (handled) {
e.preventDefault();
}
}
function paint() {
if (kill) {
return;
}
// set the character to paint in the viewport grid
function drawXY(x, y, ch) {
if (y >= 0 && x >= 0 && x < viewPort[0] && y < viewPort[1]) {
var offset = y * (viewPort[0] + 1) + x;
t = t.substr(0, offset) + ch + t.substr(offset +1);
}
}
var t = '', x, y, viewPort = [40, 40];
// build the viewport string
for (y = 0; y < viewPort[1]; y ++) {
for (x = 0; x < viewPort[0]; x ++) {
t += " ";
}
t += "\n";
}
// layout the board
iterateGrid(grid, function (x, y, v) {
if (v) {
drawXY(x, y, '#');
} else {
drawXY(x, y, '.');
}
});
// paint the current piece
if (piece.type) {
iterateGrid(piece.type, function(x, y, v) {
if (v) {
drawXY(x + piece.x, y + piece.y, 'X');
}
});
}
// draw the next piece in queue
for (var px = 0; px < queue[0].length; px ++) {
for (var py = 0; py < queue[0][px].length; py ++) {
if (queue[0][px][py]) {
drawXY(gridSize[0] + 2 + px, py + 2, 'O');
}
}
}
// paint the canvas.
canvas.innerHTML = t;
setTimeout(arguments.callee, 20);
}
function heartbeat() {
if (kill) {
return;
}
fallDown();
setTimeout(arguments.callee, 300);
}
function gameover() {
kill = true;
alert("Game over :(");
}
return {
'init': function (document) {
// initialize grid with zeroes
for (var x = 0; x < gridSize[0]; x ++) {
grid[x] = [];
for (var y = 0; y < gridSize[1]; y ++) {
grid[x][y]= 0;
}
}
console.log(document, canvas);
canvas = document.getElementById('canvas');
document.onkeypress = keyHandler;
},
'run': function () {
paint();
spawnPiece();
heartbeat();
}
};
})();