Skip to content

Commit f547de4

Browse files
author
Mauro Codella
committed
Add start pane and rising speed logic
1 parent fcbc85b commit f547de4

File tree

11 files changed

+111
-15
lines changed

11 files changed

+111
-15
lines changed

src/components/App/index.js

+31-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { Component } from 'react';
22

33
import Score from '../Score';
44
import Board from '../Board';
5+
import StartPane from '../StartPane';
56

67
import {
78
createInitialBoardState,
@@ -22,13 +23,14 @@ export default class App extends Component {
2223
super(props);
2324

2425
this.state = {
26+
started: false,
2527
score: 0,
28+
speed: 200,
2629
direction: "right",
2730
board: createInitialBoardState(MATRIX_SIZE)
2831
};
2932

30-
setInterval(this.tickHandler.bind(this), 150);
31-
33+
this.tickHandler = this.tickHandler.bind(this);
3234
this.keyDownHandler = this.keyDownHandler.bind(this);
3335
}
3436

@@ -39,18 +41,32 @@ export default class App extends Component {
3941
matrixSize: MATRIX_SIZE
4042
});
4143

42-
const nextScore = nextBoardState.food !== this.state.board.food ?
43-
this.state.score + 1 :
44-
this.state.score;
44+
let nextScore = this.state.score;
45+
let nextSpeed = this.state.speed;
46+
if (nextBoardState.food !== this.state.board.food) {
47+
nextScore++;
48+
nextSpeed = Math.max(nextSpeed - 2, 50);
49+
}
4550

46-
this.setState({ board: nextBoardState, score: nextScore });
51+
this.setState({ board: nextBoardState, score: nextScore, speed: nextSpeed }, () => {
52+
clearInterval(this.state.tickerId);
53+
const tickerId = setInterval(this.tickHandler, this.state.speed);
54+
this.setState({tickerId});
55+
});
4756
}
4857

4958
keyDownHandler(event) {
50-
const direction = KEY_TO_DIRECTION[event.key];
51-
52-
if (direction !== undefined) {
53-
this.setState({ direction: direction });
59+
if (! this.state.started) {
60+
this.setState({ started: true }, () => {
61+
const tickerId = setInterval(this.tickHandler, this.state.speed);
62+
this.setState({ tickerId });
63+
});
64+
} else {
65+
const direction = KEY_TO_DIRECTION[event.key];
66+
67+
if (direction !== undefined) {
68+
this.setState({ direction: direction });
69+
}
5470
}
5571
}
5672

@@ -63,13 +79,14 @@ export default class App extends Component {
6379
}
6480

6581
render() {
82+
const pane = this.state.started ?
83+
<Board snake={this.state.board.snake} food={this.state.board.food} /> :
84+
<StartPane />;
85+
6686
return (
6787
<div>
6888
<Score score={this.state.score} />
69-
<Board
70-
snake={this.state.board.snake}
71-
food={this.state.board.food}
72-
/>
89+
{pane}
7390
</div>
7491
);
7592
}

src/components/GameOverPane/game.ttf

280 KB
Binary file not shown.

src/components/GameOverPane/index.css

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@font-face {
2+
font-family: game;
3+
src: url('./game.ttf');
4+
}
5+
6+
.GameOverPane {
7+
font-family: game;
8+
color: #d50000;
9+
text-align: center;
10+
vertical-align: middle;
11+
line-height: 400px;
12+
13+
width: 400px;
14+
height: 400px;
15+
16+
background-color: #212121;
17+
}

src/components/GameOverPane/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react';
2+
3+
import './index.css';
4+
5+
export default function StartPane() {
6+
return (
7+
<div className="StartPane">
8+
Press any key to start
9+
</div>
10+
);
11+
};

src/components/GameOverPane/test.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import Board from './';
4+
5+
it('renders without crashing', () => {
6+
const div = document.createElement('div');
7+
8+
const snake = [];
9+
const food = {row: 8, col: 8};
10+
ReactDOM.render(<Board snake={snake} food={food} />, div);
11+
});

src/components/Score/index.css

+1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
.Score {
77
font-family: game;
88
flex: 1;
9+
color: #F57F17;
910
margin-bottom: 10px;
1011
}

src/components/StartPane/game.ttf

280 KB
Binary file not shown.

src/components/StartPane/index.css

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@font-face {
2+
font-family: game;
3+
src: url('./game.ttf');
4+
}
5+
6+
.StartPane {
7+
font-family: game;
8+
color: white;
9+
text-align: center;
10+
vertical-align: middle;
11+
line-height: 400px;
12+
13+
width: 400px;
14+
height: 400px;
15+
16+
background-color: #212121;
17+
}

src/components/StartPane/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react';
2+
3+
import './index.css';
4+
5+
export default function StartPane() {
6+
return (
7+
<div className="StartPane">
8+
Press any key to start
9+
</div>
10+
);
11+
};

src/components/StartPane/test.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import Board from './';
4+
5+
it('renders without crashing', () => {
6+
const div = document.createElement('div');
7+
8+
const snake = [];
9+
const food = {row: 8, col: 8};
10+
ReactDOM.render(<Board snake={snake} food={food} />, div);
11+
});

src/components/Tile/index.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
}
88

99
.Tile--snake {
10-
background-color: #00e676;
10+
background-color: #2E7D32;
1111
}
1212

1313
.Tile--food {

0 commit comments

Comments
 (0)