8-3 代码展示:@一杯热茶
本节编程练习不计算学习进度,请电脑登录imooc.com操作

代码展示:@一杯热茶

本人介绍:一杯热茶,网站前端, 18岁。

 

创作思路:思路上基本上是模仿案例,按视频里来的

任务

和原版相比的优势:原版点击重新按钮后游戏的数据并没有立即清零,只有当再次累加数据后才会清零,我把重新开始的按钮添加了一个立即清零。 

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <meta name="viewport"
  6. content="width=device-width,height=device-height,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
  7. <title>2048</title>
  8. <link href="style.css" type="text/css" rel="stylesheet" />
  9. <script src="http://libs.baidu.com/jquery/1.9.0/jquery.min.js" type="text/javascript"></script>
  10. <script src="Support.js"></script>
  11. <script src="animation.js" type="text/javascript"></script>
  12. <script src="main.js" type="text/javascript"></script>
  13. </head>
  14.  
  15. <body>
  16. <header>
  17. <h1>私人 2048</h1>
  18. <a href="javascript:newgame();" id="bot">New Game</a>
  19. <p>score:<span id="score">0</span></p>
  20. </header>
  21.  
  22. <div id="main">
  23. <div class="main-box" id="box-0-0"></div>
  24. <div class="main-box" id="box-0-1"></div>
  25. <div class="main-box" id="box-0-2"></div>
  26. <div class="main-box" id="box-0-3"></div>
  27.  
  28. <div class="main-box" id="box-1-0"></div>
  29. <div class="main-box" id="box-1-1"></div>
  30. <div class="main-box" id="box-1-2"></div>
  31. <div class="main-box" id="box-1-3"></div>
  32.  
  33. <div class="main-box" id="box-2-0"></div>
  34. <div class="main-box" id="box-2-1"></div>
  35. <div class="main-box" id="box-2-2"></div>
  36. <div class="main-box" id="box-2-3"></div>
  37.  
  38. <div class="main-box" id="box-3-0"></div>
  39. <div class="main-box" id="box-3-1"></div>
  40. <div class="main-box" id="box-3-2"></div>
  41. <div class="main-box" id="box-3-3"></div>
  42. </div>
  43. <!-- <div class="over">
  44. <div class="over-frame">GAMEMOVE!</div>
  45. </div>-->
  46. </body>
  47. </html>
  1. *{ padding:0; margin:0;}
  2. header{ display:block; width:100%; margin:0 auto; text-align:center;}
  3. header h1{ font-family:Arial; font-size:40px; font-weight:bold;}
  4. header #bot{ display:block; margin:20px auto; width:100px; padding:10px; background:#8f7a66; font-family:Arial; color:#fff; border-radius:10px; text-decoration:none;}
  5. header #bot:hover{ background:#9f8b77;}
  6. header p{ font-family:Arial; font-size:25px; margin:25px auto;}
  7. #main{ width:460px;height:460px;padding:20px; margin:50px auto; background:#bbada0; border-radius:10px; position:relative;}
  8. .main-box{ width:100px; height:100px; border-radius:6px; background:#ccc0b3; position:absolute;}
  9. .number-cell{ border-radius:6px; font-family:Arial; font-weight:bold; font-size:30px; line-height:100px; text-align:center; position:absolute; color:#000;}
  10. .over{ width:100%; height:100%; background:rgba(204,204,204,0.5); position:absolute; top:0; left:0; display:none;}
  11. .over-frame{ width:500px; height:200px; margin:0 auto; margin-top:20%; font-size:90px; text-align:center; line-height:200px; cursor:pointer;}
  1. function showNumberWithAnimation(i,j,randNumber){
  2.  
  3. var numberCell=$('#number-cell-'+i+"-"+j);
  4.  
  5. numberCell.css("background-color",getNumberBackgroundColor(randNumber))
  6. numberCell.css("color",getNumberColor(randNumber))
  7. numberCell.text(getNumberText( randNumber ))
  8.  
  9. numberCell.animate({
  10. width:cellSideLength,
  11. height:cellSideLength,
  12. top:getPosTop(i,j),
  13. left:getPosLeft(i,j)
  14. },50);
  15. }
  16.  
  17. function showMoveAnimation( fromx , fromy , tox, toy ){
  18. var numberCell = $('#number-cell-' + fromx + '-' + fromy );
  19. numberCell.animate({
  20. top:getPosTop(tox,toy),
  21. left:getPosLeft(tox,toy)
  22. },200)
  23. }
  24.  
  25. function updateScore(score){
  26. $("#score").text(score)
  27. }
  28. $(function(){
  29. $("#bot").click(function(){
  30. updateScore(0)
  31. })
  32. })
  1. var board=new Array();
  2. var score=0;
  3. var hasConflicted=new Array();
  4.  
  5. var startx=0;
  6. var starty=0;
  7. var endx=0;
  8. var endy=0;
  9.  
  10. $(document).ready(function(){
  11. prepareForMobile();
  12. newgame();
  13. score=0;
  14. updateScore(score);
  15. })
  16.  
  17. function prepareForMobile(){
  18.  
  19. if(documentWidth>500){
  20. gridContainerWidth=500;
  21. cellSpace=20;
  22. cellSideLength=100;
  23. }
  24.  
  25. $("#main").css("width",gridContainerWidth-2*cellSpace);
  26. $("#main").css("height",gridContainerWidth-2*cellSpace);
  27. $("#main").css("padding",cellSpace);
  28. $("#main").css("border-radius",0.01*gridContainerWidth);
  29.  
  30. $(".main-box").css("width",cellSideLength);
  31. $(".main-box").css("height",cellSideLength);
  32. $(".main-box").css("border-radius",0.01*cellSideLength);
  33. }
  34.  
  35. function newgame(){
  36. //初始化棋盘格
  37. init();
  38. //随机生成两个数字
  39. generateOneNumber();
  40. generateOneNumber();
  41. }
  42. function init(){
  43. for(var i=0;i<4;i++){
  44. for(var j=0;j<4;j++){
  45. var gridCell=$("#box-"+i+"-"+j)
  46. gridCell.css("top",getPosTop(i,j))
  47. gridCell.css("left",getPosLeft(i,j))
  48. }
  49. }
  50.  
  51. for(var i=0;i<4;i++){
  52. board[i]=new Array();
  53. hasConflicted[i]=new Array();
  54. for(var j=0;j<4;j++){
  55. board[i][j]=0;
  56. hasConflicted[i][j]=false;
  57. }
  58. }
  59. updateBoardView();
  60.  
  61. score=0;
  62. }
  63. function updateBoardView(){
  64. $(".number-cell").remove();
  65. for(var i=0; i<4;i++){
  66. for(var j=0;j<4;j++){
  67. $("#main").append('<div class="number-cell" id="number-cell-'+i+'-'+j+'"></div>')
  68. var theNumberCell=$('#number-cell-'+i+'-'+j)
  69.  
  70. if(board[i][j]==0){
  71. theNumberCell.css({"width":"0","height":"0"})
  72. theNumberCell.css("top",getPosTop(i,j)+cellSideLength/2)
  73. theNumberCell.css("left",getPosLeft(i,j)+cellSideLength/2)
  74. }else{
  75. theNumberCell.css('width',cellSideLength);
  76. theNumberCell.css('height',cellSideLength);
  77. theNumberCell.css('top',getPosTop(i,j));
  78. theNumberCell.css('left',getPosLeft(i,j));
  79. theNumberCell.css('background-color',getNumberBackgroundColor( board[i][j] ) );
  80. theNumberCell.css('color',getNumberColor( board[i][j] ) );
  81.  
  82. theNumberCell.text( getNumberText( board[i][j] ) );
  83.  
  84. }
  85. hasConflicted[i][j]=false;
  86. }
  87. $(".number-cell").css("line-height",cellSideLength+"px");
  88. $(".number-cell").css("font-size",0.3*cellSideLength+"px");
  89. }
  90. }
  91. function generateOneNumber(){
  92. if(nospace(board))
  93. return false;
  94. //随机一个位置
  95. var randx=parseInt(Math.floor(Math.random()*4));
  96. var randy=parseInt(Math.floor(Math.random()*4))
  97.  
  98. var times=0;
  99. while(times<50){
  100. if(board[randx][randy]==0)
  101. break;
  102.  
  103. randx=parseInt(Math.floor(Math.random()*4));
  104. randy=parseInt(Math.floor(Math.random()*4))
  105.  
  106. times++;
  107. }
  108. if(times==50){
  109. for(var i=0;i<4;i++)
  110. for(var j=0; j<4; j++){
  111. if(doard[i][j]==0){
  112. randx=i;
  113. randy=j;
  114. }
  115. }
  116. }
  117. //随机一个数字
  118. var randNumber=Math.random()<0.5? 2:4;
  119. //在随机的位置上显示数字
  120. board[randx][randy]=randNumber;
  121. showNumberWithAnimation(randx,randy,randNumber)
  122. return true;
  123.  
  124. }
  125.  
  126. $(document).keydown(function(event){
  127. switch(event.keyCode){
  128. case 37://left
  129. event.preventDefault()
  130. if(moveLeft()){
  131. setTimeout("generateOneNumber()",210);
  132. setTimeout("isgameover()",300);
  133. }
  134. break;
  135. case 38://up
  136. event.preventDefault()
  137. if(moveUp()){
  138. setTimeout("generateOneNumber()",210);
  139. setTimeout("isgameover()",300);
  140. }
  141. break;
  142. case 39://right
  143. event.preventDefault()
  144. if(moveRight()){
  145. setTimeout("generateOneNumber()",210);
  146. setTimeout("isgameover()",300);
  147. }
  148. break;
  149. case 40://down
  150. event.preventDefault()
  151. if(moveDown()){
  152. setTimeout("generateOneNumber()",210);
  153. setTimeout("isgameover()",300);
  154. }
  155. break;
  156. }
  157. })
  158.  
  159. document.addEventListener('touchstart',function(event){
  160. startx=event.touches[0].pageX;
  161. starty=event.touches[0].pageY;
  162. })
  163.  
  164. document.addEventListener('touchmove',function(event){
  165. event.preventDefault()
  166. })
  167.  
  168. document.addEventListener('touchend',function(event){
  169. endx=event.changedTouches[0].pageX;
  170. endy=event.changedTouches[0].pageY;
  171.  
  172. var deltax=endx-startx;
  173. var deltay=endy-starty;
  174.  
  175. if(Math.abs(deltax)<0.3*documentWidth && Math.abs(deltay)<0.3*documentWidth)
  176. return;
  177.  
  178. //x轴进行滑动
  179. if(Math.abs(deltax)>=Math.abs(deltay)){
  180. if(deltax>0){
  181. //move right
  182. if(moveRight()){
  183. setTimeout("generateOneNumber()",210);
  184. setTimeout("isgameover()",300);
  185. }
  186. }else{
  187. //move left
  188. if(moveLeft()){
  189. setTimeout("generateOneNumber()",210);
  190. setTimeout("isgameover()",300);
  191. }
  192.  
  193. }
  194. }
  195. //y轴进行滑动
  196. else{
  197. if(deltay>0){
  198. //move down
  199. if(moveDown()){
  200. setTimeout("generateOneNumber()",210);
  201. setTimeout("isgameover()",300);
  202. }
  203. }else{
  204. //move up
  205. if(moveUp()){
  206. setTimeout("generateOneNumber()",210);
  207. setTimeout("isgameover()",300);
  208. }
  209. }
  210. }
  211. })
  212.  
  213. function isgameover(){
  214. if(nospace(board) && nomove(board)){
  215. gameover();
  216. }
  217. }
  218.  
  219.  
  220.  
  221.  
  222. function moveLeft(){
  223. if(!canMoveLeft(board))
  224. return false;
  225.  
  226. //moveLeft
  227. for(var i=0; i<4;i++)
  228. for(var j=1; j<4;j++){
  229. if(board[i][j]!=0){
  230. for(var k=0; k<j;k++){
  231. if(board[i][k]==0 && noBlockHorizontal(i,k,j,board)){
  232. //move
  233. showMoveAnimation(i,j,i,k)
  234. board[i][k]=board[i][j]
  235. board[i][j]=0;
  236. continue
  237. }else if(board[i][k]==board[i][j] && noBlockHorizontal(i,k,j,board) && !hasConflicted[i][k]){
  238. //move
  239. showMoveAnimation(i,j,i,k)
  240. //add
  241. board[i][k]+=board[i][j]
  242. board[i][j]=0;
  243. //addscore
  244. score+=board[i][k]
  245. updateScore(score)
  246.  
  247. hasConflicted[i][k]=true;
  248. continue
  249. }
  250. }
  251. }
  252. }
  253.  
  254. setTimeout("updateBoardView()",200)
  255. return true;
  256. }
  257.  
  258. function moveRight(){
  259. if(!canMoveRight(board))
  260. return false;
  261.  
  262. //moveRight
  263. for(var i=0; i<4;i++)
  264. for(var j=2; j>=0;j--){
  265. if(board[i][j]!=0){
  266. for(var k=3; k>j;k--){
  267. if(board[i][k]==0 && noBlockHorizontal(i,k,j,board)){
  268. //move
  269. showMoveAnimation(i,j,i,k)
  270. board[i][k]=board[i][j]
  271. board[i][j]=0;
  272. continue
  273. }else if(board[i][k]==board[i][j] && noBlockHorizontal(i,j,k,board) && !hasConflicted[i][k]){
  274. //move
  275. showMoveAnimation(i,j,i,k)
  276. //add
  277. board[i][k] *= 2;
  278. board[i][j] = 0;
  279.  
  280. //addscore
  281. score += board[i][k]
  282. updateScore(score)
  283.  
  284. hasConflicted[i][k]=true;
  285. continue
  286.  
  287. }
  288. }
  289. }
  290. }
  291.  
  292. setTimeout("updateBoardView()",200)
  293. return true;
  294. }
  295.  
  296.  
  297.  
  298.  
  299. function moveUp(){
  300. if( !canMoveUp( board ) )
  301. return false;
  302. //moveUp
  303. for( var j = 0 ; j < 4 ; j ++ )
  304. for( var i = 1 ; i < 4 ; i ++ ){
  305. if( board[i][j] != 0 ){
  306. for( var k = 0 ; k < i ; k ++ ){
  307. if( board[k][j] == 0 && noBlockVertical( j , k , i , board ) ){
  308. showMoveAnimation( i , j , k , j );
  309. board[k][j] = board[i][j];
  310. board[i][j] = 0;
  311. continue;
  312. }
  313. else if( board[k][j] == board[i][j] && noBlockVertical( j , k , i , board ) && !hasConflicted[k][j]){
  314. showMoveAnimation( i , j , k , j );
  315. board[k][j] *= 2;
  316. board[i][j] = 0;
  317. //addscore
  318. score+=board[k][j]
  319. updateScore(score)
  320. hasConflicted[k][j]=true;
  321. continue;
  322. }
  323. }
  324. }
  325. }
  326. setTimeout("updateBoardView()",200);
  327. return true;
  328. }
  329.  
  330. function moveDown(){
  331.  
  332. if( !canMoveDown( board ) )
  333. return false;
  334.  
  335. //moveDown
  336. for( var j = 0 ; j < 4 ; j ++ )
  337. for( var i = 2 ; i>=0 ; i -- ){
  338. if( board[i][j] != 0 ){
  339. for( var k = 3 ; k >i ; k -- ){
  340.  
  341. if( board[k][j] == 0 && noBlockVertical( j , k , i , board ) ){
  342. showMoveAnimation( i , j , k , j );
  343. board[k][j] = board[i][j];
  344. board[i][j] = 0;
  345. continue;
  346. }
  347. else if( board[k][j] == board[i][j] && noBlockVertical( j , i , k , board ) && !hasConflicted[k][j]){
  348. showMoveAnimation( i , j , k , j );
  349. board[k][j] *= 2;
  350. board[i][j] = 0;
  351.  
  352. //addscore
  353. score+=board[k][j]
  354. updateScore(score)
  355.  
  356. hasConflicted[k][j] = true;
  357.  
  358. continue;
  359. }
  360. }
  361. }
  362. }
  363.  
  364. setTimeout("updateBoardView()",200);
  365. return true;
  366. }
  367.  
  368. function gameover(){
  369. $(".over").show();
  370. $(document).click(function(){
  371. $(".over").hide()
  372. })
  373. }
  374. $("#bot").click(function(){
  375. score=0;
  376. updateScore(score)
  377. })
  1. documentWidth=window.screen.availWidth;
  2. gridContainerWidth=0.92*documentWidth;
  3. cellSideLength=0.18*documentWidth;
  4. cellSpace=0.04*documentWidth;
  5.  
  6.  
  7.  
  8. function getPosTop(i,j){
  9. return cellSpace+i*(cellSpace+cellSideLength);
  10. }
  11.  
  12. function getPosLeft(i,j){
  13. return cellSpace+j*(cellSpace+cellSideLength);
  14. }
  15.  
  16. function getNumberBackgroundColor(number){
  17. switch(number){
  18. case 2:return "#eee4da"; break ;
  19. case 4:return "#ede0c8"; break ;
  20. case 8:return "#f2b179"; break ;
  21. case 16:return "#f59563"; break;
  22. case 32:return "#f67c5f"; break;
  23. case 64:return "#f65e3b"; break;
  24. case 128:return "#edcf72"; break;
  25. case 256:return "#edcc61"; break;
  26. case 512:return "#9c0"; break;
  27. case 1024:return "#33b5e5"; break;
  28. case 2048:return "#09c"; break ;
  29. case 4096:return "#a6c"; break ;
  30. case 8192:return "#93c"; break ;
  31. }
  32. return "black"
  33. }
  34.  
  35. function getNumberText(number){
  36. switch(number){
  37. case 2:return "小白"; break;
  38. case 4:return "实习生"; break;
  39. case 8:return "程序员"; break;
  40. case 16:return "工程师"; break;
  41. case 32:return "主管"; break;
  42. case 64:return "经理"; break;
  43. case 128:return "总监"; break;
  44. case 256:return "总经理"; break;
  45. case 512:return "副总裁"; break;
  46. case 1024:return "CEO"; break;
  47. case 2048:return "总裁"; break ;
  48. case 4096:return "董事长"; break ;
  49. case 8192:return "乔布斯"; break ;
  50. }
  51. return "black";
  52. }
  53. function getNumberColor(number){
  54. if(number<=4)
  55. return "#776e65";
  56.  
  57. return "#ded0c2"
  58. }
  59.  
  60. function nospace( board ){
  61.  
  62. for( var i = 0 ; i < 4 ; i ++ )
  63. for( var j = 0 ; j < 4 ; j ++ )
  64. if( board[i][j] == 0 )
  65. return false;
  66.  
  67. return true;
  68. }
  69.  
  70. function canMoveLeft(board){
  71. for(var i=0; i<4;i++)
  72. for(var j=0; j<4;j++)
  73. if(board[i][j]!=0)
  74. if(board[i][j-1]==0 || board[i][j-1]==board[i][j])
  75. return true;
  76.  
  77. return false;
  78. }
  79.  
  80. function canMoveRight(board){
  81. for(var i=0; i<4;i++)
  82. for(var j=2; j>=0;j--)
  83. if(board[i][j]!=0)
  84. if(board[i][j+1]==0 || board[i][j+1]==board[i][j])
  85. return true;
  86.  
  87. return false;
  88. }
  89.  
  90. function canMoveUp( board ){
  91.  
  92. for( var j = 0 ; j < 4 ; j ++ )
  93. for( var i = 1 ; i < 4 ; i ++ )
  94. if( board[i][j] != 0 )
  95. if( board[i-1][j] == 0 || board[i-1][j] == board[i][j] )
  96. return true;
  97.  
  98. return false;
  99. }
  100.  
  101. function canMoveDown( board ){
  102.  
  103. for( var j = 0 ; j < 4 ; j ++ )
  104. for( var i = 2 ; i >= 0 ; i -- )
  105. if( board[i][j] != 0 )
  106. if( board[i+1][j] == 0 || board[i+1][j] == board[i][j] )
  107. return true;
  108.  
  109. return false;
  110. }
  111.  
  112.  
  113.  
  114. function noBlockHorizontal(row,coll,col2,board){
  115. for(var i=coll+1;i<col2;i++)
  116. if(board[row][i]!=0)
  117. return false;
  118. return true;
  119.  
  120. }
  121.  
  122. function noBlockVertical( col , row1 , row2 , board ){
  123. for( var i = row1 + 1 ; i < row2 ; i ++ )
  124. if( board[i][col] != 0 )
  125. return false;
  126. return true;
  127. }
  128.  
  129. function nomove(board){
  130. if(canMoveLeft(board)||
  131. canMoveRight(board)||
  132. canMoveUp(board)||
  133. canMoveDown(board))
  134. return false;
  135.  
  136. return true;
  137. }
下一节