Repository navigation
Expand file tree
/
Copy pathlogic.js
More file actions
203 lines (164 loc) · 5.56 KB
/
Copy pathlogic.js
File metadata and controls
203 lines (164 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
function init(){
var canvas =document.getElementById('snake-canvas');
w=h=800;canvas.width=800;
canvas.height=800;
gameover=false;
pen = canvas.getContext('2d');
score=0; //score of player
food_img= new Image();
food_img.src="./assets/img/food-2.gif";
snake_img =new Image();
color_name=document.getElementById('snake_color').value;
call_snake_image();
cellsize=50; //cell size
food=getfood();
snake = {
slen:3,//snake initial length
color:"Green", //snake color
cells:[],
direction:"right",
board_color:"silver",
drawboard:function() //to draw board
{
for(var i=0;i<=Math.round(w/cellsize);i++){
for(var j=0;j<=Math.round(h/cellsize);j++){
pen.fillStyle=this.board_color;
pen.fillRect(i*cellsize,j*cellsize,cellsize-2,cellsize-2);
}
}
},
createsnake:function() //to create snake of given snake length i.e. slen
{
for(var i=this.slen;i>0;i--){
this.cells.push({x:i,y:0});
}
},
drawsnake:function()
{
for(var i=0;i<this.cells.length;i++){
pen.fillStyle=this.color;
pen.drawImage(snake_img,this.cells[i].x*cellsize,this.cells[i].y*cellsize,cellsize-2,cellsize-2);
}
},
updatesnake:function(){
var headx=this.cells[0].x;
var heady=this.cells[0].y;
if(headx==food.x && heady==food.y) //snake eat the food
{
food=getfood();
score++;
document.getElementById("score").innerHTML=score;
}else{ //still finding food,so in this case snake size not changed
this.cells.pop();
}
var dirX,dirY;
if(this.direction=="right"){ //keyboard arrow direction is right
dirX=headx +1;
dirY=heady;
}else if(this.direction=="left"){ //keyboard arrow direction is left
dirX=headx -1;
dirY=heady;
}else if(this.direction=="up"){ //keyboard arrow direction is up
dirX=headx ;
dirY=heady -1;
}
else{ //keyboard arrow direction is down
dirX=headx ;
dirY=heady +1;
}
this.cells.unshift({x:dirX,y:dirY});
// logic for preventing snake to go outside the box
var lastx=Math.round(w/cellsize);
var lasty=Math.round(h/cellsize);
//condition when snake hits the wall ,set gameover variable to true
if(this.cells[0].y<0 || this.cells[0].x<0 || this.cells[0].x>lastx || this.cells[0].y>lasty)
{
gameover=true;
}
}
};
snake.drawboard();
snake.createsnake();
function keypressed(e){
//conditional statements
if(e.key=="ArrowRight"){
snake.direction="right";
}
else if(e.key=="ArrowLeft"){
snake.direction="left";
}
else if(e.key=="ArrowDown"){
snake.direction="down";
}else{
snake.direction="up";
}
}
document.addEventListener('keydown',keypressed);
}
//function to make changes in canvas board
function draw(){
pen.clearRect(0,0,w,h);
snake.drawboard();
snake.drawsnake();
pen.fillStyle=food.color;
pen.drawImage(food_img,food.x*cellsize,food.y*cellsize,cellsize,cellsize);
}
//function to get food at random place
function getfood(){
var foodx= Math.round(Math.random()*(w-cellsize)/cellsize);
var foody= Math.round(Math.random()*(w-cellsize)/cellsize);
var food={
x:foodx,
y:foody,
color:"red",
}
return food;
}
//function to call updatesnake
function update(){
snake.updatesnake();
}
//function to update the game everytime
function gameloop(){
if(gameover==true){
flag=true;
clearInterval(f);
alert("GAME OVER \nYour Score is :"+score+"\nThanks for Playing");
}
draw();
update();
}
var f ;
flag=false;
//function to start game when press on start button
function start_game(){
if(flag==true){
init();
}
f =setInterval(gameloop,100);
}
//function to change snake body
function call_snake_image()
{
if(document.getElementById('snake_color').value==6){
snake_img.src="./assets/img/google.png";
}else if(document.getElementById('snake_color').value==7){
snake_img.src="./assets/img/netflix.png";
}else if(document.getElementById('snake_color').value==8){
snake_img.src="./assets/img/adobe.png";
}else if(document.getElementById('snake_color').value==1){
snake_img.src="./assets/img/snake-3.png";
}else if(document.getElementById('snake_color').value==2){
snake_img.src="./assets/img/yellow.png";
}else if(document.getElementById('snake_color').value==3){
snake_img.src="./assets/img/snake-4.png";
}else if(document.getElementById('snake_color').value==4){
snake_img.src="./assets/img/facebook.png";
}else if(document.getElementById('snake_color').value==5){
snake_img.src="./assets/img/amazon.png";
}else{
snake_img.src="./assets/img/snake.png";
}
}
//This init is for displaying game board
init();