简介

自从,上次完成了一个推箱子的简易版本,自然想实现一个牛掰一点的可以实现多个关卡的推箱子小游戏:


实现思路

1、我们首先使用对象保存每个对象的信息:

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
//level表示关卡
//state为地图,其中0表示无,1表示隔离,2表示空地,3表示目标,4表示箱子,5表示当前位置
//person代表当前位置
//box代表目标位置

const level = [
{
state:[
[0,0,0,1,1,1,1,1,1,0],
[0,1,1,1,2,2,2,2,1,0],
[1,1,3,2,4,1,1,2,1,1],
[1,3,3,4,2,4,2,2,5,1],
[1,3,3,2,4,2,4,2,1,1],
[1,1,1,1,1,1,2,2,1,0],
[0,0,0,0,0,1,1,1,1,0]
],
person:{x:8,y:3},
box: [{x:1,y:3},{x:1,y:4},{x:2,y:2},{x:2,y:3},{x:2,y:4}]
},
{
state:[
[0,1,1,1,1,1,0,0],
[0,1,2,2,1,1,1,0],
[0,1,5,4,2,2,1,0],
[1,1,1,2,1,2,1,1],
[1,3,1,2,1,2,2,1],
[1,3,4,2,2,1,2,1],
[1,3,2,2,2,4,2,1],
[1,1,1,1,1,1,1,1]
],
person: {x : 2, y : 2},
box: [{x:1, y : 4}, {x:1,y:5} ,{x:1, y:6}]
}
]

2、对每个关卡新建一个sokoban对象, 这个sokoban对象如何实现推箱子的基本逻辑可以看我的上一篇。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
chapterFirst.onclick = () => {
levelNum = 0;
const sokoban = new Sokoban(levelNum,ctx,canvas.width,canvas.height);
sokoban.init();

reset.onclick = () => {
sokoban.reset(level,levelNum);
}
}
chapterSecond.onclick = () => {
levelNum = 1;
const sokoban = new Sokoban(levelNum,ctx,canvas.width,canvas.height);
sokoban.init();

reset.onclick = () => {
sokoban.reset(level,levelNum);
}
}

3、从关卡返回首页,可以重新选择关卡

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
let backImg = new Image();
backImg.src = 'images/back.png';

const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');

function home(){
if (backImg.complete) {
showHome()
}else {
showHome()
}
}

function showHome() {
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.drawImage(backImg,0,0,canvas.width,canvas.height);
ctx.font = "60px STCaiyun";
ctx.textAlign = 'center';
ctx.strokeStyle = '#DD6C0A';
ctx.lineWidth = 2;
ctx.strokeText('选择关卡',400,70);

chapter.style.display = 'block';
reset.style.display = "none";
}

遇到的问题

重置地图这里我遇到了一个新的问题!

1
2
3
4
5
6
reset(level,levelNumber) {
const temp = level[levelNumber];
this.myMap = temp.state;
this.curLoc = temp.person;
this.init();
}

这样是不能重置地图的!
因为js中我们的数组对象是引用类型,b = [1,2], a=b 在我们修改a中的元素后,b实际上已经改变了!

再举一个小小的例子:

1
2
3
4
var a=[{state:1}, {state:3}];
var b=a.slice(0);
b[0].state= 3;
a[0].state //3

使用slice是可以复制一个新的数组,但是我们的a内容里面都是对象(引用类型),所以仍然无用。

可以使用:

1
JSON.parse(JSON.stringify(object))

完整实现

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
<!DOCTYPE html>
<html>
<header>
<meta charset="utf-8">
<title>新推箱子</title>
<link rel="stylesheet" type="text/css" href="push.css">
</header>
<body>
<div class="background">
<div class="control">
<span id="returnHome"><<返回</span>
<span id="reset">重置</span>
<!-- <span>下一关</span> -->
</div>
<div class="chapterF">
<canvas id="myCanvas" width="800px" height="600px"></canvas>
<div class="chapter">
<button class="chapterItem" data="0">
第一关
</button>
<button class="chapterItem" data='1'>
第二关
</button>
</div>
</div>

</div>
</body>
<script src="save.js"></script>
</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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
const returnHome = document.getElementById('returnHome');
const reset = document.getElementById('reset');
const chapterFirst = document.getElementsByTagName('button')[0];
const chapterSecond = document.getElementsByTagName('button')[1];
const chapter = document.getElementsByClassName('chapter')[0];

let levelNum = 0;

let backImg = new Image();
backImg.src = 'images/back.png';

chapterFirst.onclick = () => {
levelNum = 0;
reset.style.display = 'block';
chapter.style.display = 'none';
const sokoban = new Sokoban(levelNum,ctx,canvas.width,canvas.height);
sokoban.init();

reset.onclick = () => {
sokoban.reset(level,levelNum);
}
}
chapterSecond.onclick = () => {
levelNum = 1;
reset.style.display = 'block';
chapter.style.display = 'none';
const sokoban = new Sokoban(levelNum,ctx,canvas.width,canvas.height);
sokoban.init();

reset.onclick = () => {
sokoban.reset(level,levelNum);
}
}
//level表示关卡
//state为地图,其中0表示无,1表示隔离,2表示空地,3表示目标,4表示箱子,5表示当前位置
//person代表当前位置
//box代表目标位置

const level = [
{
state:[
[0,0,0,1,1,1,1,1,1,0],
[0,1,1,1,2,2,2,2,1,0],
[1,1,3,2,4,1,1,2,1,1],
[1,3,3,4,2,4,2,2,5,1],
[1,3,3,2,4,2,4,2,1,1],
[1,1,1,1,1,1,2,2,1,0],
[0,0,0,0,0,1,1,1,1,0]
],
person:{x:8,y:3},
box: [{x:1,y:3},{x:1,y:4},{x:2,y:2},{x:2,y:3},{x:2,y:4}]
},
{
state:[
[0,1,1,1,1,1,0,0],
[0,1,2,2,1,1,1,0],
[0,1,5,4,2,2,1,0],
[1,1,1,2,1,2,1,1],
[1,3,1,2,1,2,2,1],
[1,3,4,2,2,1,2,1],
[1,3,2,2,2,4,2,1],
[1,1,1,1,1,1,1,1]
],
person: {x : 2, y : 2},
box: [{x:1, y : 4}, {x:1,y:5} ,{x:1, y:6}]
}
]

let Sokoban = function(levelNumber,ctx,width,height){
const temp = JSON.parse(JSON.stringify(level[levelNumber]));
this.myMap = temp.state;
this.curLoc = temp.person;
this.targetLoc = temp.box;
this.height = height;
this.width = width;
this.ctx = ctx;
this.imageArray = [new Image(),new Image(),new Image(),new Image(),new Image(),new Image()];
}

Sokoban.prototype = {
init() {
//准备好照片后自动生成地图
this.prepareImage();
window.onkeydown = (e) => {
let keyNum = window.event?e.keyCode:e.which;
e.preventDefault();
this.action(keyNum);
}
},
prepareImage() {
const imgSrcArray = ['images/1.png','images/2.png','images/3.png','images/4.png','images/8.png','images/tree.png'];
var imgLength = this.imageArray.length;
this.imageArray.forEach((current,index) => {
current.src = imgSrcArray[index];
//判断图片是否加载完成
if (current.complete) {
if(index == imgLength-1)
this.fillCell();
} else {
current.onload = () => {
if(index == imgLength-1) this.fillCell();
}
}
})
},
fillCell() {
const cellWidth = ~~(this.width/this.myMap[0].length +1);
const cellHeight = ~~(this.height/this.myMap.length +1);
// console.log('单个元素的宽为:' + cellWidth+',高为:'+cellHeight);
let mapType = null;
let ptn = null;
//i为第几行,j为第几列
for(let i = 0 ; i < this.myMap.length ; i++){
for(let j = 0 ; j < this.myMap[i].length ; j++){
mapType = this.myMap[i][j];
// console.log(mapType);
switch(mapType){
case 0: this.ctx.save();
this.ctx.clearRect(j*cellWidth,i*cellHeight,cellWidth,cellHeight);
this.ctx.fillStyle = '#333';
this.ctx.fillRect(j*cellWidth,i*cellHeight,cellWidth,cellHeight);
this.ctx.drawImage(this.imageArray[5],j*cellWidth,i*cellHeight,cellWidth,cellHeight);
this.ctx.restore();
break;
case 1: this.ctx.save();
this.ctx.clearRect(j*cellWidth,i*cellHeight,cellWidth,cellHeight);
ptn = this.ctx.createPattern(this.imageArray[0],'repeat');
this.ctx.shadowBlur=5;
this.ctx.shadowColor="black";
this.ctx.fillStyle = ptn;
this.ctx.fillRect(j*cellWidth,i*cellHeight,cellWidth,cellHeight);
this.ctx.restore();
break;
case 2: this.ctx.save();
this.ctx.clearRect(j*cellWidth,i*cellHeight,cellWidth,cellHeight);
ptn = this.ctx.createPattern(this.imageArray[1],'repeat');
this.ctx.fillStyle = ptn;
this.ctx.fillRect(j*cellWidth,i*cellHeight,cellWidth,cellHeight);
this.ctx.restore();
break;
case 3: this.ctx.save();
this.ctx.clearRect(j*cellWidth,i*cellHeight,cellWidth,cellHeight);
ptn = this.ctx.createPattern(this.imageArray[1],'repeat');
this.ctx.fillStyle = ptn;
this.ctx.fillRect(j*cellWidth,i*cellHeight,cellWidth,cellHeight);
this.ctx.drawImage(this.imageArray[2],j*cellWidth,i*cellHeight,cellWidth,cellHeight);
this.ctx.restore();
break;
case 4: this.ctx.save();
this.ctx.clearRect(j*cellWidth,i*cellHeight,cellWidth,cellHeight);
ptn = this.ctx.createPattern(this.imageArray[1],'repeat');
this.ctx.fillStyle = ptn;
this.ctx.fillRect(j*cellWidth,i*cellHeight,cellWidth,cellHeight);
this.ctx.drawImage(this.imageArray[3],j*cellWidth,i*cellHeight,cellWidth,cellHeight);
this.ctx.restore();
break;
case 5: this.ctx.save();
this.ctx.clearRect(j*cellWidth,i*cellHeight,cellWidth,cellHeight);
ptn = this.ctx.createPattern(this.imageArray[1],'repeat');
this.ctx.fillStyle = ptn;
this.ctx.fillRect(j*cellWidth,i*cellHeight,cellWidth,cellHeight);
this.ctx.drawImage(this.imageArray[4],j*cellWidth,i*cellHeight,cellWidth,cellHeight);
this.ctx.restore();
break;
default:break;
}
}
}
},
action(keyNum) {
let nextLoc = {};
let nextTwoStepLoc = {};
switch (keyNum) {
case 37: nextLoc.x = this.curLoc.x-1;
nextLoc.y = this.curLoc.y;
nextLoc.mapType = this.myMap[nextLoc.y][nextLoc.x];
//下下一步
nextTwoStepLoc.x = this.curLoc.x-2;
nextTwoStepLoc.y = this.curLoc.y;
nextTwoStepLoc.mapType = this.myMap[nextTwoStepLoc.y][nextTwoStepLoc.x];
//空地和目的地都可以随便走
if (nextLoc.mapType == 2 || nextLoc.mapType == 3) {
this.changeLoc(nextLoc);
this.fillCell();
}else if (nextLoc.mapType == 4 && (nextTwoStepLoc.mapType == 2 || nextTwoStepLoc.mapType == 3)) {
// console.log('有障碍');
this.myMap[nextTwoStepLoc.y][nextTwoStepLoc.x] = 4;
this.changeLoc(nextLoc);
this.fillCell();
}
this.check();
break;
case 38: nextLoc.x = this.curLoc.x;
nextLoc.y = this.curLoc.y-1;
nextLoc.mapType = this.myMap[nextLoc.y][nextLoc.x];
//下下一步
nextTwoStepLoc.x = this.curLoc.x;
nextTwoStepLoc.y = this.curLoc.y-2;
nextTwoStepLoc.mapType = this.myMap[nextTwoStepLoc.y][nextTwoStepLoc.x];
if (nextLoc.mapType == 2 || nextLoc.mapType == 3) {
this.changeLoc(nextLoc);
this.fillCell();
}else if (nextLoc.mapType == 4 && (nextTwoStepLoc.mapType == 2 || nextTwoStepLoc.mapType == 3)) {
// console.log('有障碍');
this.myMap[nextTwoStepLoc.y][nextTwoStepLoc.x] = 4;
this.changeLoc(nextLoc);
this.fillCell();
}
this.check();
break;
case 39: nextLoc.x = this.curLoc.x+1;
nextLoc.y = this.curLoc.y;
nextLoc.mapType = this.myMap[nextLoc.y][nextLoc.x];
//下下一步
nextTwoStepLoc.x = this.curLoc.x+2;
nextTwoStepLoc.y = this.curLoc.y;
nextTwoStepLoc.mapType = this.myMap[nextTwoStepLoc.y][nextTwoStepLoc.x];
if (nextLoc.mapType == 2 || nextLoc.mapType == 3) {
this.changeLoc(nextLoc);
this.fillCell();
}else if (nextLoc.mapType == 4 && (nextTwoStepLoc.mapType == 2 || nextTwoStepLoc.mapType == 3)) {
// console.log('有障碍');
this.myMap[nextTwoStepLoc.y][nextTwoStepLoc.x] = 4;
this.changeLoc(nextLoc);
this.fillCell();
}
this.check();
break;
case 40: nextLoc.x = this.curLoc.x;
nextLoc.y = this.curLoc.y+1;
nextLoc.mapType = this.myMap[nextLoc.y][nextLoc.x];
//下下一步
nextTwoStepLoc.x = this.curLoc.x;
nextTwoStepLoc.y = this.curLoc.y+2;
nextTwoStepLoc.mapType = this.myMap[nextTwoStepLoc.y][nextTwoStepLoc.x];
if (nextLoc.mapType == 2 || nextLoc.mapType == 3) {
this.changeLoc(nextLoc);
this.fillCell();
}else if (nextLoc.mapType == 4 && (nextTwoStepLoc.mapType == 2 || nextTwoStepLoc.mapType == 3)) {
// console.log('有障碍');
this.myMap[nextTwoStepLoc.y][nextTwoStepLoc.x] = 4;
this.changeLoc(nextLoc);
this.fillCell();
}
this.check();
break;
default: break;
}
},
changeLoc(nextLoc) {
this.myMap[nextLoc.y][nextLoc.x] = 5;
this.myMap[this.curLoc.y][this.curLoc.x] = 2;
//判断是否踩在目标点上
for(let i = this.targetLoc.length-1 ; i >= 0 ; i--){
if(this.curLoc.x == this.targetLoc[i].x && this.curLoc.y == this.targetLoc[i].y) {
this.myMap[this.curLoc.y][this.curLoc.x] = 3;
}
}
this.curLoc.x = nextLoc.x;
this.curLoc.y = nextLoc.y;
},
check(){
let count = 0;
let tot = this.targetLoc.length;
for(let i = this.targetLoc.length-1 ; i >= 0 ; i--){
if(this.myMap[this.targetLoc[i].y][this.targetLoc[i].x] == 4) {
count++;
}
}

if(count == tot) {
ctx.font = "60px STCaiyun";
ctx.textAlign = 'center';
ctx.strokeStyle = '#FFFFFF';
ctx.lineWidth = 2;
ctx.strokeText('闯关成功!',400,70);
};
},
reset(level,levelNumber) {
const temp = JSON.parse(JSON.stringify(level[levelNumber]));
this.myMap = temp.state;
this.curLoc = temp.person;
this.init();
}
}
function home(){
if (backImg.complete) {
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.drawImage(backImg,0,0,canvas.width,canvas.height);
ctx.font = "60px STCaiyun";
ctx.textAlign = 'center';
ctx.strokeStyle = '#DD6C0A';
ctx.lineWidth = 2;
ctx.strokeText('选择关卡',400,70);

chapter.style.display = 'block';
reset.style.display = "none";
}else {
backImg.onload = () => {
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.drawImage(backImg,0,0,canvas.width,canvas.height);
ctx.font = "60px STCaiyun";
ctx.textAlign = 'center';
ctx.strokeStyle = '#DD6C0A';
ctx.lineWidth = 2;
ctx.strokeText('选择关卡',400,70);

chapter.style.display = 'block';
reset.style.display = "none";
}
}
}


home();

returnHome.onclick = () => {
home();
}

canvas.onclick = (e) => {
console.log('pageX:'+e.pageX);
console.log('layerX:'+e.layerX);
console.log('offsetX:'+e.offsetX);
console.log('clientX:'+e.clientX);
console.log(e.target.offsetLeft);
}
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
/*$width: 800px;
$height: 600px;*/
body{
/*text-align: center;*/
}

.background{
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: radial-gradient(#F4A5E8,#F0CCCC,#F9EDF7);
}
svg{
display: block;
position: absolute;
width: 590px;
height: 200px;
top: 80px;
left: 50%;
transform: translate(-50%,-50%);
overflow: hidden;
background-size: cover;
}

.filtered{
filter: url(#filter);
color: #0777C7;
fill: #073B40;
font-family: 'Oleo Script', cursive;
font-size: 120px;
}

.control {
width: 800px;
height: 60px;
margin: 160px auto 0 auto;

background: -webkit-linear-gradient(left,red, blue); /* Safari 5.1 - 6.0 */
background: -o-linear-gradient(right,red, blue); /* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(right,red, blue); /* Firefox 3.6 - 15 */
background: linear-gradient(to right,#E18E0E,#F6E97E);
border-radius: 10px 10px 0 0;
display: flex;
justify-content: space-between;

}
.control span{
font-size:24px;
width: 100px;
color: white;
align-items: center;
text-align: center;
margin:10px;
cursor:pointer;
}

.control span:hover{
color: #DED5D5;
}

#reset{
display:none;
}
.chapterF {
position: relative;
margin: auto;
width: 800px;
}
#myCanvas {
margin: 0 auto;
}

.chapter {
position: absolute;
top: 250px;
left: 270px;
/*display: none;*/
}

.chapterItem{
margin: 10px;
background-color: #C97D07; /* Green */
border: none;
border-radius: 4px;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
cursor: pointer;
transition: background-color 0.5s;
}
.chapterItem:hover{
background-color: #E18E0E;
}