javascript製作2048遊戲

來源:互聯網
上載者:User

 這幾天玩兒著2048這個遊戲,突然心血來潮想練習下寫程式的思路,於是乎就模仿做了一個,到目前位置還沒有實現動態移動,不是很好看,不過玩兒著自己模仿的小遊戲還是蠻爽的,哈哈

  

2048.html

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <!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

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 @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

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 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); } }

以上所訴就是本文的全部內容了,希望大家能夠喜歡。

相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。