1+ var canvas = document . getElementById ( 'snakegame' ) ;
2+ var context = canvas . getContext ( '2d' ) ;
3+ var grid = 16 ;
4+ var count = 0 ;
5+ var score = 0 ;
6+ var flag ;
7+ var inter ;
8+ var snake =
9+ {
10+ x : 160 ,
11+ y : 160 ,
12+ cells : [ { x :160 , y :160 } , { x :144 , y :160 } ] ,
13+ } ;
14+ var apple = {
15+ x : 320 ,
16+ y : 320
17+ } ;
18+
19+ // draw apple
20+ function drawApple ( )
21+ {
22+ context . fillStyle = '#EE7486' ;
23+ context . fillRect ( apple . x , apple . y , grid - 1 , grid - 1 ) ;
24+ for ( let i = 0 ; i < snake . cells . length ; i ++ )
25+ {
26+ if ( snake . cells [ i ] . x == apple . x && snake . cells [ i ] . y == apple . y )
27+ {
28+ snake . cells . unshift ( { x : apple . x , y : apple . y } ) ;
29+ var number1 = Math . floor ( ( Math . random ( ) * 24 ) + 1 ) ;
30+ var number2 = Math . floor ( ( Math . random ( ) * 24 ) + 1 ) ;
31+ apple . x = number1 * 16 ;
32+ apple . y = number2 * 16 ;
33+ for ( let i = 0 ; i < snake . cells . length ; i ++ )
34+ {
35+ if ( apple . x == snake . cells [ i ] . x && apple . y == snake . cells [ i ] . y )
36+ {
37+ drawApple ( ) ;
38+ }
39+ }
40+ break ;
41+ }
42+ }
43+ //drawSnake();
44+ }
45+
46+ drawApple ( ) ;
47+
48+ // draw snake
49+ function drawSnake ( )
50+ {
51+
52+ if ( snake . cells . length != 0 ) {
53+ context . fillStyle = '#ECF34D' ;
54+ context . fillRect ( snake . cells [ 0 ] . x , snake . cells [ 0 ] . y , grid - 1 , grid - 1 ) ; }
55+
56+ context . fillStyle = '#A7A2A2' ;
57+ for ( let i = 1 ; i < snake . cells . length ; i ++ )
58+ context . fillRect ( snake . cells [ i ] . x , snake . cells [ i ] . y , grid - 1 , grid - 1 ) ;
59+
60+
61+ StoreInLocalStorage ( ) ;
62+ if ( snake . cells . length <= 2 )
63+ { }
64+ else {
65+ count = snake . cells . length - 2 ;
66+ score = ( count * 5 ) ; }
67+ document . getElementById ( "ID" ) . innerHTML = "Score = " + score ;
68+
69+ }
70+
71+ function StoreInLocalStorage ( )
72+ {
73+ localStorage . snake = JSON . stringify ( snake ) ;
74+ }
75+
76+ function GetFromLocalStorage ( )
77+ {
78+ if ( ! localStorage . snake )
79+ {
80+ localStorage . snake = JSON . stringify ( [ ] ) ;
81+ snake = {
82+ x : 160 ,
83+ y : 160 ,
84+ cells : [ { x :160 , y :160 } , { x :144 , y :160 } ] ,
85+ } ;
86+ drawSnake ( ) ;
87+ }
88+ else
89+ {
90+ snake = JSON . parse ( localStorage . snake ) ;
91+ if ( snake . cells . length == 0 )
92+ {
93+ snake = {
94+ x : 160 ,
95+ y : 160 ,
96+ cells : [ { x :160 , y :160 } , { x :144 , y :160 } ] ,
97+ } ;
98+ }
99+ console . log ( snake ) ;
100+ drawSnake ( ) ;
101+ }
102+ }
103+
104+ //drawSnake();
105+ function moveSnake ( dx , dy )
106+ {
107+ snake . x += dx ;
108+ snake . y += dy ;
109+ if ( snake . x >= 400 || snake . x < 0 || snake . y >= 400 || snake . y < 0 )
110+ {
111+ GameOver ( ) ;
112+ }
113+ for ( let i = 0 ; i < snake . cells . length ; i ++ )
114+ {
115+ if ( snake . x == snake . cells [ i ] . x && snake . y == snake . cells [ i ] . y )
116+ {
117+ GameOver ( ) ;
118+ }
119+ }
120+ snake . cells . unshift ( { x : snake . x , y : snake . y } ) ; // Insert at 0th position
121+ snake . cells . pop ( ) ; // remove the last element
122+ drawSnake ( ) ;
123+ }
124+
125+ function GameOver ( )
126+ {
127+ context . clearRect ( 0 , 0 , canvas . width , canvas . height ) ; // Clear the Canvas
128+ snake . cells = [ ] ;
129+ apple = { x :- 100 , y :- 20 } ;
130+ context . fillStyle = 'white' ;
131+ context . font = "50px italic" ;
132+ context . textAlign = "center" ;
133+ context . fillText ( "Game Over" , canvas . width / 2 , canvas . height / 2 ) ;
134+ }
135+
136+ // listen to keyboard events to move the snake
137+ document . addEventListener ( 'keydown' , function ( e )
138+ {
139+ let dx = 0 , dy = 0 ;
140+ if ( e . keyCode == 37 )
141+ {
142+ if ( flag == 39 )
143+ { }
144+ else
145+ {
146+ clearInterval ( inter ) ;
147+ inter = setInterval ( function ( ) {
148+ dx = - grid ; flag = 37 ;
149+ dy = 0 ;
150+ context . clearRect ( 0 , 0 , canvas . width , canvas . height ) ; // Clear the Canvas
151+ moveSnake ( dx , dy ) ;
152+ drawApple ( ) ;
153+ } , 100 ) ;
154+ }
155+ }
156+ // up arrow key
157+ else if ( e . keyCode == 38 )
158+ {
159+ if ( flag == 40 )
160+ { }
161+ else
162+ {
163+ clearInterval ( inter ) ;
164+ inter = setInterval ( function ( ) {
165+ dx = 0 ;
166+ dy = - grid ; flag = 38 ;
167+ context . clearRect ( 0 , 0 , canvas . width , canvas . height ) ; // Clear the Canvas
168+ moveSnake ( dx , dy ) ;
169+ drawApple ( ) ;
170+ } , 100 ) ;
171+ }
172+ }
173+ // right arrow key
174+ else if ( e . keyCode == 39 )
175+ {
176+ if ( flag == 37 )
177+ { }
178+ else
179+ {
180+ clearInterval ( inter ) ;
181+ inter = setInterval ( function ( ) {
182+ dx = grid ; flag = 39 ;
183+ dy = 0 ;
184+ context . clearRect ( 0 , 0 , canvas . width , canvas . height ) ; // Clear the Canvas
185+ moveSnake ( dx , dy ) ;
186+ drawApple ( ) ;
187+ } , 100 ) ;
188+ }
189+ }
190+ // down arrow key
191+ else if ( e . keyCode == 40 )
192+ {
193+ if ( flag == 38 )
194+ { }
195+ else
196+ {
197+ clearInterval ( inter ) ;
198+ inter = setInterval ( function ( ) {
199+ dx = 0 ;
200+ dy = grid ; flag = 40 ;
201+ context . clearRect ( 0 , 0 , canvas . width , canvas . height ) ; // Clear the Canvas
202+ moveSnake ( dx , dy ) ;
203+ drawApple ( ) ;
204+ } , 100 ) ;
205+ }
206+ }
207+ else if ( e . keyCode == 13 )
208+ {
209+ document . location . reload ( ) ;
210+ snake = {
211+ x : 160 ,
212+ y : 160 ,
213+ cells : [ { x :160 , y :160 } , { x :144 , y :160 } ] ,
214+ } ;
215+ drawSnake ( ) ;
216+ }
217+ else if ( e . keyCode == 80 || e . keyCode == 112 )
218+ {
219+ clearInterval ( inter ) ;
220+ }
221+ } ) ;
0 commit comments