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.
I made a more accurate version of the game with proper all the rotating pieces. This of course is larger and the core logic takes 256 characters.
Empty Fall Frozen
........ ........ ........
........ ........ ........
........ ...@@... ........
........ ........ ........
........ ........ ...##...
======== ======== ========
Left Arrow = Move left
Right Arrow = Move right
Down Arrow = Fall
This function is the core logic of the game. It processes all input signals and manipulates the game data. I know it's cheating a bit to ignore the outer script, but I'm following the pattern of some others and agree that the display mechanism is arbitrary and less important, for my purposes.
// h = horizontal position
// v = vertical position
// t = tetronimo bits 1,3
// g = array of row lines
// d = delta key input signal -1,0,1
function Inner(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]
}
The function can be stripped of white-space, formatted as an anonymous function with less than 140 characters and assigned to a variable to be called.
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]}
Nice one!
But save 4 bytes by rearranging the return statement and use
?
instead ofif
: