javascript制作2048游戏


Posted in Javascript onMarch 30, 2015

2048.html

<!DOCTYPE>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>2048</title>
<link rel="stylesheet" type="text/css" href="css/2048.css" />
<!-- <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> -->
<script type="text/javascript" src="js/2048.js"></script>
</head>
 
<body>
  <div id="div2048">
    <a id="start">tap to start :-)</a>
  </div>
</body>
</html>

2048.css

@charset "utf-8";
 
#div2048
{
  width: 500px;
  height: 500px;
  background-color: #b8af9e;
  margin: 0 auto;
  position: relative;
}
#start
{
  width: 500px;
  height: 500px;
  line-height: 500px;
  display: block;
  text-align: center;
  font-size: 30px;
  background: #f2b179;
  color: #FFFFFF;
}
#div2048 div.tile
{
  margin: 20px 0px 0px 20px;
  width: 100px;
  height: 40px;
  padding: 30px 0;
  font-size: 40px;
  line-height: 40px;
  text-align: center;
  float: left;
}
#div2048 div.tile0{
  background: #ccc0b2;
}
#div2048 div.tile2
{
  color: #7c736a;
  background: #eee4da;
}
#div2048 div.tile4
{
  color: #7c736a;
  background: #ece0c8;
}
#div2048 div.tile8
{
  color: #fff7eb;
  background: #f2b179;
}
#div2048 div.tile16
{
  color:#fff7eb;
  background:#f59563;
}
#div2048 div.tile32
{
  color:#fff7eb;
  background:#f57c5f;
}
#div2048 div.tile64
{
  color:#fff7eb;
  background:#f65d3b;
}
#div2048 div.tile128
{
  color:#fff7eb;
  background:#edce71;
}
#div2048 div.tile256
{
  color:#fff7eb;
  background:#edcc61;
}
#div2048 div.tile512
{
  color:#fff7eb;
  background:#ecc850;
}
#div2048 div.tile1024
{
  color:#fff7eb;
  background:#edc53f;
}
#div2048 div.tile2048
{
  color:#fff7eb;
  background:#eec22e;
}

2048.js

function game2048(container)
{
  this.container = container;
  this.tiles = new Array(16);
}
 
game2048.prototype = {
  init: function(){
    for(var i = 0, len = this.tiles.length; i < len; i++){
      var tile = this.newTile(0);
      tile.setAttribute('index', i);
      this.container.appendChild(tile);
      this.tiles[i] = tile;
    }
    this.randomTile();
    this.randomTile();
  },
  newTile: function(val){
    var tile = document.createElement('div');
    this.setTileVal(tile, val)
    return tile;
  },
  setTileVal: function(tile, val){
    tile.className = 'tile tile' + val;
    tile.setAttribute('val', val);
    tile.innerHTML = val > 0 ? val : '';
  },
  randomTile: function(){
    var zeroTiles = [];
    for(var i = 0, len = this.tiles.length; i < len; i++){
      if(this.tiles[i].getAttribute('val') == 0){
        zeroTiles.push(this.tiles[i]);
      }
    }
    var rTile = zeroTiles[Math.floor(Math.random() * zeroTiles.length)];
    this.setTileVal(rTile, Math.random() < 0.8 ? 2 : 4);
  },
  move:function(direction){
    var j;
    switch(direction){
      case 'W':
        for(var i = 4, len = this.tiles.length; i < len; i++){
          j = i;
          while(j >= 4){
            this.merge(this.tiles[j - 4], this.tiles[j]);
            j -= 4;
          }
        }
        break;
      case 'S':
        for(var i = 11; i >= 0; i--){
          j = i;
          while(j <= 11){
            this.merge(this.tiles[j + 4], this.tiles[j]);
            j += 4;
          }
        }
        break;
      case 'A':
        for(var i = 1, len = this.tiles.length; i < len; i++){
          j = i;
          while(j % 4 != 0){
            this.merge(this.tiles[j - 1], this.tiles[j]);
            j -= 1;
          }
        }
        break;
      case 'D':
        for(var i = 14; i >= 0; i--){
          j = i;
          while(j % 4 != 3){
            this.merge(this.tiles[j + 1], this.tiles[j]);
            j += 1;
          }
        }
        break;
    }
    this.randomTile();
  },
  merge: function(prevTile, currTile){
    var prevVal = prevTile.getAttribute('val');
    var currVal = currTile.getAttribute('val');
    if(currVal != 0){
      if(prevVal == 0){
        this.setTileVal(prevTile, currVal);
        this.setTileVal(currTile, 0);
      }
      else if(prevVal == currVal){
        this.setTileVal(prevTile, prevVal * 2);
        this.setTileVal(currTile, 0);
      }
    }
  },
  equal: function(tile1, tile2){
    return tile1.getAttribute('val') == tile2.getAttribute('val');
  },
  max: function(){
    for(var i = 0, len = this.tiles.length; i < len; i++){
      if(this.tiles[i].getAttribute('val') == 2048){
        return true;
      }
    }
  },
  over: function(){
    for(var i = 0, len = this.tiles.length; i < len; i++){
      if(this.tiles[i].getAttribute('val') == 0){
        return false;
      }
      if(i % 4 != 3){
        if(this.equal(this.tiles[i], this.tiles[i + 1])){
          return false;
        }
      }
      if(i < 12){
        if(this.equal(this.tiles[i], this.tiles[i + 4])){
          return false;
        }
      }
    }
    return true;
  },
  clean: function(){
    for(var i = 0, len = this.tiles.length; i < len; i++){
      this.container.removeChild(this.tiles[i]);
    }
    this.tiles = new Array(16);
  }
}
 
var game, startBtn;
 
window.onload = function(){
  var container = document.getElementById('div2048');
  startBtn = document.getElementById('start');
  startBtn.onclick = function(){
    this.style.display = 'none';
    game = game || new game2048(container);
    game.init();
  }
}
 
window.onkeydown = function(e){
  var keynum, keychar;
  if(window.event){    // IE
    keynum = e.keyCode;
  }
  else if(e.which){    // Netscape/Firefox/Opera
    keynum = e.which;
  }
  keychar = String.fromCharCode(keynum);
  if(['W', 'S', 'A', 'D'].indexOf(keychar) > -1){
    if(game.over()){
      game.clean();
      startBtn.style.display = 'block';
      startBtn.innerHTML = 'game over, replay?';
      return;
    }
    game.move(keychar);
  }
}

以上所诉就是本文的全部内容了,希望大家能够喜欢。

Javascript 相关文章推荐
更优雅的事件触发兼容
Oct 24 Javascript
DOM基础教程之使用DOM设置文本框
Jan 20 Javascript
js+html5实现canvas绘制简单矩形的方法
Jun 05 Javascript
Google 地图获取API Key详细教程
Aug 06 Javascript
JS简单实现点击跳转登陆邮箱功能的方法
Oct 31 Javascript
仿京东快报向上滚动的实例
Dec 13 Javascript
jQuery EasyUI 选项卡面板tabs的使用实例讲解
Dec 25 jQuery
vue 使用eventBus实现同级组件的通讯
Mar 02 Javascript
JS实现监控微信小程序的原理
Jun 15 Javascript
微信小程序自定义tabBar组件开发详解
Sep 24 Javascript
微信小程序全局变量功能与用法详解
Jan 22 Javascript
Jquery如何使用animation动画效果改变背景色的代码
Jul 20 jQuery
JavaScript模拟实现继承的方法
Mar 30 #Javascript
jQuery制作可自定义大小的拼图游戏
Mar 30 #Javascript
JS实现向表格中动态添加行的方法
Mar 30 #Javascript
JS实现向表格行添加新单元格的方法
Mar 30 #Javascript
JS实现控制表格行文本对齐的方法
Mar 30 #Javascript
JS实现控制表格行内容垂直对齐的方法
Mar 30 #Javascript
JS实现控制表格内指定单元格内容对齐的方法
Mar 30 #Javascript
You might like
实现“上一页”和“下一页按钮
2006/10/09 PHP
PHP4在Windows2000下的安装
2006/10/09 PHP
使用TinyButStrong模板引擎来做WEB开发
2007/03/16 PHP
php PDO中文乱码解决办法
2009/07/20 PHP
PHP 读取文件内容代码(txt,js等)
2009/12/06 PHP
WordPress中获取所使用的模板的页面ID的简单方法
2015/12/31 PHP
PHP判断json格式是否正确的实现代码
2017/09/20 PHP
php进程daemon化的正确实现方法
2018/09/06 PHP
Mootools 1.2教程(2) DOM选择器
2009/09/14 Javascript
js的写法基础分析
2011/01/17 Javascript
鼠标经过显示二级菜单js特效
2013/08/13 Javascript
js调试系列 断点与动态调试[基础篇]
2014/06/18 Javascript
Js删除数组中某一项或几项的几种方法(推荐)
2016/07/27 Javascript
node.js版本管理工具n无效的原理和解决方法
2016/11/24 Javascript
react-native ListView下拉刷新上拉加载实现代码
2017/08/03 Javascript
在ES5与ES6环境下处理函数默认参数的实现方法
2018/05/13 Javascript
vue-cli脚手架打包静态资源请求出错的原因与解决
2019/06/06 Javascript
Window 64位下python3.6.2环境搭建图文教程
2018/09/19 Python
selenium + python 获取table数据的示例讲解
2018/10/13 Python
python获取微信小程序手机号并绑定遇到的坑
2018/11/19 Python
python3编写ThinkPHP命令执行Getshell的方法
2019/02/26 Python
使用python来调用CAN通讯的DLL实现方法
2019/07/03 Python
pandas通过字典生成dataframe的方法步骤
2019/07/23 Python
在Tensorflow中实现leakyRelu操作详解(高效)
2020/06/30 Python
详解Selenium 元素定位和WebDriver常用方法
2020/12/04 Python
详解python3类型注释annotations实用案例
2021/01/20 Python
详解HTML5表单新增属性
2016/12/21 HTML / CSS
俄罗斯化妆品和香水网上商店:Iledebeaute
2019/01/03 全球购物
MIXIT官网:俄罗斯最大的化妆品公司之一
2020/01/25 全球购物
会计学生自我鉴定
2014/02/06 职场文书
小学生2014国庆节演讲稿:祖国在我心中
2014/09/21 职场文书
2014感恩节演讲稿大全
2014/10/11 职场文书
2015年团支部工作总结
2015/04/03 职场文书
活动总结书怎么写
2015/05/11 职场文书
老人节主持词
2015/07/04 职场文书
Node实现搜索框进行模糊查询
2021/06/28 Javascript