-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.js
More file actions
155 lines (131 loc) · 4.13 KB
/
Copy pathsnake.js
File metadata and controls
155 lines (131 loc) · 4.13 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
function init(){
canvas = document.getElementById("mycanvas");
W=H=canvas.width = canvas.height=1000;
pen = canvas.getContext('2d');
cs=66;
game_over = false;
score = 5;
//Creat img object of food
food_img = new Image();
food_img.src = "Assets/apple.png"
trophy = new Image()
trophy.src = "Assets/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});
//So { x:i, y:0 } is an object where x, y are keys and i, 0 are corresponding values
// x, y refer to the co-ordinates on the plane of a particular box
// this.cells is the snake array object
// this.cells[i] refers to ith box in the snake
// this.cells[i].x refers to X co-ordinate of ith box in snake
}
},
drawSnake: function(){
for(var i=0;i<this.cells.length;i++){
pen.fillStyle = this.color;
pen.fillRect(this.cells[i].x*cs-2,this.cells[i].y*cs,cs-3,cs-3)
// pen.fillrect(x - coordinate of left most corner, y-coordinate of left most corner, width, height)
}
},
updateSnake:function(){
console.log("Updating snake according to direction property")
// check whether the snake had eaten the food
//generate new food
var headX = this.cells[0].x // Coordinate of last cell x
var headY = this.cells[0].y
if(headX ==food.x && headY==food.y){
console.log("Food eaten by snake")
food = getRandomFood();
score++;
}
else{
this.cells.pop();//this will remove the last cell
}
// Coordinate of last cell y
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 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){
// console.log("Key pressed",e.key);
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(e.key)
}
document.addEventListener('keydown',keyPressed)
}
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,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(){
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)