Skip to content

Commit ff40f59

Browse files
committed
Section 22: Drawing the Goal
1 parent cc4aa9d commit ff40f59

1 file changed

Lines changed: 28 additions & 14 deletions

File tree

  • 22-Javascript-with-the-Canvas-API/02-Maze copy

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

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Matter.js library
22
const { Engine, Render, Runner, World, Bodies } = Matter
33

4-
const cells = 3
4+
const cells = 15
55
const width = 600
66
const height = 600
77

@@ -24,10 +24,10 @@ Runner.run(Runner.create(), engine)
2424

2525
// Walls
2626
const walls = [
27-
Bodies.rectangle(width / 2, 0, width, 40, { isStatic: true }),
28-
Bodies.rectangle(width / 2, height, width, 40, { isStatic: true }),
29-
Bodies.rectangle(0, height / 2, 40, height, { isStatic: true }),
30-
Bodies.rectangle(width, height / 2, 40, height, { isStatic: true }),
27+
Bodies.rectangle(width / 2, 0, width, 2, { isStatic: true }),
28+
Bodies.rectangle(width / 2, height, width, 2, { isStatic: true }),
29+
Bodies.rectangle(0, height / 2, 2, height, { isStatic: true }),
30+
Bodies.rectangle(width, height / 2, 2, height, { isStatic: true }),
3131
]
3232

3333
World.add(world, walls)
@@ -91,7 +91,12 @@ const stepThroughCell = (row, column) => {
9191
const [nextRow, nextColumn, direction] = neighbor
9292

9393
// See if that neighbor is out of bounds
94-
if (nextRow < 0 || nextRow >= cells || nextColumn < 0 || nextColumn >= cells) {
94+
if (
95+
nextRow < 0 ||
96+
nextRow >= cells ||
97+
nextColumn < 0 ||
98+
nextColumn >= cells
99+
) {
95100
continue
96101
}
97102

@@ -101,18 +106,17 @@ const stepThroughCell = (row, column) => {
101106
}
102107

103108
// Remove a wall from either horizontals or verticals
104-
if(direction === 'left') {
109+
if (direction === 'left') {
105110
verticals[row][column - 1] = true
106111
} else if (direction === 'right') {
107112
verticals[row][column] = true
108113
} else if (direction === 'up') {
109114
horizontals[row - 1][column] = true
110-
} else if(direction === 'down') {
115+
} else if (direction === 'down') {
111116
horizontals[row][column] = true
112117
}
113118

114119
stepThroughCell(nextRow, nextColumn)
115-
116120
}
117121
// Visit that next cell
118122
}
@@ -123,10 +127,9 @@ stepThroughCell(startRow, startColumn)
123127
// console.log(verticals);
124128
// console.log(horizontals);
125129

126-
127130
horizontals.forEach((row, rowIndex) => {
128131
row.forEach((open, columnIndex) => {
129-
if(open === true) {
132+
if (open === true) {
130133
return
131134
}
132135

@@ -145,7 +148,7 @@ horizontals.forEach((row, rowIndex) => {
145148

146149
verticals.forEach((row, rowIndex) => {
147150
row.forEach((open, columnIndex) => {
148-
if(open) {
151+
if (open) {
149152
return
150153
}
151154

@@ -155,9 +158,20 @@ verticals.forEach((row, rowIndex) => {
155158
10,
156159
unitLength,
157160
{
158-
isStatic: true
161+
isStatic: true,
159162
}
160163
)
161164
World.add(world, wall)
162165
})
163-
})
166+
})
167+
168+
const goal = Bodies.rectangle(
169+
width - unitLength / 2,
170+
height - unitLength / 2,
171+
unitLength * 0.7,
172+
unitLength * 0.7,
173+
{
174+
isStatic: true
175+
}
176+
)
177+
World.add(world, goal)

0 commit comments

Comments
 (0)