1+ let inputDir = { x : 0 , y : 0 } ;
2+ const foodSound = new Audio ( 'pow-90398.mp3' ) ;
3+ const gameOverSound = new Audio ( 'hiss3-103123.mp3' ) ;
4+ const moveSound = new Audio ( 'snake-hissing-6092.mp3' ) ;
5+ const musicSound = new Audio ( 'music.mp3' ) ;
6+ let speed = 3 ;
7+ let score = 0 ;
8+ let lastPaintTime = 0 ;
9+ let snakeArr = [
10+ { x : 13 , y : 15 }
11+ ]
12+ food = { x : 6 , y : 7 } ;
13+
14+
15+ //Game Function
16+ function main ( ctime ) {
17+
18+ window . requestAnimationFrame ( main ) ;
19+ // console.log(ctime)
20+ if ( ( ctime - lastPaintTime ) / 1000 < 1 / speed ) {
21+ return ;
22+ }
23+ lastPaintTime = ctime ;
24+ gameEngine ( ) ;
25+
26+ }
27+ function isCollide ( snake ) {
28+ //if u bump into urself
29+ for ( let i = 1 ; i < snakeArr . length ; i ++ ) {
30+ // const element = array[index];
31+ if ( snake [ i ] . x === snake [ 0 ] . x && snake [ i ] . y === snake [ 0 ] . y ) {
32+ return true ;
33+ }
34+ }
35+ // If you bump into the wall;
36+ if ( snake [ 0 ] . x >= 18 || snake [ 0 ] . x <= 0 || snake [ 0 ] . y >= 18 || snake [ 0 ] . y <= 0 ) {
37+ return true ;
38+ }
39+ return false ;
40+
41+ }
42+
43+
44+ function gameEngine ( ) {
45+ //part1:updating the sanke array &food
46+ if ( isCollide ( snakeArr ) ) {
47+ score += 1 ;
48+ score . innerHTML = "Score" + score ;
49+ musicSound . play ( ) ;
50+ inputDir = { x : 0 , y :0 } ;
51+ alert ( "Game Over.Press any key to play again." ) ;
52+ snakeArr = [ { x : 13 , y : 15 } ] ;
53+ // musicSound.play();
54+ score = 0 ;
55+ }
56+
57+ //If u have eaten the food, increment and regenerate the food.
58+ if ( snakeArr [ 0 ] . y === food . y && snakeArr [ 0 ] . x === food . x ) {
59+ foodSound . play ( ) ;
60+ snakeArr . unshift ( { x : snakeArr [ 0 ] . x + inputDir . x , y : snakeArr [ 0 ] . y + inputDir . y } ) ;
61+ let a = 2 ;
62+ let b = 16 ;
63+ food = { x : Math . round ( a + ( b - a ) * Math . random ( ) ) , y : Math . round ( a + ( b - a ) * Math . random ( ) ) }
64+ }
65+ //moving the snake
66+ for ( let i = snakeArr . length - 2 ; i >= 0 ; i -- ) {
67+ // const element =array[i];
68+ snakeArr [ i + 1 ] = { ...snakeArr [ i ] } ;
69+ }
70+
71+ snakeArr [ 0 ] . x += inputDir . x ;
72+ snakeArr [ 0 ] . y += inputDir . y ;
73+
74+
75+
76+ //pert2:Display the snake
77+ board . innerHTML = "" ;
78+ snakeArr . forEach ( ( e , index ) => {
79+ sankeElement = document . createElement ( 'div' ) ;
80+ sankeElement . style . gridRowStart = e . y ;
81+ sankeElement . style . gridColumnStart = e . x ;
82+ // sankeElement.classList.add('snake')
83+ if ( index === 0 ) {
84+ sankeElement . classList . add ( 'head' ) ;
85+ }
86+ else {
87+ sankeElement . classList . add ( 'snake' ) ;
88+ }
89+ board . appendChild ( sankeElement ) ;
90+ } )
91+ //Display the food
92+ foodElement = document . createElement ( 'div' ) ;
93+ foodElement . style . gridRowStart = food . y ;
94+ foodElement . style . gridColumnStart = food . x ;
95+ foodElement . classList . add ( 'food' )
96+ board . appendChild ( foodElement ) ;
97+ }
98+
99+ window . requestAnimationFrame ( main ) ;
100+ window . addEventListener ( 'keydown' , e => {
101+ inputDir = { x :0 , y :1 } //start the game
102+ moveSound . play ( ) ;
103+ switch ( e . key ) {
104+ case "ArrowUp" :
105+ console . log ( "ArrowUp" ) ;
106+ inputDir . x = 0 ;
107+ inputDir . y = - 1 ;
108+ break ;
109+ case "ArrowDown" :
110+ console . log ( "ArrowDown" ) ;
111+ inputDir . x = 0 ;
112+ inputDir . y = 1 ;
113+ break ;
114+ case "ArrowLeft" :
115+ console . log ( "ArrowLeft" ) ;
116+ inputDir . x = - 1 ;
117+ inputDir . y = 0 ;
118+ break ;
119+ case "ArrowRight" :
120+ console . log ( "ArrowRight" ) ;
121+ inputDir . x = 1 ;
122+ inputDir . y = 0 ;
123+ break ;
124+ default :
125+ break ;
126+
127+ }
128+ } )
0 commit comments