Skip to content

Instantly share code, notes, and snippets.

@ivanbogomoloff
Last active February 1, 2016 14:42
Show Gist options
  • Save ivanbogomoloff/c6fd28af0623f9e22fbf to your computer and use it in GitHub Desktop.
Save ivanbogomoloff/c6fd28af0623f9e22fbf to your computer and use it in GitHub Desktop.
ttet builded blocks
/**
* Инвертирования бинарного массива
* @returns {Array}
*/
Array.prototype.binInverse = function() {
var ar = this;
var newar = new Array(ar.length - 1);
for(var i = 0; i < ar.length; i++)
{
if (typeof (ar[i]) == 'object') {
newar[i] = ar[i].binInverse();
} else {
newar[i] = ar[i] ^ 1;
}
}
return newar;
};
/**
*
* @type {{canvas: Function, context: Function, line: Function}}
*/
var c = {
canvas: function(){ return document.getElementById('canvas'); },
context: function(){ return this.canvas().getContext('2d'); },
line: function(a,b){
var ctx = this.context();
ctx.beginPath();
ctx.moveTo(a[0],a[1]);
ctx.lineTo(b[0],b[1]);
ctx.stroke();
}
};
/**
* t e t r...
* @type {{grid: Function, generateBlock: Function, init: Function}}
*/
var t = {
blockColor: 'black',
fillColor: 'black',
grid: function(w,h,gs){
var ch = c.canvas().height;
var cw = c.canvas().width;
var cols= new Array(w);
var rows=new Array(h);
var tmpgs = 0;
for(var n =0;n<cols.length;n++) {
//c.line([tmpgs, 0],[tmpgs, ch]);
tmpgs +=gs;
cols[n] = 0;
}
tmpgs = 0;
for(var n =0;n<rows.length;n++) {
//c.line([0, tmpgs],[cw,tmpgs]);
tmpgs +=gs;
rows[n] = 0;
}
return [cols, rows];
},
generateBlock: function(t) {
var figureAr = null;
switch (t) {
case 'I':
figureAr = [
[0,0,0,0],
[1,1,1,1]
];
this.blockColor = '#0686AD';
this.fillColor = '#00C4FF';
break;
case 'J':
//|--
figureAr = [
[1,0,0],
[1,1,1]
];
this.blockColor = '#033388';
this.fillColor = '#005DFF';
break;
case 'L':
//|--
figureAr = [
[0,0,1],
[1,1,1]
];
this.blockColor = '#B96100';
this.fillColor = '#FF8600';
break;
case 'O':
figureAr = [
[1,1],
[1,1]
];
this.blockColor = '#6A6B18';
this.fillColor = '#95961E';
break;
case 'S':
figureAr = [
[0,1,1],
[1,1,0]
];
this.blockColor = '#126523';
this.fillColor = '#189C32';
break;
case 'T':
figureAr = [
[0,1,0],
[1,1,1]
];
this.blockColor = '#BB22EF';
this.fillColor = '#7B189C';
break;
case 'Z':
figureAr = [
[1,1,0],
[0,1,1]
];
this.blockColor = '#791717';
this.fillColor = '#F32E2E';
break;
}
return figureAr;
},
drawBlock: function(sx, sy, t, gs) {
var ctx = c.context();
var block = this.generateBlock(t);
var blen = block[0].length;
ctx.beginPath();
ctx.strokeStyle = this.blockColor;
ctx.fillStyle = this.fillColor;
ctx.strokeWidth = '2px';
//cols
var tmpgsX = sx;
var tmpgsY = sy;
for(i = 0; i < blen; i++) {
if (block[0][i] > 0) {
ctx.rect(tmpgsX, tmpgsY, gs, gs);
ctx.fillRect(tmpgsX, tmpgsY, gs, gs);
}
tmpgsX += gs;
}
//rows
tmpgsX = sx;
tmpgsY = sy + gs;
for(i = 0; i < blen; i++) {
if (block[1][i] > 0) {
ctx.rect(tmpgsX, tmpgsY, gs, gs);
ctx.fillRect(tmpgsX, tmpgsY, gs, gs);
}
tmpgsX += gs;
}
ctx.stroke();
},
init: function() {
var w = 10;//ширина поля
var h = 20; //высота поля
var gridstep = parseInt(c.canvas().height / h);//визуально отображаемый размер ячейки
var rg = this.grid(w,h,gridstep);
this.testingBlocks();
},
/**
* Тестирование блоков
*/
testingBlocks: function() {
var w = 10;//ширина поля
var h = 20; //высота поля
var gridstep = parseInt(c.canvas().height / h);//визуально отображаемый размер ячейки
this.drawBlock(0, 0, 'I', gridstep);
this.drawBlock(gridstep, gridstep * 3, 'J', gridstep);
this.drawBlock(gridstep * 5, gridstep * 3, 'L', gridstep);
this.drawBlock(gridstep * 5, gridstep * 6, 'O', gridstep);
this.drawBlock(0, gridstep * 8, 'S', gridstep);
this.drawBlock(gridstep * 2, gridstep * 10, 'T', gridstep);
this.drawBlock(gridstep * 6, gridstep * 12, 'Z', gridstep);
}
};
window.onload = function() {
t.init();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment