-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.js
168 lines (133 loc) · 3.12 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
function init()
{
canvas = document.getElementById('mycanvas');
W = H = canvas.width = canvas.height = 1000;
pen = canvas.getContext('2d');
cs = 66;
game_over = false;
score = 5;
//Create a Image Object for food
food_img = new Image();
food_img.src = "apple.png" ;
trophy = new Image();
trophy.src = "trophy.png" ;
food = getRandomFood();
snake =
{
init_len:5,
color:"blue",
cells:[],
direction:"right",
createSnake:function(){
for(var i=this.init_len;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.fillRect(this.cells[i].x*cs , this.cells[i].y*cs,cs-3,cs-3);
}
},
updateSnake:function()
{
//console.log("updating snake according to the direction property");
//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)
{
console.log("Food eaten");
food = getRandomFood();
score++;
}
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});
/*Write a Logic that prevents snake from going out*/
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";
}
console.log(snake.direction);
}
document.addEventListener('keydown',keyPressed) ;
}
function draw(){
//console.log("In Draw");
//erase the old frame
pen.clearRect(0,0,W,H);
snake.drawSnake();
pen.fillStyle = food.color;
pen.drawImage(food_img,food.x*cs,food.y*cs,cs,cs);
pen.drawImage(trophy,18,20,cs,cs);
pen.fillStyle = "blue";
pen.font = "20px Roboto"
pen.fillText(score,50,50);
}
function update(){
//console.log("In Update");
snake.updateSnake();
}
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 f = setInterval(gameloop,100);