JavaScript 2048 游戏实例代码(简单易懂)


Posted in Javascript onMarch 25, 2016

废话不多说了,直接给大家贴代码了,觉得很满意直接拿去。

JavaScript 2048 游戏实例代码(简单易懂)

<!DOCTYPE html PUBLIC "-//WC//DTD XHTML . Transitional//EN" "http://www.w.org/TR/xhtml/DTD/xhtml-transitional.dtd">
<html xmlns="http://www.w.org//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:#cccb; 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-top: 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.top = (+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 "#edcc";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 = '#cccb';
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 = '#cccb';
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 = '#cccb';
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 = '#cccb';
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,top){
clearInterval(obj.timer);
var nSpeed = ;
var acceleration = ;
var Flag = ;
obj.timer = setInterval(function (){
nSpeed += acceleration;
nSpeed *= .;
if(obj.offsetTop + nSpeed >= top){
obj.style.top = top + 'px';
nSpeed *= -;
}else{
if(Math.abs(nSpeed) < && Math.abs(top-obj.offsetTop)<){
clearInterval(obj.timer);
}
else{
obj.style.top = 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方面的知识,代码有问题欢迎各位亲提出宝贵意见,共同学习进步,同时也非常感谢大家对三水点靠木网站的支持,谢谢!

Javascript 相关文章推荐
js 键盘记录实现(兼容FireFox和IE)
Feb 07 Javascript
Jquery知识点三 jquery表单对象操作
Jan 17 Javascript
无闪烁更新网页内容JS实现
Dec 19 Javascript
用原生js做个简单的滑动效果的回到顶部
Oct 15 Javascript
深入理解JavaScript系列(49):Function模式(上篇)
Mar 04 Javascript
js实现楼层效果的简单实例
Jul 15 Javascript
React服务端渲染(总结)
Jul 01 Javascript
提升页面加载速度的插件InstantClick
Sep 12 Javascript
nuxt+axios解决前后端分离SSR的示例代码
Oct 24 Javascript
详谈vue+webpack解决css引用图片打包后找不到资源文件的问题
Mar 06 Javascript
详解微信小程序开发用户授权登陆
Apr 24 Javascript
Vue 一键清空表单的实现方法
Feb 07 Javascript
JavaScript入门系列之知识点总结
Mar 24 #Javascript
JS实现支持Ajax验证的表单插件
Mar 24 #Javascript
拥有一个属于自己的javascript表单验证插件
Mar 24 #Javascript
jquery zTree异步加载、模糊搜索简单实例分享
Mar 24 #Javascript
js+css实现select的美化效果
Mar 24 #Javascript
基于jQuery Ajax实现上传文件
Mar 24 #Javascript
Angular.js如何从PHP读取后台数据
Mar 24 #Javascript
You might like
造就帕卡马拉的帕卡斯是怎么被发现的
2021/03/03 咖啡文化
PHP函数extension_loaded()用法实例
2015/01/19 PHP
Laravel中使用自己编写类库的3种方法
2015/02/10 PHP
Yii隐藏URL中index.php的方法
2016/07/12 PHP
PHP简单获取网站百度搜索和搜狗搜索收录量的方法
2016/08/23 PHP
php base64 编码与解码实例代码
2017/03/21 PHP
安装PHP扩展时解压官方 tgz 文件后没有configure文件无法进行配置编译的问题
2020/08/26 PHP
兼容ie和firefox js关闭代码
2008/12/11 Javascript
Extjs4 GridPanel 的几种样式使用介绍
2013/04/18 Javascript
判断在css加载完毕后执行后续代码示例
2014/09/03 Javascript
JS+CSS实现弹出全屏灰黑色透明遮罩效果的方法
2014/12/20 Javascript
jQuery实现移动 和 渐变特效的点击事件
2015/02/26 Javascript
js本地图片预览实现代码
2016/10/09 Javascript
jQuery实现导航栏头部菜单项点击后变换颜色的方法
2017/07/19 jQuery
详解Node中导入模块require和import的区别
2017/08/11 Javascript
微信小程序使用二次贝塞尔曲线画波浪
2018/12/25 Javascript
浅谈如何优雅处理JavaScript异步错误
2019/11/12 Javascript
js实现鼠标点击飘爱心效果
2020/08/19 Javascript
Python制作简单的网页爬虫
2015/11/22 Python
关于python2 csv写入空白行的问题
2018/06/22 Python
用于业余项目的8个优秀Python库
2018/09/21 Python
深入了解和应用Python 装饰器 @decorator
2019/04/02 Python
Django实现web端tailf日志文件功能及实例详解
2019/07/28 Python
使用pygame写一个古诗词填空通关游戏
2019/12/03 Python
tensorflow 20:搭网络,导出模型,运行模型的实例
2020/05/26 Python
学点简单的Django之第一个Django程序的实现
2021/02/24 Python
Nebula美国官网:便携式投影仪
2019/03/15 全球购物
Tenstickers法国:墙贴和装饰贴纸
2019/08/26 全球购物
在校大学生个人的自我评价
2014/02/13 职场文书
运动会入场词60字
2014/02/15 职场文书
工程专业应届生求职信
2014/02/19 职场文书
《长城和运河》教学反思
2014/04/14 职场文书
共筑中国梦演讲稿
2014/04/23 职场文书
四年级作文之植物
2019/09/20 职场文书
python实现黄金分割法的示例代码
2021/04/28 Python
Java各种比较对象的方式的对比总结
2021/06/20 Java/Android