/
tetris.js
371 lines (320 loc) · 11.3 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
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
// global game object
//Notes - Yet to be implemented:
//rotation states for each shape & handling input for rotation
var Tetris = (function(global) {
//create Cells class
function Cell(info) {
//storing cell coordinates
//column
this.x = info.x;
//row
this.y = info.y;
//determines if filled or not
this.filled = false;
//determines if part of stacked row - for collision
this.isStacked = false;
//html cell Id for later access - '\\3 ' to escape numbers
//escaping double-digits to ensure selector validity
if(this.y < 10){
this.cellId = '.\\3' + this.y + ' #\\3' + this.x + ' ';
}else{
this.cellId = '.\\3' + this.y.toString().charAt(0) + ' \\3' + this.y.toString().charAt(1) + ' #\\3' + this.x + ' ';
}
}
//Grid constructor
function Grid(rowSize, colSize){
this.rowSize = rowSize;
this.colSize = colSize;
this.grid = [];
//potentially unneeded, come back to this
this.cells = [];
//
this.stacked = [];
this.drawGrid();
this.update();
}
//method to initialize grid
Grid.prototype.drawGrid = function() {
for (var row = 0; row < this.rowSize; row++) {
//add new row to grid array in obj
this.grid[row] = [];
//creating row in DOM
var newRow = document.createElement('tr');
newRow.innerHTML = ' ';
//assign className (y-axis)
newRow.className = row;
document.getElementById('board').appendChild(newRow);
//populate rows with cells
for (var col = 0; col < this.colSize; col++) {
//add cell object to grid row in obj
var newCell = new Cell({x: col, y: row});
this.grid[row].push(newCell);
//potentially unneeded, come back to this
this.cells.push(newCell);
//
//create cell in DOM
var cell = document.createElement('td');
cell.innerHTML = '';
//assign id (x-axis)
cell.id = col;
newRow.appendChild(cell);
}
}
};
//array holding Shapes subclasses for random generation
Grid.prototype.shapes = [lShape, oShape, iShape, zShape];
//function to randomly generate a shape
Grid.prototype.generateShape = function(){
//selecting randomly from shapes array
var rand = this.shapes[Math.floor(Math.random() * this.shapes.length)];
return new rand;
}
//function to fill cells or unfill cells
Grid.prototype.toggleFill = function(cell, action){
var color;
if(action === 'fill'){
color = '#414934';
}else if(action === 'empty'){
color = '';
}else {
color = 'red';
// console.log('invalid action');
}
//store to use in querySelector
var grabbedCell = document.querySelector(cell.cellId);
//if cell exists on grid
if (grabbedCell != null){
//coloring cells
grabbedCell.setAttribute('bgcolor', color);
}
}
Grid.prototype.addToStack = function(shape){
//storing references to instantiated grid,
//shape coordinates, and gridCell variable
var self = this,
shapeCells = shape.coords,
gridCell;
//toggling Cell.stacked to true and pushing to grid.stacked array
shapeCells.forEach(function(cell){
gridCell = self.grid[cell.y][cell.x];
if(!gridCell.isStacked){
gridCell.isStacked = true;
console.log('adding ' + gridCell + ' to stack')
self.stacked.push(gridCell);
}else {
console.log('cell is already stacked');
}
});
this.stacked.forEach(function(cell){
console.log(cell);
gridCell = self.grid[cell.y][cell.x];
if(!gridCell.filled){
gridCell.filled = true;
}
})
}
//update gameGrid with filled/unfilledCells
Grid.prototype.update = function(){
var self = this,
gridCell;
for (var row in self['grid']){
for(var cell in self['grid'][row]){
gridCell = self['grid'][row][cell];
if (gridCell.filled){
self.toggleFill(gridCell, 'fill');
}else if(!gridCell.filled){
self.toggleFill(gridCell, 'empty');
}
}
}
//continuously updates??
// setTimeout(this.update, 1000);
};
//reset function generates a new playerShape
Grid.prototype.reset = function(){
this.addToStack(playerShape);
playerShape = tetris.generateShape();
tetris.update();
}
//create Shapes superclass
function Shape(){
//initial spawn location is always the same
this.center = {x: 5, y: 0};
//default rotationState
this.rotationState = 0;
//initializing falling movement
intervalID = setInterval(this.fall, 250);
}
//toggles .filled property of gridCells
Shape.prototype.toggleCells = function(action){
var shapeCells = this.coords,
gridCell;
shapeCells.forEach(function(cell){
gridCell = tetris.grid[cell.y][cell.x];
if(action === 'off'){
gridCell.filled = false;
// console.log('toggled ' + gridCell + ' to false');
}else if(action === 'on'){
gridCell.filled = true;
// console.log('toggled ' + gridCell + ' to true');
}
});
}
//moves shapes on player input
//BUG: collision detection depending on center
//means some arent working
Shape.prototype.move = function(direction){
var shapeCells = this.coords,
gridCell
offboard = false;
//if left
if(direction === 'left' && shapeCells[2].x > 0){
//toggleCells off
this.toggleCells('off');
shapeCells.forEach(function(cell){
// gridCell = tetris.grid[cell.y][cell.x];
cell.x--;
});
tetris.update();
//shift direction right
}if(direction === 'right' && shapeCells[1].x < tetris.colSize - 1){
//toggleCells off
this.toggleCells('off');
shapeCells.forEach(function(cell){
cell.x++;
});
tetris.update();
}
//toggleBack
this.toggleCells('on');
tetris.update();
}
//implementing falling
var intervalID;
Shape.prototype.fall = function(){
//erasing shape
var shapeCells = playerShape.coords,
gridCell,
offboard = false,
collision = false;
//erase
playerShape.toggleCells('off');
//each cell in shape
shapeCells.forEach(function(cell){
if(cell != null){
// console.log(gridCell);
//move one box down
cell.y++;
if(cell.y >= tetris.rowSize){
//toggle off-board check
offboard = true;
console.log('Shape reached the bottom');
//stop falling
window.clearInterval(intervalID);
}else if (tetris.grid[cell.y][cell.x].isStacked){
collision = true;
console.log('Shape touched another shape');
//stop falling
window.clearInterval(intervalID);
}
}
});
//set piece back on board
if (collision){
shapeCells.forEach(function(cell){
cell.y--;
});
//begin reset
tetris.reset();
}
if (offboard){
shapeCells.forEach(function(cell){
cell.y--;
});
//begin reset
tetris.reset();
}
playerShape.toggleCells('on');
tetris.update();
}
//Defining shape subclasses
//O Shape
function oShape() {
//creates oShape as a new Shape obj
Shape.call(this);
//holding on to this.center for inner obj reference
var center = this.center;
this.type = 'o';
this.coords = [center,
{x: center.x, y: center.y + 1},
{x: center.x - 1, y: center.y + 1},
{x: center.x - 1, y: center.y}];
this.toggleCells('on');
tetris.update();
};
//setting prototypal delegation to Shape superclass
oShape.prototype = Object.create(Shape.prototype);
oShape.prototype.constructor = oShape;
//I Shape
function iShape() {
//creates oShape as a new Shape obj
Shape.call(this);
//holding on to this.center for inner obj reference
var center = this.center;
this.type = 'i';
this.coords = [center,
{x: center.x + 1, y: center.y},
{x: center.x - 2, y: center.y},
{x: center.x - 1, y: center.y}];
// this.fillCells(this.coords, '#414934');
};
//setting prototypal delegation to Shape superclass
iShape.prototype = Object.create(Shape.prototype);
iShape.prototype.constructor = iShape;
//L Shape
function lShape() {
//creates oShape as a new Shape obj
Shape.call(this);
//holding on to this.center for inner obj reference
var center = this.center;
this.type = 'l';
this.coords = [center,
{x: center.x + 1, y: center.y},
{x: center.x - 1, y: center.y},
{x: center.x - 1, y: center.y + 1}];
// this.fillCells(this.coords, '#414934');
};
//setting prototypal delegation to Shape superclass
lShape.prototype = Object.create(Shape.prototype);
lShape.prototype.constructor = lShape;
//Z Shape
function zShape() {
//creates oShape as a new Shape obj
Shape.call(this);
this.type = 'z';
//holding on to this.center for inner obj reference
var center = this.center;
this.coords = [center,
{x: center.x + 1, y: center.y},
{x: center.x - 1, y: center.y + 1},
{x: center.x, y: center.y + 1}];
// this.fillCells(this.coords, '#414934');
};
//setting prototypal delegation to Shape superclass
zShape.prototype = Object.create(Shape.prototype);
zShape.prototype.constructor = zShape;
//drawing actual board
var tetris = new Grid(24, 10);
// console.table(tetris.grid);
var playerShape = tetris.generateShape();
// console.table(tetris.cells);
// playerShape.fallSpeed(200);
document.addEventListener('keydown', function(e){
if(e.keyCode === 39){
playerShape.move('right');
}else if(e.keyCode === 37){
playerShape.move('left');
}
});
//set global parameter to global
})(this);