@@ -5,9 +5,12 @@ import { some } from 'lodash';
5
5
6
6
import './App.css' ;
7
7
8
+ const GRID_SIZE = 40 ;
9
+ const TICK_RATE = 100 ;
10
+
8
11
var GRID_ARRAY = [ ] ;
9
12
10
- for ( var i = 0 ; i <= 50 ; i ++ ) {
13
+ for ( var i = 0 ; i <= GRID_SIZE ; i ++ ) {
11
14
GRID_ARRAY . push ( i ) ;
12
15
}
13
16
@@ -35,8 +38,8 @@ const DIRECTION_TICKS = {
35
38
const isPosition = ( x , y , diffX , diffY ) =>
36
39
x === diffX && y === diffY ;
37
40
38
- const get = ( array , property ) =>
39
- array . map ( v => v [ property ] ) ;
41
+ const getRandomCoordinate = ( ) =>
42
+ ( { x : Math . floor ( Math . random ( ) * GRID_SIZE ) , y : Math . floor ( Math . random ( ) * GRID_SIZE ) } ) ;
40
43
41
44
// TODO make own some, use compose
42
45
const isSnake = ( x , y , snakeCoordinates ) => {
@@ -56,20 +59,30 @@ const applySnakePosition = (prevState) => {
56
59
57
60
// const snakeCoordinatesWithoutLast = prevState.snake.coordinates.slice()
58
61
62
+ const isSnakeEating = getIsSnakeEating ( prevState ) ;
63
+
59
64
const snakeHead = DIRECTION_TICKS [ prevState . controls . direction ] (
60
65
prevState . snake . coordinates [ 0 ] . x ,
61
66
prevState . snake . coordinates [ 0 ] . y ,
62
67
) ;
63
68
64
- const isSnakeEating = getIsSnakeEating ( prevState ) ;
65
69
const snakeTail = isSnakeEating
66
70
? prevState . snake . coordinates
67
- : prevState . snake . coordinates . slice ( 0 , prevState . snake . coordinates . length - 1 )
71
+ : prevState . snake . coordinates . slice ( 0 , prevState . snake . coordinates . length - 1 ) ;
72
+
73
+ const snackCoordinate = isSnakeEating
74
+ ? getRandomCoordinate ( )
75
+ : prevState . snack . coordinate ;
76
+
77
+ console . log ( snackCoordinate ) ;
68
78
69
79
return {
70
80
snake : {
71
81
coordinates : [ snakeHead ] . concat ( snakeTail )
72
82
} ,
83
+ snack : {
84
+ coordinate : snackCoordinate
85
+ }
73
86
} ;
74
87
} ;
75
88
@@ -91,23 +104,17 @@ class App extends Component {
91
104
direction : CONTROLS . RIGHT ,
92
105
} ,
93
106
snake : {
94
- coordinates : [ {
95
- x : 10 ,
96
- y : 25 ,
97
- } ] ,
107
+ coordinates : [ getRandomCoordinate ( ) ] ,
98
108
} ,
99
109
snack : {
100
- coordinate : {
101
- x : 40 ,
102
- y : 25 ,
103
- } ,
110
+ coordinate : getRandomCoordinate ( ) ,
104
111
} ,
105
112
} ;
106
113
107
114
}
108
115
109
116
componentDidMount ( ) {
110
- this . interval = setInterval ( this . onTick , 150 ) ;
117
+ this . interval = setInterval ( this . onTick , TICK_RATE ) ;
111
118
112
119
window . addEventListener ( 'keyup' , this . onChangeDirection , false ) ;
113
120
}
0 commit comments