1+
2+
13let canvas = document . getElementById ( "Canvas" ) ;
24let ctx = canvas . getContext ( "2d" ) ;
35//Variables
6+ function mathRendom ( ) {
7+ return Math . round ( Math . random ( ) * 23 + 1 ) * cell ;
8+ }
49let cell = 20 ;
510let score = 0 ;
6- let deltaX = Math . round ( Math . random ( ) * 23 + 1 ) * cell ;
7- let deltaY = Math . round ( Math . random ( ) * 23 + 1 ) * cell ;
8- let fx = Math . round ( Math . random ( ) * 23 + 1 ) * cell ;
9- let fy = Math . round ( Math . random ( ) * 23 + 1 ) * cell ;
11+ let deltaX = mathRendom ( ) ;
12+ let deltaY = mathRendom ( ) ;
13+ let fx = mathRendom ( ) ;
14+ let fy = mathRendom ( ) ;
1015//snake boody
1116let snake = [ ] ;
1217snake [ 0 ] = {
@@ -21,6 +26,8 @@ snake[2] = {
2126 x : deltaX - cell * 2 ,
2227 y : deltaY
2328} ;
29+
30+
2431function drawSnake ( ) {
2532 for ( i = 0 ; i < snake . length ; i ++ ) {
2633 ctx . beginPath ( ) ;
@@ -49,7 +56,6 @@ function drawSnake() {
4956
5057 }
5158}
52- drawSnake ( )
5359/*apple drawl*/
5460function food ( ) {
5561 ctx . beginPath ( ) ;
@@ -58,7 +64,7 @@ function food() {
5864 ctx . fillStyle = "#fa0a0a" ;
5965 ctx . fill ( ) ;
6066} ;
61- food ( ) ;
67+
6268
6369//Collapse
6470function eatTail ( head , arr ) {
@@ -126,15 +132,15 @@ function drawEyes() {
126132 ctx . fill ( ) ;
127133 }
128134}
129- drawEyes ( )
135+
130136function reDraw ( ) {
131137 for ( i = 0 ; i < snake . length ; i ++ ) {
132138 while ( snake [ i ] . x == fx && snake [ i ] . y == fy ) {
133139 fx = Math . round ( Math . random ( ) * 23 + 1 ) * cell , fy = Math . round ( Math . random ( ) * 23 + 1 ) * cell
134140 }
135141 }
136142}
137- reDraw ( ) ;
143+
138144
139145//Igra
140146function drawGame ( ) {
@@ -146,19 +152,15 @@ function drawGame() {
146152
147153 if ( deltaX > canvas . width + cell ) {
148154 deltaX = - cell ;
149- return deltaX ;
150155 }
151156 if ( deltaX < - cell ) {
152157 deltaX = canvas . width + cell ;
153- return deltaX ;
154158 }
155159 if ( deltaY > canvas . height + cell ) {
156160 deltaY = - cell ;
157- return deltaY ;
158161 }
159162 if ( deltaY < - cell ) {
160163 deltaY = canvas . height + cell ;
161- return deltaY ;
162164 }
163165 if ( dir == "left" ) deltaX -= cell ;
164166 if ( dir == "right" ) deltaX += cell ;
@@ -172,7 +174,7 @@ function drawGame() {
172174 function eat ( ) {
173175 if ( newHead . x === fx && newHead . y === fy ) {
174176 score += 1
175- return fx = Math . round ( Math . random ( ) * 23 + 1 ) * cell , fy = Math . round ( Math . random ( ) * 23 + 1 ) * cell ;
177+ return fx = mathRendom ( ) , fy = mathRendom ( ) ;
176178
177179 } else {
178180 snake . pop ( ) ;
@@ -189,15 +191,23 @@ function drawGame() {
189191}
190192
191193let game = '' ;
192- let b1 = document . querySelector ( '.b1 ' ) ;
194+ let start = document . querySelector ( '.start ' ) ;
193195
194- b1 . onclick = function f1 ( ) {
196+ start . onclick = ( ) => {
195197 if ( game == '' ) {
196198 game = setInterval ( drawGame , 200 ) ;
197- b1 . innerHTML = 'RESTART'
199+ start . innerHTML = 'RESTART'
198200 }
199201 else {
200202 document . location . reload ( true ) ;
201203 }
202204}
203205
206+ function init ( ) {
207+ drawSnake ( ) ;
208+ food ( ) ;
209+ // drawEyes();
210+ reDraw ( ) ;
211+ }
212+
213+ init ( ) ;
0 commit comments