-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
195 lines (168 loc) · 4.81 KB
/
script.js
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
$(document).ready(function(){
$("#gameover").hide();
// generate random cordinates
var generateRandomCordinate = function(max,min){
var cordinates = {
x:Math.floor(Math.random()*max + min),
y:Math.floor(Math.random()*max + min)
}
return cordinates;
}
// check for collision
var collision = function(x,y,arr){
var len = arr.length;
for(var i=0;i<len;i++){
if(x==arr[i].x && y==arr[i].y)return true;
}
return false;
}
//adding key listener
$(document).keydown(function (event){
let key = event.keyCode;
console.log(key);
if( key == 37 && direction != "RIGHT"){
direction = "LEFT";
}else if(key == 38 && direction != "DOWN"){
direction = "UP";
}else if(key == 39 && direction != "LEFT"){
direction = "RIGHT";
}else if(key == 40 && direction != "UP"){
direction = "DOWN";
}
});
$("#up").click(function (){
if(direction != "DOWN"){
direction = "UP";
}
});
$("#left").click(function (){
if(direction != "RIGHT"){
direction = "LEFT";
}
});
$("#right").click(function (){
if(direction != "LEFT"){
direction = "RIGHT";
}
});
$("#down").click(function (){
if(direction != "UP"){
direction = "DOWN";
}
});
var direction = "LEFT";
const box = 32;
const boxColor = '#2ecc71';
const boxBorder = 'black';
const headColor = "#056F31";
const foodColor = '#2c3e50';//"#c3ee4c";
var game;
var gameScore = 0;
var gameover = false;
var snake = [];
snake.push({x:5,y:7});
snake.push({x:6,y:7});
snake.push({x:7,y:7});
snake.push({x:8,y:7});
var preDire ="LEFT";
var food = generateRandomCordinate(17,0);
//draw Rectangle
var rect = function(cordinate,color){
$("canvas").drawRect({
strokeStyle:boxBorder,
fillStyle:color,
fromCenter:false,
x:cordinate.x*box+1,
y:cordinate.y*box+1,
height:box-2,
width:box-2,
strokeWidth:1
});
}
var cir = function(cordinate,color){
$("canvas").drawArc({
strokeStyle:boxBorder,
fillStyle:color,
x:cordinate.x*box+1,
y:cordinate.y*box+1,
radius:(box/2)-1,
start:0,end:360,
fromCenter:false,
strokeWidth:0
});
}
var draw = function(){
$("canvas").clearCanvas();
cir(food,foodColor);
for(var i=0;i<snake.length;i++){
if(i==0)rect(snake[i],headColor);
else
rect(snake[i],boxColor);
}
let snakeX = snake[0].x;
let snakeY = snake[0].y;
if( direction == "LEFT") snakeX -= 1;
else if( direction == "UP") snakeY -= 1;
else if( direction == "RIGHT") snakeX += 1;
else snakeY += 1;
//check for border
if(snakeX < 0 || snakeY < 0 || snakeX > 17 || snakeY > 17){
clearInterval(game);
gameover = true;
$("#PlayPause").html("Play");
$("#gameover").show();
return;
}
//check for self collision
if(collision(snakeX,snakeY,snake)){
clearInterval(game);
gameover = true;
$("#PlayPause").html("Play");
$("#gameover").show();
return;
}
//eating food
if(food.x == snakeX && food.y == snakeY){
gameScore++;
$("#score").html(gameScore);
food = generateRandomCordinate(17,0);
}else{
snake.pop();
}
snake.unshift({
x:snakeX,
y:snakeY
});
cir(food,foodColor);
}
//methods for play pause controls
$("#PlayPause").click(function(){
var value = $("#PlayPause").text();
console.log(value);
if(value=="Play"){
if(gameover){
snake = [];
snake.push({x:5,y:7});
snake.push({x:6,y:7});
snake.push({x:7,y:7});
snake.push({x:8,y:7});
direction = "LEFT";
food = generateRandomCordinate(17,0);
gameScore = 0;
gameover = false;
$("#score").html(0);
}
else{
direction = preDire;
}
$("#gameover").hide();
game = setInterval(draw,200);
$("#PlayPause").html("Pause");
}
if(value=="Pause"){
clearInterval(game);
preDire = direction;
$("#PlayPause").html("Play");
}
});
});