Skip to content

Commit 3a64d13

Browse files
committed
hit box when gameover
1 parent 9c2d222 commit 3a64d13

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

src/App.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
background-color: #008000;
3030
}
3131

32+
.grid-cell-hit {
33+
background-color: red;
34+
}
35+
3236
.grid-cell:last-child {
3337
border-right: 1px solid #000000;
3438
}

src/App.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ const getIsSnakeClumy = (snake) =>
7979
const getIsSnakeEating = ({ snake, snack }) =>
8080
isPosition(getSnakeHead(snake).x, getSnakeHead(snake).y, snack.coordinate.x, snack.coordinate.y);
8181

82+
const applyGameOver = (prevState) => ({
83+
playground: {
84+
isGameOver: true
85+
},
86+
})
87+
8288
// TODO compose instead: direction ticks
8389
// TODO make last a previous compose step
8490
const applySnakePosition = (prevState) => {
@@ -113,12 +119,13 @@ const doChangeDirection = (direction) => () => ({
113119
},
114120
});
115121

116-
const getCellCs = (snake, snack, x, y) =>
122+
const getCellCs = (isGameOver, snake, snack, x, y) =>
117123
cs(
118124
'grid-cell',
119125
{
120126
'grid-cell-snake': isSnake(x, y, snake.coordinates),
121127
'grid-cell-snack': isPosition(x, y, snack.coordinate.x, snack.coordinate.y),
128+
'grid-cell-hit': isGameOver && isPosition(x, y, getSnakeHead(snake).x, getSnakeHead(snake).y)
122129
}
123130
);
124131

@@ -153,10 +160,9 @@ class App extends Component {
153160
}
154161

155162
onTick = () => {
156-
if (getIsSnakeOutside(this.state.snake) || getIsSnakeClumy(this.state.snake)) {
157-
return;
158-
}
159-
this.setState(applySnakePosition);
163+
getIsSnakeOutside(this.state.snake) || getIsSnakeClumy(this.state.snake)
164+
? this.setState(applyGameOver)
165+
: this.setState(applySnakePosition);
160166
}
161167

162168
onChangeDirection = (e) => {
@@ -166,31 +172,42 @@ class App extends Component {
166172
}
167173

168174
render() {
169-
const { snake, snack } = this.state;
175+
const {
176+
playground,
177+
snake,
178+
snack,
179+
} = this.state;
180+
181+
const {
182+
isGameOver,
183+
} = playground;
184+
170185
return (
171186
<div>
172187
<Grid
173188
snake={snake}
174189
snack={snack}
190+
isGameOver={isGameOver}
175191
/>
176192
</div>
177193
);
178194
}
179195
}
180196

181-
const Grid = ({ snake, snack }) =>
197+
const Grid = ({ isGameOver, snake, snack }) =>
182198
<div className="grid">
183199
<Border grid={GRID_ARRAY} />
184200
{GRID_ARRAY.map(y => <Row
185201
y={y}
186202
key={y}
187203
snake={snake}
188204
snack={snack}
205+
isGameOver={isGameOver}
189206
/>)}
190207
<Border grid={GRID_ARRAY} />
191208
</div>
192209

193-
const Row = ({ snake, snack, y }) =>
210+
const Row = ({ isGameOver, snake, snack, y }) =>
194211
<div className="grid-row">
195212
<Block />
196213
{GRID_ARRAY.map(x => <Cell
@@ -199,6 +216,7 @@ const Row = ({ snake, snack, y }) =>
199216
key={x}
200217
snake={snake}
201218
snack={snack}
219+
isGameOver={isGameOver}
202220
/>)}
203221
<Block />
204222
</div>
@@ -213,7 +231,7 @@ const Border = ({ grid }) =>
213231
const Block = () =>
214232
<div className="grid-cell grid-cell-border" />
215233

216-
const Cell = ({ snake, snack, x, y }) =>
217-
<div className={getCellCs(snake, snack, x, y)} />;
234+
const Cell = ({ isGameOver, snake, snack, x, y }) =>
235+
<div className={getCellCs(isGameOver, snake, snack, x, y)} />;
218236

219237
export default App;

0 commit comments

Comments
 (0)