-
Notifications
You must be signed in to change notification settings - Fork 0
/
Basic Sokoban Game
309 lines (259 loc) · 8.22 KB
/
Basic Sokoban Game
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
<!DOCTYPE html>
<html>
<head>
<title>Basic Sokoban HTML Game</title>
<meta charset="UTF-8">
<style>
html, body {
height: 100%;
margin: 0;
}
body {
background: #ded6ae;
display: flex;
align-items: center;
justify-content: center;
}
</style>
</head>
<body>
<canvas width="400" height="400" id="game"></canvas>
<script>
const canvas = document.getElementById('game');
const context = canvas.getContext('2d');
const grid = 64;
// create a new canvas and draw the wall image. then we can use this
// canvas to draw the images later on
const wallCanvas = document.createElement('canvas');
const wallCtx = wallCanvas.getContext('2d');
wallCanvas.width = wallCanvas.height = grid;
wallCtx.fillStyle = '#5b5530';
wallCtx.fillRect(0, 0, grid, grid);
wallCtx.fillStyle = '#a19555';
// 1st row brick
wallCtx.fillRect(1, 1, grid - 2, 20);
// 2nd row bricks
wallCtx.fillRect(0, 23, 20, 18);
wallCtx.fillRect(22, 23, 42, 18);
// 3rd row bricks
wallCtx.fillRect(0, 43, 42, 20);
wallCtx.fillRect(44, 43, 20, 20);
// the direction to move the player each frame. we'll use change in
// direction so "row: 1" means move down 1 row, "row: -1" means move
// up one row, etc.
let playerDir = { row: 0, col: 0 };
let playerPos = { row: 0, col: 0 }; // player position in the 2d array
let rAF = null; // keep track of the animation frame so we can cancel it
let width = 0; // find the largest row and use that as the game width
// create a mapping of object types using the sok file format
// @see http://www.sokobano.de/wiki/index.php?title=Level_format
const types = {
wall: '#',
player: '@',
playerOnGoal: '+',
block: '$',
blockOnGoal: '*',
goal: '.',
empty: ' '
};
// a sokoban level using the sok file format
const level1 = `
#####
### #
#.@$ #
### $.#
#.##$ #
# # . ##
#$ *$$.#
# . #
########
`;
// keep track of what is in every cell of the game using a 2d array
const cells = [];
// use each line of the level as the row (remove empty lines)
level1.split('\n')
.filter(rowData => !!rowData)
.forEach((rowData, row) => {
cells[row] = [];
if (rowData.length > width) {
width = rowData.length;
}
// use each character of the level as the col
rowData.split('').forEach((colData, col) => {
cells[row][col] = colData;
if (colData === types.player || colData === types.playerOnGoal) {
playerPos = { row, col };
}
});
});
// update the size of the canvas to the level size
canvas.width = width * grid;
canvas.height = cells.length * grid;
// move an entity from one cell to another
function move(startPos, endPos) {
const startCell = cells[startPos.row][startPos.col];
const endCell = cells[endPos.row][endPos.col];
const isPlayer = startCell === types.player || startCell === types.playerOnGoal;
// first remove then entity from its current cell
switch(startCell) {
// if the start cell is the player or a block (no goal)
// then leave empty
case types.player:
case types.block:
cells[startPos.row][startPos.col] = types.empty;
break;
// if the start cell has a goal then leave a goal
case types.playerOnGoal:
case types.blockOnGoal:
cells[startPos.row][startPos.col] = types.goal;
break;
}
// then move then entity into the new cell
switch(endCell) {
// if the end cell is empty, add the block or player
case types.empty:
cells[endPos.row][endPos.col] = isPlayer ? types.player : types.block;
break;
// if the cell has a goal then make sure to preserve the goal
case types.goal:
cells[endPos.row][endPos.col] = isPlayer ? types.playerOnGoal : types.blockOnGoal;
break;
}
}
// show the win screen
function showWin() {
cancelAnimationFrame(rAF);
context.fillStyle = 'black';
context.globalAlpha = 0.75;
context.fillRect(0, canvas.height / 2 - 30, canvas.width, 60);
context.globalAlpha = 1;
context.fillStyle = 'white';
context.font = '36px monospace';
context.textAlign = 'center';
context.textBaseline = 'middle';
context.fillText('YOU WIN!', canvas.width / 2, canvas.height / 2);
}
// game loop
function loop() {
rAF = requestAnimationFrame(loop);
context.clearRect(0,0,canvas.width,canvas.height);
// check to see if the player can move in the desired direction
const row = playerPos.row + playerDir.row;
const col = playerPos.col + playerDir.col;
const cell = cells[row][col];
switch(cell) {
// allow the player to move into empty or goal cells
case types.empty:
case types.goal:
move(playerPos, { row, col });
playerPos.row = row;
playerPos.col = col;
break;
// don't allow the player to move into a wall cell
case types.wall:
break;
// only allow the player to move into a block cell if the cell
// after the block is empty or a goal
case types.block:
case types.blockOnGoal:
const nextRow = row + playerDir.row;
const nextCol = col + playerDir.col;
const nextCell = cells[nextRow][nextCol];
if (nextCell === types.empty || nextCell === types.goal) {
// move the block first, then the player
move({ row, col }, { row: nextRow, col: nextCol });
move(playerPos, { row, col });
playerPos.row = row;
playerPos.col = col;
}
break;
}
// reset player dir after checking move
playerDir = { row: 0, col: 0 };
// check to see if all blocks are on goals
let allBlocksOnGoals = true;
// draw the board. because multiple things can be drawn on the same
// cell we shouldn't use a switch as that would only allow us to draw
// a single thing per cell
context.strokeStyle = 'black';
context.lineWidth = 2;
for (let row = 0; row < cells.length; row++) {
for (let col = 0; col < cells[row].length; col++) {
const cell = cells[row][col];
if (cell === types.wall) {
context.drawImage(wallCanvas, col * grid, row * grid);
}
if (cell === types.block || cell === types.blockOnGoal) {
if (cell === types.block) {
context.fillStyle = '#ffbb5b';
// block is not on goal
allBlocksOnGoals = false;
}
else {
context.fillStyle = '#ba6a15';
}
context.fillRect(col * grid, row * grid, grid, grid);
context.strokeRect(col * grid, row * grid, grid, grid);
context.strokeRect((col + 0.1) * grid, (row + 0.1) * grid, grid - (0.2 * grid), grid - (0.2 * grid));
// X
context.beginPath();
context.moveTo((col + 0.1) * grid, (row + 0.1) * grid);
context.lineTo((col + 0.9) * grid, (row + 0.9) * grid);
context.moveTo((col + 0.9) * grid, (row + 0.1) * grid);
context.lineTo((col + 0.1) * grid, (row + 0.9) * grid);
context.stroke();
}
if (cell === types.goal || cell === types.playerOnGoal) {
context.fillStyle = '#914430';
context.beginPath();
context.arc((col + 0.5) * grid, (row + 0.5) * grid, 10, 0, Math.PI * 2);
context.fill();
}
if (cell === types.player || cell === types.playerOnGoal) {
context.fillStyle = 'black';
context.beginPath();
// head
context.arc((col + 0.5) * grid, (row + 0.3) * grid, 8, 0, Math.PI * 2);
context.fill();
// body
context.fillRect((col + 0.48) * grid, (row + 0.3) * grid, 2, grid/ 2.5 );
// arms
context.fillRect((col + 0.3) * grid, (row + 0.5) * grid, grid / 2.5, 2);
// legs
context.moveTo((col + 0.5) * grid, (row + 0.7) * grid);
context.lineTo((col + 0.65) * grid, (row + 0.9) * grid);
context.moveTo((col + 0.5) * grid, (row + 0.7) * grid);
context.lineTo((col + 0.35) * grid, (row + 0.9) * grid);
context.stroke();
}
}
}
if (allBlocksOnGoals) {
showWin();
}
}
// listen to keyboard events to move the snake
document.addEventListener('keydown', function(e) {
playerDir = { row: 0, col: 0};
// left arrow key
if (e.which === 37) {
playerDir.col = -1;
}
// up arrow key
else if (e.which === 38) {
playerDir.row = -1;
}
// right arrow key
else if (e.which === 39) {
playerDir.col = 1;
}
// down arrow key
else if (e.which === 40) {
playerDir.row = 1;
}
});
// start the game
requestAnimationFrame(loop);
</script>
</body>
</html>