Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Cloud IDE
2048
提交
bd7f8963
2048
项目概览
Cloud IDE
/
2048
通知
101
Star
9
Fork
69
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
2048
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
bd7f8963
编写于
3月 22, 2014
作者:
G
Gabriele Cirulli
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'resume-game' of github.com:mfrederiksen/2048 into mfrederiksen-resume-game
上级
a875a3ab
02a24c06
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
127 addition
and
30 deletion
+127
-30
index.html
index.html
+1
-1
js/game_manager.js
js/game_manager.js
+37
-14
js/grid.js
js/grid.js
+35
-7
js/keyboard_input_manager.js
js/keyboard_input_manager.js
+4
-0
js/local_score_manager.js
js/local_score_manager.js
+18
-5
js/tile.js
js/tile.js
+10
-0
style/main.css
style/main.css
+14
-2
style/main.scss
style/main.scss
+8
-1
未找到文件。
index.html
浏览文件 @
bd7f8963
...
...
@@ -23,7 +23,7 @@
</div>
</div>
<p
class=
"game-intro"
>
Join the numbers and get to the
<strong>
2048 tile!
</strong></p>
<a
class=
"restart-button"
>
New Game
</a>
<div
class=
"game-container"
>
<div
class=
"game-message"
>
<p></p>
...
...
js/game_manager.js
浏览文件 @
bd7f8963
function
GameManager
(
size
,
InputManager
,
Actuator
,
S
cor
eManager
)
{
function
GameManager
(
size
,
InputManager
,
Actuator
,
S
torag
eManager
)
{
this
.
size
=
size
;
// Size of the grid
this
.
inputManager
=
new
InputManager
;
this
.
s
coreManager
=
new
Scor
eManager
;
this
.
s
torageManager
=
new
Storag
eManager
;
this
.
actuator
=
new
Actuator
;
this
.
startTiles
=
2
;
...
...
@@ -15,6 +15,7 @@ function GameManager(size, InputManager, Actuator, ScoreManager) {
// Restart the game
GameManager
.
prototype
.
restart
=
function
()
{
this
.
storageManager
.
clearGameState
();
this
.
actuator
.
continue
();
this
.
setup
();
};
...
...
@@ -35,15 +36,24 @@ GameManager.prototype.isGameTerminated = function () {
// Set up the game
GameManager
.
prototype
.
setup
=
function
()
{
this
.
grid
=
new
Grid
(
this
.
size
);
this
.
score
=
0
;
this
.
over
=
false
;
this
.
won
=
false
;
this
.
keepPlaying
=
false
;
// Add the initial tiles
this
.
addStartTiles
();
var
previousGameState
=
this
.
storageManager
.
getGameState
();
if
(
previousGameState
)
{
this
.
grid
=
new
Grid
(
previousGameState
.
grid
.
size
,
previousGameState
.
grid
.
cells
);
this
.
score
=
previousGameState
.
score
;
this
.
over
=
previousGameState
.
over
;
this
.
won
=
previousGameState
.
won
;
this
.
keepPlaying
=
previousGameState
.
keepPlaying
;
}
else
{
this
.
grid
=
new
Grid
(
this
.
size
);
this
.
score
=
0
;
this
.
over
=
false
;
this
.
won
=
false
;
this
.
keepPlaying
=
false
;
// Add the initial tiles
this
.
addStartTiles
();
}
// Update the actuator
this
.
actuate
();
...
...
@@ -68,20 +78,33 @@ GameManager.prototype.addRandomTile = function () {
// Sends the updated grid to the actuator
GameManager
.
prototype
.
actuate
=
function
()
{
if
(
this
.
s
coreManager
.
get
()
<
this
.
score
)
{
this
.
s
coreManager
.
set
(
this
.
score
);
if
(
this
.
s
torageManager
.
getBestScore
()
<
this
.
score
)
{
this
.
s
torageManager
.
setBestScore
(
this
.
score
);
}
this
.
storageManager
.
setGameState
(
this
.
serializeGameState
());
this
.
actuator
.
actuate
(
this
.
grid
,
{
score
:
this
.
score
,
over
:
this
.
over
,
won
:
this
.
won
,
bestScore
:
this
.
s
coreManager
.
get
(),
bestScore
:
this
.
s
torageManager
.
getBestScore
(),
terminated
:
this
.
isGameTerminated
()
});
};
GameManager
.
prototype
.
serializeGameState
=
function
()
{
return
{
size
:
this
.
size
,
grid
:
this
.
grid
.
gridState
(),
score
:
this
.
score
,
over
:
this
.
over
,
won
:
this
.
won
,
keepPlaying
:
this
.
keepPlaying
};
}
// Save all tile positions and remove merger info
GameManager
.
prototype
.
prepareTiles
=
function
()
{
this
.
grid
.
eachCell
(
function
(
x
,
y
,
tile
)
{
...
...
js/grid.js
浏览文件 @
bd7f8963
function
Grid
(
size
)
{
function
Grid
(
size
,
previousCellState
)
{
this
.
size
=
size
;
this
.
cells
=
[];
this
.
build
();
this
.
cells
=
previousCellState
?
this
.
buildFromPreviousState
(
previousCellState
)
:
this
.
buildNew
();
}
// Build a grid of the specified size
Grid
.
prototype
.
build
=
function
()
{
Grid
.
prototype
.
buildNew
=
function
()
{
var
cells
=
[];
for
(
var
x
=
0
;
x
<
this
.
size
;
x
++
)
{
var
row
=
this
.
cells
[
x
]
=
[];
var
row
=
cells
[
x
]
=
[];
for
(
var
y
=
0
;
y
<
this
.
size
;
y
++
)
{
row
.
push
(
null
);
}
}
return
cells
;
};
Grid
.
prototype
.
buildFromPreviousState
=
function
(
state
)
{
var
cells
=
[];
for
(
var
x
=
0
;
x
<
this
.
size
;
x
++
)
{
var
row
=
cells
[
x
]
=
[];
for
(
var
y
=
0
;
y
<
this
.
size
;
y
++
)
{
var
tileState
=
state
[
x
][
y
];
row
.
push
(
tileState
?
new
Tile
(
tileState
.
position
,
tileState
.
value
)
:
null
);
}
}
return
cells
;
};
// Find the first available random position
...
...
@@ -82,3 +94,19 @@ Grid.prototype.withinBounds = function (position) {
return
position
.
x
>=
0
&&
position
.
x
<
this
.
size
&&
position
.
y
>=
0
&&
position
.
y
<
this
.
size
;
};
Grid
.
prototype
.
gridState
=
function
()
{
var
cellState
=
[];
for
(
var
x
=
0
;
x
<
this
.
size
;
x
++
)
{
var
row
=
cellState
[
x
]
=
[];
for
(
var
y
=
0
;
y
<
this
.
size
;
y
++
)
{
row
.
push
(
this
.
cells
[
x
][
y
]
?
this
.
cells
[
x
][
y
].
tileState
()
:
null
);
}
}
return
{
size
:
this
.
size
,
cells
:
cellState
}
};
js/keyboard_input_manager.js
浏览文件 @
bd7f8963
...
...
@@ -68,6 +68,10 @@ KeyboardInputManager.prototype.listen = function () {
retry
.
addEventListener
(
"
click
"
,
this
.
restart
.
bind
(
this
));
retry
.
addEventListener
(
this
.
eventTouchend
,
this
.
restart
.
bind
(
this
));
var
restart
=
document
.
querySelector
(
"
.restart-button
"
);
restart
.
addEventListener
(
"
click
"
,
this
.
restart
.
bind
(
this
));
restart
.
addEventListener
(
"
touchend
"
,
this
.
restart
.
bind
(
this
));
var
keepPlaying
=
document
.
querySelector
(
"
.keep-playing-button
"
);
keepPlaying
.
addEventListener
(
"
click
"
,
this
.
keepPlaying
.
bind
(
this
));
keepPlaying
.
addEventListener
(
"
touchend
"
,
this
.
keepPlaying
.
bind
(
this
));
...
...
js/local_score_manager.js
浏览文件 @
bd7f8963
...
...
@@ -19,7 +19,8 @@ window.fakeStorage = {
};
function
LocalScoreManager
()
{
this
.
key
=
"
bestScore
"
;
this
.
bestScoreKey
=
"
bestScore
"
;
this
.
gameStateKey
=
"
gameState
"
;
var
supported
=
this
.
localStorageSupported
();
this
.
storage
=
supported
?
window
.
localStorage
:
window
.
fakeStorage
;
...
...
@@ -38,11 +39,23 @@ LocalScoreManager.prototype.localStorageSupported = function () {
}
};
LocalScoreManager
.
prototype
.
get
=
function
()
{
return
this
.
storage
.
getItem
(
this
.
k
ey
)
||
0
;
LocalScoreManager
.
prototype
.
get
BestScore
=
function
()
{
return
this
.
storage
.
getItem
(
this
.
bestScoreK
ey
)
||
0
;
};
LocalScoreManager
.
prototype
.
set
=
function
(
score
)
{
this
.
storage
.
setItem
(
this
.
k
ey
,
score
);
LocalScoreManager
.
prototype
.
set
BestScore
=
function
(
score
)
{
this
.
storage
.
setItem
(
this
.
bestScoreK
ey
,
score
);
};
LocalScoreManager
.
prototype
.
getGameState
=
function
()
{
var
stateJSON
=
this
.
storage
.
getItem
(
this
.
gameStateKey
);
return
stateJSON
?
JSON
.
parse
(
stateJSON
)
:
null
;
};
LocalScoreManager
.
prototype
.
setGameState
=
function
(
gameState
)
{
this
.
storage
.
setItem
(
this
.
gameStateKey
,
JSON
.
stringify
(
gameState
));
};
LocalScoreManager
.
prototype
.
clearGameState
=
function
()
{
this
.
storage
.
removeItem
(
this
.
gameStateKey
);
};
js/tile.js
浏览文件 @
bd7f8963
...
...
@@ -15,3 +15,13 @@ Tile.prototype.updatePosition = function (position) {
this
.
x
=
position
.
x
;
this
.
y
=
position
.
y
;
};
Tile
.
prototype
.
tileState
=
function
()
{
return
{
position
:
{
x
:
this
.
x
,
y
:
this
.
y
},
value
:
this
.
value
};
}
\ No newline at end of file
style/main.css
浏览文件 @
bd7f8963
...
...
@@ -143,8 +143,21 @@ hr {
100
%
{
opacity
:
1
;
}
}
.restart-button
{
display
:
inline-block
;
background
:
#8f7a66
;
border-radius
:
3px
;
padding
:
0
20px
;
text-decoration
:
none
;
color
:
#f9f6f2
;
height
:
40px
;
line-height
:
42px
;
display
:
block
;
width
:
100px
;
margin
:
10px
auto
10px
auto
;
text-align
:
center
;
}
.game-container
{
margin-top
:
40px
;
position
:
relative
;
padding
:
15px
;
cursor
:
default
;
...
...
@@ -516,7 +529,6 @@ hr {
margin-bottom
:
10px
;
}
.game-container
{
margin-top
:
40px
;
position
:
relative
;
padding
:
10px
;
cursor
:
default
;
...
...
style/main.scss
浏览文件 @
bd7f8963
...
...
@@ -168,10 +168,17 @@ hr {
line-height
:
42px
;
}
.restart-button
{
@include
button
;
display
:
block
;
width
:
100px
;
margin
:
10px
auto
10px
auto
;
text-align
:
center
;
}
// Game field mixin used to render CSS at different width
@mixin
game-field
{
.game-container
{
margin-top
:
40px
;
position
:
relative
;
padding
:
$grid-spacing
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录