当前位置: 首页 > 编程日记 > 正文

使用javascript开发2048

嗯,团队队友开发了一个简单的2048...哈哈,没办法,这游戏那么疯狂,必须搞搞啦,大家能够直接粘贴代码到一个html文件,直接执行就可以

依赖文件:jquery,假设乜有,大家能够自己下载一份

<!DOCTYPE HTML>
<html>
<head>
<script  language="javascript" src="scripts/jquery-1.9.1.js"></script> <!--<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.css"><script src="http://code.jquery.com/jquery-1.5.min.js"></script><script src="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.js"></script>
-->
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1"/>
<meta name="renderer" content="webkit"><style >
body,div{margin:0;padding:0;}
body{width:100%;height:500px;}
.field{width:85%;height:55%;background:gray;margin:5% auto;border-radius:2%;}.tab{
width:21%;
height:100%;
background:yellow;
position:relative;
z-index:10;
margin:2%;
font-size:200%;
font-weight:bold;
line-height:200%;
text-align:center ;
vertical-align:bottom;
border-radius:5px;transition:all .5s;
}
.row{height:20%;width:100%;clear:both;}
.row div{float:left;}.usetime{
width:40%;
height:10%;
background:yellow;
font-size:150%;
font-weight:bold;
border-radius:5px;
margin:0 auto;
line-height:160%;}#times,#time{color:red;}
</style></head><body>
<center><h2>合成2048算你赢</h2></center>
<div class="field"><div class="row row1">
<div class="tab tab1"> </div>
<div class="tab tab2"> </div>
<div class="tab tab3"> </div>
<div class="tab tab4"> </div>
</div><div class="row row2">
<div class="tab tab5"> </div>
<div class="tab tab6"> </div>
<div class="tab tab7"> </div>
<div class="tab tab8"> </div>
</div><div class="row row3">
<div class="tab tab9"> </div>
<div class="tab tab10"> </div>
<div class="tab tab11"> </div>
<div class="tab tab12"> </div>
</div><div class="row row4">
<div class="tab tab13"> </div>
<div class="tab tab14"> </div>
<div class="tab tab15"> </div>
<div class="tab tab16"> </div>
</div></div><div class="usetime">用时:<span id="time">1</span>  s</div><div class="usetime">移动:<span id="times">1</span> 次</div><div id="result"></div><script language="javascript">arr=[[0,0,0,0],[0,0,2,2],[0,0,0,0],[0,4,0,0]];color=["yellow","#0000FF","#ADFF2F","#55aabb","#ff99bb","#ffaabb","#FF4500","#ffaa00","#eebbaa","#ee00bb","#8B0000"];/*	 
function init(){
index=1;$(".tab").each(function(){if(index <5 )$(this).html(Math.pow(2,index++))})
}
init();
*/
setInterval(function(){
$("#time").html(parseInt($("#time").html())+1);
},1000);/****************** Consts 定义 開始 ********************************/
/****************** Consts 定义 開始 ********************************/
Consts={
arr_rows_num:4,
arr_cols_num:4,
game_fail:-1,		//游戏失败
game_succeed:1,		//游戏成功
game_normal:0,		//游戏正常
game_init_num:2		//游戏 撒下随机的数字
}
/****************** Consts 定义 结束 ********************************//****************** Game 開始 ********************************/
/****************** Game 開始 ********************************/
Game={
/*
*重绘游戏  
*(1)把数组里面的数字放到div 里面去
*(2)依据数字变更颜色
*/
repaint:function(){$(".tab").each(function(){	tab_index=parseInt($(".tab").index($(this)));if(tab_index>3){ rows=parseInt(tab_index/4);}else{ rows=0;}$(this).html(arr[rows][tab_index%4]?arr[rows][tab_index%4]:"");switch(parseInt($(this).html())){case 2:$(this).css("background",color[1]);break;case 4:$(this).css("background",color[2]);break;case 8:$(this).css("background",color[3]);break;case 16:$(this).css("background",color[4]);break;case 32:$(this).css("background",color[5]);break;case 64:$(this).css("background",color[6]);break;case 128:$(this).css("background",color[7]);break;case 256:$(this).css("background",color[8]);break;case 512:$(this).css("background",color[9]);break;case 1024:$(this).css("background",color[10]);break;default:$(this).css("background",color[0]);break;} });
},
/*
*随机产生2的位置
*/
setRandomPos:function(){rows=getRandom(3);cols=getRandom(3);//随机生成一个下标while(arr[rows][cols] != 0){rows=getRandom(3);cols=getRandom(3);}arr[rows][cols]=Consts.game_init_num;   //
},//检查游戏是否失败,或者成功
check:function(){flag=Consts.game_fail;						//-1for(rows=0;rows<Consts.arr_rows_num; rows++){for(cols=0;cols<Consts.arr_cols_num; cols++){if(arr[rows][cols] == 0){ flag=Consts.game_normal;		//0break;}else if(arr[rows][cols] == 1024){flag=Consts.game_succeed;		//1break;}}}return flag;
},/*
*执行一次游戏
*(1)检查游戏是否失败或者成功
*(2)依据游戏状态做出应对
*/
run:function(){if(this.check() == Consts.game_fail){alert("游戏失败了~");window.opener = null;window.open(' ', '_self', ' '); window.close();}else if(this.check() == Consts.game_normal){this.repaint();  this.setRandomPos();if(this.check()== Consts.game_fail) {alert("游戏失败了~");}else if(this.check()== Consts.game_succeed){alert("恭喜您,通关啦~~");}}else{alert("恭喜您,通关啦~~");}}}/******************Game 结束********************************//******************reset 開始********************************/
/******************计算数字,同一方向有同样的则合并**********/
reset={
//【向左】  计算而且合并同样数字
left:function(){for(rows=0;rows<Consts.arr_rows_num;rows++){for(self=0;self<Consts.arr_cols_num;self++){for(compare=self+1;compare<Consts.arr_cols_num;compare++){if(arr[rows][compare]!=0 && arr[rows][compare]!=arr[rows][self]) break;if((arr[rows][self]==arr[rows][compare])&&(arr[rows][self]!=0)){arr[rows][self]=arr[rows][self]*2;arr[rows][compare]=0;//Game.setRandomPos();			//加入一个随机的2}					}}}
},
//【向右】  计算而且合并同样数字
right:function(){for(rows=0;rows<Consts.arr_rows_num;rows++){for(self=3;self>-1;self--){for(compare=self-1;compare>-1;compare--){//不相等,直接退出if(arr[rows][compare] !=0 && arr[rows][compare] != arr[rows][self]) break;//相等,合并元素if((arr[rows][self]==arr[rows][compare])&&(arr[rows][self]!=0)){arr[rows][self]=arr[rows][self]*2;arr[rows][compare]=0;//Game.setRandomPos();			//加入一个随机的2}					}}}
},
//【向下】  计算而且合并同样数字
down:function(){for(cols=Consts.arr_cols_num;cols>-1;cols--){for(rows=Consts.arr_cols_num-1;rows>0;rows--){for(compare=rows-1;compare>-1;compare--){if(arr[compare][cols]!=0 && arr[compare][cols]!=arr[rows][cols]) break;if((arr[rows][cols]==arr[compare][cols])&&(arr[rows][cols]!=0)){arr[rows][cols]=arr[rows][cols]*2;arr[compare][cols]=0;//Game.setRandomPos();			//加入一个随机的2}					}}}},//【向上】  计算而且合并同样数字
up:function(){for(cols=0;cols<Consts.arr_cols_num;cols++){for(self=0;self<Consts.arr_rows_num;self++){for(compare=self+1;compare<Consts.arr_rows_num;compare++){if(arr[compare][cols]!=0 && arr[compare][cols]!=arr[self][cols]) break;if((arr[self][cols]==arr[compare][cols])&&(arr[self][cols]!=0)){arr[self][cols]=arr[self][cols]*2;arr[compare][cols]=0;//Game.setRandomPos();			//加入一个随机的2}					}}}
}
}/******************reset 结束********************************//******************move 開始********************************/
/******************移动的方向没有数字存在,那么就移动********************************/
move={//【向左】
left:function(){for(num=0;num<4;num++){for(rows=0;rows<Consts.arr_rows_num;rows++){for(cols=0;cols<Consts.arr_cols_num;cols++){if(arr[rows][cols] == 0){for(index = cols; index<Consts.arr_cols_num-1;index++){arr[rows][index]=arr[rows][index+1];}arr[rows][index]=0;}}}}
},//【向右】
right:function(){for(num=0;num<4;num++){for(rows=Consts.arr_rows_num-1;rows>-1;rows--){for(cols=Consts.arr_cols_num-1;cols>-1;cols--){if(arr[rows][cols]==0){for(index=cols;index>0;index--){arr[rows][index]=arr[rows][index-1];}	arr[rows][0]=0;}}}}
},
//【向下】
down:function(){for(num=0;num<4;num++){for(cols=0;cols<Consts.arr_cols_num;cols++){for(rows=Consts.arr_rows_num-1;rows>-1;rows--){if(arr[rows][cols]==0){for(index=rows;index>0;index--){arr[index][cols]=arr[index-1][cols];}	arr[0][cols]=0;}			}}}},//【向上】
up:function(){for(num=0;num<4;num++){for(cols=0;cols<Consts.arr_cols_num;cols++){for(rows=0;rows<Consts.arr_rows_num;rows++){if(arr[rows][cols]==0){for(index=rows;index<Consts.arr_rows_num-1;index++){arr[index][cols]=arr[index+1][cols];}	arr[3][cols]=0;}			}}}
}}/******************move 结束********************************///获取0~num_field之内的数字
function getRandom(num_field){
return Math.round(Math.random()*num_field);
}Game.run();
window.οnkeydοwn=function(e){
switch(e.keyCode){
case 37:reset.left();move.left();break;
case 38:reset.up();move.up();break;
case 39:reset.right(); move.right(); break;
case 40: reset.down();move.down();break;
}Game.run();$("#times").html(parseInt($("#times").html())+1);
}
</script><script type="text/javascript">//全局变量,触摸開始位置var startX = 0, startY = 0;var endX = 0, endY = 0;//touchstart事件function touchSatrtFunc(evt) {try{//evt.preventDefault(); //阻止触摸时浏览器的缩放、滚动栏滚动等var touch = evt.touches[0]; //获取第一个触点var x = Number(touch.pageX); //页面触点X坐标var y = Number(touch.pageY); //页面触点Y坐标//记录触点初始位置startX = x;startY = y;}catch (e) {alert('touchSatrtFunc:' + e.message);}}//touchmove事件,这个事件无法获取坐标function touchMoveFunc(evt) {try{//evt.preventDefault(); //阻止触摸时浏览器的缩放、滚动栏滚动等var touch = evt.touches[0]; //获取第一个触点endX = Number(touch.pageX); //页面触点X坐标endy = Number(touch.pageY); //页面触点Y坐标var text = 'TouchMove事件触发:(' + x + ', ' + y + ')';//推断滑动方向/* if ((x - startX) >5) {text += '<br/>右滑动';}else if((x - startX) <-5){text += '<br/>左滑动';}if ((y - startY) > 5) {text += '<br/>上滑动';}else  if ((y - startY) < -5) {text += '<br/>下滑动';}*/document.getElementById("result").innerHTML = text;}catch (e) {alert('touchMoveFunc:' + e.message);}}//touchend事件function touchEndFunc(evt) {try {//evt.preventDefault(); //阻止触摸时浏览器的缩放、滚动栏滚动等var text = 'TouchEnd事件触发';document.getElementById("result").innerHTML = text;}catch (e) {alert('touchEndFunc:' + e.message);}}//绑定事件function bindEvent() {document.addEventListener('touchstart', touchSatrtFunc, false);document.addEventListener('touchmove', touchMoveFunc, false);document.addEventListener('touchend', touchEndFunc, false);}//推断是否支持触摸事件function isTouchDevice() {document.getElementById("version").innerHTML = navigator.appVersion;try {document.createEvent("TouchEvent");alert("支持TouchEvent事件!");bindEvent(); //绑定事件}catch (e) {alert("不支持TouchEvent事件!" + e.message);}}window.onload = isTouchDevice;</script><div id="version"></div>
</body>
</html>

执行时,建议採用火狐浏览器...

以下是执行效果:



转载于:https://www.cnblogs.com/mengfanrong/p/3982879.html

相关文章:

html 自动弹出框

1.点击div外部隐藏&#xff0c; //*代表tip_box所包含的子元素 $(body).click(function(e) {var target $(e.target);if(!target.is(#tip_box *) ) {//事件处理} });2.div动态展开 .tip_box{width:300px;height:0;border-radius:3px;background-color:#fff;overflow:hidden;bo…

3-runtime 之 Tagged Pointer

Tagged Pointer 是自从iPhone 5s 之后引入的特性 1 先说一下iOS的内存布局 代码区&#xff1a;存放编译之后的代码数据段 &#xff1a;字符串常量 &#xff1a; NSString *hello “hello”;已经初始化和未初始化的全局变量&#xff0c;静态变量堆&#xff1a;通过alloc&#…

编程术语_伟大的编程术语烘烤

编程术语by Preethi Kasireddy通过Preethi Kasireddy 伟大的编程术语烘烤 (The Great Programming Jargon Bake-off) Imperative vs. Declarative. Pure vs. Impure. Static vs. Dynamic.命令式与声明式。 纯与不纯。 静态与动态。 Terminology like this is sprinkled throu…

Swift 圆环进度条

Swift 圆环进度条 import UICircularProgressRing import UIKit import UICircularProgressRing class ViewController: UIViewController {var progress:UICircularProgressRing!;override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading …

Linux文件系统构成(第二版)

Linux文件系统构成/boot目录&#xff1a;内核文件、系统自举程序文件保存位置,存放了系统当前的内核【一般128M即可】如:引导文件grub的配置文件等/etc目录&#xff1a;系统常用的配置文件&#xff0c;所以备份系统时一定要备份此目录如&#xff1a;系统管理员经常需要修改的文…

include_once 问题

最近在做微信小程序&#xff0c;在include_once 微信文件后&#xff0c;该方法return 前面会用特殊字符&#xff0c;导致我return 给前端的本来是json串变成了字符 解决方法 &#xff1a; ob_clean(); return json_encode(array);转载于:https://www.cnblogs.com/zouzhe0/p/630…

babel6 babel7_当您已经准备好Babel时设置Flow

babel6 babel7by Jamie Kyle杰米凯尔(Jamie Kyle) 当您已经准备好Babel时设置Flow (Setting up Flow when you’ve already got Babel in place) Flow is a static type checker for JavaScript. It makes you more productive by providing feedback as you write code. Flow…

如何为Android上的产品设计一款合适的图标

如 果你已经完成了你的app&#xff0c;你一定会马上向其它人宣布这件事情。但是你需要注意一个很重要的问题&#xff0c;那就是app的图标。你的图标可能在项目启动之 前就已经设计好了&#xff0c;但我不喜欢这样&#xff0c;如果app没有完成实际上图标也没什么用了。如果你不是…

得到windows聚焦图片(windows 10)

有些Windows聚焦图片确实很漂亮&#xff0c;很希望保留下来&#xff0c;但是Windows聚焦图片总更好&#xff0c;网上有得到聚焦图片的方法&#xff0c;每次都手动去弄真麻烦&#xff0c;于是自己编了一个小程序&#xff0c;自动得到Windows聚焦图片&#xff0c;下面是运行这个小…

swift 加载gif 框架图片

swift 加载gif 框架图片 SwiftGifOrigin 以下代码 轻松搞定 let imgView UIImageView(frame: CGRect(x: 50, y: 100, width: 280, height: 200));imgView.loadGif(name: "gfff");self.view.addSubview(imgView);

devops_最低可行DevOps

devopsby Michael Shilman通过迈克尔希尔曼(Michael Shilman) 最低可行DevOps (Minimum Viable DevOps) 快速而肮脏的指南&#xff0c;用于扩展您的发布并拥抱互联网的死亡 (A quick and dirty guide to scaling your launch and embracing the Internet hug of death) Startu…

java基础之——类的初始化顺序(转载)

原文地址&#xff1a;http://www.cnblogs.com/chrischennx/p/3612295.html 由浅入深&#xff0c;首先&#xff0c;我们来看一下&#xff0c;一个类初始化有关的都有些啥米&#xff1a; 静态成员变量、静态代码块、普通成员变量、普通代码块、构造器。&#xff08;成员方法&…

如何用CSS快速布局(一)—— 布局元素详细

要快速进行网页排版布局&#xff0c;则必须对布局的元素有清晰的了解&#xff0c;才不会总是在细节处出错。这一篇先详解有关布局的因素作为布局基础&#xff1a;块级元素and内联元素、盒模型、准确定位、元素对齐、样式继承。下一篇则重点描述快速布局思路。 一、什么是块级元…

iOS 改变字符串中数字的颜色

匹配中文字符 [\u4e00-\u9fa5] 匹配双字节字符(包括汉字在内) [^\x00-\xff] 匹配网址&#xff1a;[a-zA-z]://[^\s]* 匹配国内电话 \d{3}-\d{8}|\d{4}-\{7,8} 匹配腾讯QQ号 [1-9][0-9]{4,} 匹配18位身份证号^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$ Swift String…

jpg在线合并jpg_JPG如何运作

jpg在线合并jpgby Colt McAnlis通过Colt McAnlis JPG如何运作 (How JPG Works) The JPG file format was one of the most technologically impressive advancements to image compression to come on the scene in 1992. Since then, it’s been a dominant force in represe…

革命就是请客吃饭(案例分析吧)

前不久路过南京, 拜会了本科同学. 刚好他要见个青年才俊谈些事情, 于是就神神秘秘地把我拉了过去. 一路高谈阔论, 指点江山, 忆往昔峥嵘岁月之后, 此次"拜访"的目的也渐渐清晰起来. 我们所要见的人是位年轻的创业者, 他有些移动互联网的点子, 想和我们分享下, 并尝试…

TextView-- 测量文字宽度

https://my.oschina.net/lengwei/blog/637380; http://blog.csdn.net/mare_blue/article/details/51388403; http://blog.csdn.net/baidu_31093133/article/details/52413893; --1,Android中调用Paint的measureText()方法取得字符串显示的宽度值: public static float GetTextW…

swift 简单风格的Toaster

简单风格的Toaster Toaster //1 弹出文本 "Hello World" 延迟时间 2 展示时间 1 Toast(text: "Hello World", delay: 2, duration: 1).show();//2 初始化toast 方法 let toast Toast(text: "你好世界"); toast.show(); toast.cancel();// 3 …

工业革命前数千年人口经济_我们已经进行了数千年的编程

工业革命前数千年人口经济by Tautvilas Mečinskas由TautvilasMečinskas 我们已经进行了数千年的编程 (We have been programming for thousands of years) Computer programs are all around us. We interact with them every day. It looks as if software is becoming mor…

1-flutter 安装步骤

flutter 安装步骤 1 下载SDK SDK 下载地址 2 解压压缩包 将sdk 文件夹丢进系统的应用程序&#xff08;Application&#xff09;的目录 3 配置环境变量 命令行 open ~/.bash_profile &#xff0c;然后在bash 文件中写入下面配置 export PATH$PATH:/Applications/flutter/bi…

codevs1258 关路灯(☆区间dp)

1258 关路灯 时间限制: 1 s空间限制: 128000 KB题目等级 : 大师 Master题目描述 Description多瑞卡得到了一份有趣而高薪的工作。每天早晨他必须关掉他所在村庄的街灯。所有的街灯都被设置在一条直路的同一侧。 多瑞卡每晚到早晨5点钟都在晚会上&#xff0c;然后他开始关灯。开…

BroadcastReceiver自学笔记

1. 使用步骤&#xff1a; 1.1 声明Intent Intent intent new Intent("name");------静态常用 IntentFilter filter new IntentFilter("name");--------动态常用 1.2 注册 1.3 接收&#xff1a;利用action或者Bundle 在OnReceive()中&#xff0c;接收信息…

小狗钱钱_小狗设计

小狗钱钱by Patryk Adaś通过PatrykAdaś 小狗设计 (Design for doggies) Education should be universal and free for everyone.教育应该是普及的&#xff0c;对所有人来说都是免费的。 When I first entered the field, designers were patient with me. They gave me fee…

层化(stratification)的方法

有时候我们会遇到调整后的模型反而不如调整前表现好的情况&#xff0c;这可能和数据的随机分割有关系。在这个不平衡的数据情况下&#xff0c;最好用层化&#xff08;stratification&#xff09;的方法&#xff0c;比如&#xff1a; from sklearn.cross_validation import Stra…

零基础入门jQuery视频教程

零基础入门jQuery最新版开发.NET富客户端应用(选择器、DOM操作、事件和动画、Ajax应用、插件、Mobile)课程分类&#xff1a;.NETJquery适合人群&#xff1a;初级课时数量&#xff1a;35课时用到技术&#xff1a;javascript,ajax,jquery,handler涉及项目&#xff1a;各知识点的项…

2-flutter 之HelloWorld

widget 在flutter 中&#xff0c;几乎所有的东西都是widget&#xff0c;本身是用户界面的基本构建快&#xff0c;将widget组成一个层次结构&#xff0c; 调用widget树。每一个窗口widget都嵌套在父窗口widget中&#xff0c;并且从其父窗口中继承属性。甚至应用程序对象本身也…

浏览器获取浏览历史_浏览器历史的未来

浏览器获取浏览历史by Patryk Adaś通过PatrykAdaś 浏览器历史的未来 (The Future of Browser History) I am really unsatisfied with the current state of Browser History. I think that this is the most underestimated feature of every modern web browser. Let’s t…

【水】JSOI完美的对称

完美的对称 题目描述 在峰会期间&#xff0c;必须使用许多保镖保卫北约组织的各国代表。代表们除了由他自己的随身保镖保护外&#xff0c;组委会还指派了一些其他的特工和阻击手保护他们。为了使他们的工作卓有成效&#xff0c;使被保卫的人的安全尽可能得到保障&#xff0c;保…

3-flutter 项目结构 资源 依赖

1 项目的名称 android 安卓相关工程文件 build 项目的构建输出目录 ios ios 相关的部分工程文件 lib 项目中的dart 源文件 src 包含其他的源文件main.dart 自动生成的项目入口文件 test 测试相关的文件 pubspec.ymal 项目依赖配置文件 3 归档图片资源和处理不同的分辨率…

python简史_命令行简史

python简史by Gitter通过吉特 命令行简史 (A Brief History of the Command Line) This post by Andy Trevorah, Engineer at Gitter, has been adapted from a talk that he originally gave at codebar, a non-profit initiative that facilitates the growth of a diverse …