A tweet-sized, fork-to-play, miniature version of Tetris written in JavaScript.
See the 140byt.es site brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.
A tweet-sized, fork-to-play, miniature version of Tetris written in JavaScript.
See the 140byt.es site brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.
// h = horizontal position | |
// v = vertical position | |
// t = tetronimo shape bits 1,3 | |
// g = array of row lines | |
// d = delta key input signal -1,0,1 | |
function (h,v,t,g,d) | |
{ | |
// Check if the next block-position collides with the game board. | |
// t << h+d = the shape bits shifted to (h) horizontal position + (d) movement | |
if(g[v+=!d] & t<<h+d) | |
{ | |
// If (d==0) the block is falling and not moving horizontally. | |
if(!d) | |
{ | |
// The shape is blocked from below so freeze the bits onto the game view | |
// If the resulting bit pattern equals (F) the full-row pattern | |
// Then splice() to remove the row from the array of lines | |
// And unshift() to add the (Z) blank value at the top of the view | |
// The F&Z works because the splice() returns the row value removed. | |
(g[--v] |= t<<h) < F ? 0 : g.unshift( g.splice(v,1)&Z ); | |
// Reset to the middle of the screen W/2 | |
h=4; | |
// Reset to the top of the screen | |
v=0; | |
// Pick a random bit pattern (1 or 3) for the tetris block to drop | |
t = new Date&2|1 | |
} | |
// Cancel the horizontal movement value. | |
d=0 | |
} | |
// Return the updated x,y,shape,game values. | |
return[h+d,v,t,g] | |
} |
function(h,v,t,g,d){if(g[v+=!d]&t<<h+d){if(!d){(g[--v]|=t<<h)<F?0:g.unshift(g.splice(v,1)&Z);h=4;v=0;t=new Date&2|1}d=0}return[h+d,v,t,g]} |
DO WHAT THE HECK YOU WANT TO PUBLIC LICENSE | |
Version 3, February 2012 | |
Copyright (C) 2012 -- Mr. P. ISAAC | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE HECK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just do whatever you want. |
{ | |
"Mini Tetris": "miniTetris", | |
"description": "A slightly more well-behaved version of Tetris than some other 140 byte projects. The left and right sides of the screen block player movement. The game view is larger and the width and height of the screen are included as constants you can manipulate.", | |
"keywords": [ | |
"game", | |
"mini", | |
"tetris", | |
"classic", | |
"140" | |
] | |
} |
<!DOCTYPE html> | |
<title>Mini-Tetris</title> | |
<style>body{ font-family: monospace; font-size: 20px}</style> | |
<div id="ret"></div> | |
<script> | |
var out = document.getElementById("ret"); | |
var N=8; // Number of rows in the play field | |
var W=8; // Number of columns | |
var Z=2<<W|1; // Zero row with side-bars | |
var F=Z*2-3; // Full row value | |
var time = 4<<8; | |
var px=0,py=N-1,pz=1; | |
var game=[]; for(var i=N;i--;)game[i]=Z;game[N]=F; | |
// var MyFunction = | |
function Inner(h,v,t,g,d) | |
{ | |
if(g[v+=!d]&t<<h+d) | |
{ | |
if(!d) | |
{ | |
(g[--v]|=t<<h)<F?0:g.unshift(g.splice(v,1)&Z); | |
h=4;v=0;t=new Date&2|1 | |
} | |
d=0 | |
} | |
return[h+d,v,t,g] | |
} | |
function DrawGame() | |
{ | |
var display = ""; | |
for (var j=0; j<N; j++) | |
{ | |
for (var b=2; b<2<<W; b*=2) | |
{ | |
display += (j==py)*(pz<<px)&b?"■":game[j]&b?"#":"."; | |
} | |
display += "<br>"; | |
} | |
out.innerHTML = display+"========"; | |
} | |
function Update(d) | |
{ | |
var r = Inner(px,py,pz,game,d); | |
px=r[0], py=r[1], pz=r[2], game=r[3]; | |
DrawGame(); | |
} | |
// Map arrow keys Left(37)=(-1) Right(39)=(+1) Down(40)=(0) | |
onkeydown = function(e) { if(e="1032"[e.keyCode-37])Update(e-2); } | |
function Mainloop() | |
{ | |
Update(0); | |
setTimeout(Mainloop, time-=2); | |
} | |
Mainloop(); | |
</script> |