JavaScript 2048 游戏实例代码(简单易懂)
网络编程 2021-07-04 21:04www.168986.cn编程入门
使用javascript制作的2048小游戏非常好玩,有意思,狼蚁网站SEO优化给大家分享使用javascript代码制作的2048游戏,需要的朋友参考下吧
废话不多说了,直接给大家贴代码了,觉得很满意直接拿去。
<!DOCTYPE html PUBLIC "-//WC//DTD XHTML . Transitional//EN" "http://.w./TR/xhtml/DTD/xhtml-transitional.dtd"> <html xmlns="http://.w.//xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-" /> <title>无标题文档</title> <style> header{display:block; margin: auto; width:%; text-align:center;} header h{font-size:px; font-family:Arial, Helvetica, sans-serif; font-weight:bold;} header #newgamebutton{display:block; margin:px auto; width:px; padding:px px; background-color:#fa; font-family:Arial; color:white; border-radius:px; text-decoration:none;} header #newgamebutton:hover{background-color:#fb;} header p{font-family:Arial; font-size:px; margin:px auto;} #grid-container{width:px; height:px; padding:px; margin:px auto; background-color:#bbada; border-radius:px; position:relative;} .grid-cell{width:px; height:px; border-radius:px; color:white; background-color:#cb; position:absolute; font-family:Arial; font-weight:bold; font-size:px; line-height:px; text-align:center;} .gameover{display:block; margin: auto; width:px; text-align:center; vertical-align:middle; position:absolute;} .gameover p { font-family: Arial; font-size: px; color: white; margin: px auto; margin-: px; } .gameover span { font-family: Arial; font-size: px; color: white; margin: px auto; } #restartgamebutton { display: block; margin: px auto; width: px; padding: px px; background-color: #fa; font-family: Arial; font-size: px; color: white; border-radius: px; text-decoration: none; } #restartgamebutton:hover { background-color: #fb; } #showGameover{width:px; padding:px px; background-color:#fa; font-family:Arial; color:white; border-radius:px; text-decoration:none; font-size:px; position:absolute;} </style> <script> window.onload = function (){ //newgame(); var showScore = document.getElementById('score'); score = ; init(); generateOneNumber(); generateOneNumber(); window.onkeydown = function (e){ var e = e || window.event; switch(e.keyCode){ case : if(canMoveLeft(board)){ moveLeft(); generateOneNumber(); isgameover(); } break; case : if(canMoveUp(board)){ moveUp(); generateOneNumber(); isgameover(); } break; case : if(canMoveRight(board)){ moveRight(); generateOneNumber(); isgameover(); } break; case : if(canMoveDown(board)){ moveDown(); generateOneNumber(); isgameover(); } break; default : break; } }; }; function generateOneNumber() { if(!nospace(board)){ var randx = parseInt(Math.floor(Math.random() )); var randy = parseInt(Math.floor(Math.random() )); while (true) { if (board[randx][randy] == ) { break; } var randx = parseInt(Math.floor(Math.random() )); var randy = parseInt(Math.floor(Math.random() )); } var randNumber = Math.random() < . ? : ; board[randx][randy] = randNumber; var gridCell = document.getElementById('grid-cell-'+randx+'-'+randy); gridCell.innerHTML = randNumber; // } else { gameover(); } } function newgame() { window.location.reload(); } var board = new Array(); function init(){ for(var i=;i<;i++){ board[i] = new Array(); for(var j=;j<;j++){ board[i][j] = ; var gridCell = document.getElementById('grid-cell-'+i+'-'+j); gridCell.style. = (+i)+'px'; gridCell.style.left = (+j)+'px'; } } } function getNumberBackgroundColor(number) { switch (number) { case :return "#eeeda";break; case :return "#edec";break; case :return "#fb";break; case :return "#f";break; case :return "#fcf";break; case :return "#feb";break; case :return "#edcf";break; case :return "#ed";break; case :return "#c";break; case :return "#be";break; case :return "#c";break; case :return "#ac";break; case :return "#c";break; } } function getNumberColor(number) { if (number <= ) { return "#e" } return "white"; } function nospace(board) { for (var i = ; i < ; i++) { for (var j = ; j < ; j++) { if (board[i][j] == ) { return false; } } } return true; } function canMoveLeft(board) { for (var i = ; i < ; i++) { for (var j = ; j < ; j++) { if (board[i][j] != ) { if (board[i][j - ] == || board[i][j - ] == board[i][j]) {return true; } } } } return false; } function moveLeft() { for (var i = ; i < ; i++) { for (var j = ; j < ; j++) { if (board[i][j] != ) { for (var k = j-; k > -; k--) { if(board[i][k] == || board[i][k] == board[i][j]){ board[i][k] = board[i][k] + board[i][j]; board[i][j] = ; var gridCell = document.getElementById('grid-cell-'+i+'-'+j); gridCell.innerHTML = ''; var gridCellk = document.getElementById('grid-cell-'+i+'-'+k); gridCellk.innerHTML = board[i][k]; gridCellk.style.backgroundColor = getNumberBackgroundColor(board[i][k]); gridCell.style.backgroundColor = '#cb'; gridCellk.style.color = getNumberColor(board[i][k]); gridCell.style.color = 'white'; score += board[i][k]; var showScore = document.getElementById('score'); showScore.innerHTML = score; j = k; // } else { break; //跳出 var k 的for循环 } } } } } } function canMoveUp(board) { for (var j = ; j < ; j++) { for (var i = ; i < ; i++) { if (board[i][j] != ) { if (board[i - ][j] == || board[i - ][j] == board[i][j]) {return true; } } } } return false; } function moveUp() { for (var j = ; j < ; j++) { for (var i = ; i < ; i++) { if (board[i][j] != ) { for (var k = i-; k > -; k--) { if(board[k][j] == || board[k][j] == board[i][j]){ board[k][j] = board[k][j] + board[i][j]; board[i][j] = ; var gridCell = document.getElementById('grid-cell-'+i+'-'+j); gridCell.innerHTML = ''; var gridCellk = document.getElementById('grid-cell-'+k+'-'+j); gridCellk.innerHTML = board[k][j]; gridCellk.style.backgroundColor = getNumberBackgroundColor(board[k][j]); gridCell.style.backgroundColor = '#cb'; gridCellk.style.color = getNumberColor(board[i][k]); gridCell.style.color = 'white'; score += board[k][j]; var showScore = document.getElementById('score'); showScore.innerHTML = score; i = k; // } else { break; //跳出 var k 的for循环 } } } } } } function canMoveRight(board) { for (var i = ; i < ; i++) { for (var j = ; j > -; j--) { if (board[i][j] != ) { if (board[i][j + ] == || board[i][j + ] == board[i][j]) {return true; } } } } return false; } function moveRight() { for (var i = ; i < ; i++) { for (var j = ; j > -; j--) { if (board[i][j] != ) { for (var k = j+; k < ; k++) { if(board[i][k] == || board[i][k] == board[i][j]){ board[i][k] = board[i][k] + board[i][j]; board[i][j] = ; var gridCell = document.getElementById('grid-cell-'+i+'-'+j); gridCell.innerHTML = ''; var gridCellk = document.getElementById('grid-cell-'+i+'-'+k); gridCellk.innerHTML = board[i][k]; gridCellk.style.backgroundColor = getNumberBackgroundColor(board[i][k]); gridCell.style.backgroundColor = '#cb'; gridCellk.style.color = getNumberColor(board[i][k]); gridCell.style.color = 'white'; score += board[i][k]; var showScore = document.getElementById('score'); showScore.innerHTML = score; j = k; // } else { break; //跳出 var k 的for循环 } } } } } } function canMoveDown(board) { for (var j = ; j < ; j++) { for (var i = ; i > -; i--) { if (board[i][j] != ) { if (board[i + ][j] == || board[i + ][j] == board[i][j]) {return true; } } } } return false; } function moveDown() { for (var j = ; j < ; j++) { for (var i = ; i > -; i--) { if (board[i][j] != ) { for (var k = i+; k < ; k++) { if(board[k][j] == || board[k][j] == board[i][j]){ board[k][j] = board[k][j] + board[i][j]; board[i][j] = ; var gridCell = document.getElementById('grid-cell-'+i+'-'+j); gridCell.innerHTML = ''; var gridCellk = document.getElementById('grid-cell-'+k+'-'+j); gridCellk.innerHTML = board[k][j]; gridCellk.style.backgroundColor = getNumberBackgroundColor(board[k][j]); gridCell.style.backgroundColor = '#cb'; gridCellk.style.color = getNumberColor(board[i][k]); gridCell.style.color = 'white'; score += board[k][j]; var showScore = document.getElementById('score'); showScore.innerHTML = score; i = k; // } else { break; //跳出 var k 的for循环 } } } } } } function isgameover() { if (nospace(board) && nomove(board)) { gameover(); } } function nomove(board) { if (canMoveDown(board) || canMoveLeft(board) || canMoveRight(board) || canMoveUp(board)) { return false; } return true; } function bounce(obj,){ clearInterval(obj.timer); var nSpeed = ; var aeleration = ; var Flag = ; obj.timer = setInterval(function (){ nSpeed += aeleration; nSpeed = .; if(obj.offsetTop + nSpeed >= ){ obj.style. = + 'px'; nSpeed = -; }else{ if(Math.abs(nSpeed) < && Math.abs(-obj.offsetTop)<){ clearInterval(obj.timer); } else{ obj.style. = obj.offsetTop + nSpeed + 'px'; } } },); } function gameover() { //alert("gameover!"); var gameover = document.createElement('div'); gameover.id = 'showGameover'; gameover.innerHTML = 'GAME OVER'; var gridContainer = document.getElementById('grid-container'); gridContainer.appendChild(gameover); var showGameover = document.getElementById('showGameover'); showGameover.style.left = (gridContainer.offsetWidth - showGameover.offsetWidth ) / + 'px'; bounce(showGameover,); } </script> </head> <body> <header> <!--<h> </h>--> <a href="javascript:newgame();" id="newgamebutton"> New Game </a> <p> score: <span id="score"></span></p> <div id="grid-container"> <div class="grid-cell" id="grid-cell--"></div> <div class="grid-cell" id="grid-cell--"></div> <div class="grid-cell" id="grid-cell--"></div> <div class="grid-cell" id="grid-cell--"></div> <div class="grid-cell" id="grid-cell--"></div> <div class="grid-cell" id="grid-cell--"></div> <div class="grid-cell" id="grid-cell--"></div> <div class="grid-cell" id="grid-cell--"></div> <div class="grid-cell" id="grid-cell--"></div> <div class="grid-cell" id="grid-cell--"></div> <div class="grid-cell" id="grid-cell--"></div> <div class="grid-cell" id="grid-cell--"></div> <div class="grid-cell" id="grid-cell--"></div> <div class="grid-cell" id="grid-cell--"></div> <div class="grid-cell" id="grid-cell--"></div> <div class="grid-cell" id="grid-cell--"></div> </div> </header> </body> </html>
以上代码还很满意吧,都是些div+css方面的知识,代码有问题欢迎各位亲提出宝贵意见,共同学习进步,也非常感谢大家对狼蚁SEO网站的支持,谢谢!
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程