Skip to content

Commit d8926a8

Browse files
committed
原生js
1 parent 57eba1f commit d8926a8

4 files changed

Lines changed: 268 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE>
2+
<html xmlns="http://www.w3.org/1999/xhtml">
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5+
<title>2048</title>
6+
<link rel="stylesheet" type="text/css" href="css/2048.css" />
7+
<!-- <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> -->
8+
<script type="text/javascript" src="js/2048.js"></script>
9+
</head>
10+
11+
<body>
12+
<div id="div2048">
13+
<a id="start">tap to start :-)</a>
14+
</div>
15+
</body>
16+
</html>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
原生javascript代码写的2048游戏。建议在谷歌浏览器下跑。'WASD'控制方向。
2+
演示地址请移步:http://runjs.cn/detail/bp8baf8b
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
@charset "utf-8";
2+
3+
#div2048
4+
{
5+
width: 500px;
6+
height: 500px;
7+
background-color: #b8af9e;
8+
margin: 0 auto;
9+
position: relative;
10+
}
11+
#start
12+
{
13+
width: 500px;
14+
height: 500px;
15+
line-height: 500px;
16+
display: block;
17+
text-align: center;
18+
font-size: 30px;
19+
background: #f2b179;
20+
color: #FFFFFF;
21+
}
22+
#div2048 div.tile
23+
{
24+
margin: 20px 0px 0px 20px;
25+
width: 100px;
26+
height: 40px;
27+
padding: 30px 0;
28+
font-size: 40px;
29+
line-height: 40px;
30+
text-align: center;
31+
float: left;
32+
}
33+
#div2048 div.tile0{
34+
background: #ccc0b2;
35+
}
36+
#div2048 div.tile2
37+
{
38+
color: #7c736a;
39+
background: #eee4da;
40+
}
41+
#div2048 div.tile4
42+
{
43+
color: #7c736a;
44+
background: #ece0c8;
45+
}
46+
#div2048 div.tile8
47+
{
48+
color: #fff7eb;
49+
background: #f2b179;
50+
}
51+
#div2048 div.tile16
52+
{
53+
color:#fff7eb;
54+
background:#f59563;
55+
}
56+
#div2048 div.tile32
57+
{
58+
color:#fff7eb;
59+
background:#f57c5f;
60+
}
61+
#div2048 div.tile64
62+
{
63+
color:#fff7eb;
64+
background:#f65d3b;
65+
}
66+
#div2048 div.tile128
67+
{
68+
color:#fff7eb;
69+
background:#edce71;
70+
}
71+
#div2048 div.tile256
72+
{
73+
color:#fff7eb;
74+
background:#edcc61;
75+
}
76+
#div2048 div.tile512
77+
{
78+
color:#fff7eb;
79+
background:#ecc850;
80+
}
81+
#div2048 div.tile1024
82+
{
83+
color:#fff7eb;
84+
background:#edc53f;
85+
}
86+
#div2048 div.tile2048
87+
{
88+
color:#fff7eb;
89+
background:#eec22e;
90+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
function game2048(container)
2+
{
3+
this.container = container;
4+
this.tiles = new Array(16);
5+
}
6+
7+
game2048.prototype = {
8+
init: function(){
9+
for(var i = 0, len = this.tiles.length; i < len; i++){
10+
var tile = this.newTile(0);
11+
tile.setAttribute('index', i);
12+
this.container.appendChild(tile);
13+
this.tiles[i] = tile;
14+
}
15+
this.randomTile();
16+
this.randomTile();
17+
},
18+
newTile: function(val){
19+
var tile = document.createElement('div');
20+
this.setTileVal(tile, val)
21+
return tile;
22+
},
23+
setTileVal: function(tile, val){
24+
tile.className = 'tile tile' + val;
25+
tile.setAttribute('val', val);
26+
tile.innerHTML = val > 0 ? val : '';
27+
},
28+
randomTile: function(){
29+
var zeroTiles = [];
30+
for(var i = 0, len = this.tiles.length; i < len; i++){
31+
if(this.tiles[i].getAttribute('val') == 0){
32+
zeroTiles.push(this.tiles[i]);
33+
}
34+
}
35+
var rTile = zeroTiles[Math.floor(Math.random() * zeroTiles.length)];
36+
this.setTileVal(rTile, Math.random() < 0.8 ? 2 : 4);
37+
},
38+
move:function(direction){
39+
var j;
40+
switch(direction){
41+
case 'W':
42+
for(var i = 4, len = this.tiles.length; i < len; i++){
43+
j = i;
44+
while(j >= 4){
45+
this.merge(this.tiles[j - 4], this.tiles[j]);
46+
j -= 4;
47+
}
48+
}
49+
break;
50+
case 'S':
51+
for(var i = 11; i >= 0; i--){
52+
j = i;
53+
while(j <= 11){
54+
this.merge(this.tiles[j + 4], this.tiles[j]);
55+
j += 4;
56+
}
57+
}
58+
break;
59+
case 'A':
60+
for(var i = 1, len = this.tiles.length; i < len; i++){
61+
j = i;
62+
while(j % 4 != 0){
63+
this.merge(this.tiles[j - 1], this.tiles[j]);
64+
j -= 1;
65+
}
66+
}
67+
break;
68+
case 'D':
69+
for(var i = 14; i >= 0; i--){
70+
j = i;
71+
while(j % 4 != 3){
72+
this.merge(this.tiles[j + 1], this.tiles[j]);
73+
j += 1;
74+
}
75+
}
76+
break;
77+
}
78+
this.randomTile();
79+
},
80+
merge: function(prevTile, currTile){
81+
var prevVal = prevTile.getAttribute('val');
82+
var currVal = currTile.getAttribute('val');
83+
if(currVal != 0){
84+
if(prevVal == 0){
85+
this.setTileVal(prevTile, currVal);
86+
this.setTileVal(currTile, 0);
87+
}
88+
else if(prevVal == currVal){
89+
this.setTileVal(prevTile, prevVal * 2);
90+
this.setTileVal(currTile, 0);
91+
}
92+
}
93+
},
94+
equal: function(tile1, tile2){
95+
return tile1.getAttribute('val') == tile2.getAttribute('val');
96+
},
97+
max: function(){
98+
for(var i = 0, len = this.tiles.length; i < len; i++){
99+
if(this.tiles[i].getAttribute('val') == 2048){
100+
return true;
101+
}
102+
}
103+
},
104+
over: function(){
105+
for(var i = 0, len = this.tiles.length; i < len; i++){
106+
if(this.tiles[i].getAttribute('val') == 0){
107+
return false;
108+
}
109+
if(i % 4 != 3){
110+
if(this.equal(this.tiles[i], this.tiles[i + 1])){
111+
return false;
112+
}
113+
}
114+
if(i < 12){
115+
if(this.equal(this.tiles[i], this.tiles[i + 4])){
116+
return false;
117+
}
118+
}
119+
}
120+
return true;
121+
},
122+
clean: function(){
123+
for(var i = 0, len = this.tiles.length; i < len; i++){
124+
this.container.removeChild(this.tiles[i]);
125+
}
126+
this.tiles = new Array(16);
127+
}
128+
}
129+
130+
var game, startBtn;
131+
132+
window.onload = function(){
133+
var container = document.getElementById('div2048');
134+
startBtn = document.getElementById('start');
135+
startBtn.onclick = function(){
136+
this.style.display = 'none';
137+
game = game || new game2048(container);
138+
game.init();
139+
}
140+
}
141+
142+
window.onkeydown = function(e){
143+
var keynum, keychar;
144+
if(window.event){ // IE
145+
keynum = e.keyCode;
146+
}
147+
else if(e.which){ // Netscape/Firefox/Opera
148+
keynum = e.which;
149+
}
150+
keychar = String.fromCharCode(keynum);
151+
if(['W', 'S', 'A', 'D'].indexOf(keychar) > -1){
152+
if(game.over()){
153+
game.clean();
154+
startBtn.style.display = 'block';
155+
startBtn.innerHTML = 'game over, replay?';
156+
return;
157+
}
158+
game.move(keychar);
159+
}
160+
}

0 commit comments

Comments
 (0)