-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.js
182 lines (155 loc) · 3.61 KB
/
snake.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
// Init function to start the game
function init(){
// canvas element
canvas = document.getElementById('mycanvas');
W = H = canvas.width = canvas.height = 1000;
pen = canvas.getContext('2d');
cs = 67;
game_over = false;
score = 5;
// Image object for snake face
snake_face = new Image();
snake_face.src = "snake.png";
//Image Object for food
food_img = new Image();
food_img.src = "apple.png";
//Image object for trophy
trophy = new Image();
trophy.src = "trophy.png";
food = getRandomFood();
//Snake object
snake = {
init_len:5,
color:"blue",
cells:[],
direction:"right",
// create snake
createSnake:function(){
for(var i=this.init_len;i>0;i--){
this.cells.push({x:i,y:0});
}
},
// draw snake
drawSnake:function(){
var a=0,b=1,c=0,d=1;
if(this.direction=="right")
{
b=2;
}
else if(this.direction=="left"){
b = 2;
a = (cs-3)/2 ;
}
else if(this.direction=="down")
{
d=2;
}
else{
c = (cs-3)/2;
d = 2;
}
for(var i=1;i<this.cells.length;i++){
pen.fillStyle = this.color;
pen.fillRect(this.cells[i].x*cs,this.cells[i].y*cs,cs-3,cs-3);
}
pen.fillRect(this.cells[0].x*cs+a,this.cells[0].y*cs+c, (cs-3)/b, (cs-3)/d);
pen.drawImage(snake_face,this.cells[0].x*cs,this.cells[0].y*cs,cs-3,cs-3);
},
updateSnake:function(){
//check if the snake has eaten food, increase the length of the snake and generate new food object
var headX = this.cells[0].x;
var headY = this.cells[0].y;
if(headX==food.x && headY==food.y){
food = getRandomFood();
score += 5;
if(score == 50)
time -= 50;
if(score%100 == 0)
time -= 50;
}
else
this.cells.pop();
var nextX,nextY;
if(this.direction=="right"){
nextX = headX + 1;
nextY = headY;
}
else if(this.direction=="left"){
nextX = headX - 1;
nextY = headY;
}
else if(this.direction=="down"){
nextX = headX;
nextY = headY + 1;
}
else{
nextX = headX;
nextY = headY - 1;
}
this.cells.unshift({x: nextX,y:nextY});
/*Prevents snake from going out of area*/
var last_x = Math.round(W/cs);
var last_y = Math.round(H/cs);
if(this.cells[0].y<0 || this.cells[0].x < 0 || this.cells[0].x >= last_x || this.cells[0].y >= last_y){
game_over = true;
}
}
};
snake.createSnake();
//Add a Event Listener on the Document Object
function keyPressed(e){
//Conditional Statments
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) ;
}
// Draw on Canvas
function draw(){
//erase the old frame
pen.clearRect(0,0,W,H);
snake.drawSnake();
pen.fillStyle = food.color;
pen.drawImage(food_img,food.x*cs-3,food.y*cs-2,cs,cs);
pen.drawImage(trophy,0,0,cs,cs);
pen.fillStyle = "blue";
pen.font = "20px Roboto"
pen.fillText(score,28,31);
}
// Update Snake
function update(){
snake.updateSnake();
}
// Generate Food
function getRandomFood(){
var foodX = Math.round(Math.random()*(W-cs)/cs);
var foodY = Math.round(Math.random()*(H-cs)/cs);
var food = {
x:foodX,
y:foodY,
color:"red",
}
return food;
}
function gameloop(){
if(game_over==true){
clearInterval(f);
alert("Game Over");
return;
}
draw();
update();
}
init();
var time = 200;
var f = setInterval(gameloop,time);