8-5 代码展示:@未曾至
本节编程练习不计算学习进度,请电脑登录imooc.com操作

代码展示:@未曾至

本人介绍:

即将进入研一的学生一枚,学的是通信,前端开发是兴趣。

 

创作思路:

以张靓颖新专辑《第七感》为主题,网页的主色调定为粉色系,游戏的呈现方式由原版的文字改为图片,为配合主题,图片从2到2048的选择依次为以发行时间排序的张靓颖正式专辑或EP封面。网页左侧为张靓颖音乐专辑发行过程回顾,与游戏主界面遥相呼应。通过这样概念化的主题呈现,2048游戏变得更有视觉冲击性和娱乐性,用户体验得到改善。

任务

与原版2048比你作品的优势是什么?

注:考虑到手机屏幕尺寸较小,不适合图片式的主体化呈现,故以上设计为PC版设计。

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
  3. <head>
  4. <title>2048-张靓颖版</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <meta name="description" content="My Own 2048" />
  7. <link rel="stylesheet" href="2048style.css" />
  8.  
  9. <script type="text/javascript" src="http://libs.baidu.com/jquery/1.9.0/jquery.min.js"></script>
  10. <script type="text/javascript" src="support2048.js"></script>
  11. <script type="text/javascript" src="showanimation2048.js"></script>
  12. <script type="text/javascript" src="main2048.js"></script>
  13.  
  14.  
  15.  
  16. </head>
  17. <body>
  18. <header>
  19. <div class="cdinf">
  20. <p>2006.1.12 《Jane.爱》</p>
  21. <p>2006.8.7 《我爱邓丽君》</p>
  22. <p>2006.10.11 《The One》</p>
  23. <p>2007.8.2 《Update》</p>
  24. <p>2006.12.8 《Dear Jane》</p>
  25. <p>2009.1.16 《张靓颖@音乐》</p>
  26. <p>2010.2.2 《我相信》</p>
  27. <p>2011.6.1 《改变》</p>
  28. <p>2012.6.8 《倾听》</p>
  29. <p>2013.6.25 《感谢》</p>
  30. <p>2014.7.21 《第七感》</p>
  31. <p>To Be Continue...</p>
  32. </div>
  33. <div class="mask"></div>
  34. <div class="success">
  35. <p>恭喜</p>
  36. <p>成功解锁第七感!</p>
  37. </div>
  38. <div class="fail">
  39. <p>加油,亲</p>
  40. <p>前方就有第七感!</p>
  41. </div>
  42. <h1>2048-张靓颖版</h1>
  43. <div class="inf">
  44. <a href="javascript:newGame();" class="newGameButton">新游戏</a>
  45. <p class="newGameButton myscore">得分 : <span id="score">0</span></p>
  46. </div>
  47. </header>
  48.  
  49. <div id="grid-container">
  50. <div class="grid-cell" id="grid-cell-0-0"></div>
  51. <div class="grid-cell" id="grid-cell-0-1"></div>
  52. <div class="grid-cell" id="grid-cell-0-2"></div>
  53. <div class="grid-cell" id="grid-cell-0-3"></div>
  54.  
  55. <div class="grid-cell" id="grid-cell-1-0"></div>
  56. <div class="grid-cell" id="grid-cell-1-1"></div>
  57. <div class="grid-cell" id="grid-cell-1-2"></div>
  58. <div class="grid-cell" id="grid-cell-1-3"></div>
  59.  
  60. <div class="grid-cell" id="grid-cell-2-0"></div>
  61. <div class="grid-cell" id="grid-cell-2-1"></div>
  62. <div class="grid-cell" id="grid-cell-2-2"></div>
  63. <div class="grid-cell" id="grid-cell-2-3"></div>
  64.  
  65. <div class="grid-cell" id="grid-cell-3-0"></div>
  66. <div class="grid-cell" id="grid-cell-3-1"></div>
  67. <div class="grid-cell" id="grid-cell-3-2"></div>
  68. <div class="grid-cell" id="grid-cell-3-3"></div>
  69. </div>
  70. </body>
  71. </html>
  1. body{
  2. background: url(http://img.mukewang.com/53f314300001f03519201200.jpg) 170px -50px;
  3. }
  4.  
  5. .cdinf{
  6. position: absolute;
  7. top: 125px;
  8. left: 110px;
  9. font: 14px '微软雅黑';
  10. color: white;
  11. text-align: left;
  12. z-index: 1;
  13. }
  14.  
  15. .mask{
  16. height: 500px;
  17. width: 500px;
  18. top: 131px;
  19. margin: 0 auto;
  20. background-color: black;
  21. opacity: 0.5;
  22. filter:alpha(opacity=50);
  23. display: none;
  24. position: absolute;
  25. z-index: 100;
  26. border-radius: 10px;
  27. }
  28.  
  29. .success{
  30. position: absolute;
  31. top: 255px;
  32. left: 519px;
  33. z-index: 101;
  34. display: none;
  35. font: bold 40px '微软雅黑';
  36. color:#FF9999;
  37. }
  38.  
  39. .fail{
  40. position: absolute;
  41. top: 255px;
  42. left: 519px;
  43. z-index: 101;
  44. display: none;
  45. font: bold 40px '微软雅黑';
  46. color:pink;
  47. }
  48.  
  49. header{
  50. display: block;
  51. margin: 0 auto;
  52. width: 500px;
  53. text-align: center;
  54. }
  55.  
  56. header h1{
  57. font-family: '微软雅黑';
  58. font-size: 35px;
  59. font-weight: bold;
  60. color: #FF9999;
  61. margin: 20px auto 10px auto;
  62. }
  63.  
  64. .inf{
  65. margin: 0 auto;
  66. }
  67.  
  68. header .newGameButton{
  69. display: inline-block;
  70. width: 100px;
  71. padding: 7px 8px 8px 8px;
  72. background-color: pink;
  73.  
  74. font: 15px '微软雅黑';
  75. color: white;
  76.  
  77. border-radius: 10px;
  78.  
  79. text-decoration: none;
  80. margin-right: 10px;
  81. }
  82.  
  83. .myscore{
  84. margin-top: 0px;
  85. margin-bottom: 0px;
  86. }
  87.  
  88. header .newGameButton:hover{
  89. background-color: #FF9999;
  90. }
  91.  
  92. #grid-container{
  93. width: 460px;
  94. height: 460px;
  95. padding: 20px;
  96.  
  97. margin: 20px auto 0 auto;
  98. background-color: pink;
  99. border-radius: 10px;
  100. position: relative;
  101. }
  102.  
  103. .grid-cell{
  104. width: 100px;
  105. height: 100px;
  106. border-radius: 6px;
  107. background-color: #FFCCC9;
  108.  
  109. position: absolute;
  110. }
  111.  
  112. .number-cell{
  113. border-radius: 6px;
  114.  
  115. font-family: Arial;
  116. font-size: 60px;
  117. font-weight: bold;
  118. line-height: 100px;
  119. text-align: center;
  120.  
  121. position: absolute;
  122. }
  1. var board = new Array();
  2. var score = 0;
  3. var hasConflicted = new Array();
  4.  
  5. $(document).ready(function(){
  6. newGame();
  7. });
  8.  
  9. function newGame(){
  10. //初始化操作
  11. init();
  12. //随机产生两个数字
  13. generateOneNumber();
  14. generateOneNumber();
  15. }
  16.  
  17. function init(){
  18. for (var i = 0; i < 4; i++) {
  19. for (var j = 0; j < 4; j++) {
  20. var gridCell = $("#grid-cell-" + i + "-" + j);
  21. gridCell.css('top',getPosTop(i,j));
  22. gridCell.css('left',getPosLeft(i,j));
  23. }
  24. }
  25.  
  26. for (var i = 0; i < 4; i++) {
  27. board[i] = new Array();
  28. hasConflicted[i] = new Array();
  29. for(var j = 0; j < 4; j++ ){
  30. board[i][j] = 0;
  31. hasConflicted[i][j] = false;
  32. }
  33. }
  34.  
  35. updateBoardView();
  36. score = 0;
  37. updateScore( score );
  38. $('.mask').css('display','none');
  39. $('.fail').css('display','none');
  40. $('.success').css('display','none');
  41. }
  42.  
  43. function updateBoardView(){
  44. $(".number-cell").remove();
  45. for(var i = 0; i < 4; i++){
  46. for(var j = 0; j < 4; j++){
  47. $("#grid-container").append( '<div class="number-cell" id="number-cell-'+i+'-'+j+'"></div>' );//这里的append及里面的引号使用值得注意
  48. var theNumberCell = $('#number-cell-'+i+'-'+j);
  49.  
  50. if(board[i][j] == 0){
  51. theNumberCell.css('width','0px');
  52. theNumberCell.css('height','0px');
  53. theNumberCell.css('top',getPosTop(i,j)+50);
  54. theNumberCell.css('left',getPosLeft(i,j)+50);
  55. }
  56. else{
  57. theNumberCell.css('width','100px');
  58. theNumberCell.css('height','100px');
  59. theNumberCell.css('top',getPosTop(i,j));
  60. theNumberCell.css('left',getPosLeft(i,j));
  61. theNumberCell.css('background-image',getNumberBackgroundColor(board[i][j]));
  62. theNumberCell.css('color',getNumberColor(board[i][j]));
  63. //theNumberCell.text(board[i][j]);
  64. }
  65.  
  66. hasConflicted[i][j] = false;
  67. }
  68. }
  69. }
  70.  
  71. function generateOneNumber(){
  72. if( noSpace( board ) ){
  73. return false;
  74. }
  75.  
  76. //随机一个位置
  77. var randx = parseInt(Math.floor(Math.random()*4));
  78. var randy = parseInt(Math.floor(Math.random()*4));
  79.  
  80. var times = 0;
  81.  
  82. while( times < 50 ){
  83. if(board[randx][randy]==0){
  84. break;
  85. }
  86. randx = parseInt(Math.floor(Math.random()*4));
  87. randy = parseInt(Math.floor(Math.random()*4));
  88. times++;
  89. }
  90.  
  91. if( times == 50 ){
  92. for( var i = 0; i < 4; i++){
  93. for( var j = 0; j < 4; j++){
  94. if( board[i][j] == 0 ){
  95. randx = i;
  96. randy = j;
  97. break;
  98. }
  99. }
  100. }
  101. }
  102.  
  103. //随机一个数字
  104. var randNumber = Math.random() < 0.5 ? 2 : 4;
  105. //在随机位置上显示随机数字
  106. board[randx][randy] = randNumber;
  107. showNumberWithAnimation(randx,randy,randNumber);
  108.  
  109. return true;
  110. }
  111.  
  112. $(document).keydown( function(event){
  113. switch(event.keyCode){ //注意大小写,keycode中C是大写
  114. case 37://left
  115. if( moveLeft() ){
  116. setTimeout("generateOneNumber()",210);
  117. setTimeout("isGameOver()",300);
  118. }
  119. break;
  120. case 38://up
  121. if( moveUp() ){
  122. setTimeout("generateOneNumber()",210);
  123. setTimeout("isGameOver()",300);
  124. }
  125. break;
  126. case 39://right
  127. if( moveRight() ){
  128. setTimeout("generateOneNumber()",210);
  129. setTimeout("isGameOver()",300);
  130. }
  131. break;
  132. case 40://down
  133. if( moveDown() ){
  134. setTimeout("generateOneNumber()",210);
  135. setTimeout("isGameOver()",300);
  136. }
  137. break;
  138. default:
  139. break;
  140. }
  141. });
  142.  
  143. function isGameOver(){
  144. if( noSpace(board) && noMove(board) ){
  145. gameOver();
  146. }
  147.  
  148. for(var i = 0; i < 4; i++){
  149. for(var j = 0; j < 4; j++){
  150. if( board[i][j] == 2048 ){
  151. youWin();
  152. }
  153. }
  154. }
  155. }
  156.  
  157. function gameOver(){
  158. $('.mask').show();
  159. $('.fail').show();
  160. }
  161.  
  162. function youWin(){
  163. $('.mask').show();
  164. $('.success').show();
  165. }
  166.  
  167. function moveLeft(){
  168. if(!canMoveLeft(board)){
  169. return false;
  170. }
  171.  
  172. //move left
  173. for(var i = 0;i < 4;i++){
  174. for(var j = 1;j < 4;j++){
  175. if( board[i][j] != 0 ){
  176. for(var k = 0; k < j; k++){
  177. if( board[i][k] == 0 && noBlockx(i,k,j,board) ){
  178. //move left
  179. showMoveAnimation(i,j,i,k); //注意参数的顺序
  180. board[i][k] = board[i][j];
  181. board[i][j] = 0;
  182. continue;
  183. }
  184. else if( board[i][k] == board [i][j] && noBlockx(i,k,j,board) && !hasConflicted[i][k] ){
  185. //move left
  186. showMoveAnimation(i,j,i,k);
  187. //add
  188. board[i][k] += board[i][j];
  189. board[i][j] = 0;
  190. score += board[i][k];
  191. updateScore( score );
  192.  
  193. hasConflicted[i][k] = true;
  194. continue;
  195. }
  196. }
  197. }
  198. }
  199. }
  200.  
  201. setTimeout("updateBoardView()",200);
  202. return true;
  203. }
  204.  
  205. function moveUp(){
  206. if(!canMoveUp(board)){
  207. return false;
  208. }
  209.  
  210. //move up
  211. for(var i = 1;i < 4;i++){
  212. for(var j = 0;j < 4;j++){
  213. if( board[i][j] != 0 ){
  214. for(var k = 0; k < i; k++){
  215. if( board[k][j] == 0 && noBlocky(j,k,i,board) ){
  216. //move left
  217. showMoveAnimation(i,j,k,j); //注意参数的顺序
  218. board[k][j] = board[i][j];
  219. board[i][j] = 0;
  220. continue;
  221. }
  222. else if( board[k][j] == board [i][j] && noBlocky(j,k,i,board) && !hasConflicted[k][j] ){
  223. //move left
  224. showMoveAnimation(i,j,k,j);
  225. //add
  226. board[k][j] += board[i][j];
  227. board[i][j] = 0;
  228. score += board[k][j];
  229. updateScore( score );
  230.  
  231. hasConflicted[k][j] = true;
  232. continue;
  233. }
  234. }
  235. }
  236. }
  237. }
  238.  
  239. setTimeout("updateBoardView()",200);
  240. return true;
  241. }
  242.  
  243. function moveRight(){
  244. if(!canMoveRight(board)){
  245. return false;
  246. }
  247.  
  248. //move left
  249. for(var i = 0;i < 4;i++){
  250. for(var j = 2;j >= 0; j--){
  251. if( board[i][j] != 0 ){
  252. for(var k = 3; k > j; k--){
  253. if( board[i][k] == 0 && noBlockx(i,j,k,board) ){
  254. //move left
  255. showMoveAnimation(i,j,i,k); //注意参数的顺序
  256. board[i][k] = board[i][j];
  257. board[i][j] = 0;
  258. continue;
  259. }
  260. else if( board[i][k] == board [i][j] && noBlockx(i,j,k,board) && !hasConflicted[i][k] ){
  261. //move left
  262. showMoveAnimation(i,j,i,k);
  263. //add
  264. board[i][k] += board[i][j];
  265. board[i][j] = 0;
  266. score += board[i][k];
  267. updateScore( score );
  268.  
  269. hasConflicted[i][k] = true;
  270. continue;
  271. }
  272. }
  273. }
  274. }
  275. }
  276.  
  277. setTimeout("updateBoardView()",200);
  278. return true;
  279. }
  280.  
  281. function moveDown(){
  282. if(!canMoveDown(board)){
  283. return false;
  284. }
  285.  
  286. //move up
  287. for(var i = 2;i >= 0;i--){
  288. for(var j = 0;j < 4;j++){
  289. if( board[i][j] != 0 ){
  290. for(var k = 3; k > i; k--){
  291. if( board[k][j] == 0 && noBlocky(j,i,k,board) ){
  292. //move left
  293. showMoveAnimation(i,j,k,j); //注意参数的顺序
  294. board[k][j] = board[i][j];
  295. board[i][j] = 0;
  296. continue;
  297. }
  298. else if( board[k][j] == board [i][j] && noBlocky(j,i,k,board) && !hasConflicted[k][j] ){
  299. //move left
  300. showMoveAnimation(i,j,k,j);
  301. //add
  302. board[k][j] += board[i][j];
  303. board[i][j] = 0;
  304. score += board[k][j];
  305. updateScore( score );
  306.  
  307. hasConflicted[k][j] = true;
  308. continue;
  309. }
  310. }
  311. }
  312. }
  313. }
  314.  
  315. setTimeout("updateBoardView()",200);
  316. return true;
  317. }
  318.  
  319. function noMove( board ){
  320. if( canMoveDown( board ) ||
  321. canMoveRight( board )||
  322. canMoveUp( board ) ||
  323. canMoveLeft( board ) ){
  324. return false;
  325. }
  326.  
  327. return true;
  328. }
  1. function showNumberWithAnimation( i, j, randNumber){
  2. var numberCell = $('#number-cell-'+i+'-'+j);
  3.  
  4. numberCell.css('background-image',getNumberBackgroundColor(randNumber));
  5. numberCell.css('color',getNumberColor(randNumber));
  6. //numberCell.text(randNumber);
  7.  
  8. numberCell.animate({
  9. width:"100px",
  10. height:"100px",
  11. top:getPosTop(i,j),
  12. left:getPosLeft(i,j)
  13. },150);
  14. }
  15.  
  16. function showMoveAnimation(fromx,fromy,tox,toy){
  17. var numberCell = $('#number-cell-'+fromx+'-'+fromy);
  18. numberCell.animate({
  19. top:getPosTop(tox,toy),
  20. left:getPosLeft(tox,toy)
  21. },300);
  22. }
  23.  
  24. function updateScore( score ){
  25. $('#score').text( score );
  26. }
  1. function getPosTop(i,j){
  2. return 20 + i * 120;
  3. }
  4.  
  5. function getPosLeft(i,j){
  6. return 20 + j * 120;
  7. }
  8.  
  9. function getNumberBackgroundColor( number ){
  10. switch( number ){
  11. case 2:return "url(http://www.imooc.com/activity/2048/117139/pics/1.jpg)";break;
  12. case 4:return "url(http://www.imooc.com/activity/2048/117139/pics/2.jpg)";break;
  13. case 8:return "url(http://www.imooc.com/activity/2048/117139/pics/3.jpg)";break;
  14. case 16:return "url(http://www.imooc.com/activity/2048/117139/pics/4.jpg)";break;
  15. case 32:return "url(http://www.imooc.com/activity/2048/117139/pics/5.jpg)";break;
  16. case 64:return "url(http://www.imooc.com/activity/2048/117139/pics/6.jpg)";break;
  17. case 128:return "url(http://www.imooc.com/activity/2048/117139/pics/7.jpg)";break;
  18. case 256:return "url(http://www.imooc.com/activity/2048/117139/pics/8.jpg)";break;
  19. case 512:return "url(http://www.imooc.com/activity/2048/117139/pics/9.jpg)";break;
  20. case 1024:return "url(http://www.imooc.com/activity/2048/117139/pics/10.jpg)";break;
  21. case 2048:return "url(http://www.imooc.com/activity/2048/117139/pics/11.jpg)";break;
  22. }
  23. }
  24.  
  25. function getNumberColor( number ){
  26. if( number <= 4 ){
  27. return "#776e65";
  28. }
  29.  
  30. return "white";
  31. }
  32.  
  33. function noSpace( board ){
  34. for(var i = 0; i < 4; i++){
  35. for(var j = 0; j < 4; j++){
  36. if(board[i][j] == 0){
  37. return false;
  38. }
  39. }
  40. }
  41. return true;
  42. }
  43.  
  44. function canMoveLeft( board ){
  45. for(var i = 0; i < 4; i++){
  46. for(var j = 1; j < 4; j++){
  47. if( board[i][j] != 0){
  48. if( board[i][j-1]==0 || board[i][j-1]==board[i][j] ){
  49. return true;
  50. }
  51. }
  52. }
  53. }
  54. return false;
  55. }
  56.  
  57. function canMoveUp( board ){
  58. for(var i = 1; i < 4; i++){
  59. for(var j = 0; j < 4; j++){
  60. if( board[i][j] != 0){
  61. if( board[i-1][j]==0 || board[i-1][j]==board[i][j] ){
  62. return true;
  63. }
  64. }
  65. }
  66. }
  67. return false;
  68. }
  69.  
  70. function canMoveRight( board ){
  71. for(var i = 0; i < 4; i++){
  72. for(var j = 0; j < 3; 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. }
  78. }
  79. }
  80. return false;
  81. }
  82.  
  83. function canMoveDown( board ){
  84. for(var i = 0; i < 3; i++){
  85. for(var j = 0; j < 4; j++){
  86. if( board[i][j] != 0){
  87. if( board[i+1][j]==0 || board[i+1][j]==board[i][j] ){
  88. return true;
  89. }
  90. }
  91. }
  92. }
  93. return false;
  94. }
  95.  
  96. function noBlockx(row,col1,col2,board){
  97. for(var i = col1 + 1; i < col2; i++){
  98. if( board[row][i] != 0 ){
  99. return false;
  100. }
  101. }
  102. return true;
  103. }
  104.  
  105. function noBlocky(col,row1,row2,board){
  106. for(var i = row1 + 1; i < row2; i++){
  107. if( board[i][col] != 0 ){
  108. return false;
  109. }
  110. }
  111. return true;
  112. }
下一节