Skip to content

Commit ab6b3df

Browse files
committed
Section 22: Refactoring for Rectangular Mazes
1 parent a299cbd commit ab6b3df

3 files changed

Lines changed: 28 additions & 21 deletions

File tree

22-Javascript-with-the-Canvas-API/02-Maze copy/index.html renamed to 22-Javascript-with-the-Canvas-API/02-Maze/index.html

File renamed without changes.

22-Javascript-with-the-Canvas-API/02-Maze copy/index.js renamed to 22-Javascript-with-the-Canvas-API/02-Maze/index.js

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Render.run(render)
2626
Runner.run(Runner.create(), engine)
2727

2828
// Walls
29+
2930
const walls = [
3031
Bodies.rectangle(width / 2, 0, width, 2, { isStatic: true }),
3132
Bodies.rectangle(width / 2, height, width, 2, { isStatic: true }),
@@ -53,22 +54,22 @@ const shuffle = (arr) => {
5354
return arr
5455
}
5556

56-
const grid = Array(cells)
57+
const grid = Array(cellsVertical)
5758
.fill(null)
58-
.map(() => Array(cells).fill(false))
59+
.map(() => Array(cellsHorizontal).fill(false))
5960

60-
const verticals = Array(cells)
61+
const verticals = Array(cellsVertical)
6162
.fill(null)
62-
.map(() => Array(cells - 1).fill(false))
63+
.map(() => Array(cellsHorizontal - 1).fill(false))
6364

64-
const horizontals = Array(cells - 1)
65+
const horizontals = Array(cellsVertical - 1)
6566
.fill(null)
66-
.map(() => Array(cells).fill(false))
67+
.map(() => Array(cellsHorizontal).fill(false))
6768

6869
// console.log(horizontals)
6970

70-
const startRow = Math.floor(Math.random() * 3)
71-
const startColumn = Math.floor(Math.random() * 3)
71+
const startRow = Math.floor(Math.random() * cellsVertical)
72+
const startColumn = Math.floor(Math.random() * cellsHorizontal)
7273

7374
// console.log(startRow, startColumn);
7475

@@ -96,9 +97,9 @@ const stepThroughCell = (row, column) => {
9697
// See if that neighbor is out of bounds
9798
if (
9899
nextRow < 0 ||
99-
nextRow >= cells ||
100+
nextRow >= cellsVertical ||
100101
nextColumn < 0 ||
101-
nextColumn >= cells
102+
nextColumn >= cellsHorizontal
102103
) {
103104
continue
104105
}
@@ -137,9 +138,9 @@ horizontals.forEach((row, rowIndex) => {
137138
}
138139

139140
const wall = Bodies.rectangle(
140-
columnIndex * unitLength + unitLength / 2,
141-
rowIndex * unitLength + unitLength,
142-
unitLength,
141+
columnIndex * unitLengthX + unitLengthX / 2,
142+
rowIndex * unitLengthY + unitLengthY,
143+
unitLengthX,
143144
10,
144145
{
145146
label: 'wall',
@@ -157,10 +158,10 @@ verticals.forEach((row, rowIndex) => {
157158
}
158159

159160
const wall = Bodies.rectangle(
160-
columnIndex * unitLength + unitLength,
161-
rowIndex * unitLength + unitLength / 2,
161+
columnIndex * unitLengthX + unitLengthX,
162+
rowIndex * unitLengthY + unitLengthY / 2,
162163
10,
163-
unitLength,
164+
unitLengthY,
164165
{
165166
label: 'wall',
166167
isStatic: true,
@@ -171,11 +172,12 @@ verticals.forEach((row, rowIndex) => {
171172
})
172173

173174
// Goal
175+
174176
const goal = Bodies.rectangle(
175-
width - unitLength / 2,
176-
height - unitLength / 2,
177-
unitLength * 0.7,
178-
unitLength * 0.7,
177+
width - unitLengthX / 2,
178+
height - unitLengthY / 2,
179+
unitLengthX * 0.7,
180+
unitLengthY * 0.7,
179181
{
180182
label: 'goal',
181183
isStatic: true,
@@ -184,7 +186,9 @@ const goal = Bodies.rectangle(
184186
World.add(world, goal)
185187

186188
// Ball
187-
const ball = Bodies.circle(unitLength / 2, unitLength / 2, unitLength / 4, {
189+
190+
const ballRadius = Math.min(unitLengthX, unitLengthY) / 4
191+
const ball = Bodies.circle(unitLengthX / 2, unitLengthY / 2, ballRadius, {
188192
label: 'ball',
189193
})
190194
World.add(world, ball)
@@ -214,6 +218,7 @@ document.addEventListener('keydown', (event) => {
214218
})
215219

216220
// Win Condition
221+
217222
Events.on(engine, 'collisionStart', (event) => {
218223
// console.log(event);
219224
event.pairs.forEach((collision) => {

22-Javascript-with-the-Canvas-API/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,5 @@ console.log(grid)
9898
## 29. 33 Understanding the New Unit Variables
9999

100100
## 30. 34 Refactoring for Rectangular Mazes
101+
102+
## 31. 35 Adding Fill Colors

0 commit comments

Comments
 (0)