Skip to content

Commit 248eae6

Browse files
committed
remove unnecessary rerender
1 parent 3a332cc commit 248eae6

File tree

2 files changed

+29
-37
lines changed

2 files changed

+29
-37
lines changed

button.js

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ class Button {
1818
return true;
1919
}
2020

21-
render() {}
22-
2321
checkCoord() {
2422
return (
2523
mouseX >= this.pos.x &&

tetris.js

+29-35
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
let grey = "#6e6e6e";
1+
let grey = '#6e6e6e';
22
let colors = [
3-
"#f02011",
4-
"#f07211",
5-
"#f0c311",
6-
"#c7f011",
7-
"#11ecf0",
8-
"#1172f0",
9-
"#b511f0",
10-
"#f0118c",
3+
'#f02011',
4+
'#f07211',
5+
'#f0c311',
6+
'#c7f011',
7+
'#11ecf0',
8+
'#1172f0',
9+
'#b511f0',
10+
'#f0118c',
1111
];
1212

1313
class Tetris {
1414
constructor(sizeX, sizeY) {
15-
this.highScore = localStorage.getItem("highscore") || 0;
15+
this.highScore = localStorage.getItem('highscore') || 0;
1616
this.sizeX = sizeX;
1717
this.sizeY = sizeY;
1818

@@ -205,7 +205,6 @@ class Tetris {
205205
this.nextActiveMaterial = random(colors);
206206
this.nextActiveType = floor(random(6));
207207
this.nextActiveCoord = createVector(this.sizeX / 2, 1);
208-
this.renderActive();
209208
}
210209

211210
render() {
@@ -231,16 +230,12 @@ class Tetris {
231230
push();
232231
fill(255);
233232
textSize(this.scale * 1.1);
234-
text("Next", -this.scale * 6.5, this.scale * 1.5);
233+
text('Next', -this.scale * 6.5, this.scale * 1.5);
235234
pop();
236235

237-
let coords = this.getCoords(
238-
this.nextActiveType,
239-
this.nextActiveCoord,
240-
0
241-
);
236+
const coords = this.getCoords(this.nextActiveType, this.nextActiveCoord, 0);
242237

243-
for (let coord of coords) {
238+
for (const coord of coords) {
244239
Cell.render(
245240
coord.x - 10.5,
246241
coord.y + 3,
@@ -283,20 +278,19 @@ class Tetris {
283278
if (dir == LEFT || dir == RIGHT) return;
284279
this.applyBoxes(before);
285280
this.spawn();
286-
this.update();
281+
// this.update();
287282
return;
288283
}
289284
let intersects = this.checkIntersection(after);
290285
if (intersects == SPAWN) {
291286
if (dir == LEFT || dir == RIGHT) return;
292287
this.applyBoxes(before);
293288
this.spawn();
294-
this.update();
289+
// this.update();
295290
return;
296291
}
297292

298293
this.activeCoord = newCoord;
299-
this.renderActive();
300294
}
301295

302296
applyBoxes(coords) {
@@ -391,7 +385,7 @@ class Tetris {
391385
}
392386

393387
rotate(rot) {
394-
let wall = this.checkWallOnRotation();
388+
const wall = this.checkWallOnRotation();
395389
if (wall == WALL) return;
396390
let after = this.rotation;
397391

@@ -402,16 +396,16 @@ class Tetris {
402396
after = this.shapesOffsets[this.activeType].length - 1;
403397
}
404398

405-
let coords = this.getCoords(this.activeType, this.activeCoord, after);
406-
let intersects = this.checkIntersection(coords);
399+
const coords = this.getCoords(this.activeType, this.activeCoord, after);
400+
const intersects = this.checkIntersection(coords);
407401

408402
if (intersects == SPAWN) return;
409403

410404
this.rotation = after;
411405
}
412406

413407
getCoords(type, center, rotation) {
414-
let result = [];
408+
const result = [];
415409
for (let i = 0; i < this.shapesOffsets[type][rotation].length; i++) {
416410
let offset = this.shapesOffsets[type][rotation][i];
417411
result.push(createVector(offset.x + center.x, offset.y + center.y));
@@ -457,13 +451,13 @@ class Tetris {
457451
}
458452

459453
hardDropCoords() {
460-
let coords = this.getCoords(
454+
const coords = this.getCoords(
461455
this.activeType,
462456
this.activeCoord,
463457
this.rotation
464458
);
465459
let minDistance = this.sizeY;
466-
for (let coord of coords) {
460+
for (const coord of coords) {
467461
let hit = false;
468462
for (let i = coord.y + 1; i < this.boxes.length; i++) {
469463
for (let j = 0; j < this.boxes[i].length; j++) {
@@ -489,32 +483,32 @@ class Tetris {
489483
updateHighScore() {
490484
if (this.score > this.highScore) {
491485
this.highScore = this.score;
492-
localStorage.setItem("highscore", this.score);
486+
localStorage.setItem('highscore', this.score);
493487
}
494488
}
495489

496490
hardDrop() {
497491
let coords = this.hardDropCoords();
498492
this.applyBoxes(coords);
499493
this.spawn();
500-
this.update();
494+
// this.update();
501495
}
502496

503497
updateStats() {
504498
push();
505499
fill(255);
506500
textSize(this.scale * 1.1);
507501
let xPos = this.width + this.scale * 3;
508-
text("Score", xPos, this.scale * 1.5);
509-
text("Lines", xPos, this.scale * 3.5);
510-
text("Level", xPos, this.scale * 5.5);
502+
text('Score', xPos, this.scale * 1.5);
503+
text('Lines', xPos, this.scale * 3.5);
504+
text('Level', xPos, this.scale * 5.5);
511505
textSize(this.scale);
512506
fill(color(colors[3]));
513-
text("" + this.score, xPos, this.scale * 2.5);
507+
text('' + this.score, xPos, this.scale * 2.5);
514508
fill(color(colors[1]));
515-
text("" + this.lines, xPos, this.scale * 4.5);
509+
text('' + this.lines, xPos, this.scale * 4.5);
516510
fill(color(colors[0]));
517-
text("" + this.level, xPos, this.scale * 6.5);
511+
text('' + this.level, xPos, this.scale * 6.5);
518512
pop();
519513
}
520514
}

0 commit comments

Comments
 (0)