Chtelo by to neco, co se da spustit :) Je tu moznost treba jsfiddle.net. Takhle fakt nemuzu hadat chybu, kdyz mi konzola hlasi neexistujici htm elementy.
Zkousel jsi cyklus pro cells[i].style.backgroundPosition?
Na to startovani a ukonceni intervalu bych si udelal funkci.
Casovac bys mel nejdriv zastavit a az pak menit promenne, jako treba accelerated.
Co se stane, kdyz na keydown klavesy 40 a keyup se spusti setinterval? Maler, protoze tyto dve udalosti mohou navazovat kratce na sebe a interval nema cas dokoncit prvni funkci.
<div id=log style="height:20em; overflow:auto;"></div>
<script>
function loguj(str)
{
document.getElementById('log').innerHTML+= str + "<br>";
}
</script>
<aside></aside>
<div id=level></div>
<div id=score></div>
<canvas id=canvas></canvas>
<input type=button id=newGame value=newGame>
<script>
function fA() {loguj(1); clearInterval(gameInterval);}
function fB() {loguj(2); gameInterval = setInterval(tetrominoFall, interval);}
var
x = 0,
y = 0,
canvas,
lines = 0,
level = 0,
score = 0,
run = false,
levelElement,
scoreElement,
gameInterval,
interval = 900,
startLevel = 0,
accelerated = false,
actualTetromino = 0,
rowScore = [ 40, 100, 300, 1200 ],
tetromino =
[
[ "", "", "", "", "-24px 0px", "-24px 0px", "-24px 0px", "-24px 0px", "", "", "", "", "", "", "", "" ],
[ "", "", "", "", "", "-48px 0px", "-48px 0px", "", "", "-48px 0px", "-48px 0px", "", "", "", "", "" ],
[ "", "", "", "", "", "-72px 0px", "", "", "-72px 0px", "-72px 0px", "-72px 0px", "", "", "", "", "" ],
[ "", "", "", "", "", "-96px 0px", "-96px 0px", "", "-96px 0px", "-96px 0px", "", "", "", "", "", "" ],
[ "", "", "", "", "-120px 0px", "-120px 0px", "", "", "", "-120px 0px", "-120px 0px", "", "", "", "", "" ],
[ "", "", "", "", "-144px 0px", "", "", "", "-144px 0px", "-144px 0px", "-144px 0px", "", "", "", "", "" ],
[ "", "", "", "", "", "", "-168px 0px", "", "-168px 0px", "-168px 0px", "-168px 0px", "", "", "", "", "" ]
],
aside;
window.onload = function()
{
var aside = document.getElementsByTagName("aside")[0]; //pp
levelElement = document.getElementById("level");
scoreElement = document.getElementById("score");
canvas = document.getElementById("canvas");
canvas.insertRow = function() {return {insertCell:function(){}};}; //pp
canvas.rows = [0,1,2,3,4,5,6,7,8,9,10, 11,12,13,14,15,16,17,18,19,20]; //pp
document.getElementById("newGame").onclick = newGame;
var pageY = window.innerHeight / 2 - 240; //pp
var pageX = window.innerWidth / 2 - 170; //pp
canvas.style.left = pageX + "px";
canvas.style.top = pageY + "px";
aside.style.left = (pageX + 240) + "px";
aside.style.top = pageY + "px";
canvas.onkeydown = function(event)
{
switch((event || window.event).keyCode)
{
case 37:
if(run && displayTetromino(x - 1, y, -1, 0))
resetTetromino();
break;
case 38:
if(run)
tetrominoFilp();
break;
case 39:
if(run && displayTetromino(x + 1, y, 1, 0))
resetTetromino();
break;
case 40:
if(run && !accelerated)
{
accelerated = true;
fA();
loguj(3);
fB();
}
break;
}
};
canvas.onkeyup = function(event)
{
switch((event || window.event).keyCode)
{
case 40:
if(run)
{
accelerated = false;
fA();
loguj(4);
fB();
}
break;
case 113:
newGame();
break;
}
};
for(var height = 0; height < 20; height++) //pp
{
var row = canvas.insertRow(0); //pp
for(var width = 0; width < 10; width++)
row.insertCell(0);
}
canvas.focus();
}
function newGame()
{
fA();
loguj(5);
score = 0;
run = true;
canvas.focus();
resetTetromino();
level = startLevel;
levelElement.innerHTML = level;
scoreElement.innerHTML = score;
interval = 900 - startLevel * 30;
for(height = 0; height < 20; height++)
{
cells = canvas.rows[height].cells;
for(width = 0; width < 10; width++)
cells[width].style.backgroundPosition = "";
}
fB();
}
function resetTetromino()
{
x = 4;
var n = -1;
actualTetromino = Math.floor(Math.random() * 7);
for(var row = 1; row < 20; row++)
if(testRow(row))
{
removeRow(row);
lines++;
n++;
}
if(lines > 9)
{
level++;
lines -= 10;
interval -= 30;
fA();
loguj(6);
levelElement.innerHTML = level;
}
if(n > -1)
{
score += rowScore[n] * level + rowScore[n];
scoreElement.innerHTML = score;
}
for(flip = Math.floor(Math.random() * 4); flip > 0; flip--)
{
newTetromino =
[
tetromino[actualTetromino][12],
tetromino[actualTetromino][8],
tetromino[actualTetromino][4],
tetromino[actualTetromino][0],
tetromino[actualTetromino][13],
tetromino[actualTetromino][9],
tetromino[actualTetromino][5],
tetromino[actualTetromino][1],
tetromino[actualTetromino][14],
tetromino[actualTetromino][10],
tetromino[actualTetromino][6],
tetromino[actualTetromino][2],
tetromino[actualTetromino][15],
tetromino[actualTetromino][11],
tetromino[actualTetromino][7],
tetromino[actualTetromino][3]
];
tetromino[actualTetromino] = newTetromino;
}
for(currentY = 3; currentY >= 0; currentY--)
{
cells = tetromino[actualTetromino];
if(cells[0] || cells[1] || cells[2] || cells[3])
{
y = currentY * -1;
break;
}
}
}
function displayTetromino(newX, newY, xWay, yWay)
{
for(currentY = newY, index = 0; currentY < newY + 4; currentY++)
for(currentX = newX; currentX < newX + 4; currentX++, index++)
if(tetromino[actualTetromino][index] && currentY >= 0)
{
nextIndex = index + xWay + yWay * 4;
if
(
currentX < 0 || currentX > 9 || currentY == 20 ||
(
canvas.rows[currentY].cells[currentX].style.backgroundPosition
&&
(
nextIndex > 15
||
!tetromino[actualTetromino][nextIndex]
)
)
)
return yWay;
}
clearTetromino();
x = newX;
y = newY;
for(currentY = y, index = 0; currentY < y + 4; currentY++)
for(currentX = x; currentX < x + 4; currentX++, index++)
if(tetromino[actualTetromino][index] && currentY >= 0)
canvas.rows[currentY].cells[currentX].style.backgroundPosition = tetromino[actualTetromino][index];
return false;
}
function clearTetromino()
{
for(currentY = y, index = 0; currentY < y + 4; currentY++)
for(currentX = x; currentX < x + 4; currentX++, index++)
if(tetromino[actualTetromino][index] && currentY > -1)
canvas.rows[currentY].cells[currentX].style.backgroundPosition = "";
}
function tetrominoFall()
{
if(displayTetromino(x, y + 1, 0, 1))
{
if(testFirstRow())
{
fA();
loguj(7);
run = false;
}
else
resetTetromino();
}
}
function tetrominoFilp()
{
can = true;
clearTetromino();
newTetromino =
[
tetromino[actualTetromino][12],
tetromino[actualTetromino][8],
tetromino[actualTetromino][4],
tetromino[actualTetromino][0],
tetromino[actualTetromino][13],
tetromino[actualTetromino][9],
tetromino[actualTetromino][5],
tetromino[actualTetromino][1],
tetromino[actualTetromino][14],
tetromino[actualTetromino][10],
tetromino[actualTetromino][6],
tetromino[actualTetromino][2],
tetromino[actualTetromino][15],
tetromino[actualTetromino][11],
tetromino[actualTetromino][7],
tetromino[actualTetromino][3]
];
for(currentY = y, index = 0; currentY < y + 4; currentY++)
for(currentX = x; currentX < x + 4; currentX++, index++)
if(newTetromino[index])
if
(
currentX < 0 || currentX > 9 || currentY == 20 ||
canvas.rows[currentY].cells[currentX].style.backgroundPosition
)
{
can = false;
break;
}
if(can)
tetromino[actualTetromino] = newTetromino;
displayTetromino(x, y, 0, 0);
}
function testRow(row)
{
var cells = canvas.rows[row].cells;
return cells[0].style.backgroundPosition && cells[1].style.backgroundPosition &&
cells[2].style.backgroundPosition && cells[3].style.backgroundPosition &&
cells[4].style.backgroundPosition && cells[5].style.backgroundPosition &&
cells[6].style.backgroundPosition && cells[7].style.backgroundPosition &&
cells[8].style.backgroundPosition && cells[9].style.backgroundPosition;
}
function testFirstRow()
{
var cells = canvas.rows[0].cells;
return cells[0].style.backgroundPosition || cells[1].style.backgroundPosition ||
cells[2].style.backgroundPosition || cells[3].style.backgroundPosition ||
cells[4].style.backgroundPosition || cells[5].style.backgroundPosition ||
cells[6].style.backgroundPosition || cells[7].style.backgroundPosition ||
cells[8].style.backgroundPosition || cells[9].style.backgroundPosition;
}
function removeRow(rowIndex)
{
canvas.deleteRow(rowIndex);
newRow = canvas.insertRow(0);
for(x = 0; x < 10; x++)
newRow.insertCell(0);
}
</script>
Vypsalo mi to 1 a 5, pak to skoncilo dalsi chybou v konzoli (uz asi 20, co jsem opravoval, abych se pokusil z toho udelat neco funkcniho) a prestalo mne to bavit.