Skip to content

Commit

Permalink
Day 7, this is starting to feel easy
Browse files Browse the repository at this point in the history
  • Loading branch information
jahmezz committed Aug 17, 2016
1 parent 31526c1 commit 09b1a29
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 8 deletions.
1 change: 0 additions & 1 deletion tetrisclone/js/engine.js
Expand Up @@ -217,7 +217,6 @@ var canvas, input, content;
document.onkeyup = onup;
document.onmouseup = onup;

console.log(Keys.T);
return i;
})();
})();
4 changes: 4 additions & 0 deletions tetrisclone/js/src/GameBoard.js
Expand Up @@ -19,6 +19,10 @@ define(["src/Numfont"], function(Numfont) {
var tet = stat.tetraminos;
ctx.drawImage(this.back, 0, 0);

this.font.gray.draw(ctx, stat.lvl, 113, 16, 5);
this.font.gray.draw(ctx, stat.lines, 113, 34, 5);
this.font.gray.draw(ctx, stat.score, 80, 52, 10);

this.font.purple.draw(ctx, 42, 10, 10, 5);

this.font.orange.draw(ctx, tet.L, 432, 52, 5);
Expand Down
28 changes: 27 additions & 1 deletion tetrisclone/js/src/StatManager.js
Expand Up @@ -3,7 +3,7 @@ define(function() {
init: function() {
this.reset(0);
},
reset: function() {
reset: function(startlvl) {
this.tetraminos = {
L: 0,
I: 0,
Expand All @@ -14,10 +14,36 @@ define(function() {
J: 0,
tot: 0
}

this._firstlvl = false;

this.startlvl = startlvl || 0;

this.lvl = this.startlvl;
this.score = 0;
this.lines = 0;
},
incTetramino: function(id) {
this.tetraminos[id] += 1;
this.tetraminos.tot += 1;
},

addScore: function(cleared) {
var p = [0, 40, 100, 300, 1200][cleared];
this.score += (this.lvl + 1) * p;
},

checkLvlUp: function() {
if (this._firstlvl) {
if (this.lines >= (this.lvl + 1) * 10) {
this.lvl++;
}
} else {
if (this.lines >= (this.startlvl + 1) * 10 || 100) {
this._firstlvl = true;
this.lvl++;
}
}
}
});
return StatManager;
Expand Down
2 changes: 1 addition & 1 deletion tetrisclone/js/src/Tetramino.js
Expand Up @@ -4,7 +4,7 @@ define(function() {
I: "0000 1111 0000 0000",
T: "010 111 000",
S: "011 110 000",
Z: "011 110 000",
Z: "110 011 000",
O: "011 011 000",
J: "100 111 000"
}
Expand Down
88 changes: 83 additions & 5 deletions tetrisclone/js/src/Tetris.js
Expand Up @@ -24,21 +24,31 @@ define([
this.blockControl[i][j] = new Block(Block.NONE);
}
}
this.currentTetramino = new Tetramino(Tetramino.Z);
this.currentTetramino.x = 3;
this.currentTetramino.setTo(this.blockControl);
this.setNextTetramino();
},
update: function(input) {
this.currentTetramino.setTo(this.blockControl, Block.NONE);

if(input.pressed("up")) {
this.moveRotate();
}
if(input.pressed("down")) {
this.moveDown();
}
if(input.pressed("left")) {
this.moveLeft();
}
if(input.pressed("right")) {
this.moveRight();
}

if (this.frames++ % 20 === 0) {
this.moveDown();
}
this.currentTetramino.setTo(this.blockControl);
},
draw: function(ctx) {
this.gameBoard.draw(ctx, this.stat);

console.log(this.blockControl[0][0]);
for (var i = 0; i < this.cols; i++) {
for (var j = 0; j < this.rows; j++) {
var b = this.blockControl[i][j];
Expand All @@ -48,13 +58,81 @@ define([
}
}
},
setNextTetramino: function() {
this.currentTetramino = new Tetramino(Tetramino.Z);
this.currentTetramino.x = 3;
this.currentTetramino.x = 0;

this.stat.incTetramino(this.currentTetramino.id);
},
moveLeft: function() {
var bc = this.blockControl,
ct = this.currentTetramino;

if (ct.check(bc, -1, 0)) {
ct.x -= 1;
}
},
moveRight: function() {
var bc = this.blockControl,
ct = this.currentTetramino;

if (ct.check(bc, 1, 0)) {
ct.x += 1;
}
},
moveRotate: function(dr) {
dr = dr || 1;
var bc = this.blockControl,
ct = this.currentTetramino;

if (ct.check(bc, 0, 0, dr)) {
ct.rotation = ct.getRotation(dr);
}
},
moveDown: function() {
var bc = this.blockControl,
ct = this.currentTetramino;

if (ct.check(bc, 0, 1)) {
ct.y += 1;
} else {
ct.setTo(bc);
this.checkRows();
this.setNextTetramino();
}
},
checkRows: function() {
var full, removed = 0;

for (var i = this.rows-1; i >= 0; i--) {
full = true;
for (var j = 0; j < this.cols; j++) {
if (!this.blockControl[j][i].solid) {
full = false;
break;
}
}

if (full) {
this.removeRow(i);
removed++;
this.stat.lines++;
i++;
}
}
if (removed > 0) {
this.stat.addScore(removed);
this.stat.checkLvlUp();
}
},

removeRow: function(row) {
var bc = this.blockControl;
for (var i = row; i > 0; i--) {
for (var j = 0; j < this.cols; j++) {
bc[j][i].setType(bc[j][i - 1].id);
}
}
}
});
Expand Down

0 comments on commit 09b1a29

Please sign in to comment.