安歌公子
2018-01-30 17:31
var board = new Array(); var scroe = 0; $(document).ready(function(){ newgame(); }); function newgame() { // 初始化棋盘格 init(); //在随机两个格子生成数字 generateOneNumber(); generateOneNumber(); } function init(){ for(var i = 0 ; i < 4 ; i ++ ){ for(var j = 0 ; j < 4 ; j ++ ){ var gridCell = $("#grid-cell-"+i+"-"+j); gridCell.css('top', getPosTop( i , j ) ); gridCell.css('left', getPosLeft( i , j ) ); } } for(var i = 0 ; i < 4 ; i ++ ){ board[i] = new Array(); for(var j = 0 ; j < 4 ; j ++ ){ board[i][j] = 0; } } updateBoardView(); score = 0; console.table(board); } function updateBoardView(){ $(".number-cell").remove(); for(var i = 0 ; i < 4 ; i ++ ){ for(var j = 0 ; j < 4 ; j ++ ){ $("#grid-container").append('<div class="number-cell" id="number-cell-'+i+'-'+j+'">'); var theNumberCell = $('#number-cell-'+i+'-'+j); if( board[i][j] == 0 ){ theNumberCell.css('width',0); theNumberCell.css('height',0); theNumberCell.css('top', getPosTop(i,j)+50 ); theNumberCell.css('left', getPosLeft(i,j)+50 ); }else{ theNumberCell.css('width',100); theNumberCell.css('height',100); theNumberCell.css('top', getPosTop(i,j) ); theNumberCell.css('left', getPosLeft(i,j) ); theNumberCell.css('background-color',getNumberBgColor( board[i][j] ) ); theNumberCell.css('color',getNumberColor( board[i][j] ) ); theNumberCell.text( board[i][j] ); } } } } function generateOneNumber(){ if( noSpace( board )) return false; //随机一个位置 var randx = parseInt( Math.floor( Math.random()*4 ) ); var randy = parseInt( Math.floor( Math.random()*4 ) ); while( true ){ if ( board[randx][randy] == 0 ) break; var randx = parseInt( Math.floor( Math.random()*4 ) ); var randy = parseInt( Math.floor( Math.random()*4 ) ); } //随机一个数字 var randNumber = Math.random() < 0.5 ? 2 : 4 ; //在随机位置显示随机数字 board[randx][randy] = randNumber; showNumberWithAnimation( randx, randy, randNumber ); return true; } $(document).keydown(function(event){ switch( event.keyCode ){ case 37: //左 if( moveLeft() ){ generateOneNumber(); isGameOver(); }; break; case 38: //上 if( moveTop() ){ generateOneNumber(); isGameOver(); }; break; case 39: //右 if( moveRight() ){ generateOneNumber(); isGameOver(); }; break; case 40: //下 if( moveDown() ){ generateOneNumber(); isGameOver(); }; break; default: //默认 break; } console.table(board); }); function isGameOver(){ } function moveLeft(){ if( !canMoveLeft( board ) ) return false; //move left for(var i = 0 ; i < 4 ; i ++ ) for(var j = 1 ; j < 4 ; j ++ ) if( board[i][j] != 0 ) for( var k = 0; k < j ; k ++ ) if( board[j][k] == 0 && noBlockHorizontal( i,k,j,board ) ){ //move showMoveAnimation(i,j,i,k); board[i][k] = board[i][j]; board[i][j] = 0; continue; } else if( board[i][k] == board[i][j] && noBlockHorizontal(i,k,j,board) ){ //move showMoveAnimation(i,j,i,k); //add board[i][k] += board[i][j]; board[i][j] = 0; //add score score += board[i][k]; updateScore( score ); continue; } setTimeout("updateBoardView()",200); return true; }
MoveLeft()里面的 判断if(board[i][k]==0)有错
theNumberCell.css('left', getPosLeft(i,j)+50 ); 不需要+50
function showNumberWithAnimation( i, j, num ){ var numberCell = $( "#number-cell-"+i+"-"+j ); numberCell.css('background-color',getNumberBgColor( num ) ); numberCell.css('color',getNumberColor( num ) ); numberCell.text( num ); numberCell.animate({ width:"100px", height:"100px", top:getPosTop( i, j ), left:getPosLeft( i, j ), },50); } function showMoveAnimation ( fromx, fromy, tox, toy ){ var numberCell = $( '#number-cell-'+fromx+'-'+fromy ); numberCell.animate({ top: getPosTop( tox, toy), left: getPosLeft( tox, toy) },200); } function updateScore( score ){ $("#score").text( score ); }
function getPosTop( i , j ){ return 20+120*i; } function getPosLeft( i , j ){ return 20+120*j; } function getNumberBgColor(number){ switch( number ){ case 2: return "#eee4da"; break; case 4: return "#ede0c8"; break; case 8: return "#f2b179"; break; case 16: return "#f59563"; break; case 32: return "#f67c5f"; break; case 64: return "#f65e3b"; break; case 128: return "#edcf72"; break; case 256: return "#edcc61"; break; case 512: return "#9c0"; break; case 1024: return "#33b5e5"; break; case 2048: return "#09c"; break; case 4096: return "#a6c"; break; case 8192: return "#93c"; break; } return "black"; } function getNumberColor( number ){ if( number <= 4 ){ return "#776e65"; } return "white"; } function noSpace( board ){ for(var i = 0 ; i < 4 ; i ++ ) for(var j = 0 ; j < 4 ; j ++ ) if( board[i][j] == 0 ) return false; return true; } function canMoveLeft( board ){ for(var i = 0 ; i < 4 ; i ++ ) for(var j = 1 ; j < 4 ; j ++ ) if( board[i][j] != 0 ) if( board[i][j-1] == 0 || board[i][j-1] == board[i][j] ) return true; return false; } function noBlockHorizontal( row,col1,col2,board ){ for(var i = col1+1 ; i < col2 ; i ++ ) if( board[row][i] != 0 ) return false; return true; }
慕课网2048私人订制
70015 学习 · 588 问题
相似问题