11/*
22Author : Vignesh Hari
33Title : Coding train Challenge 5 : Snake
4- Date : 27th February 2020
4+ Date : 1st March 2020
55*/
66
7- const width = 500
8- const height = 500
7+ const width = screen . width
8+ const height = screen . height - ( 7.31 / 100 * screen . height )
9+ const size = 20 ;
10+ const starting_direction = [ 1 , 0 ]
11+
12+ var s = starting_direction
13+ class Box {
14+
15+ x = 0 ;
16+ y = 0 ;
17+ moves = [ ]
18+
19+ constructor ( x , y ) {
20+ this . x = x ;
21+ this . y = y ;
22+ }
23+ set_random_pos ( ) {
24+ this . x = Math . round ( random ( 0 , width - 10 ) )
25+ this . y = Math . round ( random ( 0 , height - 10 ) )
26+ }
27+
28+ setmoves ( moves ) {
29+ this . moves = moves ;
30+ }
31+
32+ move ( ) {
33+ var cur_s = this . moves . shift ( )
34+ this . x = this . x + ( cur_s [ 0 ] * size )
35+ this . y = this . y + ( cur_s [ 1 ] * size )
36+ if ( this . x < 0 ) {
37+ this . x = width
38+ } else if ( this . x > width ) {
39+ this . x = 0
40+ }
41+ if ( this . y < 0 ) {
42+ this . y = height
43+ } else if ( this . y > height ) {
44+ this . y = 0
45+ }
46+ }
47+
48+ addmove ( s ) {
49+ this . moves . push ( [ s [ 0 ] , s [ 1 ] ] )
50+ }
51+
52+ show ( ) {
53+ rect ( this . x , this . y , size , size ) ;
54+ }
55+
56+ getlistpos ( ) {
57+ return [ this . x , this . y ]
58+ }
59+
60+ }
61+ class Snake {
62+
63+ body = [ ]
64+
65+ constructor ( headbox ) {
66+ this . body [ 0 ] = headbox
67+ }
68+
69+
70+ move ( ) {
71+ for ( var i = 0 ; i < this . body . length ; i ++ ) {
72+ this . body [ i ] . move ( )
73+ }
74+
75+ }
76+
77+ show ( ) {
78+ for ( var i = 0 ; i < this . body . length ; i ++ ) {
79+ fill ( 255 )
80+ if ( i == 0 ) {
81+ fill ( 255 , 100 , 0 )
82+ }
83+ this . body [ i ] . show ( )
84+ }
85+ }
86+
87+ propogate_move ( s ) {
88+ for ( var i = 0 ; i < this . body . length ; i ++ ) {
89+ this . body [ i ] . addmove ( s )
90+ }
91+ }
92+
93+ create_new ( ) {
94+ var last_box = this . body [ this . body . length - 1 ]
95+ var new_box = new Box ( last_box . x , last_box . y )
96+ new_box . setmoves ( last_box . moves . slice ( ) )
97+ return new_box
98+ }
99+
100+ getheadpos ( ) {
101+ return [ this . body [ 0 ] . x , this . body [ 0 ] . y ]
102+ }
103+ }
104+
105+ var food = new Box ( 0 , 0 )
106+ var addmore = false
107+ var snake = null
108+
9109
10110function setup ( ) {
111+ frameRate ( 60 ) ;
11112 createCanvas ( width , height ) ;
12113 background ( 51 ) ;
114+ head_box = new Box ( 0 , 0 )
115+ snake = new Snake ( head_box )
116+ food . set_random_pos ( )
117+ }
13118
119+
120+ i = 1
121+
122+ function eucDistance ( a , b ) {
123+ return a
124+ . map ( ( x , i ) => Math . abs ( x - b [ i ] ) ** 2 ) // square the difference
125+ . reduce ( ( sum , now ) => sum + now ) // sum
126+ **
127+ ( 1 / 2 )
128+ }
129+
130+ function check_food_collision ( ) {
131+ current = snake . getheadpos ( )
132+ foodpos = food . getlistpos ( )
133+ var dis = eucDistance ( foodpos , current )
134+ if ( dis < size ) {
135+ food . set_random_pos ( )
136+ addmore = true
137+ }
14138}
15139
16140function draw ( ) {
141+ background ( 51 ) ;
17142 fill ( 255 ) ;
18- rect ( 479 , 0 , 20 , 20 ) ;
143+ noStroke ( ) ;
144+ snake . propogate_move ( s )
145+ if ( addmore ) {
146+ new_obj = snake . create_new ( )
147+ snake . move ( )
148+ snake . body . push ( new_obj )
149+ addmore = false
150+ } else {
151+ snake . move ( )
152+ }
153+ check_food_collision ( )
154+ snake . show ( )
155+ fill ( 255 , 0 , 100 ) ;
156+ food . show ( )
157+ }
158+
19159
160+ function keyPressed ( ) {
161+ if ( keyCode === UP_ARROW ) {
162+ s = [ 0 , - 1 ]
163+ } else if ( keyCode === DOWN_ARROW ) {
164+ s = [ 0 , 1 ] ;
165+ } else if ( keyCode === RIGHT_ARROW ) {
166+ s = [ 1 , 0 ] ;
167+ } else if ( keyCode === LEFT_ARROW ) {
168+ s = [ - 1 , 0 ] ;
169+ }
20170}
0 commit comments