-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtetris.js
More file actions
173 lines (158 loc) · 5.16 KB
/
tetris.js
File metadata and controls
173 lines (158 loc) · 5.16 KB
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
Array.prototype.each = function(fn) {
var result = [];
for (var i = 0; i < this.length; i++) {
result[i] = fn(this[i], i);
}
return result;
};
var Matrix = function(matrix) {
this.matrix = matrix;
this.height = matrix.length;
this.width = matrix[0].length;
};
Matrix.prototype.each = function(fn) {
return this.matrix.each(fn);
};
Matrix.prototype.get = function(x, y) {
return (this.matrix[y] && this.matrix[y][x]) || 0;
};
Matrix.prototype.paint = function(offsetx, offsety, color) {
ctx.fillStyle = color;
this.matrix.each(function(row, y) {
row.each(function(val, x) {
if (val) ctx.fillRect((x + offsetx) * 20, (y + offsety) * 20, 20, 20);
});
});
};
Matrix.prototype.check = function(block, offsetx, offsety) {
if (offsetx < 0 || offsety < 0 ||
this.height < offsety + block.height ||
this.width < offsetx + block.width) {
return false;
}
var matrix = this.matrix;
var ok = true;
block.each(function(row, y) {
row.each(function(val, x) {
if (val && matrix[y + offsety][x + offsetx]) ok = false;
});
});
return ok;
};
Matrix.prototype.merge = function(block, offsetx, offsety) {
this.matrix.each(function(row, y) {
row.each(function(val, x) {
row[x] += block.get(x - offsetx, y - offsety);
});
});
};
Matrix.prototype.clearRows = function() {
var matrix = this.matrix;
var width = this.width;
matrix.each(function(row, y) {
var full = true;
row.each(function(val, x) {
if (!val) full = false;
});
if (full) {
matrix.splice(y, 1);
matrix.unshift(new Array(width).each(function() {return 0;}));
}
});
};
Matrix.prototype.rotate = function() {
var matrix = this.matrix;
return new Matrix(new Array(matrix[0].length).each(function(_, y) {
return new Array(matrix.length).each(function(_, x) {
return matrix[matrix.length - x - 1][y];
});
}));
};
var ctx;
var blocks = [
new Matrix([
[1, 1],
[0, 1],
[0, 1]
]),
new Matrix([
[1, 1],
[1, 0],
[1, 0]
]),
new Matrix([
[1, 1],
[1, 1]
]),
new Matrix([
[1, 0],
[1, 1],
[1, 0]
]),
new Matrix([
[1, 0],
[1, 1],
[0, 1]
]),
new Matrix([
[0, 1],
[1, 1],
[1, 0]
]),
new Matrix([
[1],
[1],
[1],
[1]
])
];
var block = blocks[Math.floor(Math.random() * blocks.length)];
var posx = 0, posy = 0;
var mapWidth = 10, mapHeight = 20;
var map = new Matrix(new Array(20).each(function() {
return new Array(10).each(function() {return 0;});
}));
function load() {
var elmTarget = document.getElementById('target');
ctx = elmTarget.getContext('2d');
setInterval(function() {
ctx.clearRect(0, 0, 200, 400);
block.paint(posx, posy, 'rgb(255, 0, 0)');
map.paint(0, 0, 'rgb(128, 128, 128)');
if (map.check(block, posx, posy + 1)) {
posy = posy + 1;
}
else {
map.merge(block, posx, posy);
map.clearRows();
posx = 0; posy = 0;
block = blocks[Math.floor(Math.random() * blocks.length)];
}
}, 200);
};
function key(keyCode) {
switch(keyCode) {
case 38:
if (!map.check(block.rotate(), posx, posy)) return;
block = block.rotate(block);
break;
case 39:
if (!map.check(block, posx + 1, posy)) return;
posx = posx + 1;
break;
case 37:
if (!map.check(block, posx - 1, posy)) return;
posx = posx - 1;
break;
case 40:
var y = posy;
while (map.check(block, posy, y)) { y++; }
posy = y - 1;
break;
default:
return;
}
ctx.clearRect(0, 0, 200, 400);
block.paint(posx, posy, 'rgb(255, 0, 0)');
map.paint(0, 0, 'rgb(128, 128, 128)');
}