日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

网页版2048游戏html,HTML网页版2048小游戏

發(fā)布時(shí)間:2023/12/14 HTML 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 网页版2048游戏html,HTML网页版2048小游戏 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

存放路徑如下,2048.html ? css/2048.css ? ?js/2048.js

效果顯示:點(diǎn)擊這里! ? ? ? ? ? ? ? ? 源代碼下載:

源代碼如下:

2048.html文件

2048

Please click the left mouse button to start,通過w?a?s?d?來控制方向

<>網(wǎng)絡(luò)人VS灰鴿子工作室制作


2048.css文件

#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);

}

}

總結(jié)

以上是生活随笔為你收集整理的网页版2048游戏html,HTML网页版2048小游戏的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。