您好,欢迎访问一九零五行业门户网

javascript制作2048游戏_jquery

2048.html
2048 tap to start :-)

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 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; 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 = 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 -1){ if(game.over()){ game.clean(); startbtn.style.display = 'block'; startbtn.innerhtml = 'game over, replay?'; return; } game.move(keychar); }}
以上所诉就是本文的全部内容了,希望大家能够喜欢。
其它类似信息

推荐信息