Zkousel jsi ten muj kod?
s tim jedinym casovacem souhlas.
Příspěvky odeslané z IP adresy 2001:718:2601:1f7:302a:fa...–
Dobre, tak to neni uplne ono, raci to opravim, jak pises.
Jeee, parada, toci se, toci a ma modre oci. Udelal jsem jinou zmenu. Pridal jsem tam promenou 3. A upravil F a funkce. A pak taky chybku na konci programu, kde mela byt zeme
1) function spocitej(orbit,orbit2,center)
2) F = - k.kappa * orbit2.m / rr;
3) spocitej(zeme , slunce, slunce);
spocitej(mesic, zeme, slunce);
4) kresli(zeme , slunce);
kresli(mesic , zeme);
Ted by to mohlo fungovat i se zmenou polohy slunce, snad :)
Jo, to start, stop, to mam terminologii podle upraveneho kodu, myslim tim samozrejme setInterval a clearInterval pro start a stop casovace.
No, v tom mem poslednim kodu se mi prave nic uz nezdvojuje a zda se to, aspon ve FF, funkcni. Az teda na t dopady, kdy vyleze kosticka z nicehonic dole, obcas.
A ten kod u newgame (NG), mozna jsem to mel popsat. V NG nejdriv provedes STOP, pak je nejaky kod, pak se spousti resetTetromino (RT), ktere provede STOP take. Po sem by to bylo jaksi ok. Ale pak RT provede START a ukonci se. Pak pokracuje dal kod v new game a to na konci take spusti START. Tudiz se v newgame provede v podstate 2x za sebou START, coz asi vyvolava to zdvojovani, ktere nejspis hledas.
A pak jsem v tom posl. kodu jeste caroval s klavesami. Mozna to taky neco poresilo, ale tim si nejsem uz jisty, pac tu RT jsem resil az pak.
Moje posl. verze vypada takto a zda se, ze funguje podle ocekavani. jestli jsi teda nemel jina ocekavani :) Ale mozna tam mam par veci navic. Total commander ma takovou peknou funkci porovnavani obsahu souboru a ocerveni tam zmeny.
var
key_down = -1,
key_up = -1,
x = 0,
y = 0,
canvas,
lines = 0,
level = 0,
score = 0,
run = false,
levelElement,
scoreElement,
gameInterval = null,
interval = 700,
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", "", "", "", "", "" ]
];
function timerStart()
{
gameInterval = setInterval(tetrominoFall, interval);
}
function timerStart2()
{
gameInterval = setInterval(tetrominoFall, 50);
}
function timerStop()
{
clearInterval(gameInterval);
}
function keyDownAction()
{
switch(key_down)
{
case 37: // left
if(run && displayTetromino(x - 1, y, -1, 0))
resetTetromino();
break;
case 38: // up
if(run)
tetrominoFilp();
break; // right
case 39:
if(run && displayTetromino(x + 1, y, 1, 0))
resetTetromino();
break;
case 40: // down
if(run && !accelerated)
{
accelerated = true;
timerStop();
key_down = -1;
timerStart2();
//displayTetromino(x, y+1, 0, 1)
////resetTetromino();
//loguj('40 down')
}
break;
}
key_down = -1;
}
function keyUpAction()
{
switch(key_up)
{
case 40:
if(run)
{
accelerated = false;
timerStop();
key_up = -1;
timerStart();
//loguj('40 up')
}
break;
case 113:
newGame();
//loguj('newgame')
break;
}
key_up = -1;
}
window.onload = function()
{
var pageY, pageX, height, row, width, aside;
aside = document.getElementsByTagName("aside")[0];
levelElement = document.getElementById("level");
scoreElement = document.getElementById("score");
canvas = document.getElementById("canvas");
document.getElementById("newGame").onclick = newGame;
pageY = window.innerHeight / 2 - 240;
pageX = window.innerWidth / 2 - 170;
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)
{
key_down = (event || window.event).keyCode;
keyDownAction();
};
canvas.onkeyup = function(event)
{
key_up = (event || window.event).keyCode;
keyUpAction();
};
for(height = 0; height < 20; height++)
{
row = canvas.insertRow(0);
for(width = 0; width < 10; width++)
row.insertCell(0);
}
canvas.focus();
}
function newGame()
{
var height, width, cells;
loguj('newgame timer stop')
timerStop();
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 = "";
}
timerStart();
loguj('newgame timer start')
}
function resetTetromino()
{
// timerStop();
var n, row, flip, newTetromino, currentX,currentY, height, width, index, newIndex, cells;
x = 4;
n = -1;
actualTetromino = Math.floor(Math.random() * 7);
for(row = 1; row < 20; row++)
if(testRow(row))
{
removeRow(row);
lines++;
n++;
}
if(lines > 9)
{
level++;
lines -= 10;
interval -= 30;
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;
}
}
// timerStart();
loguj('reset timer')
}
function displayTetromino(newX, newY, xWay, yWay)
{
var currentX, currentY, nextIndex, index;
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()
{
var currentX, currentY, index;
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())
{
timerStop();
run = false;
}
else
resetTetromino();
}
//keyUpAction();
}
function tetrominoFilp()
{
var can, newTetromino, currentX , currentY, index;
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;
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;
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);
}
function loguj(str)
{
document.getElementById('log').innerHTML+= str + "<br>";
}
Dalsi problem jsem ted nasel treba v newgame. Pri kliknuti na newgame se spusti casovac 2x. Mozna to mas podobne i jinde.
function newGame()
{
timerStop();
resetTetromino()
{
timerStop();
timerStart();
}
timerStart();
}
Zdvojovani sipky dolu jsem ti psal v #6.
Zmacknuti klavesy je udalost onkeydown + onkeyup. Ty mas v programu onkeydown : spustCasovac a onkeyup: spustcasovac podruhe. takze na jednu zmacknutou a pustenou sipku dolu se 2x spusti casovac. Tu udalost bys mel mit navazanou jen na onkeydown, si myslim. Ale treba to tak mas zamerne, tezko rici.
"var je blbost - úplně k ničemu, bez významu..."
:) Ma vyznam.
- definuje rozdil mezi globalni promenou a vnitrni, jinak si to musi zjistit prohlizec sam, coz chvilku trva
- tez je dobre treba pred cyklem promenou naplnit, aby si vybral spravny typ, coz opet prohlizeci chvilku trva pretypovat z nejakeho default typu.
- soucasne tim reknes tomu, kdo to po tobe bude cist, zda je ta promenna vyznamna vne nebo uvnitr. Kdyz ji uvidim ve var tak ji nebudu hledat po celem programu.
resit to nemusis, jen ti rikam, ze mi to dela strasne potize pri hledani chyb. A je celkem mozne, ze se ti priste na to vybodnu. Mozna budes i rad, kdo vi :)
Mno, hele, zapni si uz v prohlizeci tu konzolu a vsechna varovani a dopln vary pro promenne :) Stalo mne to asi 10 min.
Kdyz uz jsem to vycistil, tak jsem to zkousel a vsiml si, ze tesne pred objevenim nove kostky, kdyz mackas klavesy, tak se objevi hned dole. Tam bych mozna hledal jeden problem.
Pak jsem si v kodu vsiml, ze na klavesu spoustis casovac. To je chyba. Klavesu uloz do promenne a nech zpracovat az dalsim casovanim. Ja bych si udelal funkci casovac, ktera pricita +1 do promene, po 20 nuluje a udelal tam rozhodovani prom = 5, prom=20 atd. Casovac pak spustis jednou pri zapnuti hry a ukoncis pri konci. Interval bych dal treba 50 ms (nebo mozna bude stacit 100 ms).
Treba, kdyz se zmackne klavesa, tak po 0-100 ms zareaguje casovac, ktery na ni nejak zareaguje ihned, cili bez prom=neco. Ale, kdyz uplyne 1s, tak se spusti posun dolu, v casovaci na to zareaguje prom==10 (pri intervalu casovace 100ms * 10 cyklu = 1s).
Pozdeji to omrknu vic a zkusmi mozna prepsat...
var timer, timerInterval, timerIncrement;
timerInterval = 50
function timerStart()
{
timer = setInterval(timerFunc, timerInterval);
}
function timerStop()
{
clearInterval(timer);
}
function timerFunc()
{
if (key!=-1) {pohyb(key);}
else
{
if {timerIncrement == 20} {tetrominoFall();}
}
...
timerIncrement++;
timerIncrement%=20; // omezeni 20 pomoci ziskani zbytku po deleni
}