A simple javascript tetris game
HTML
Remove unused syntax
Richard Birkbya844e74025ec
diff --git a/index.html b/index.htmlindex 6bf1cd3..37d4af0 100644--- a/index.html+++ b/index.html@@ -1,4 +1,4 @@-<!DOCTYPE html>+<!DOCTYPE html><html><head><title>Javascript Tetris</title>@@ -22,9 +22,9 @@@media screen and (min-width: 800px) and (min-height: 800px) { #tetris { font-size: 2.00em; width: 750px; } #menu { width: 350px; height: 700px; } #upcoming { width: 175px; height: 175px; } #canvas { width: 350px; height: 700px; } } /* 35px chunks */@media screen and (min-width: 900px) and (min-height: 900px) { #tetris { font-size: 2.25em; width: 850px; } #menu { width: 400px; height: 800px; } #upcoming { width: 200px; height: 200px; } #canvas { width: 400px; height: 800px; } } /* 40px chunks */</style>-</head>+</head>-<body>+<body><div id="tetris"><div id="menu">@@ -45,20 +45,20 @@// base helper methods//-------------------------------------------------------------------------- function get(id) { return document.getElementById(id); };- function hide(id) { get(id).style.visibility = 'hidden'; };- function show(id) { get(id).style.visibility = null; };- function html(id, html) { get(id).innerHTML = html; };+ function get(id) { return document.getElementById(id); }+ function hide(id) { get(id).style.visibility = 'hidden'; }+ function show(id) { get(id).style.visibility = null; }+ function html(id, html) { get(id).innerHTML = html; }- function timestamp() { return new Date().getTime(); };- function random(min, max) { return (min + (Math.random() * (max - min))); };- function randomChoice(choices) { return choices[Math.round(random(0, choices.length-1))]; };+ function timestamp() { return new Date().getTime(); }+ function random(min, max) { return (min + (Math.random() * (max - min))); }+ function randomChoice(choices) { return choices[Math.round(random(0, choices.length-1))]; }if (!window.requestAnimationFrame) { // http://paulirish.com/2011/requestanimationframe-for-smart-animating/- window.requestAnimationFrame = window.webkitRequestAnimationFrame ||- window.mozRequestAnimationFrame ||- window.oRequestAnimationFrame ||- window.msRequestAnimationFrame ||+ window.requestAnimationFrame = window.webkitRequestAnimationFrame ||+ window.mozRequestAnimationFrame ||+ window.oRequestAnimationFrame ||+ window.msRequestAnimationFrame ||function(callback, element) {window.setTimeout(callback, 1000 / 60);}@@ -112,13 +112,13 @@////-------------------------------------------------------------------------- var i = { id: 'i', size: 4, blocks: [0x0F00, 0x2222, 0x00F0, 0x4444], color: 'cyan' };- var j = { id: 'j', size: 3, blocks: [0x44C0, 0x8E00, 0x6440, 0x0E20], color: 'blue' };- var l = { id: 'l', size: 3, blocks: [0x4460, 0x0E80, 0xC440, 0x2E00], color: 'orange' };- var o = { id: 'o', size: 2, blocks: [0xCC00, 0xCC00, 0xCC00, 0xCC00], color: 'yellow' };- var s = { id: 's', size: 3, blocks: [0x06C0, 0x8C40, 0x6C00, 0x4620], color: 'green' };- var t = { id: 't', size: 3, blocks: [0x0E40, 0x4C40, 0x4E00, 0x4640], color: 'purple' };- var z = { id: 'z', size: 3, blocks: [0x0C60, 0x4C80, 0xC600, 0x2640], color: 'red' };+ var i = { size: 4, blocks: [0x0F00, 0x2222, 0x00F0, 0x4444], color: 'cyan' };+ var j = { size: 3, blocks: [0x44C0, 0x8E00, 0x6440, 0x0E20], color: 'blue' };+ var l = { size: 3, blocks: [0x4460, 0x0E80, 0xC440, 0x2E00], color: 'orange' };+ var o = { size: 2, blocks: [0xCC00, 0xCC00, 0xCC00, 0xCC00], color: 'yellow' };+ var s = { size: 3, blocks: [0x06C0, 0x8C40, 0x6C00, 0x4620], color: 'green' };+ var t = { size: 3, blocks: [0x0E40, 0x4C40, 0x4E00, 0x4640], color: 'purple' };+ var z = { size: 3, blocks: [0x0C60, 0x4C80, 0xC600, 0x2640], color: 'red' };//------------------------------------------------// do the bit manipulation and iterate through each@@ -135,7 +135,7 @@++row;}}- };+ }//-----------------------------------------------------// check if a piece can fit into a position in the grid@@ -147,11 +147,11 @@result = true;});return result;- };+ }function unoccupied(type, x, y, dir) {return !occupied(type, x, y, dir);- };+ }//-----------------------------------------// start with 4 instances of each piece and@@ -163,7 +163,7 @@pieces = [i,i,i,i,j,j,j,j,l,l,l,l,o,o,o,o,s,s,s,s,t,t,t,t,z,z,z,z];var type = pieces.splice(random(0, pieces.length-1), 1)[0];return { type: type, dir: DIR.UP, x: Math.round(random(0, nx - type.size)), y: 0 };- };+ }//-------------------------------------------------------------------------@@ -189,17 +189,17 @@reset(); // reset the per-game variablesframe(); // start the first frame- };+ }function showStats() {stats.domElement.id = 'stats';get('menu').appendChild(stats.domElement);- };+ }function addEvents() {document.addEventListener('keydown', keydown, false);window.addEventListener('resize', resize, false);- };+ }function resize(event) {canvas.width = canvas.clientWidth; // set canvas logical size equal to its physical size@@ -210,7 +210,7 @@dy = canvas.height / ny; // (ditto)invalidate();invalidateNext();- };+ }function keydown(ev) {var handled = false;@@ -229,28 +229,28 @@}if (handled)ev.preventDefault(); // prevent arrow keys from scrolling the page (supported in IE9+ and all other browsers)- };+ }//-------------------------------------------------------------------------// GAME LOGIC//-------------------------------------------------------------------------- function play() { hide('start'); reset(); playing = true; };- function lose() { show('start'); setVisualScore(); playing = false; };-- function setVisualScore(n) { vscore = n || score; invalidateScore(); };- function setScore(n) { score = n; setVisualScore(n); };- function addScore(n) { score = score + n; };- function clearScore() { setScore(0); };- function clearRows() { setRows(0); };- function setRows(n) { rows = n; step = Math.max(speed.min, speed.start - (speed.decrement*rows)); invalidateRows(); };- function addRows(n) { setRows(rows + n); };- function getBlock(x,y) { return (blocks && blocks[x] ? blocks[x][y] : null); };- function setBlock(x,y,type) { blocks[x] = blocks[x] || []; blocks[x][y] = type; invalidate(); };+ function play() { hide('start'); reset(); playing = true; }+ function lose() { show('start'); setVisualScore(); playing = false; }++ function setVisualScore(n) { vscore = n || score; invalidateScore(); }+ function setScore(n) { score = n; setVisualScore(n); }+ function addScore(n) { score = score + n; }+ function clearScore() { setScore(0); }+ function clearRows() { setRows(0); }+ function setRows(n) { rows = n; step = Math.max(speed.min, speed.start - (speed.decrement*rows)); invalidateRows(); }+ function addRows(n) { setRows(rows + n); }+ function getBlock(x,y) { return (blocks && blocks[x] ? blocks[x][y] : null); }+ function setBlock(x,y,type) { blocks[x] = blocks[x] || []; blocks[x][y] = type; invalidate(); }function clearBlocks() { blocks = []; invalidate(); }- function clearActions() { actions = []; };- function setCurrentPiece(piece) { current = piece || randomPiece(); invalidate(); };- function setNextPiece(piece) { next = piece || randomPiece(); invalidateNext(); };+ function clearActions() { actions = []; }+ function setCurrentPiece(piece) { current = piece || randomPiece(); invalidate(); }+ function setNextPiece(piece) { next = piece || randomPiece(); invalidateNext(); }function reset() {dt = 0;@@ -260,7 +260,7 @@clearScore();setCurrentPiece(next);setNextPiece();- };+ }function update(idt) {if (playing) {@@ -271,9 +271,9 @@if (dt > step) {dt = dt - step;drop();- }+ }}- };+ }function handle(action) {switch(action) {@@ -282,7 +282,7 @@case DIR.UP: rotate(); break;case DIR.DOWN: drop(); break;}- };+ }function move(dir) {var x = current.x, y = current.y;@@ -300,15 +300,15 @@else {return false;}- };+ }- function rotate(dir) {+ function rotate() {var newdir = (current.dir == DIR.MAX ? DIR.MIN : current.dir + 1);if (unoccupied(current.type, current.x, current.y, newdir)) {current.dir = newdir;invalidate();}- };+ }function drop() {if (!move(DIR.DOWN)) {@@ -322,13 +322,13 @@lose();}}- };+ }function dropPiece() {eachblock(current.type, current.x, current.y, current.dir, function(x, y) {setBlock(x, y, current.type);});- };+ }function removeLines() {var x, y, complete, n = 0;@@ -348,7 +348,7 @@addRows(n);addScore(100*Math.pow(2,n-1)); // 1: 100, 2: 200, 3: 400, 4: 800}- };+ }function removeLine(n) {var x, y;@@ -356,7 +356,7 @@for(x = 0 ; x < nx ; ++x)setBlock(x, y, (y == 0) ? null : getBlock(x, y-1));}- };+ }//-------------------------------------------------------------------------// RENDERING@@ -378,7 +378,7 @@drawScore();drawRows();ctx.restore();- };+ }function drawCourt() {if (invalid.court) {@@ -395,7 +395,7 @@ctx.strokeRect(0, 0, nx*dx - 1, ny*dy - 1); // court boundaryinvalid.court = false;}- };+ }function drawNext() {if (invalid.next) {@@ -409,33 +409,33 @@uctx.restore();invalid.next = false;}- };+ }function drawScore() {if (invalid.score) {html('score', ("00000" + Math.floor(vscore)).slice(-5));invalid.score = false;}- };+ }function drawRows() {if (invalid.rows) {html('rows', rows);invalid.rows = false;}- };+ }function drawPiece(ctx, type, x, y, dir) {eachblock(type, x, y, dir, function(x, y) {drawBlock(ctx, x, y, type.color);});- };+ }function drawBlock(ctx, x, y, color) {ctx.fillStyle = color;ctx.fillRect(x*dx, y*dy, dx, dy);ctx.strokeRect(x*dx, y*dy, dx, dy)- };+ }//-------------------------------------------------------------------------// FINALLY, lets run the game@@ -445,5 +445,5 @@</script>-</body>+</body></html>