テトリスをつくってみました　　その２　ソースコードtetris2.js

tetris2.js



```js
function blockGenerate () {
	var i = Math.floor ((Math.random() *10 )% SHAPES.length );
	var CopyOfROTATE_CHANGE = deepCopy ( ROTATE_CHANGE) ;
	playerBlock.rotatePattern = CopyOfROTATE_CHANGE [ COLOR [ i ] ];
	var CopyOfTETRIS_BLOCK = deepCopy ( TETRIS_BLOCK [i ] );
	
	//adjustment: make one time rotation
	var itsShapes = playerBlock.rotatePattern.shift();
	playerBlock.rotatePattern.push (itsShapes );

	return CopyOfTETRIS_BLOCK;	// if it isn't copy but original, occurs bug.
																//start block got wrong shape
}

function checkLine() {
	
	var line = [];
	for ( var i =0 ; i <RECT_W; i++) {
		for ( var a = 0; a < COLS; a++){
			if ( !Board [a ] [ playerBlock.y + i ] )  {
				break;
			}
			else{
				if ( a == COLS -1) line.unshift( playerBlock.y + i);
			}
		}
	}
	
	return line;
}
		 

function clearLine ( line ) {
	var i;
	line.sort( function (a, b) {
		return a > b;
		}
	);

	while( i = line.shift() ) {
		for ( var j = 0; j < COLS; j++) {
			for ( var k = i; k >= 0; k--) {
				BoardWithColor [ j ] [ k ].Board	= BoardWithColor [ j ] [ k - 1 ].Board;
				BoardWithColor [ j ] [ k ].Color	= BoardWithColor [ j ] [ k - 1 ].Color;
				
			}
		}
	}
	render.drawBoard();
}

		

function start () {
	
	
	var 	START_PLACE_X = 4, START_PLACE_Y = 0;
	
	playerBlock.x = START_PLACE_X;
	playerBlock.y = START_PLACE_Y;
	playerBlock.tetrisBlock = blockGenerate ();
	
	render.drawBlockType ( playerBlock.x, playerBlock.y, playerBlock.tetrisBlock );
	if ( check.overBlock(0, 0) ) gameOver();

}

var timerID;

function gameOver () {
	clearInterval (timerID );
	alert( "gameover") ;
	init();										//  clear board and so on.
	start();
}


function Check ()	 {

	this.overBlock =
	function overBlock ( offsetX, offsetY, rotate ) {
		var tmpPB = {};
		tmpPB.tetrisBlock = {};
		
		tmpPB.x = playerBlock.x + offsetX;
		tmpPB.y = playerBlock.y + offsetY;
		if (rotate) {
			tmpPB.tetrisBlock.blockColor = playerBlock.tetrisBlock.blockColor;
			var tmpShapes = deepCopy (playerBlock.rotatePattern);
			tmpPB.tetrisBlock.blockShape = rotateBlock ( tmpShapes );
		}
		else {
			tmpPB.tetrisBlock.blockColor = playerBlock.tetrisBlock.blockColor;
			tmpPB.tetrisBlock.blockShape = playerBlock.tetrisBlock.blockShape;
		}
			
		for (var i = tmpPB.x, j = tmpPB.y , k = 0;  
		k < tmpPB.tetrisBlock.blockShape.length ; 
		i ++, k++ ) {
															//0123456789
			var a = Math.floor (k/4);		//0000111122223333
			var b = i - a*RECT_W;			//012301230123
			var c = j + a;							//
			
//			console.table(Board);
			if ( 
				(Board [ b] [ c ] == 1 && tmpPB.tetrisBlock.blockShape [ k ]  == 1)
			||	(Board [ b] [ c ] == undefined && tmpPB.tetrisBlock.blockShape [ k ] == 1)
			 ) {
				 return true;
			 } 
		}
		return false;
}
	
	function attach () {
		for (var i = playerBlock.x, j = playerBlock.y , k = 0;  
		k < playerBlock.tetrisBlock.blockShape.length ; 
		i ++, k++ , j += k / CUBEUNIT_LENGTH) {
			if ( Board [ i ] [ j + 1 ] == 1
			&& playerBlock.tetrisBlock.blockShape [ k ]  == 1 ) {
				return true;
			}
			return false
		}
	}
}


function moveBlock ( offsetX, offsetY, rotate ) {
	
	if (check.overBlock (offsetX, offsetY, rotate ) ){
		if ( offsetY == 1 ) {
			attachBlock ();
			var line = checkLine();
			if ( line.length  ) clearLine ( line ) ; 
			playerBlock = {};
			start ();
		}
	
	}
	else {
		var nullBlock ={};
		nullBlock.tetrisBlock = {};
		nullBlock.tetrisBlock.blockColor = INIT_BACKCOLOR;
		nullBlock.tetrisBlock.blockShape = [].concat(playerBlock.tetrisBlock.blockShape );
		render.nullBlock = 'true';
		render.drawBlockType ( playerBlock.x, playerBlock.y, nullBlock.tetrisBlock );
		render.nullBlock = null;
			
		playerBlock.x += offsetX;
		playerBlock.y += offsetY;
		if ( rotate ) {
			playerBlock.tetrisBlock.blockShape = rotateBlock ( playerBlock.rotatePattern );
		}
		
		render.drawBoard();		//消した元あったブロック場所に、接するブロックの枠線が消えてしまう　みっともない
													//なくても動く
		render.drawBlockType ( playerBlock.x, playerBlock.y, playerBlock.tetrisBlock )
		
	}
}

	
function attachBlock () { 
	
	for ( var i =0, j = playerBlock.y, k = playerBlock.x;
	 i < playerBlock.tetrisBlock.blockShape.length;
	i++ , k++) {
		 var a = j + Math.floor( i / RECT_W);
		 var b = k - Math.floor( i / RECT_W)* RECT_W;

		 if ( 1 == playerBlock.tetrisBlock.blockShape [ i ] ) {
			 Board [ b ] [ a ] = playerBlock.tetrisBlock.blockShape [ i ];
			 BoardWithColor [ b ][ a ].Color = playerBlock.tetrisBlock.blockColor;
		}
	}
}
	

function gameInit () {
		 render	= new Render();
		 check	= new Check();
		 //~ control = new Control();
		 //~ block = new Block();
		 //~ time = new Time();
		 //~ clearLine = new clearLine();
		 //~ attach = new Attach();
		 //~ gameover = new Gameover;
}

function rotateBlock ( shapes ) {
	var i = shapes.shift();
	shapes.push ( i );
	
	return i ;
}


function init () {
	
	// init Board & BoardWithColor
	for (var i = -3;  i < COLS + 3; i ++) {					// at [ -1 ] [ -1], Board == 0, Color == 0  
		for ( var j = -1; j < ROWS + 3; j++) {
			if ( i < 0 || j >= ROWS || i >= COLS ) Board [ i ] [ j ] = null;
			else if (j < 0) Board [i ] [j ] = 0;
			else Board [ i ] [ j ] = 0;	
			BoardWithColor [ i ] [ j ] .Color =  'white';		// initiate Color
			
		}
	}
	
	render.init();
	
	
	timerID = setInterval ("moveBlock( 0, 1)", 1550 );

}

gameInit();
init ();
start ();
	
	 
    
   
    

//function dataBreak () {
	return null;
}

//TETRIS_BLOCK.watch('0', dataBreak);
//TETRIS_BLOCK[0].watch('blockShape', dataBreak);
//TETRIS_BLOCK[0].blockShape.watch('2', dataBreak);
```