-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy path035.推箱子之送行者.html
253 lines (233 loc) · 8.35 KB
/
035.推箱子之送行者.html
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.1">
<link rel="stylesheet" href="./assets/global.css">
<style>
body {
padding-top: 20px;
}
.sokoban {
margin: 0 20px 20px;
position: relative;
display: flex;
align-items: center;
justify-content: center;
font-size: 12px;
}
.sokoban-item {
width: 20px;
height: 20px;
background-color: saddlebrown;
position: absolute;
display: flex;
align-items: center;
justify-content: center;
color: #fff;
font-size: 16px;
}
.operate {
display: flex;
flex-wrap: wrap;
margin: 0 20px;
width: 120px;
padding: 20px 0;
}
.operate .btn {
width: 40px;
height: 40px;
display: flex;
align-items: center;
justify-content: center;
user-select: none;
}
.operate .btn:active {
background-color: saddlebrown;
color: #fff;
transition: all .4s ease-in-out;
}
.operate .flex {
width: 100%;
}
.desc {
margin: 20px;
font-size: 12px;
color: rgba(0, 0, 0, 0.7);
}
</style>
</head>
<body>
<div class="sokoban"> </div>
<div class="desc">之所以使用如此简约的页面,当然是...找不到素材了...无奈之举自己也不会画,玩法:通过上下左右键或屏幕上的上下左右将棺埋入的口中。</div>
<div class="operate">
<div class="btn flex">上</div>
<div class="btn">左</div>
<div class="btn">下</div>
<div class="btn">右</div>
</div>
<script type="module">
// 宽度
let width = 20, height = 20;
// 地图枚举
let mapEnum = {
blank: 1, // 空白格
player: 2, // 玩家
box: 4, // 箱子
target: 8, // 目标
wall: 16 // 墙
}
let map =
// [
// [16, 16, 16, 16, 16],
// [16, 9, 1, 1, 16],
// [16, 1, 5, 1, 16],
// [16, 1, 1, 3, 16],
// [16, 16, 16, 16, 16]
// ]
// [
// [16, 16, 16, 16, 16, 16],
// [16, 1, 1, 9, 9, 16],
// [16, 1, 5, 1, 1, 16],
// [16, 1, 5, 13, 1, 16],
// [16, 16, 3, 1, 16, 16],
// [1, 16, 16, 16, 16, 1],
// ]
// [
// [16, 16, 16, 16, 16, 16],
// [16, 1, 1, 9, 1, 16],
// [16, 1, 5, 3, 1, 16],
// [16, 9, 16, 5, 1, 16],
// [16, 1, 5, 1, 9, 16],
// [16, 16, 16, 16, 16, 16],
// ]
[
[16, 16, 16, 16, 16, 16, 16],
[16, 1, 1, 1, 1, 1, 16],
[16, 1, 1, 1, 1, 1, 16],
[16, 5, 5, 5, 5, 5, 16],
[16, 9, 11, 9, 9, 9, 16],
[16, 16, 16, 16, 16, 16, 16],
]
let player = { x: 0, y: 0 }
let sokobanDom = document.querySelector('.sokoban')
let sokobanMapDom = {}
let operateDom = document.querySelectorAll('.operate > .btn')
// 初始化
function init() {
let ylength = map.length, xlength = 0
for (let y = 0; y < map.length; y++) {
const xmap = map[y];
xlength = Math.max(xlength, xmap.length)
for (let x = 0; x < xmap.length; x++) {
if ((xmap[x] & mapEnum.player) != 0) {
player = { x, y }
}
}
}
sokobanDom.setAttribute('style', `width:${xlength * width}px;height:${ylength * height}px`)
for (let i = 0; i < operateDom.length; i++) {
const btn = operateDom.item(i);
btn.addEventListener('click', function () {
onKeyDown(['ArrowUp', 'ArrowLeft', 'ArrowDown', 'ArrowRight'][i])
})
}
}
// 渲染
function renderMap() {
for (let y = 0; y < map.length; y++) {
const xmap = map[y];
for (let x = 0; x < xmap.length; x++) {
let sokobanItem = null;
if (sokobanMapDom[`${x},${y}`]) {
sokobanItem = sokobanMapDom[`${x},${y}`];
sokobanItem.textContent = ''
}
else {
sokobanItem = document.createElement('div')
sokobanItem.classList.add('sokoban-item')
sokobanItem.setAttribute(`style`, `left:${x * width}px;top:${y * height}px`)
sokobanDom.appendChild(sokobanItem)
sokobanMapDom[`${x},${y}`] = sokobanItem
}
if (xmap[x] == mapEnum.wall) {
sokobanItem.textContent = '墙'
}
if (xmap[x] == (mapEnum.blank + mapEnum.target)) {
sokobanItem.textContent = '口'
}
if (xmap[x] == (mapEnum.blank + mapEnum.box)) {
sokobanItem.textContent = '棺'
}
if (xmap[x] == (mapEnum.blank + mapEnum.player)) {
sokobanItem.textContent = '人'
}
if (xmap[x] == (mapEnum.blank + mapEnum.box + mapEnum.target)) {
sokobanItem.textContent = '坟'
}
if (xmap[x] == (mapEnum.blank + mapEnum.player + mapEnum.target)) {
sokobanItem.textContent = '囚'
}
}
}
}
// 检查游戏是否结束
function checkGameOver() {
for (let y = 0; y < map.length; y++) {
const xmap = map[y];
for (let x = 0; x < xmap.length; x++) {
if ((xmap[x] & mapEnum.target) != 0 && (xmap[x] & mapEnum.box) == 0) {
return false;
}
}
}
return true;
}
// 走
function walk(x, y) {
let tx = player.x + x, ty = player.y + y;
// 假如这里是盒子的话
if ((map[ty][tx] & mapEnum.box) != 0) {
let bx = tx + x, by = ty + y;
// 如果是盒子叠盒子无法移动
if ((map[by][bx] & mapEnum.box) != 0) {
return;
}
// 假如盒子移动的方向包含地板,则允许移动
if ((map[by][bx] & mapEnum.blank) != 0) {
map[ty][tx] -= mapEnum.box;
map[by][bx] += mapEnum.box;
// 假如盒子移动的目标位置包含目标位置则检测游戏是否胜利
if ((map[by][bx] & mapEnum.target) != 0 && checkGameOver()) {
sokobanDom.innerHTML = `恭喜你安置了一切`
}
}
}
// 假如这里包含地板
if ((map[ty][tx] & mapEnum.blank) != 0 && (map[ty][tx] & mapEnum.box) == 0) {
map[player.y][player.x] -= mapEnum.player;
map[ty][tx] += mapEnum.player
player.x = tx;
player.y = ty;
}
renderMap()
}
function onKeyDown(key) {
let x = 0, y = 0;
if (key == 'ArrowUp') y -= 1
if (key == 'ArrowDown') y += 1
if (key == 'ArrowLeft') x -= 1
if (key == 'ArrowRight') x += 1
try {
if (x || y) walk(x, y)
} catch (error) {
// 报错就不处理,任性哈哈哈哈
}
}
document.addEventListener("keydown", (ev) => onKeyDown(ev.key))
init();
renderMap()
</script>
</body>
</html>